-
-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
BREAKING CHANGE: the `getLocalIndent` option should be always `Function` and should always return `String` value
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,18 +46,22 @@ function unescape(str) { | |
} | ||
|
||
// eslint-disable-next-line no-control-regex | ||
const filenameReservedRegex = /[<>:"/\\|?*\x00-\x1F]/g; | ||
const filenameReservedRegex = /[<>:"/\\|?*]/g; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
alexander-akait
Member
|
||
// eslint-disable-next-line no-control-regex | ||
const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g; | ||
const reRelativePath = /^\.+/; | ||
|
||
function getLocalIdent(loaderContext, localIdentName, localName, options) { | ||
const request = normalizePath( | ||
path.relative(options.context, loaderContext.resourcePath) | ||
); | ||
function defaultGetLocalIdent( | ||
loaderContext, | ||
localIdentName, | ||
localName, | ||
options | ||
) { | ||
const { context, hashPrefix } = options; | ||
const { resourcePath } = loaderContext; | ||
const request = normalizePath(path.relative(context, resourcePath)); | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
options.content = `${options.hashPrefix + request}+${unescape(localName)}`; | ||
options.content = `${hashPrefix + request}\x00${unescape(localName)}`; | ||
This comment has been minimized.
Sorry, something went wrong.
meglio
|
||
|
||
// Using `[path]` placeholder outputs `/` we need escape their | ||
// Also directories can contains invalid characters for css we need escape their too | ||
|
@@ -67,10 +71,9 @@ function getLocalIdent(loaderContext, localIdentName, localName, options) { | |
.replace(/^((-?[0-9])|--)/, '_$1') | ||
.replace(filenameReservedRegex, '-') | ||
.replace(reControlChars, '-') | ||
.replace(reRelativePath, '-') | ||
.replace(/\./g, '-'), | ||
{ isIdentifier: true } | ||
).replace(/\\\[local\\\]/gi, localName); | ||
).replace(/\\\[local\\]/gi, localName); | ||
} | ||
|
||
function normalizeUrl(url, isStringValue) { | ||
|
@@ -126,7 +129,7 @@ function getModulesOptions(rawOptions, loaderContext) { | |
localIdentHashPrefix: '', | ||
// eslint-disable-next-line no-undefined | ||
localIdentRegExp: undefined, | ||
getLocalIdent, | ||
getLocalIdent: defaultGetLocalIdent, | ||
namedExport: false, | ||
exportLocalsConvention: 'asIs', | ||
exportOnlyLocals: false, | ||
|
@@ -224,44 +227,29 @@ function shouldUseModulesPlugins(options) { | |
} | ||
|
||
function getModulesPlugins(options, loaderContext) { | ||
const { | ||
mode, | ||
getLocalIdent, | ||
localIdentName, | ||
localIdentContext, | ||
localIdentHashPrefix, | ||
localIdentRegExp, | ||
} = options.modules; | ||
|
||
let plugins = []; | ||
|
||
try { | ||
plugins = [ | ||
modulesValues, | ||
localByDefault({ mode: options.modules.mode }), | ||
localByDefault({ mode }), | ||
extractImports(), | ||
modulesScope({ | ||
generateScopedName: function generateScopedName(exportName) { | ||
let localIdent; | ||
|
||
if (options.modules.getLocalIdent) { | ||
localIdent = options.modules.getLocalIdent( | ||
loaderContext, | ||
options.modules.localIdentName, | ||
exportName, | ||
{ | ||
context: options.modules.localIdentContext, | ||
hashPrefix: options.modules.localIdentHashPrefix, | ||
regExp: options.modules.localIdentRegExp, | ||
} | ||
); | ||
} | ||
|
||
if (!localIdent) { | ||
localIdent = getLocalIdent( | ||
loaderContext, | ||
options.modules.localIdentName, | ||
exportName, | ||
{ | ||
context: options.modules.localIdentContext, | ||
hashPrefix: options.modules.localIdentHashPrefix, | ||
regExp: options.modules.localIdentRegExp, | ||
} | ||
); | ||
} | ||
|
||
return localIdent; | ||
generateScopedName(exportName) { | ||
return getLocalIdent(loaderContext, localIdentName, exportName, { | ||
context: localIdentContext, | ||
hashPrefix: localIdentHashPrefix, | ||
regExp: localIdentRegExp, | ||
}); | ||
}, | ||
exportGlobals: options.modules.exportGlobals, | ||
}), | ||
|
Guys, you should strive for backward compatibility as much as you can. It took me days of debugging to find out that this little change made all libraries that depend on an older version of the
generic-names
package stop working with webpack. For example, at the time of writing, on 21 Apr 2021, more than a year after this commit, quite a popular pluginbabel-plugin-react-css-modules
is still incompatible, please see this issue.