diff --git a/packages/commonjs/src/index.js b/packages/commonjs/src/index.js index 19d2b203b..2e2c8a3ff 100644 --- a/packages/commonjs/src/index.js +++ b/packages/commonjs/src/index.js @@ -80,11 +80,14 @@ export default function commonjs(options = {}) { return null; } + const moduleInfo = this.getModuleInfo(id); + const transformed = transformCommonjs( this.parse, code, id, - this.getModuleInfo(id).isEntry, + moduleInfo.isEntry, + moduleInfo.importers && moduleInfo.importers.length > 0, isEsModule, ignoreGlobal || isEsModule, ignoreRequire, @@ -218,7 +221,11 @@ export default function commonjs(options = {}) { return `import { __moduleExports } from ${JSON.stringify( actualId )}; export default __moduleExports;`; - else if (esModulesWithoutDefaultExport.has(actualId)) + /* return `export { default } from ${JSON.stringify( + actualId + )};`;*/ else if ( + esModulesWithoutDefaultExport.has(actualId) + ) return `import * as ${name} from ${JSON.stringify(actualId)}; export default ${name};`; else if (esModulesWithDefaultExport.has(actualId)) { return `export {default} from ${JSON.stringify(actualId)};`; diff --git a/packages/commonjs/src/transform.js b/packages/commonjs/src/transform.js index 9118f7964..043359cdc 100644 --- a/packages/commonjs/src/transform.js +++ b/packages/commonjs/src/transform.js @@ -106,6 +106,7 @@ export function transformCommonjs( code, id, isEntry, + hasImporters, isEsModule, ignoreGlobal, ignoreRequire, @@ -569,7 +570,7 @@ export function transformCommonjs( let wrapperEnd = ''; const moduleName = deconflict(scope, globals, getName(id)); - if (!isEntry && !isEsModule) { + if ((!isEntry || hasImporters) && !isEsModule) { const exportModuleExports = { str: `export { ${moduleName} as __moduleExports };`, name: '__moduleExports' @@ -656,7 +657,7 @@ export function transformCommonjs( } } - if (!hasDefaultExport && (names.length || (!isEntry && !isEsModule))) { + if (!hasDefaultExport && (names.length || ((!isEntry || hasImporters) && !isEsModule))) { wrapperEnd = `\n\nvar ${moduleName} = {\n${names .map(({ name, deconflicted }) => `\t${name}: ${deconflicted}`) .join(',\n')}\n};`; @@ -686,7 +687,8 @@ export function transformCommonjs( .trim() .append(wrapperEnd); - const injectExportBlock = hasDefaultExport || named.length > 0 || shouldWrap || !isEntry; + const injectExportBlock = + hasDefaultExport || named.length > 0 || shouldWrap || !isEntry || hasImporters; if (injectExportBlock) { magicString.append(exportBlock); } diff --git a/packages/commonjs/test/fixtures/form/multi-entry-module-exports/_config.js b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/_config.js new file mode 100644 index 000000000..54e5ab230 --- /dev/null +++ b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/_config.js @@ -0,0 +1,9 @@ +module.exports = { + multi: { + output1: 'input1.js', + output2: 'input2.js' + }, + importers: { + output2: ['input1.js'] + } +}; diff --git a/packages/commonjs/test/fixtures/form/multi-entry-module-exports/input1.js b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/input1.js new file mode 100644 index 000000000..7aff633ef --- /dev/null +++ b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/input1.js @@ -0,0 +1,4 @@ +const t2 = require('./input2.js'); + +console.log(t2); +module.exports = 1; diff --git a/packages/commonjs/test/fixtures/form/multi-entry-module-exports/input2.js b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/input2.js new file mode 100644 index 000000000..39d4e85af --- /dev/null +++ b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/input2.js @@ -0,0 +1,3 @@ +module.exports = { + a: 2 +}; diff --git a/packages/commonjs/test/fixtures/form/multi-entry-module-exports/output1.js b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/output1.js new file mode 100644 index 000000000..fd1e0719e --- /dev/null +++ b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/output1.js @@ -0,0 +1,8 @@ +import './input2.js'; +import t2 from '_./input2.js?commonjs-proxy'; + +console.log(t2); +var input1 = 1; + +export default input1; +export { input1 as __moduleExports }; diff --git a/packages/commonjs/test/fixtures/form/multi-entry-module-exports/output2.js b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/output2.js new file mode 100644 index 000000000..7813ceb5f --- /dev/null +++ b/packages/commonjs/test/fixtures/form/multi-entry-module-exports/output2.js @@ -0,0 +1,8 @@ +var input2 = { + a: 2 +}; + +export default input2; +export { input2 as __moduleExports }; +var input2_1 = input2.a; +export { input2_1 as a }; diff --git a/packages/commonjs/test/form.js b/packages/commonjs/test/form.js index 0ecf6e33d..c0cba3061 100644 --- a/packages/commonjs/test/form.js +++ b/packages/commonjs/test/form.js @@ -27,35 +27,52 @@ readdirSync('./fixtures/form').forEach((dir) => { config = {}; } + const { transform } = commonjs(config.options); + + const inputEntries = []; + + if (typeof config.multi === 'object') { + for (const [key, entry] of Object.entries(config.multi)) { + inputEntries.push([key, `fixtures/form/${dir}/${entry}`]); + } + } else { + inputEntries.push(['output', `fixtures/form/${dir}/input.js`]); + } + (config.solo ? test.only : test)(dir, async (t) => { - const { transform } = commonjs(config.options); - const id = `./fixtures/form/${dir}/input.js`; + for (const [outputName, id] of inputEntries) { + const { transform } = commonjs(config.options); - transformContext.getModuleInfo = (moduleId) => { - return { - isEntry: config.entry && moduleId === id + transformContext.getModuleInfo = (moduleId) => { + return { + isEntry: config.entry && moduleId === id, + importers: + config.importers && config.importers[outputName] + ? config.importers[outputName].map((x) => `fixtures/form/${dir}/${x}`) + : [] + }; }; - }; - transformContext.error = (base, props) => { - let error = base; - if (!(base instanceof Error)) error = Object.assign(new Error(base.message), base); - if (props) Object.assign(error, props); - throw error; - }; - - const input = readFileSync(id, 'utf-8'); - - let outputFile = `fixtures/form/${dir}/output`; - if (existsSync(`${outputFile}.${process.platform}.js`)) { - outputFile += `.${process.platform}.js`; - } else { - outputFile += '.js'; - } + transformContext.error = (base, props) => { + let error = base; + if (!(base instanceof Error)) error = Object.assign(new Error(base.message), base); + if (props) Object.assign(error, props); + throw error; + }; + + const input = readFileSync(id, 'utf-8'); - const expected = readFileSync(outputFile, 'utf-8').trim(); - const transformed = transform.call(transformContext, input, id); - const actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '_'); + let outputFile = `fixtures/form/${dir}/${outputName}`; + if (existsSync(`${outputFile}.${process.platform}.js`)) { + outputFile += `.${process.platform}.js`; + } else { + outputFile += '.js'; + } - t.is(actual, expected); + const expected = readFileSync(outputFile, 'utf-8').trim(); + const transformed = transform.call(transformContext, input, id); + const actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '_'); + + t.is(actual, expected); + } }); }); diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index c1fd38dee..b7607343d 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