Skip to content

Commit 24c0a12

Browse files
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

0 commit comments

Comments
 (0)