Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't output invalid es5 code when locals do not exists #1035

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});