Skip to content

Commit

Permalink
refactor: drop getLocalIdent compatibility with webpack@2 and webpack…
Browse files Browse the repository at this point in the history
…@3 (#829)
  • Loading branch information
evilebottnawi committed Nov 30, 2018
1 parent be29be0 commit 947e666
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 16 deletions.
23 changes: 7 additions & 16 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,17 @@ function compileExports(exports, camelCaseKeys, valueHandler) {

function getLocalIdent(loaderContext, localIdentName, localName, options) {
if (!options.context) {
if (loaderContext.rootContext) {
// eslint-disable-next-line no-param-reassign
options.context = loaderContext.rootContext;
} else if (
loaderContext.options &&
typeof loaderContext.options.context === 'string'
) {
// eslint-disable-next-line no-param-reassign
options.context = loaderContext.options.context;
} else {
// eslint-disable-next-line no-param-reassign
options.context = loaderContext.context;
}
// eslint-disable-next-line no-param-reassign
options.context = loaderContext.rootContext;
}

const request = path.relative(options.context, loaderContext.resourcePath);
const request = path
.relative(options.context, loaderContext.resourcePath)
.replace(/\\/g, '/');

// eslint-disable-next-line no-param-reassign
options.content = `${options.hashPrefix +
request.replace(/\\/g, '/')}+${localName}`;
options.content = `${options.hashPrefix + request}+${localName}`;

// eslint-disable-next-line no-param-reassign
localIdentName = localIdentName.replace(/\[local\]/gi, localName);

Expand Down
60 changes: 60 additions & 0 deletions test/__snapshots__/getLocalIdent-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getLocalIdent option should accepts arguments: errors 1`] = `Array []`;

exports[`getLocalIdent option should accepts arguments: locals 1`] = `
Object {
"abc": "foo",
"def": "foo",
"ghi": "foo",
"jkl": "foo",
}
`;

exports[`getLocalIdent option should accepts arguments: module (evaluated) 1`] = `
Array [
Array [
1,
".foo .foo {
color: red;
}
.foo .foo {
color: blue;
}
",
"",
],
]
`;

exports[`getLocalIdent option should accepts arguments: warnings 1`] = `Array []`;

exports[`getLocalIdent option should respect \`context\` option: errors 1`] = `Array []`;

exports[`getLocalIdent option should respect \`context\` option: locals 1`] = `
Object {
"abc": "_1hksQTRR0UD9eKPUBlgn0X",
"def": "_3oo37UGTAgyDe0MeQom-28",
"ghi": "_2ZRWT_7WiIKpOei7U0eJzJ",
"jkl": "aQ1rQfhbWSRMXFXxIfQcx",
}
`;

exports[`getLocalIdent option should respect \`context\` option: module (evaluated) 1`] = `
Array [
Array [
1,
"._1hksQTRR0UD9eKPUBlgn0X ._3oo37UGTAgyDe0MeQom-28 {
color: red;
}
._2ZRWT_7WiIKpOei7U0eJzJ .aQ1rQfhbWSRMXFXxIfQcx {
color: blue;
}
",
"",
],
]
`;

exports[`getLocalIdent option should respect \`context\` option: warnings 1`] = `Array []`;

exports[`getLocalIdent option should work (\`modules: false\`): errors 1`] = `Array []`;

exports[`getLocalIdent option should work (\`modules: false\`): locals 1`] = `
Expand Down
58 changes: 58 additions & 0 deletions test/getLocalIdent-option.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require('path');

const { webpack, evaluated } = require('./helpers');

describe('getLocalIdent option', () => {
Expand Down Expand Up @@ -46,4 +48,60 @@ describe('getLocalIdent option', () => {
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('should accepts arguments', async () => {
const config = {
loader: {
options: {
modules: true,
localIdentRegExp: 'regExp',
context: 'context',
hashPrefix: 'hash',
getLocalIdent(loaderContext, localIdentName, localName, options) {
expect(loaderContext).toBeDefined();
expect(typeof localIdentName).toBe('string');
expect(typeof localName).toBe('string');
expect(options).toBeDefined();

expect(options.regExp).toBe('regExp');
expect(options.context).toBe('context');
expect(options.hashPrefix).toBe('hash');

return 'foo';
},
},
},
};
const testId = './modules/getLocalIdent.css';
const stats = await webpack(testId, config);
const { modules } = stats.toJson();
const module = modules.find((m) => m.id === testId);
const evaluatedModule = evaluated(module.source);

expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
expect(evaluatedModule.locals).toMatchSnapshot('locals');
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('should respect `context` option', async () => {
const config = {
loader: {
options: {
context: path.resolve(__dirname, 'fixtures/modules'),
modules: true,
},
},
};
const testId = './modules/getLocalIdent.css';
const stats = await webpack(testId, config);
const { modules } = stats.toJson();
const module = modules.find((m) => m.id === testId);
const evaluatedModule = evaluated(module.source);

expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
expect(evaluatedModule.locals).toMatchSnapshot('locals');
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});
});

0 comments on commit 947e666

Please sign in to comment.