Skip to content

Commit 41e9eb7

Browse files
refactor: code (#576)
1 parent 1ea4b7f commit 41e9eb7

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

src/loader.js

+28-25
Original file line numberDiff line numberDiff line change
@@ -206,28 +206,33 @@ export function pitch(request) {
206206
}
207207

208208
let locals;
209-
let result = '';
209+
210+
const esModule =
211+
typeof options.esModule !== 'undefined' ? options.esModule : false;
212+
const namedExport =
213+
esModule && options.modules && options.modules.namedExport;
210214

211215
try {
212-
let dependencies;
213-
let exports = evalModuleCode(this, source, request);
214-
215-
if (
216-
options.modules &&
217-
options.modules.namedExport &&
218-
// eslint-disable-next-line no-underscore-dangle
219-
exports.__esModule
220-
) {
221-
Object.keys(exports).forEach((key) => {
216+
const originalExports = evalModuleCode(this, source, request);
217+
218+
// eslint-disable-next-line no-underscore-dangle
219+
exports = originalExports.__esModule
220+
? originalExports.default
221+
: originalExports;
222+
223+
if (namedExport) {
224+
locals = '';
225+
226+
Object.keys(originalExports).forEach((key) => {
222227
if (key !== 'default') {
223-
result += `\nexport const ${key} = "${exports[key]}";`;
228+
locals += `\nexport const ${key} = "${originalExports[key]}";`;
224229
}
225230
});
231+
} else {
232+
locals = exports && exports.locals;
226233
}
227234

228-
// eslint-disable-next-line no-underscore-dangle
229-
exports = exports.__esModule ? exports.default : exports;
230-
locals = exports && exports.locals;
235+
let dependencies;
231236

232237
if (!Array.isArray(exports)) {
233238
dependencies = [[null, exports]];
@@ -244,23 +249,21 @@ export function pitch(request) {
244249
};
245250
});
246251
}
252+
247253
addDependencies(dependencies);
248254
} catch (e) {
249255
return callback(e);
250256
}
251257

252-
const esModule =
253-
typeof options.esModule !== 'undefined' ? options.esModule : false;
254-
255-
if (!result) {
256-
result += locals
257-
? `\n${
258+
const result = locals
259+
? namedExport
260+
? locals
261+
: `\n${
258262
esModule ? 'export default' : 'module.exports ='
259263
} ${JSON.stringify(locals)};`
260-
: esModule
261-
? `\nexport {};`
262-
: '';
263-
}
264+
: esModule
265+
? `\nexport {};`
266+
: '';
264267

265268
let resultSource = `// extracted by ${pluginName}`;
266269

test/TestCases.test.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ function clearDirectory(dirPath) {
1515
} catch (e) {
1616
return;
1717
}
18-
if (files.length > 0)
18+
if (files.length > 0) {
1919
for (let i = 0; i < files.length; i++) {
2020
const filePath = `${dirPath}/${files[i]}`;
21-
if (fs.statSync(filePath).isFile()) fs.unlinkSync(filePath);
22-
else clearDirectory(filePath);
21+
22+
if (fs.statSync(filePath).isFile()) {
23+
fs.unlinkSync(filePath);
24+
} else {
25+
clearDirectory(filePath);
26+
}
2327
}
28+
}
29+
2430
fs.rmdirSync(dirPath);
2531
}
2632

@@ -116,16 +122,6 @@ describe('TestCases', () => {
116122

117123
done();
118124

119-
// eslint-disable-next-line no-console
120-
// console.log(
121-
// stats.toString({
122-
// context: path.resolve(__dirname, '..'),
123-
// chunks: true,
124-
// chunkModules: true,
125-
// modules: false,
126-
// })
127-
// );
128-
129125
if (stats.hasErrors() && stats.hasWarnings()) {
130126
done(
131127
new Error(

0 commit comments

Comments
 (0)