Skip to content

Commit 18b4d3b

Browse files
committed
general fixes for css-loader compatibility
update yarn.lock
1 parent 2cb55d8 commit 18b4d3b

3 files changed

Lines changed: 36 additions & 10 deletions

File tree

lib/loader.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const path = require('path');
22
const loaderUtils = require('loader-utils');
33
const postcss = require('postcss');
4+
const normalizePath = require('normalize-path');
5+
const cssesc = require('cssesc');
46

57
const localByDefault = require('postcss-modules-local-by-default');
68
const modulesScope = require('postcss-modules-scope');
@@ -9,16 +11,30 @@ const postCssPlugins = require('./postcss-plugins');
911
const extractThemeRulesPlugin = postCssPlugins.extractThemeRulesPlugin;
1012
const removeCssModuleExports = postCssPlugins.removeCssModuleExports;
1113

12-
// copied from css-loader/lib/getLocalIdent.js
14+
// copied from css-loader/src/utils.js
1315
function getLocalIdent(loaderContext, localIdentName, localName, options) {
14-
if(!options.context)
15-
options.context = loaderContext.options && typeof loaderContext.options.context === "string" ? loaderContext.options.context : loaderContext.context;
16-
var request = path.relative(options.context, loaderContext.resourcePath);
17-
options.content = options.hashPrefix + request + "+" + localName;
18-
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
19-
var hash = loaderUtils.interpolateName(loaderContext, localIdentName, options);
20-
return hash.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-").replace(/^([^a-zA-Z_])/, "_$1");
21-
};
16+
if (!options.context) {
17+
// eslint-disable-next-line no-param-reassign
18+
options.context = loaderContext.rootContext;
19+
}
20+
21+
const request = normalizePath(
22+
path.relative(options.context || '', loaderContext.resourcePath)
23+
);
24+
25+
// eslint-disable-next-line no-param-reassign
26+
options.content = `${options.hashPrefix + request}+${unescape(localName)}`;
27+
28+
// Using `[path]` placeholder outputs `/` we need escape their
29+
// Also directories can contains invalid characters for css we need escape their too
30+
return cssesc(
31+
loaderUtils
32+
.interpolateName(loaderContext, localIdentName, options)
33+
// For `[hash]` placeholder
34+
.replace(/^((-?[0-9])|--)/, '_$1'),
35+
{ isIdentifier: true }
36+
).replace(/\\\[local\\\]/gi, localName);
37+
}
2238

2339

2440
module.exports = function (source, map) {
@@ -72,7 +88,7 @@ module.exports = function (source, map) {
7288
return customGetLocalIdent(loader, localIdentName, exportName, {
7389
regExp: options.localIdentRegExp,
7490
hashPrefix: options.hashPrefix || '',
75-
context: loader.options.context
91+
context: loader.rootContext
7692
});
7793
}
7894
}),

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
},
2929
"homepage": "https://github.com/willowtreeapps/css-variable-theme-webpack-plugin",
3030
"dependencies": {
31+
"cssesc": "^3.0.0",
3132
"loader-utils": "^1.0.2",
33+
"normalize-path": "^3.0.0",
3234
"postcss": "^5.2.15",
3335
"postcss-modules-local-by-default": "^1.1.1",
3436
"postcss-modules-scope": "^1.0.2"

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ cssesc@^0.1.0:
3636
version "0.1.0"
3737
resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-0.1.0.tgz#c814903e45623371a0477b40109aaafbeeaddbb4"
3838

39+
cssesc@^3.0.0:
40+
version "3.0.0"
41+
resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-3.0.0.tgz#37741919903b868565e1c09ea747445cd18983ee"
42+
3943
emojis-list@^2.0.0:
4044
version "2.1.0"
4145
resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"
@@ -78,6 +82,10 @@ loader-utils@^1.0.2:
7882
emojis-list "^2.0.0"
7983
json5 "^0.5.0"
8084

85+
normalize-path@^3.0.0:
86+
version "3.0.0"
87+
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
88+
8189
postcss-modules-local-by-default@^1.1.1:
8290
version "1.1.1"
8391
resolved "https://registry.yarnpkg.com/postcss-modules-local-by-default/-/postcss-modules-local-by-default-1.1.1.tgz#29a10673fa37d19251265ca2ba3150d9040eb4ce"

0 commit comments

Comments
 (0)