Skip to content

Add a new option importPathFormatter to format the path of css files #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ const defaultOptions = {
generateScopedName: '[name]__[local]___[hash:base64:5]'
};

function updateStyleSheetPath(pathStringLiteral, importPathFormatter) {
if (!importPathFormatter) { return pathStringLiteral; }

return {
...pathStringLiteral,
value: importPathFormatter(pathStringLiteral.value)
};
}


function findExpressionStatementChild(path, t) {
const parent = path.parentPath;
if (!parent) {
Expand Down Expand Up @@ -113,6 +123,7 @@ export default function transformCssModules({ types: t }) {
// this is not a css-require-ook config
delete currentConfig.extractCss;
delete currentConfig.keepImport;
delete currentConfig.importPathFormatter;

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

Expand Down Expand Up @@ -191,7 +205,7 @@ export default function transformCssModules({ types: t }) {
t.expressionStatement(
t.callExpression(
t.identifier('require'),
[t.stringLiteral(value)]
[updateStyleSheetPath(t.stringLiteral(value), thisPluginOptions.importPathFormatter)]
)
),
varDeclaration
Expand Down Expand Up @@ -227,7 +241,7 @@ export default function transformCssModules({ types: t }) {
t.expressionStatement(
t.callExpression(
t.identifier('require'),
[t.stringLiteral(stylesheetPath)]
[updateStyleSheetPath(t.stringLiteral(stylesheetPath), thisPluginOptions.importPathFormatter)]
)
)
);
Expand Down
23 changes: 23 additions & 0 deletions src/options_resolvers/importPathFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isFunction, isModulePath, requireLocalFileOrNodeModule } from '../utils';

/**
* Resolves importPathFormatter option
*
* @param {String|Function} value
* @returns {Function}
*/
export default function importPathFormatter(value/* , currentConfig */) {
if (isFunction(value)) {
return value;
} else if (isModulePath(value)) {
const requiredOption = requireLocalFileOrNodeModule(value);

if (!isFunction(requiredOption)) {
throw new Error(`Configuration file for 'importPathFormatter' is not exporting a function`);
}

return requiredOption;
}

throw new Error(`Configuration 'importPathFormatter' is not a function nor a valid module path`);
}
1 change: 1 addition & 0 deletions src/options_resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export { default as processorOpts } from './processorOpts';
export { default as rootDir } from './rootDir';
export { default as resolve } from './resolve';
export { default as use } from './use';
export { default as importPathFormatter } from './importPathFormatter';
9 changes: 5 additions & 4 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ describe('babel-plugin-css-modules-transform', () => {
});

it('should write a multiple css files using import', () => {
expect(transform('fixtures/import.js', {
expect(transform(`${__dirname}/fixtures/import.js`, {
extractCss: {
dir: `${__dirname}/output/`,
filename: '[name].css',
relativeRoot: `${__dirname}`
}
filename: '[path]/[name].css',
relativeRoot: __dirname
},
extensions: ['.scss', '.css']
}).code).to.be.equal(readExpected('fixtures/import.expected.js'));

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