Skip to content

Commit 24c0a12

Browse files
authoredAug 6, 2020
fix: regression with exporting only locals
1 parent d24f9c7 commit 24c0a12

File tree

7 files changed

+522
-233
lines changed

7 files changed

+522
-233
lines changed
 

‎src/utils.js

+36-16
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,25 @@ function getImportCode(imports, options) {
354354
for (const item of imports) {
355355
const { importName, url, icss } = item;
356356

357-
code += options.esModule
358-
? icss && options.modules.namedExport
359-
? `import ${importName}, * as ${importName}_NAMED___ from ${url};\n`
360-
: `import ${importName} from ${url};\n`
361-
: `var ${importName} = require(${url});\n`;
357+
if (options.esModule) {
358+
if (icss && options.modules.namedExport) {
359+
code += `import ${
360+
options.modules.exportOnlyLocals ? '' : `${importName}, `
361+
}* as ${importName}_NAMED___ from ${url};\n`;
362+
} else {
363+
code += `import ${importName} from ${url};\n`;
364+
}
365+
} else {
366+
code += `var ${importName} = require(${url});\n`;
367+
}
362368
}
363369

364370
return code ? `// Imports\n${code}` : '';
365371
}
366372

367373
function getModuleCode(result, api, replacements, options) {
368374
if (options.modules.exportOnlyLocals === true) {
369-
return 'var ___CSS_LOADER_EXPORT___ = {};\n';
375+
return '';
370376
}
371377

372378
const { css, map } = result;
@@ -423,7 +429,7 @@ function dashesCamelCase(str) {
423429
}
424430

425431
function getExportCode(exports, replacements, options) {
426-
let code = '';
432+
let code = '// Exports\n';
427433
let localsCode = '';
428434

429435
const addExportToLocalsCode = (name, value) => {
@@ -483,13 +489,17 @@ function getExportCode(exports, replacements, options) {
483489
if (localName) {
484490
const { importName } = item;
485491

486-
localsCode = localsCode.replace(new RegExp(replacementName, 'g'), () =>
487-
options.modules.namedExport
488-
? `" + ${importName}_NAMED___[${JSON.stringify(
489-
camelCase(localName)
490-
)}] + "`
491-
: `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
492-
);
492+
localsCode = localsCode.replace(new RegExp(replacementName, 'g'), () => {
493+
if (options.modules.namedExport) {
494+
return `" + ${importName}_NAMED___[${JSON.stringify(
495+
camelCase(localName)
496+
)}] + "`;
497+
} else if (options.modules.exportOnlyLocals) {
498+
return `" + ${importName}[${JSON.stringify(localName)}] + "`;
499+
}
500+
501+
return `" + ${importName}.locals[${JSON.stringify(localName)}] + "`;
502+
});
493503
} else {
494504
localsCode = localsCode.replace(
495505
new RegExp(replacementName, 'g'),
@@ -498,17 +508,27 @@ function getExportCode(exports, replacements, options) {
498508
}
499509
}
500510

511+
if (options.modules.exportOnlyLocals) {
512+
code += options.modules.namedExport
513+
? localsCode
514+
: `${
515+
options.esModule ? 'export default' : 'module.exports ='
516+
} {\n${localsCode}\n};\n`;
517+
518+
return code;
519+
}
520+
501521
if (localsCode) {
502522
code += options.modules.namedExport
503-
? `${localsCode}`
523+
? localsCode
504524
: `___CSS_LOADER_EXPORT___.locals = {\n${localsCode}\n};\n`;
505525
}
506526

507527
code += `${
508528
options.esModule ? 'export default' : 'module.exports ='
509529
} ___CSS_LOADER_EXPORT___;\n`;
510530

511-
return `// Exports\n${code}`;
531+
return code;
512532
}
513533

