Skip to content

Commit

Permalink
refactor: code (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 27, 2020
1 parent 1ea4b7f commit 41e9eb7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
53 changes: 28 additions & 25 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,28 +206,33 @@ export function pitch(request) {
}

let locals;
let result = '';

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

try {
let dependencies;
let exports = evalModuleCode(this, source, request);

if (
options.modules &&
options.modules.namedExport &&
// eslint-disable-next-line no-underscore-dangle
exports.__esModule
) {
Object.keys(exports).forEach((key) => {
const originalExports = evalModuleCode(this, source, request);

// eslint-disable-next-line no-underscore-dangle
exports = originalExports.__esModule
? originalExports.default
: originalExports;

if (namedExport) {
locals = '';

Object.keys(originalExports).forEach((key) => {
if (key !== 'default') {
result += `\nexport const ${key} = "${exports[key]}";`;
locals += `\nexport const ${key} = "${originalExports[key]}";`;
}
});
} else {
locals = exports && exports.locals;
}

// eslint-disable-next-line no-underscore-dangle
exports = exports.__esModule ? exports.default : exports;
locals = exports && exports.locals;
let dependencies;

if (!Array.isArray(exports)) {
dependencies = [[null, exports]];
Expand All @@ -244,23 +249,21 @@ export function pitch(request) {
};
});
}

addDependencies(dependencies);
} catch (e) {
return callback(e);
}

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

if (!result) {
result += locals
? `\n${
const result = locals
? namedExport
? locals
: `\n${
esModule ? 'export default' : 'module.exports ='
} ${JSON.stringify(locals)};`
: esModule
? `\nexport {};`
: '';
}
: esModule
? `\nexport {};`
: '';

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

Expand Down
22 changes: 9 additions & 13 deletions test/TestCases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ function clearDirectory(dirPath) {
} catch (e) {
return;
}
if (files.length > 0)
if (files.length > 0) {
for (let i = 0; i < files.length; i++) {
const filePath = `${dirPath}/${files[i]}`;
if (fs.statSync(filePath).isFile()) fs.unlinkSync(filePath);
else clearDirectory(filePath);

if (fs.statSync(filePath).isFile()) {
fs.unlinkSync(filePath);
} else {
clearDirectory(filePath);
}
}
}

fs.rmdirSync(dirPath);
}

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

done();

// eslint-disable-next-line no-console
// console.log(
// stats.toString({
// context: path.resolve(__dirname, '..'),
// chunks: true,
// chunkModules: true,
// modules: false,
// })
// );

if (stats.hasErrors() && stats.hasWarnings()) {
done(
new Error(
Expand Down

0 comments on commit 41e9eb7

Please sign in to comment.