Skip to content

Commit

Permalink
feat: esModule export named
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Jul 15, 2020
1 parent 2054896 commit 71f812f
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 68 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ module.exports = {
};
```

### `exportNamed`
### `namedExport`

Type: `Boolean`
Default: `false`
Expand Down Expand Up @@ -1076,8 +1076,9 @@ module.exports = {
loader: 'css-loader',
options: {
esModule: true,
modules: true,
exportNamed: true,
modules: {
namedExport: true,
},
},
},
],
Expand Down
19 changes: 10 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,23 @@ export default function loader(content, map, meta) {
);
});

const exportNamed =
typeof options.exportNamed !== 'undefined'
? options.exportNamed
const namedExport =
typeof options.modules === 'object' &&
typeof options.modules.namedExport !== 'undefined'
? options.modules.namedExport
: false;

const { localsConvention } = exportNamed
const { localsConvention } = namedExport
? { localsConvention: 'camelCaseOnly' }
: options;

const esModule =
typeof options.esModule !== 'undefined' ? options.esModule : false;

if (Boolean(exportNamed) && Boolean(exportNamed) !== Boolean(esModule)) {
if (Boolean(namedExport) && Boolean(namedExport) !== Boolean(esModule)) {
this.emitError(
new Error(
'`Options.exportNamed` can not use without `options.esModule`'
'`Options.module.namedExport` cannot be used without `options.esModule`'
)
);
}
Expand All @@ -196,7 +197,7 @@ export default function loader(content, map, meta) {
exportType,
imports,
esModule,
exportNamed
namedExport
);
const moduleCode = getModuleCode(
result,
Expand All @@ -206,15 +207,15 @@ export default function loader(content, map, meta) {
urlReplacements,
icssReplacements,
esModule,
exportNamed
namedExport
);
const exportCode = getExportCode(
exports,
exportType,
localsConvention,
icssReplacements,
esModule,
exportNamed
namedExport
);

return callback(null, `${importCode}${moduleCode}${exportCode}`);
Expand Down
8 changes: 4 additions & 4 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
"instanceof": "Function"
}
]
},
"namedExport": {
"description": "Use the named export ES modules.",
"type": "boolean"
}
}
}
Expand Down Expand Up @@ -121,10 +125,6 @@
"esModule": {
"description": "Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule).",
"type": "boolean"
},
"exportNamed": {
"description": "Use the named export ES modules.",
"type": "boolean"
}
},
"type": "object"
Expand Down
14 changes: 7 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function getImportCode(
exportType,
imports,
esModule,
exportNamed
namedExport
) {
let code = '';

Expand All @@ -282,7 +282,7 @@ function getImportCode(
const { importName, url, icss } = item;

code += esModule
? icss && exportNamed
? icss && namedExport
? `import ${importName}, * as ${importName}_NAMED___ from ${url};\n`
: `import ${importName} from ${url};\n`
: `var ${importName} = require(${url});\n`;
Expand All @@ -299,7 +299,7 @@ function getModuleCode(
urlReplacements,
icssReplacements,
esModule,
exportNamed
namedExport
) {
if (exportType !== 'full') {
return 'var ___CSS_LOADER_EXPORT___ = {};\n';
Expand Down Expand Up @@ -349,7 +349,7 @@ function getModuleCode(
const { replacementName, importName, localName } = replacement;

code = code.replace(new RegExp(replacementName, 'g'), () =>
exportNamed
namedExport
? `" + ${importName}_NAMED___[${JSON.stringify(
camelCase(localName)
)}] + "`
Expand All @@ -372,7 +372,7 @@ function getExportCode(
localsConvention,
icssReplacements,
esModule,
exportNamed
namedExport
) {
let code = '';
let localsCode = '';
Expand All @@ -385,7 +385,7 @@ function getExportCode(

localsCode += `\t${JSON.stringify(name)}: ${JSON.stringify(value)}`;

if (exportNamed) {
if (namedExport) {
namedCode += `export const ${name} = ${JSON.stringify(value)};\n`;
}
};
Expand Down Expand Up @@ -435,7 +435,7 @@ function getExportCode(
() => `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
);

if (exportNamed) {
if (namedExport) {
namedCode = namedCode.replace(
new RegExp(replacementName, 'g'),
() =>
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/esModule-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports[`"esModule" option should emit error when class has unsupported name: wa
exports[`"esModule" option should emit error when exportNamed true && esModule false: errors 1`] = `
Array [
"ModuleError: Module Error (from \`replaced original path\`):
\`Options.exportNamed\` can not use without \`options.esModule\`",
\`Options.module.namedExport\` cannot be used without \`options.esModule\`",
]
`;

Expand Down
Loading

0 comments on commit 71f812f

Please sign in to comment.