514534
async function resolveRequests(resolve, context, possibleRequests) {

‎test/__snapshots__/loader.test.js.snap

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ exports[`loader issue #1033 (2): warnings 1`] = `Array []`;
2828
exports[`loader issue #1033: errors 1`] = `Array []`;
2929

3030
exports[`loader issue #1033: module 1`] = `
31-
"var ___CSS_LOADER_EXPORT___ = {};
32-
// Exports
33-
export default ___CSS_LOADER_EXPORT___;
31+
"// Exports
32+
export default {
33+
34+
};
3435
"
3536
`;
3637

‎test/__snapshots__/modules-option.test.js.snap

+424-204
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as classes from './composes.css';
2+
3+
__export__ = classes;
4+
5+
export default classes;

‎test/fixtures/modules/composes/composes.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
color: v-def;
1919
}
2020
21-
.class {
21+
.my-class {
2222
color: v-foo;
2323
}
2424
@@ -54,7 +54,7 @@
5454
composes: imported-relative from './top-relative.css';
5555
}
5656

57-
.module {
57+
.my-module {
5858
color: gray;
5959
composes: imported-module from '../issue-861/node_modules/package/style.css';
6060
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
.top-relative {
1+
.imported-relative {
22
display: flex;
33
}

‎test/modules-option.test.js

+50-7
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ describe('"modules" option', () => {
10011001
expect(getErrors(stats)).toMatchSnapshot('errors');
10021002
});
10031003

1004-
it('should work and respect the "exportOnlyLocals" option with the "esModule" option', async () => {
1004+
it('should work with "exportOnlyLocals" and "esModule" with "true" value options', async () => {
10051005
const compiler = getCompiler('./modules/composes/composes.js', {
10061006
modules: {
10071007
mode: 'local',
@@ -1022,6 +1022,27 @@ describe('"modules" option', () => {
10221022
expect(getErrors(stats)).toMatchSnapshot('errors');
10231023
});
10241024

1025+
it('should work with "exportOnlyLocals" and "esModule" with "false" value options', async () => {
1026+
const compiler = getCompiler('./modules/composes/composes.js', {
1027+
modules: {
1028+
mode: 'local',
1029+
localIdentName: '_[local]',
1030+
exportOnlyLocals: true,
1031+
},
1032+
esModule: false,
1033+
});
1034+
const stats = await compile(compiler);
1035+
1036+
expect(
1037+
getModuleSource('./modules/composes/composes.css', stats)
1038+
).toMatchSnapshot('module');
1039+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
1040+
'result'
1041+
);
1042+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
1043+
expect(getErrors(stats)).toMatchSnapshot('errors');
1044+
});
1045+
10251046
it('should work with an empty object value', async () => {
10261047
const compiler = getCompiler('./modules/pure/pure.js', { modules: {} });
10271048
const stats = await compile(compiler);
@@ -1133,15 +1154,21 @@ describe('"modules" option', () => {
11331154
expect(getErrors(stats, true)).toMatchSnapshot('errors');
11341155
});
11351156

1136-
it('should work with "url"', async () => {
1137-
const compiler = getCompiler('./modules/url/source.js', {
1138-
modules: true,
1157+
it('should work with "exportOnlyLocals" and "namedExport" option', async () => {
1158+
const compiler = getCompiler('./modules/composes/composes-named.js', {
1159+
modules: {
1160+
mode: 'local',
1161+
localIdentName: '_[local]',
1162+
namedExport: true,
1163+
exportOnlyLocals: true,
1164+
},
1165+
esModule: true,
11391166
});
11401167
const stats = await compile(compiler);
11411168

1142-
expect(getModuleSource('./modules/url/source.css', stats)).toMatchSnapshot(
1143-
'module'
1144-
);
1169+
expect(
1170+
getModuleSource('./modules/composes/composes.css', stats)
1171+
).toMatchSnapshot('module');
11451172
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
11461173
'result'
11471174
);
@@ -1167,6 +1194,22 @@ describe('"modules" option', () => {
11671194
expect(getErrors(stats)).toMatchSnapshot('errors');
11681195
});
11691196

1197+
it('should work with "url"', async () => {
1198+
const compiler = getCompiler('./modules/url/source.js', {
1199+
modules: true,
1200+
});
1201+
const stats = await compile(compiler);
1202+
1203+
expect(getModuleSource('./modules/url/source.css', stats)).toMatchSnapshot(
1204+
'module'
1205+
);
1206+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
1207+
'result'
1208+
);
1209+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
1210+
expect(getErrors(stats)).toMatchSnapshot('errors');
1211+
});
1212+
11701213
const icssTestCasesPath = path.join(
11711214
__dirname,
11721215
'fixtures/modules/icss/tests-cases'

0 commit comments

Comments
 (0)
Please sign in to comment.