Skip to content

Commit 947e666

Browse files
refactor: drop getLocalIdent compatibility with webpack@2 and webpack@3 (#829)
1 parent be29be0 commit 947e666

File tree

3 files changed

+125
-16
lines changed

3 files changed

+125
-16
lines changed

lib/utils.js

+7-16
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,17 @@ function compileExports(exports, camelCaseKeys, valueHandler) {
9191

9292
function getLocalIdent(loaderContext, localIdentName, localName, options) {
9393
if (!options.context) {
94-
if (loaderContext.rootContext) {
95-
// eslint-disable-next-line no-param-reassign
96-
options.context = loaderContext.rootContext;
97-
} else if (
98-
loaderContext.options &&
99-
typeof loaderContext.options.context === 'string'
100-
) {
101-
// eslint-disable-next-line no-param-reassign
102-
options.context = loaderContext.options.context;
103-
} else {
104-
// eslint-disable-next-line no-param-reassign
105-
options.context = loaderContext.context;
106-
}
94+
// eslint-disable-next-line no-param-reassign
95+
options.context = loaderContext.rootContext;
10796
}
10897

109-
const request = path.relative(options.context, loaderContext.resourcePath);
98+
const request = path
99+
.relative(options.context, loaderContext.resourcePath)
100+
.replace(/\\/g, '/');
110101

111102
// eslint-disable-next-line no-param-reassign
112-
options.content = `${options.hashPrefix +
113-
request.replace(/\\/g, '/')}+${localName}`;
103+
options.content = `${options.hashPrefix + request}+${localName}`;
104+
114105
// eslint-disable-next-line no-param-reassign
115106
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
116107

test/__snapshots__/getLocalIdent-option.test.js.snap

+60
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`getLocalIdent option should accepts arguments: errors 1`] = `Array []`;
4+
5+
exports[`getLocalIdent option should accepts arguments: locals 1`] = `
6+
Object {
7+
"abc": "foo",
8+
"def": "foo",
9+
"ghi": "foo",
10+
"jkl": "foo",
11+
}
12+
`;
13+
14+
exports[`getLocalIdent option should accepts arguments: module (evaluated) 1`] = `
15+
Array [
16+
Array [
17+
1,
18+
".foo .foo {
19+
color: red;
20+
}
21+
22+
.foo .foo {
23+
color: blue;
24+
}
25+
",
26+
"",
27+
],
28+
]
29+
`;
30+
31+
exports[`getLocalIdent option should accepts arguments: warnings 1`] = `Array []`;
32+
33+
exports[`getLocalIdent option should respect \`context\` option: errors 1`] = `Array []`;
34+
35+
exports[`getLocalIdent option should respect \`context\` option: locals 1`] = `
36+
Object {
37+
"abc": "_1hksQTRR0UD9eKPUBlgn0X",
38+
"def": "_3oo37UGTAgyDe0MeQom-28",
39+
"ghi": "_2ZRWT_7WiIKpOei7U0eJzJ",
40+
"jkl": "aQ1rQfhbWSRMXFXxIfQcx",
41+
}
42+
`;
43+
44+
exports[`getLocalIdent option should respect \`context\` option: module (evaluated) 1`] = `
45+
Array [
46+
Array [
47+
1,
48+
"._1hksQTRR0UD9eKPUBlgn0X ._3oo37UGTAgyDe0MeQom-28 {
49+
color: red;
50+
}
51+
52+
._2ZRWT_7WiIKpOei7U0eJzJ .aQ1rQfhbWSRMXFXxIfQcx {
53+
color: blue;
54+
}
55+
",
56+
"",
57+
],
58+
]
59+
`;
60+
61+
exports[`getLocalIdent option should respect \`context\` option: warnings 1`] = `Array []`;
62+
363
exports[`getLocalIdent option should work (\`modules: false\`): errors 1`] = `Array []`;
464

565
exports[`getLocalIdent option should work (\`modules: false\`): locals 1`] = `

test/getLocalIdent-option.test.js

+58
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const path = require('path');
2+
13
const { webpack, evaluated } = require('./helpers');
24

35
describe('getLocalIdent option', () => {
@@ -46,4 +48,60 @@ describe('getLocalIdent option', () => {
4648
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
4749
expect(stats.compilation.errors).toMatchSnapshot('errors');
4850
});
51+
52+
it('should accepts arguments', async () => {
53+
const config = {
54+
loader: {
55+
options: {
56+
modules: true,
57+
localIdentRegExp: 'regExp',
58+
context: 'context',
59+
hashPrefix: 'hash',
60+
getLocalIdent(loaderContext, localIdentName, localName, options) {
61+
expect(loaderContext).toBeDefined();
62+
expect(typeof localIdentName).toBe('string');
63+
expect(typeof localName).toBe('string');
64+
expect(options).toBeDefined();
65+
66+
expect(options.regExp).toBe('regExp');
67+
expect(options.context).toBe('context');
68+
expect(options.hashPrefix).toBe('hash');
69+
70+
return 'foo';
71+
},
72+
},
73+
},
74+
};
75+
const testId = './modules/getLocalIdent.css';
76+
const stats = await webpack(testId, config);
77+
const { modules } = stats.toJson();
78+
const module = modules.find((m) => m.id === testId);
79+
const evaluatedModule = evaluated(module.source);
80+
81+
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
82+
expect(evaluatedModule.locals).toMatchSnapshot('locals');
83+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
84+
expect(stats.compilation.errors).toMatchSnapshot('errors');
85+
});
86+
87+
it('should respect `context` option', async () => {
88+
const config = {
89+
loader: {
90+
options: {
91+
context: path.resolve(__dirname, 'fixtures/modules'),
92+
modules: true,
93+
},
94+
},
95+
};
96+
const testId = './modules/getLocalIdent.css';
97+
const stats = await webpack(testId, config);
98+
const { modules } = stats.toJson();
99+
const module = modules.find((m) => m.id === testId);
100+
const evaluatedModule = evaluated(module.source);
101+
102+
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
103+
expect(evaluatedModule.locals).toMatchSnapshot('locals');
104+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
105+
expect(stats.compilation.errors).toMatchSnapshot('errors');
106+
});
49107
});

0 commit comments

Comments
 (0)