Skip to content

Commit ad77b97

Browse files
Merge pull request #85 from xile611/master
Add a new option `importPathFormatter` to format the path of css files
2 parents ec95cbe + 0234196 commit ad77b97

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

src/index.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ const defaultOptions = {
1010
generateScopedName: '[name]__[local]___[hash:base64:5]'
1111
};
1212

13+
function updateStyleSheetPath(pathStringLiteral, importPathFormatter) {
14+
if (!importPathFormatter) { return pathStringLiteral; }
15+
16+
return {
17+
...pathStringLiteral,
18+
value: importPathFormatter(pathStringLiteral.value)
19+
};
20+
}
21+
22+
1323
function findExpressionStatementChild(path, t) {
1424
const parent = path.parentPath;
1525
if (!parent) {
@@ -113,6 +123,7 @@ export default function transformCssModules({ types: t }) {
113123
// this is not a css-require-ook config
114124
delete currentConfig.extractCss;
115125
delete currentConfig.keepImport;
126+
delete currentConfig.importPathFormatter;
116127

117128
// match file extensions, speeds up transform by creating one
118129
// RegExp ahead of execution time
@@ -139,6 +150,9 @@ export default function transformCssModules({ types: t }) {
139150
Object.keys(requireHooksOptions).forEach(key => {
140151
// skip undefined options
141152
if (currentConfig[key] === undefined) {
153+
if (key === 'importPathFormatter' && thisPluginOptions && thisPluginOptions[key]) {
154+
thisPluginOptions[key] = requireHooksOptions[key](thisPluginOptions[key]);
155+
}
142156
return;
143157
}
144158

@@ -191,7 +205,7 @@ export default function transformCssModules({ types: t }) {
191205
t.expressionStatement(
192206
t.callExpression(
193207
t.identifier('require'),
194-
[t.stringLiteral(value)]
208+
[updateStyleSheetPath(t.stringLiteral(value), thisPluginOptions.importPathFormatter)]
195209
)
196210
),
197211
varDeclaration
@@ -227,7 +241,7 @@ export default function transformCssModules({ types: t }) {
227241
t.expressionStatement(
228242
t.callExpression(
229243
t.identifier('require'),
230-
[t.stringLiteral(stylesheetPath)]
244+
[updateStyleSheetPath(t.stringLiteral(stylesheetPath), thisPluginOptions.importPathFormatter)]
231245
)
232246
)
233247
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { isFunction, isModulePath, requireLocalFileOrNodeModule } from '../utils';
2+
3+
/**
4+
* Resolves importPathFormatter option
5+
*
6+
* @param {String|Function} value
7+
* @returns {Function}
8+
*/
9+
export default function importPathFormatter(value/* , currentConfig */) {
10+
if (isFunction(value)) {
11+
return value;
12+
} else if (isModulePath(value)) {
13+
const requiredOption = requireLocalFileOrNodeModule(value);
14+
15+
if (!isFunction(requiredOption)) {
16+
throw new Error(`Configuration file for 'importPathFormatter' is not exporting a function`);
17+
}
18+
19+
return requiredOption;
20+
}
21+
22+
throw new Error(`Configuration 'importPathFormatter' is not a function nor a valid module path`);
23+
}

src/options_resolvers/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export { default as processorOpts } from './processorOpts';
1313
export { default as rootDir } from './rootDir';
1414
export { default as resolve } from './resolve';
1515
export { default as use } from './use';
16+
export { default as importPathFormatter } from './importPathFormatter';

test/index.spec.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,13 @@ describe('babel-plugin-css-modules-transform', () => {
109109
});
110110

111111
it('should write a multiple css files using import', () => {
112-
expect(transform('fixtures/import.js', {
112+
expect(transform(`${__dirname}/fixtures/import.js`, {
113113
extractCss: {
114114
dir: `${__dirname}/output/`,
115-
filename: '[name].css',
116-
relativeRoot: `${__dirname}`
117-
}
115+
filename: '[path]/[name].css',
116+
relativeRoot: __dirname
117+
},
118+
extensions: ['.scss', '.css']
118119
}).code).to.be.equal(readExpected('fixtures/import.expected.js'));
119120

120121
expect(readExpected(`${__dirname}/output/parent.css`))

0 commit comments

Comments
 (0)