Skip to content

Commit 71f812f

Browse files
feat: esModule export named
1 parent 2054896 commit 71f812f

9 files changed

+77
-68
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ module.exports = {
10361036
};
10371037
```
10381038

1039-
### `exportNamed`
1039+
### `namedExport`
10401040

10411041
Type: `Boolean`
10421042
Default: `false`
@@ -1076,8 +1076,9 @@ module.exports = {
10761076
loader: 'css-loader',
10771077
options: {
10781078
esModule: true,
1079-
modules: true,
1080-
exportNamed: true,
1079+
modules: {
1080+
namedExport: true,
1081+
},
10811082
},
10821083
},
10831084
],

src/index.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -171,22 +171,23 @@ export default function loader(content, map, meta) {
171171
);
172172
});
173173

174-
const exportNamed =
175-
typeof options.exportNamed !== 'undefined'
176-
? options.exportNamed
174+
const namedExport =
175+
typeof options.modules === 'object' &&
176+
typeof options.modules.namedExport !== 'undefined'
177+
? options.modules.namedExport
177178
: false;
178179

179-
const { localsConvention } = exportNamed
180+
const { localsConvention } = namedExport
180181
? { localsConvention: 'camelCaseOnly' }
181182
: options;
182183

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

186-
if (Boolean(exportNamed) && Boolean(exportNamed) !== Boolean(esModule)) {
187+
if (Boolean(namedExport) && Boolean(namedExport) !== Boolean(esModule)) {
187188
this.emitError(
188189
new Error(
189-
'`Options.exportNamed` can not use without `options.esModule`'
190+
'`Options.module.namedExport` cannot be used without `options.esModule`'
190191
)
191192
);
192193
}
@@ -196,7 +197,7 @@ export default function loader(content, map, meta) {
196197
exportType,
197198
imports,
198199
esModule,
199-
exportNamed
200+
namedExport
200201
);
201202
const moduleCode = getModuleCode(
202203
result,
@@ -206,15 +207,15 @@ export default function loader(content, map, meta) {
206207
urlReplacements,
207208
icssReplacements,
208209
esModule,
209-
exportNamed
210+
namedExport
210211
);
211212
const exportCode = getExportCode(
212213
exports,
213214
exportType,
214215
localsConvention,
215216
icssReplacements,
216217
esModule,
217-
exportNamed
218+
namedExport
218219
);
219220

220221
return callback(null, `${importCode}${moduleCode}${exportCode}`);

src/options.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090
"instanceof": "Function"
9191
}
9292
]
93+
},
94+
"namedExport": {
95+
"description": "Use the named export ES modules.",
96+
"type": "boolean"
9397
}
9498
}
9599
}
@@ -121,10 +125,6 @@
121125
"esModule": {
122126
"description": "Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule).",
123127
"type": "boolean"
124-
},
125-
"exportNamed": {
126-
"description": "Use the named export ES modules.",
127-
"type": "boolean"
128128
}
129129
},
130130
"type": "object"

src/utils.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function getImportCode(
263263
exportType,
264264
imports,
265265
esModule,
266-
exportNamed
266+
namedExport
267267
) {
268268
let code = '';
269269

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

284284
code += esModule
285-
? icss && exportNamed
285+
? icss && namedExport
286286
? `import ${importName}, * as ${importName}_NAMED___ from ${url};\n`
287287
: `import ${importName} from ${url};\n`
288288
: `var ${importName} = require(${url});\n`;
@@ -299,7 +299,7 @@ function getModuleCode(
299299
urlReplacements,
300300
icssReplacements,
301301
esModule,
302-
exportNamed
302+
namedExport
303303
) {
304304
if (exportType !== 'full') {
305305
return 'var ___CSS_LOADER_EXPORT___ = {};\n';
@@ -349,7 +349,7 @@ function getModuleCode(
349349
const { replacementName, importName, localName } = replacement;
350350

351351
code = code.replace(new RegExp(replacementName, 'g'), () =>
352-
exportNamed
352+
namedExport
353353
? `" + ${importName}_NAMED___[${JSON.stringify(
354354
camelCase(localName)
355355
)}] + "`
@@ -372,7 +372,7 @@ function getExportCode(
372372
localsConvention,
373373
icssReplacements,
374374
esModule,
375-
exportNamed
375+
namedExport
376376
) {
377377
let code = '';
378378
let localsCode = '';
@@ -385,7 +385,7 @@ function getExportCode(
385385

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

388-
if (exportNamed) {
388+
if (namedExport) {
389389
namedCode += `export const ${name} = ${JSON.stringify(value)};\n`;
390390
}
391391
};
@@ -435,7 +435,7 @@ function getExportCode(
435435
() => `" + ${importName}.locals[${JSON.stringify(localName)}] + "`
436436
);
437437

438-
if (exportNamed) {
438+
if (namedExport) {
439439
namedCode = namedCode.replace(
440440
new RegExp(replacementName, 'g'),
441441
() =>

test/__snapshots__/esModule-option.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ exports[`"esModule" option should emit error when class has unsupported name: wa
1212
exports[`"esModule" option should emit error when exportNamed true && esModule false: errors 1`] = `
1313
Array [
1414
"ModuleError: Module Error (from \`replaced original path\`):
15-
\`Options.exportNamed\` can not use without \`options.esModule\`",
15+
\`Options.module.namedExport\` cannot be used without \`options.esModule\`",
1616
]
1717
`;
1818

0 commit comments

Comments
 (0)