Skip to content

Commit

Permalink
fix: don't output invalid es5 code when locals do not exists (#1035)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Jan 3, 2020
1 parent b95a779 commit b60e62a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ function getExportCode(

if (exportType === 'locals') {
exportItems.push(
`${
esModule ? 'export default' : 'module.exports ='
} {\n${exportLocalsCode}\n};`
`${esModule ? 'export default' : 'module.exports ='} ${
exportLocalsCode ? `{\n${exportLocalsCode}\n}` : '{}'
};`
);
} else {
if (exportLocalsCode) {
Expand Down
37 changes: 37 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`loader issue #1033 (2): errors 1`] = `Array []`;

exports[`loader issue #1033 (2): module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
exports = ___CSS_LOADER_API_IMPORT___(false);
// Module
exports.push([module.id, \\"\\", \\"\\"]);
// Exports
module.exports = exports;
"
`;

exports[`loader issue #1033 (2): result 1`] = `
Array [
Array [
"./modules/issue-1033/issue-1033.css",
"",
"",
],
]
`;

exports[`loader issue #1033 (2): warnings 1`] = `Array []`;

exports[`loader issue #1033: errors 1`] = `Array []`;

exports[`loader issue #1033: module 1`] = `
"// Exports
module.exports = {};
"
`;

exports[`loader issue #1033: result 1`] = `Object {}`;

exports[`loader issue #1033: warnings 1`] = `Array []`;

exports[`loader should reuse \`ast\` from "postcss-loader": errors 1`] = `Array []`;

exports[`loader should reuse \`ast\` from "postcss-loader": module 1`] = `
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions test/fixtures/modules/issue-1033/issue-1033.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import css from './issue-1033.css';

__export__ = css;

export default css;
33 changes: 33 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,37 @@ describe('loader', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('issue #1033', async () => {
const compiler = getCompiler('./modules/issue-1033/issue-1033.js', {
modules: { mode: 'local', localIdentName: '_[local]' },
onlyLocals: true,
});
const stats = await compile(compiler);

expect(
getModuleSource('./modules/issue-1033/issue-1033.css', stats)
).toMatchSnapshot('module');
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
'result'
);
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('issue #1033 (2)', async () => {
const compiler = getCompiler('./modules/issue-1033/issue-1033.js', {
modules: { mode: 'local', localIdentName: '_[local]' },
});
const stats = await compile(compiler);

expect(
getModuleSource('./modules/issue-1033/issue-1033.css', stats)
).toMatchSnapshot('module');
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
'result'
);
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});

0 comments on commit b60e62a

Please sign in to comment.