Skip to content

refactor: code #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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