Skip to content
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

feat: modules options now accepts object config #937

Merged
merged 1 commit into from
May 28, 2019
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
237 changes: 178 additions & 59 deletions README.md

Large diffs are not rendered by default.

64 changes: 37 additions & 27 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,45 @@
{
"type": "string",
"enum": ["local", "global"]
}
]
},
"localIdentName": {
"type": "string"
},
"localIdentRegExp": {
"anyOf": [
{
"type": "string"
},
{
"instanceof": "RegExp"
}
]
},
"context": {
"type": "string"
},
"hashPrefix": {
"type": "string"
},
"getLocalIdent": {
"anyOf": [
{
"type": "boolean"
},
{
"instanceof": "Function"
"type": "object",
"additionalProperties": false,
"properties": {
"mode": {
"type": "string",
"enum": ["local", "global"]
},
"localIdentName": {
"type": "string"
},
"localIdentRegExp": {
"anyOf": [
{
"type": "string"
},
{
"instanceof": "RegExp"
}
]
},
"context": {
"type": "string"
},
"hashPrefix": {
"type": "string"
},
"getLocalIdent": {
"anyOf": [
{
"type": "boolean"
},
{
"instanceof": "Function"
}
]
}
}
}
]
},
Expand Down
39 changes: 29 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,22 +267,41 @@ function getImports(messages, importUrlPrefix, loaderContext, callback) {
}

function getModulesPlugins(options, loaderContext) {
const mode = typeof options.modules === 'boolean' ? 'local' : options.modules;
let modulesOptions = {
mode: 'local',
localIdentName: '[hash:base64]',
getLocalIdent,
context: null,
hashPrefix: '',
localIdentRegExp: null,
};

if (
typeof options.modules === 'boolean' ||
typeof options.modules === 'string'
) {
modulesOptions.mode =
typeof options.modules === 'string' ? options.modules : 'local';
} else {
modulesOptions = Object.assign({}, modulesOptions, options.modules);
}

return [
modulesValues,
localByDefault({ mode }),
localByDefault({ mode: modulesOptions.mode }),
extractImports(),
modulesScope({
generateScopedName: function generateScopedName(exportName) {
const localIdentName = options.localIdentName || '[hash:base64]';
const customGetLocalIdent = options.getLocalIdent || getLocalIdent;

return customGetLocalIdent(loaderContext, localIdentName, exportName, {
regExp: options.localIdentRegExp,
hashPrefix: options.hashPrefix || '',
context: options.context,
});
return modulesOptions.getLocalIdent(
loaderContext,
modulesOptions.localIdentName,
exportName,
{
context: modulesOptions.context,
hashPrefix: modulesOptions.hashPrefix,
regExp: modulesOptions.localIdentRegExp,
}
);
},
}),
];
Expand Down
117 changes: 0 additions & 117 deletions test/__snapshots__/errors.test.js.snap

This file was deleted.

150 changes: 0 additions & 150 deletions test/__snapshots__/getLocalIdent-option.test.js.snap

This file was deleted.

Loading