Skip to content

Commit b60e62a

Browse files
fix: don't output invalid es5 code when locals do not exists (#1035)
1 parent b95a779 commit b60e62a

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

src/utils.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,9 @@ function getExportCode(
480480

481481
if (exportType === 'locals') {
482482
exportItems.push(
483-
`${
484-
esModule ? 'export default' : 'module.exports ='
485-
} {\n${exportLocalsCode}\n};`
483+
`${esModule ? 'export default' : 'module.exports ='} ${
484+
exportLocalsCode ? `{\n${exportLocalsCode}\n}` : '{}'
485+
};`
486486
);
487487
} else {
488488
if (exportLocalsCode) {

test/__snapshots__/loader.test.js.snap

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

3+
exports[`loader issue #1033 (2): errors 1`] = `Array []`;
4+
5+
exports[`loader issue #1033 (2): module 1`] = `
6+
"// Imports
7+
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
8+
exports = ___CSS_LOADER_API_IMPORT___(false);
9+
// Module
10+
exports.push([module.id, \\"\\", \\"\\"]);
11+
// Exports
12+
module.exports = exports;
13+
"
14+
`;
15+
16+
exports[`loader issue #1033 (2): result 1`] = `
17+
Array [
18+
Array [
19+
"./modules/issue-1033/issue-1033.css",
20+
"",
21+
"",
22+
],
23+
]
24+
`;
25+
26+
exports[`loader issue #1033 (2): warnings 1`] = `Array []`;
27+
28+
exports[`loader issue #1033: errors 1`] = `Array []`;
29+
30+
exports[`loader issue #1033: module 1`] = `
31+
"// Exports
32+
module.exports = {};
33+
"
34+
`;
35+
36+
exports[`loader issue #1033: result 1`] = `Object {}`;
37+
38+
exports[`loader issue #1033: warnings 1`] = `Array []`;
39+
340
exports[`loader should reuse \`ast\` from "postcss-loader": errors 1`] = `Array []`;
441

542
exports[`loader should reuse \`ast\` from "postcss-loader": module 1`] = `

test/fixtures/modules/issue-1033/issue-1033.css

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import css from './issue-1033.css';
2+
3+
__export__ = css;
4+
5+
export default css;

test/loader.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,37 @@ describe('loader', () => {
308308
expect(getWarnings(stats)).toMatchSnapshot('warnings');
309309
expect(getErrors(stats)).toMatchSnapshot('errors');
310310
});
311+
312+
it('issue #1033', async () => {
313+
const compiler = getCompiler('./modules/issue-1033/issue-1033.js', {
314+
modules: { mode: 'local', localIdentName: '_[local]' },
315+
onlyLocals: true,
316+
});
317+
const stats = await compile(compiler);
318+
319+
expect(
320+
getModuleSource('./modules/issue-1033/issue-1033.css', stats)
321+
).toMatchSnapshot('module');
322+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
323+
'result'
324+
);
325+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
326+
expect(getErrors(stats)).toMatchSnapshot('errors');
327+
});
328+
329+
it('issue #1033 (2)', async () => {
330+
const compiler = getCompiler('./modules/issue-1033/issue-1033.js', {
331+
modules: { mode: 'local', localIdentName: '_[local]' },
332+
});
333+
const stats = await compile(compiler);
334+
335+
expect(
336+
getModuleSource('./modules/issue-1033/issue-1033.css', stats)
337+
).toMatchSnapshot('module');
338+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
339+
'result'
340+
);
341+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
342+
expect(getErrors(stats)).toMatchSnapshot('errors');
343+
});
311344
});

0 commit comments

Comments
 (0)