Skip to content

Commit

Permalink
fix: regression with exporting only locals
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 6, 2020
1 parent d24f9c7 commit 24c0a12
Show file tree
Hide file tree
Showing 7 changed files with 522 additions and 233 deletions.
52 changes: 36 additions & 16 deletions src/utils.js
Expand Up @@ -354,19 +354,25 @@ function getImportCode(imports, options) {
for (const item of imports) {
const { importName, url, icss } = item;

code += options.esModule
? icss && options.modules.namedExport
? `import ${importName}, * as ${importName}_NAMED___ from ${url};\n`
: `import ${importName} from ${url};\n`
: `var ${importName} = require(${url});\n`;
if (options.esModule) {
if (icss && options.modules.namedExport) {
code += `import ${
options.modules.exportOnlyLocals ? '' : `${importName}, `
}* as ${importName}_NAMED___ from ${url};\n`;
} else {
code += `import ${importName} from ${url};\n`;
}
} else {
code += `var ${importName} = require(${url});\n`;
}
}

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

function getModuleCode(result, api, replacements, options) {
if (options.modules.exportOnlyLocals === true) {
return 'var ___CSS_LOADER_EXPORT___ = {};\n';
return '';
}

const { css, map } = result;
Expand Down Expand Up @@ -423,7 +429,7 @@ function dashesCamelCase(str) {
}

function getExportCode(exports, replacements, options) {
let code = '';
let code = '// Exports\n';
let localsCode = '';

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

localsCode = localsCode.replace(new RegExp(replacementName, 'g'), () =>
options.modules.namedExport
? `" + ${importName}_NAMED___[${JSON.stringify(
camelCase(localName)
)}] + "`
: `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
);
localsCode = localsCode.replace(new RegExp(replacementName, 'g'), () => {
if (options.modules.namedExport) {
return `" + ${importName}_NAMED___[${JSON.stringify(
camelCase(localName)
)}] + "`;
} else if (options.modules.exportOnlyLocals) {
return `" + ${importName}[${JSON.stringify(localName)}] + "`;
}

return `" + ${importName}.locals[${JSON.stringify(localName)}] + "`;
});
} else {
localsCode = localsCode.replace(
new RegExp(replacementName, 'g'),
Expand All @@ -498,17 +508,27 @@ function getExportCode(exports, replacements, options) {
}
}

if (options.modules.exportOnlyLocals) {
code += options.modules.namedExport
? localsCode
: `${
options.esModule ? 'export default' : 'module.exports ='
} {\n${localsCode}\n};\n`;

return code;
}

if (localsCode) {
code += options.modules.namedExport
? `${localsCode}`
? localsCode
: `___CSS_LOADER_EXPORT___.locals = {\n${localsCode}\n};\n`;
}

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

return `// Exports\n${code}`;
return code;
}

async function resolveRequests(resolve, context, possibleRequests) {
Expand Down
7 changes: 4 additions & 3 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -28,9 +28,10 @@ exports[`loader issue #1033 (2): warnings 1`] = `Array []`;
exports[`loader issue #1033: errors 1`] = `Array []`;

exports[`loader issue #1033: module 1`] = `
"var ___CSS_LOADER_EXPORT___ = {};
// Exports
export default ___CSS_LOADER_EXPORT___;
"// Exports
export default {
};
"
`;

Expand Down

0 comments on commit 24c0a12

Please sign in to comment.