From daf0b818f7c217a7d892e465ec0b96772f903225 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Thu, 27 Aug 2020 16:31:19 +0300 Subject: [PATCH] refactor: code --- src/loader.js | 53 ++++++++++++++++++++++-------------------- test/TestCases.test.js | 22 +++++++----------- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/loader.js b/src/loader.js index 3cb19a3f..7b0a0ce5 100644 --- a/src/loader.js +++ b/src/loader.js @@ -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]]; @@ -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}`; diff --git a/test/TestCases.test.js b/test/TestCases.test.js index 575a1514..08d0233d 100644 --- a/test/TestCases.test.js +++ b/test/TestCases.test.js @@ -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); } @@ -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(