From e53c73421b12ab663a62560a0d3d8203fc2535e7 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 25 Mar 2019 06:39:56 +0100 Subject: [PATCH] Support exporting live-bindings from other chunks or external dependencies (#2765) * Implement basic live binding support across chunks and external imports * Refine cjs finalizer --- src/Chunk.ts | 6 +- src/finalisers/amd.ts | 7 +- src/finalisers/cjs.ts | 109 +++++++----------- src/finalisers/iife.ts | 3 +- src/finalisers/shared/getExportBlock.ts | 22 +++- src/finalisers/umd.ts | 3 +- .../samples/chunking-reexport/_config.js | 3 +- .../chunking-reexport/_expected/amd/main1.js | 7 +- .../chunking-reexport/_expected/amd/main2.js | 7 +- .../chunking-reexport/_expected/cjs/main1.js | 7 +- .../chunking-reexport/_expected/cjs/main2.js | 7 +- .../samples/chunking-source-maps/_config.js | 2 +- .../samples/chunking-star-external/_config.js | 3 +- .../_expected/amd/main1.js | 16 ++- .../_expected/amd/main2.js | 16 ++- .../_expected/cjs/main1.js | 16 ++- .../_expected/cjs/main2.js | 16 ++- .../samples/compact-empty-external/_config.js | 9 ++ .../compact-empty-external/_expected/amd.js | 1 + .../compact-empty-external/_expected/cjs.js | 1 + .../compact-empty-external/_expected/es.js | 1 + .../compact-empty-external/_expected/iife.js | 1 + .../_expected/system.js | 1 + .../compact-empty-external/_expected/umd.js | 1 + .../samples/compact-empty-external/main.js | 1 + .../compact-multiple-imports/_config.js | 11 ++ .../compact-multiple-imports/_expected/amd.js | 2 + .../compact-multiple-imports/_expected/cjs.js | 2 + .../compact-multiple-imports/_expected/es.js | 2 + .../_expected/iife.js | 2 + .../_expected/system.js | 2 + .../compact-multiple-imports/_expected/umd.js | 2 + .../samples/compact-multiple-imports/main.js | 8 ++ test/form/samples/compact/main.js | 2 +- .../export-all-multiple/_expected/amd.js | 27 ++++- .../export-all-multiple/_expected/cjs.js | 27 ++++- .../export-all-multiple/_expected/iife.js | 27 ++++- .../export-all-multiple/_expected/umd.js | 27 ++++- .../samples/export-default-import/_config.js | 5 +- .../external-export-tracing/_config.js | 1 + .../external-export-tracing/_expected/amd.js | 7 +- .../external-export-tracing/_expected/cjs.js | 7 +- .../external-export-tracing/_expected/iife.js | 7 +- .../external-export-tracing/_expected/umd.js | 7 +- .../_config.js | 1 + .../external-namespace-and-named/_config.js | 5 +- .../external-namespace-reexport/_config.js | 1 + .../_config.js | 5 +- .../import-specifier-deshadowing/_config.js | 5 +- .../samples/interop-false-reexport/_config.js | 3 +- .../interop-false-reexport/_expected/amd.js | 7 +- .../interop-false-reexport/_expected/cjs.js | 7 +- .../interop-false-reexport/_expected/iife.js | 7 +- .../interop-false-reexport/_expected/umd.js | 7 +- test/form/samples/interop-false/_config.js | 3 +- .../namespace-import-reexport/_config.js | 1 + test/form/samples/paths-function/_config.js | 1 + test/form/samples/paths/_config.js | 1 + test/form/samples/prefer-const/_config.js | 1 + .../samples/re-export-aliasing/_config.js | 2 +- .../re-export-aliasing/_expected/amd.js | 7 +- .../re-export-aliasing/_expected/cjs.js | 7 +- .../re-export-aliasing/_expected/iife.js | 7 +- .../re-export-aliasing/_expected/umd.js | 7 +- .../_expected/amd.js | 9 +- .../_expected/cjs.js | 9 +- .../_expected/iife.js | 9 +- .../_expected/umd.js | 9 +- .../reexports-from-external/_expected/amd.js | 9 +- .../reexports-from-external/_expected/cjs.js | 9 +- .../reexports-from-external/_expected/iife.js | 9 +- .../reexports-from-external/_expected/umd.js | 9 +- .../_expected/amd.js | 7 +- .../_expected/cjs.js | 7 +- .../_expected/iife.js | 7 +- .../_expected/umd.js | 7 +- test/form/samples/unused-import/_config.js | 5 +- test/form/samples/url-external/_config.js | 3 + .../compact-multiple-imports/_config.js | 16 +++ .../compact-multiple-imports/_expected/amd.js | 6 + .../compact-multiple-imports/_expected/cjs.js | 6 + .../compact-multiple-imports/_expected/es.js | 6 + .../_expected/iife.js | 6 + .../_expected/system.js | 6 + .../compact-multiple-imports/_expected/umd.js | 6 + .../samples/compact-multiple-imports/main.js | 8 ++ .../_config.js | 20 ++++ .../cross-chunk-live-binding-compact/main.js | 2 + .../cross-chunk-live-binding-compact/named.js | 2 + .../cross-chunk-live-binding-compact/star.js | 2 + .../cross-chunk-live-binding/_config.js | 17 +++ .../samples/cross-chunk-live-binding/main.js | 2 + .../samples/cross-chunk-live-binding/named.js | 2 + .../samples/cross-chunk-live-binding/star.js | 2 + .../external-live-binding-compact/_config.js | 45 ++++++++ .../external-live-binding-compact/main.js | 2 + .../samples/external-live-binding/_config.js | 44 +++++++ .../samples/external-live-binding/main.js | 2 + 98 files changed, 696 insertions(+), 140 deletions(-) create mode 100644 test/form/samples/compact-empty-external/_config.js create mode 100644 test/form/samples/compact-empty-external/_expected/amd.js create mode 100644 test/form/samples/compact-empty-external/_expected/cjs.js create mode 100644 test/form/samples/compact-empty-external/_expected/es.js create mode 100644 test/form/samples/compact-empty-external/_expected/iife.js create mode 100644 test/form/samples/compact-empty-external/_expected/system.js create mode 100644 test/form/samples/compact-empty-external/_expected/umd.js create mode 100644 test/form/samples/compact-empty-external/main.js create mode 100644 test/form/samples/compact-multiple-imports/_config.js create mode 100644 test/form/samples/compact-multiple-imports/_expected/amd.js create mode 100644 test/form/samples/compact-multiple-imports/_expected/cjs.js create mode 100644 test/form/samples/compact-multiple-imports/_expected/es.js create mode 100644 test/form/samples/compact-multiple-imports/_expected/iife.js create mode 100644 test/form/samples/compact-multiple-imports/_expected/system.js create mode 100644 test/form/samples/compact-multiple-imports/_expected/umd.js create mode 100644 test/form/samples/compact-multiple-imports/main.js create mode 100644 test/function/samples/compact-multiple-imports/_config.js create mode 100644 test/function/samples/compact-multiple-imports/_expected/amd.js create mode 100644 test/function/samples/compact-multiple-imports/_expected/cjs.js create mode 100644 test/function/samples/compact-multiple-imports/_expected/es.js create mode 100644 test/function/samples/compact-multiple-imports/_expected/iife.js create mode 100644 test/function/samples/compact-multiple-imports/_expected/system.js create mode 100644 test/function/samples/compact-multiple-imports/_expected/umd.js create mode 100644 test/function/samples/compact-multiple-imports/main.js create mode 100644 test/function/samples/cross-chunk-live-binding-compact/_config.js create mode 100644 test/function/samples/cross-chunk-live-binding-compact/main.js create mode 100644 test/function/samples/cross-chunk-live-binding-compact/named.js create mode 100644 test/function/samples/cross-chunk-live-binding-compact/star.js create mode 100644 test/function/samples/cross-chunk-live-binding/_config.js create mode 100644 test/function/samples/cross-chunk-live-binding/main.js create mode 100644 test/function/samples/cross-chunk-live-binding/named.js create mode 100644 test/function/samples/cross-chunk-live-binding/star.js create mode 100644 test/function/samples/external-live-binding-compact/_config.js create mode 100644 test/function/samples/external-live-binding-compact/main.js create mode 100644 test/function/samples/external-live-binding/_config.js create mode 100644 test/function/samples/external-live-binding/main.js diff --git a/src/Chunk.ts b/src/Chunk.ts index 94b4da625c5..c489b10d10e 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -68,6 +68,7 @@ export type ChunkExports = { export interface ReexportSpecifier { imported: string; + needsLiveBinding: boolean; reexported: string; } @@ -815,6 +816,7 @@ export default class Chunk { for (let exportName of this.getExportNames()) { let exportModule: Chunk | ExternalModule; let importName: string; + let needsLiveBinding = false; if (exportName[0] === '*') { exportModule = this.graph.moduleById.get(exportName.substr(1)); importName = exportName = '*'; @@ -826,14 +828,16 @@ export default class Chunk { if (module instanceof Module) { exportModule = module.chunk; importName = module.chunk.getVariableExportName(variable); + needsLiveBinding = variable.isReassigned; } else { exportModule = module; importName = variable.name; + needsLiveBinding = true; } } let exportDeclaration = reexportDeclarations.get(exportModule); if (!exportDeclaration) reexportDeclarations.set(exportModule, (exportDeclaration = [])); - exportDeclaration.push({ imported: importName, reexported: exportName }); + exportDeclaration.push({ imported: importName, reexported: exportName, needsLiveBinding }); } const importsAsArray = Array.from(this.imports); diff --git a/src/finalisers/amd.ts b/src/finalisers/amd.ts index cd6b63cfca9..963bc462b59 100644 --- a/src/finalisers/amd.ts +++ b/src/finalisers/amd.ts @@ -13,7 +13,7 @@ export default function amd( dynamicImport, exports, hasExports, - indentString, + indentString: t, intro, isEntryModuleFacade, namedExportsMode, @@ -69,7 +69,8 @@ export default function amd( dependencies, namedExportsMode, options.interop, - options.compact + options.compact, + t ); if (exportBlock) magicString.append(n + n + exportBlock); if (namedExportsMode && hasExports && isEntryModuleFacade && options.esModule) @@ -77,7 +78,7 @@ export default function amd( if (outro) magicString.append(outro); return magicString - .indent(indentString) + .indent(t) .append(n + n + '});') .prepend(wrapperStart); } diff --git a/src/finalisers/cjs.ts b/src/finalisers/cjs.ts index a06b65e208f..7f0320fe64d 100644 --- a/src/finalisers/cjs.ts +++ b/src/finalisers/cjs.ts @@ -11,6 +11,7 @@ export default function cjs( dependencies, exports, hasExports, + indentString: t, intro, isEntryModuleFacade, namedExportsMode, @@ -32,78 +33,49 @@ export default function cjs( const interop = options.interop !== false; let importBlock: string; - if (options.compact) { - let definingVariable = false; - importBlock = ''; - - dependencies.forEach( - ({ - id, - namedExportsMode, - isChunk, - name, - reexports, - imports, - exportsNames, - exportsDefault - }) => { - if (!reexports && !imports) { - importBlock += definingVariable ? ';' : ','; - definingVariable = false; - importBlock += `require('${id}')`; - } else { - importBlock += definingVariable ? ',' : `${importBlock ? ';' : ''}${varOrConst} `; - definingVariable = true; - - if (!interop || isChunk || !exportsDefault || !namedExportsMode) { - importBlock += `${name}=require('${id}')`; - } else { - needsInterop = true; - if (exportsNames) - importBlock += `${name}=require('${id}'),${name}__default=${INTEROP_DEFAULT_VARIABLE}(${name})`; - else importBlock += `${name}=${INTEROP_DEFAULT_VARIABLE}(require('${id}'))`; - } - } + let definingVariable = false; + importBlock = ''; + for (const { + id, + namedExportsMode, + isChunk, + name, + reexports, + imports, + exportsNames, + exportsDefault + } of dependencies) { + if (!reexports && !imports) { + if (importBlock) { + importBlock += !options.compact || definingVariable ? `;${n}` : ','; } - ); - if (importBlock.length) importBlock += ';'; - } else { - importBlock = dependencies - .map( - ({ - id, - namedExportsMode, - isChunk, - name, - reexports, - imports, - exportsNames, - exportsDefault - }) => { - if (!reexports && !imports) return `require('${id}');`; - - if (!interop || isChunk || !exportsDefault || !namedExportsMode) - return `${varOrConst} ${name} = require('${id}');`; - - needsInterop = true; - - if (exportsNames) - return ( - `${varOrConst} ${name} = require('${id}');` + - `\n${varOrConst} ${name}__default = ${INTEROP_DEFAULT_VARIABLE}(${name});` - ); - - return `${varOrConst} ${name} = ${INTEROP_DEFAULT_VARIABLE}(require('${id}'));`; - } - ) - .join('\n'); + definingVariable = false; + importBlock += `require('${id}')`; + } else { + importBlock += + options.compact && definingVariable ? ',' : `${importBlock ? `;${n}` : ''}${varOrConst} `; + definingVariable = true; + + if (!interop || isChunk || !exportsDefault || !namedExportsMode) { + importBlock += `${name}${_}=${_}require('${id}')`; + } else { + needsInterop = true; + if (exportsNames) + importBlock += `${name}${_}=${_}require('${id}')${ + options.compact ? ',' : `;\n${varOrConst} ` + }${name}__default${_}=${_}${INTEROP_DEFAULT_VARIABLE}(${name})`; + else importBlock += `${name}${_}=${_}${INTEROP_DEFAULT_VARIABLE}(require('${id}'))`; + } + } } + if (importBlock) importBlock += ';'; if (needsInterop) { - if (options.compact) - intro += `function ${INTEROP_DEFAULT_VARIABLE}(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}`; - else - intro += `function ${INTEROP_DEFAULT_VARIABLE} (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }\n\n`; + const ex = options.compact ? 'e' : 'ex'; + intro += + `function ${INTEROP_DEFAULT_VARIABLE}${_}(${ex})${_}{${_}return${_}` + + `(${ex}${_}&&${_}(typeof ${ex}${_}===${_}'object')${_}&&${_}'default'${_}in ${ex})${_}` + + `?${_}${ex}['default']${_}:${_}${ex}${options.compact ? '' : '; '}}${n}${n}`; } if (importBlock) intro += importBlock + n + n; @@ -114,6 +86,7 @@ export default function cjs( namedExportsMode, options.interop, options.compact, + t, `module.exports${_}=${_}` ); diff --git a/src/finalisers/iife.ts b/src/finalisers/iife.ts index 1a66a775376..a092c8a2234 100644 --- a/src/finalisers/iife.ts +++ b/src/finalisers/iife.ts @@ -95,7 +95,8 @@ export default function iife( dependencies, namedExportsMode, options.interop, - options.compact + options.compact, + t ); if (exportBlock) magicString.append(n + n + exportBlock); if (outro) magicString.append(outro); diff --git a/src/finalisers/shared/getExportBlock.ts b/src/finalisers/shared/getExportBlock.ts index 4cc77ce503e..bf3c6b45362 100644 --- a/src/finalisers/shared/getExportBlock.ts +++ b/src/finalisers/shared/getExportBlock.ts @@ -6,9 +6,11 @@ export default function getExportBlock( namedExportsMode: boolean, interop: boolean, compact: boolean, + t: string, mechanism = 'return ' ) { const _ = compact ? '' : ' '; + const n = compact ? '' : '\n'; if (!namedExportsMode) { let local; @@ -43,7 +45,13 @@ export default function getExportBlock( reexports.forEach(specifier => { if (specifier.reexported === '*') { if (!compact && exportBlock) exportBlock += '\n'; - exportBlock += `Object.keys(${name}).forEach(function${_}(key)${_}{${_}exports[key]${_}=${_}${name}[key];${_}});`; + exportBlock += + `Object.keys(${name}).forEach(function${_}(key)${_}{${n}` + + `${t}Object.defineProperty(exports,${_}key,${_}{${n}` + + `${t}${t}enumerable:${_}true,${n}` + + `${t}${t}get:${_}function${_}()${_}{${n}` + + `${t}${t}${t}return ${name}[key];${n}` + + `${t}${t}}${n}${t}});${n}});`; } }); } @@ -55,10 +63,7 @@ export default function getExportBlock( reexports.forEach(specifier => { if (specifier.imported === 'default' && !isChunk) { const exportsNamesOrNamespace = - (imports && - imports.some( - specifier => specifier.imported === '*' || specifier.imported !== 'default' - )) || + (imports && imports.some(specifier => specifier.imported !== 'default')) || (reexports && reexports.some( specifier => specifier.imported !== 'default' && specifier.imported !== '*' @@ -82,7 +87,12 @@ export default function getExportBlock( specifier.imported === 'default' && !depNamedExportsMode ? name : `${name}.${specifier.imported}`; - exportBlock += `exports.${specifier.reexported}${_}=${_}${importName};`; + exportBlock += specifier.needsLiveBinding + ? `Object.defineProperty(exports,${_}'${specifier.reexported}',${_}{${n}` + + `${t}enumerable:${_}true,${n}` + + `${t}get:${_}function${_}()${_}{${n}` + + `${t}${t}return ${importName};${n}${t}}${n}});` + : `exports.${specifier.reexported}${_}=${_}${importName};`; } else if (specifier.reexported !== '*') { if (exportBlock && !compact) exportBlock += '\n'; exportBlock += `exports.${specifier.reexported}${_}=${_}${name};`; diff --git a/src/finalisers/umd.ts b/src/finalisers/umd.ts index 86c91032f29..20330b4c027 100644 --- a/src/finalisers/umd.ts +++ b/src/finalisers/umd.ts @@ -157,7 +157,8 @@ export default function umd( dependencies, namedExportsMode, options.interop, - options.compact + options.compact, + t ); if (exportBlock) magicString.append(n + n + exportBlock); if (namedExportsMode && hasExports && options.esModule) diff --git a/test/chunking-form/samples/chunking-reexport/_config.js b/test/chunking-form/samples/chunking-reexport/_config.js index 71a3c63971e..19248895def 100644 --- a/test/chunking-form/samples/chunking-reexport/_config.js +++ b/test/chunking-form/samples/chunking-reexport/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'chunking star external', options: { - input: ['main1.js', 'main2.js'] + input: ['main1.js', 'main2.js'], + external: ['external'] } }; diff --git a/test/chunking-form/samples/chunking-reexport/_expected/amd/main1.js b/test/chunking-form/samples/chunking-reexport/_expected/amd/main1.js index 0d89fcb4a38..2aaaa7c33cb 100644 --- a/test/chunking-form/samples/chunking-reexport/_expected/amd/main1.js +++ b/test/chunking-form/samples/chunking-reexport/_expected/amd/main1.js @@ -2,7 +2,12 @@ define(['exports', 'external', './generated-chunk.js'], function (exports, exter - exports.dep = external.asdf; + Object.defineProperty(exports, 'dep', { + enumerable: true, + get: function () { + return external.asdf; + } + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/chunking-form/samples/chunking-reexport/_expected/amd/main2.js b/test/chunking-form/samples/chunking-reexport/_expected/amd/main2.js index 0d89fcb4a38..2aaaa7c33cb 100644 --- a/test/chunking-form/samples/chunking-reexport/_expected/amd/main2.js +++ b/test/chunking-form/samples/chunking-reexport/_expected/amd/main2.js @@ -2,7 +2,12 @@ define(['exports', 'external', './generated-chunk.js'], function (exports, exter - exports.dep = external.asdf; + Object.defineProperty(exports, 'dep', { + enumerable: true, + get: function () { + return external.asdf; + } + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/chunking-form/samples/chunking-reexport/_expected/cjs/main1.js b/test/chunking-form/samples/chunking-reexport/_expected/cjs/main1.js index ce93546681d..870bcdb1f33 100644 --- a/test/chunking-form/samples/chunking-reexport/_expected/cjs/main1.js +++ b/test/chunking-form/samples/chunking-reexport/_expected/cjs/main1.js @@ -7,4 +7,9 @@ require('./generated-chunk.js'); -exports.dep = external.asdf; +Object.defineProperty(exports, 'dep', { + enumerable: true, + get: function () { + return external.asdf; + } +}); diff --git a/test/chunking-form/samples/chunking-reexport/_expected/cjs/main2.js b/test/chunking-form/samples/chunking-reexport/_expected/cjs/main2.js index ce93546681d..870bcdb1f33 100644 --- a/test/chunking-form/samples/chunking-reexport/_expected/cjs/main2.js +++ b/test/chunking-form/samples/chunking-reexport/_expected/cjs/main2.js @@ -7,4 +7,9 @@ require('./generated-chunk.js'); -exports.dep = external.asdf; +Object.defineProperty(exports, 'dep', { + enumerable: true, + get: function () { + return external.asdf; + } +}); diff --git a/test/chunking-form/samples/chunking-source-maps/_config.js b/test/chunking-form/samples/chunking-source-maps/_config.js index f33c0b34d7e..172d5f652b0 100644 --- a/test/chunking-form/samples/chunking-source-maps/_config.js +++ b/test/chunking-form/samples/chunking-source-maps/_config.js @@ -1,5 +1,5 @@ module.exports = { - description: 'simple chunking', + description: 'source maps', options: { input: ['main1.js', 'main2.js'], output: { diff --git a/test/chunking-form/samples/chunking-star-external/_config.js b/test/chunking-form/samples/chunking-star-external/_config.js index 71a3c63971e..854a3591362 100644 --- a/test/chunking-form/samples/chunking-star-external/_config.js +++ b/test/chunking-form/samples/chunking-star-external/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'chunking star external', options: { - input: ['main1.js', 'main2.js'] + input: ['main1.js', 'main2.js'], + external: ['external1', 'external2', 'starexternal1', 'starexternal2'] } }; diff --git a/test/chunking-form/samples/chunking-star-external/_expected/amd/main1.js b/test/chunking-form/samples/chunking-star-external/_expected/amd/main1.js index d7bc4d6fbbd..274e709b0fb 100644 --- a/test/chunking-form/samples/chunking-star-external/_expected/amd/main1.js +++ b/test/chunking-form/samples/chunking-star-external/_expected/amd/main1.js @@ -2,8 +2,20 @@ define(['exports', 'starexternal1', 'external1', 'starexternal2', 'external2', ' var main = '1'; - Object.keys(starexternal1).forEach(function (key) { exports[key] = starexternal1[key]; }); - exports.e = external1.e; + Object.keys(starexternal1).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return starexternal1[key]; + } + }); + }); + Object.defineProperty(exports, 'e', { + enumerable: true, + get: function () { + return external1.e; + } + }); exports.dep = __chunk_1.dep; exports.main = main; diff --git a/test/chunking-form/samples/chunking-star-external/_expected/amd/main2.js b/test/chunking-form/samples/chunking-star-external/_expected/amd/main2.js index a7654613dc0..43b41d0322b 100644 --- a/test/chunking-form/samples/chunking-star-external/_expected/amd/main2.js +++ b/test/chunking-form/samples/chunking-star-external/_expected/amd/main2.js @@ -2,8 +2,20 @@ define(['exports', 'starexternal2', 'external2', './generated-chunk.js'], functi var main = '2'; - Object.keys(starexternal2).forEach(function (key) { exports[key] = starexternal2[key]; }); - exports.e = external2.e; + Object.keys(starexternal2).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return starexternal2[key]; + } + }); + }); + Object.defineProperty(exports, 'e', { + enumerable: true, + get: function () { + return external2.e; + } + }); exports.dep = __chunk_1.dep; exports.main = main; diff --git a/test/chunking-form/samples/chunking-star-external/_expected/cjs/main1.js b/test/chunking-form/samples/chunking-star-external/_expected/cjs/main1.js index b0bf10e780a..553d344b880 100644 --- a/test/chunking-form/samples/chunking-star-external/_expected/cjs/main1.js +++ b/test/chunking-form/samples/chunking-star-external/_expected/cjs/main1.js @@ -10,7 +10,19 @@ var __chunk_1 = require('./generated-chunk.js'); var main = '1'; -Object.keys(starexternal1).forEach(function (key) { exports[key] = starexternal1[key]; }); -exports.e = external1.e; +Object.keys(starexternal1).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return starexternal1[key]; + } + }); +}); +Object.defineProperty(exports, 'e', { + enumerable: true, + get: function () { + return external1.e; + } +}); exports.dep = __chunk_1.dep; exports.main = main; diff --git a/test/chunking-form/samples/chunking-star-external/_expected/cjs/main2.js b/test/chunking-form/samples/chunking-star-external/_expected/cjs/main2.js index f1585a19769..6bb51ae987c 100644 --- a/test/chunking-form/samples/chunking-star-external/_expected/cjs/main2.js +++ b/test/chunking-form/samples/chunking-star-external/_expected/cjs/main2.js @@ -8,7 +8,19 @@ var __chunk_1 = require('./generated-chunk.js'); var main = '2'; -Object.keys(starexternal2).forEach(function (key) { exports[key] = starexternal2[key]; }); -exports.e = external2.e; +Object.keys(starexternal2).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return starexternal2[key]; + } + }); +}); +Object.defineProperty(exports, 'e', { + enumerable: true, + get: function () { + return external2.e; + } +}); exports.dep = __chunk_1.dep; exports.main = main; diff --git a/test/form/samples/compact-empty-external/_config.js b/test/form/samples/compact-empty-external/_config.js new file mode 100644 index 00000000000..d27624aaa2a --- /dev/null +++ b/test/form/samples/compact-empty-external/_config.js @@ -0,0 +1,9 @@ +module.exports = { + description: 'correctly handles empty external imports in compact mode', + options: { + external: ['external'], + output: { + compact: true + } + } +}; diff --git a/test/form/samples/compact-empty-external/_expected/amd.js b/test/form/samples/compact-empty-external/_expected/amd.js new file mode 100644 index 00000000000..f200316fb2f --- /dev/null +++ b/test/form/samples/compact-empty-external/_expected/amd.js @@ -0,0 +1 @@ +define(['external'],function(external){'use strict';}); \ No newline at end of file diff --git a/test/form/samples/compact-empty-external/_expected/cjs.js b/test/form/samples/compact-empty-external/_expected/cjs.js new file mode 100644 index 00000000000..e87ada6a0ff --- /dev/null +++ b/test/form/samples/compact-empty-external/_expected/cjs.js @@ -0,0 +1 @@ +'use strict';require('external'); \ No newline at end of file diff --git a/test/form/samples/compact-empty-external/_expected/es.js b/test/form/samples/compact-empty-external/_expected/es.js new file mode 100644 index 00000000000..331e02651c1 --- /dev/null +++ b/test/form/samples/compact-empty-external/_expected/es.js @@ -0,0 +1 @@ +import'external'; \ No newline at end of file diff --git a/test/form/samples/compact-empty-external/_expected/iife.js b/test/form/samples/compact-empty-external/_expected/iife.js new file mode 100644 index 00000000000..695ff0122dd --- /dev/null +++ b/test/form/samples/compact-empty-external/_expected/iife.js @@ -0,0 +1 @@ +(function(){'use strict';}()); \ No newline at end of file diff --git a/test/form/samples/compact-empty-external/_expected/system.js b/test/form/samples/compact-empty-external/_expected/system.js new file mode 100644 index 00000000000..4ec1f57364b --- /dev/null +++ b/test/form/samples/compact-empty-external/_expected/system.js @@ -0,0 +1 @@ +System.register(['external'],function(exports,module){'use strict';return{setters:[function(){}],execute:function(){}}}); \ No newline at end of file diff --git a/test/form/samples/compact-empty-external/_expected/umd.js b/test/form/samples/compact-empty-external/_expected/umd.js new file mode 100644 index 00000000000..478d201a431 --- /dev/null +++ b/test/form/samples/compact-empty-external/_expected/umd.js @@ -0,0 +1 @@ +(function(f){typeof define==='function'&&define.amd?define(['external'],f):f();}(function(){'use strict';})); \ No newline at end of file diff --git a/test/form/samples/compact-empty-external/main.js b/test/form/samples/compact-empty-external/main.js new file mode 100644 index 00000000000..b2bc48d5b02 --- /dev/null +++ b/test/form/samples/compact-empty-external/main.js @@ -0,0 +1 @@ +import 'external'; diff --git a/test/form/samples/compact-multiple-imports/_config.js b/test/form/samples/compact-multiple-imports/_config.js new file mode 100644 index 00000000000..16fa2fd903e --- /dev/null +++ b/test/form/samples/compact-multiple-imports/_config.js @@ -0,0 +1,11 @@ +module.exports = { + description: 'correctly handles empty external imports in compact mode', + options: { + external(id) { + return id.startsWith('external'); + }, + output: { + compact: true + } + } +}; diff --git a/test/form/samples/compact-multiple-imports/_expected/amd.js b/test/form/samples/compact-multiple-imports/_expected/amd.js new file mode 100644 index 00000000000..e65d614c8d1 --- /dev/null +++ b/test/form/samples/compact-multiple-imports/_expected/amd.js @@ -0,0 +1,2 @@ +define(['external-1','external-2','external-3','external-4','external-5'],function(external1,external2,external3,external4,external5){'use strict';assert.equal(external3.value, '3'); +assert.equal(external4.value, '4');}); \ No newline at end of file diff --git a/test/form/samples/compact-multiple-imports/_expected/cjs.js b/test/form/samples/compact-multiple-imports/_expected/cjs.js new file mode 100644 index 00000000000..80f6ffd9625 --- /dev/null +++ b/test/form/samples/compact-multiple-imports/_expected/cjs.js @@ -0,0 +1,2 @@ +'use strict';require('external-1'),require('external-2');var external3=require('external-3'),external4=require('external-4');require('external-5');assert.equal(external3.value, '3'); +assert.equal(external4.value, '4'); \ No newline at end of file diff --git a/test/form/samples/compact-multiple-imports/_expected/es.js b/test/form/samples/compact-multiple-imports/_expected/es.js new file mode 100644 index 00000000000..ab02585b6ff --- /dev/null +++ b/test/form/samples/compact-multiple-imports/_expected/es.js @@ -0,0 +1,2 @@ +import'external-1';import'external-2';import {value}from'external-3';import {value as value$1}from'external-4';import'external-5';assert.equal(value, '3'); +assert.equal(value$1, '4'); \ No newline at end of file diff --git a/test/form/samples/compact-multiple-imports/_expected/iife.js b/test/form/samples/compact-multiple-imports/_expected/iife.js new file mode 100644 index 00000000000..76e54472fc9 --- /dev/null +++ b/test/form/samples/compact-multiple-imports/_expected/iife.js @@ -0,0 +1,2 @@ +(function(external1,external2,external3,external4){'use strict';assert.equal(external3.value, '3'); +assert.equal(external4.value, '4');}(null,null,external3,external4)); \ No newline at end of file diff --git a/test/form/samples/compact-multiple-imports/_expected/system.js b/test/form/samples/compact-multiple-imports/_expected/system.js new file mode 100644 index 00000000000..4c924ee26a9 --- /dev/null +++ b/test/form/samples/compact-multiple-imports/_expected/system.js @@ -0,0 +1,2 @@ +System.register(['external-1','external-2','external-3','external-4','external-5'],function(exports,module){'use strict';var value,value$1;return{setters:[function(){},function(){},function(module){value=module.value;},function(module){value$1=module.value;},function(){}],execute:function(){assert.equal(value, '3'); +assert.equal(value$1, '4');}}}); \ No newline at end of file diff --git a/test/form/samples/compact-multiple-imports/_expected/umd.js b/test/form/samples/compact-multiple-imports/_expected/umd.js new file mode 100644 index 00000000000..e0bdf02dcc1 --- /dev/null +++ b/test/form/samples/compact-multiple-imports/_expected/umd.js @@ -0,0 +1,2 @@ +(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?f(require('external-1'),require('external-2'),require('external-3'),require('external-4'),require('external-5')):typeof define==='function'&&define.amd?define(['external-1','external-2','external-3','external-4','external-5'],f):(g=g||self,f(null,null,g.external3,g.external4));}(this,function(external1, external2, external3, external4){'use strict';assert.equal(external3.value, '3'); +assert.equal(external4.value, '4');})); \ No newline at end of file diff --git a/test/form/samples/compact-multiple-imports/main.js b/test/form/samples/compact-multiple-imports/main.js new file mode 100644 index 00000000000..0ab38d40533 --- /dev/null +++ b/test/form/samples/compact-multiple-imports/main.js @@ -0,0 +1,8 @@ +import 'external-1'; +import 'external-2'; +import {value as a} from 'external-3'; +import {value as b} from 'external-4'; +import 'external-5'; + +assert.equal(a, '3'); +assert.equal(b, '4'); diff --git a/test/form/samples/compact/main.js b/test/form/samples/compact/main.js index a6eb91ae5bb..9f751d973da 100644 --- a/test/form/samples/compact/main.js +++ b/test/form/samples/compact/main.js @@ -4,4 +4,4 @@ console.log(self); export default function foo () { console.log( x ); } -// trailing comment \ No newline at end of file +// trailing comment diff --git a/test/form/samples/export-all-multiple/_expected/amd.js b/test/form/samples/export-all-multiple/_expected/amd.js index 37ed5098050..0e735d68d7c 100644 --- a/test/form/samples/export-all-multiple/_expected/amd.js +++ b/test/form/samples/export-all-multiple/_expected/amd.js @@ -2,9 +2,30 @@ define(['exports', 'foo', 'bar', 'baz'], function (exports, foo, bar, baz) { 'us - Object.keys(foo).forEach(function (key) { exports[key] = foo[key]; }); - Object.keys(bar).forEach(function (key) { exports[key] = bar[key]; }); - Object.keys(baz).forEach(function (key) { exports[key] = baz[key]; }); + Object.keys(foo).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return foo[key]; + } + }); + }); + Object.keys(bar).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return bar[key]; + } + }); + }); + Object.keys(baz).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return baz[key]; + } + }); + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/export-all-multiple/_expected/cjs.js b/test/form/samples/export-all-multiple/_expected/cjs.js index e3af62c3159..e56ac6b0de9 100644 --- a/test/form/samples/export-all-multiple/_expected/cjs.js +++ b/test/form/samples/export-all-multiple/_expected/cjs.js @@ -8,6 +8,27 @@ var baz = require('baz'); -Object.keys(foo).forEach(function (key) { exports[key] = foo[key]; }); -Object.keys(bar).forEach(function (key) { exports[key] = bar[key]; }); -Object.keys(baz).forEach(function (key) { exports[key] = baz[key]; }); +Object.keys(foo).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return foo[key]; + } + }); +}); +Object.keys(bar).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return bar[key]; + } + }); +}); +Object.keys(baz).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return baz[key]; + } + }); +}); diff --git a/test/form/samples/export-all-multiple/_expected/iife.js b/test/form/samples/export-all-multiple/_expected/iife.js index b7ff2a15e7f..69d670055db 100644 --- a/test/form/samples/export-all-multiple/_expected/iife.js +++ b/test/form/samples/export-all-multiple/_expected/iife.js @@ -3,9 +3,30 @@ var myBundle = (function (exports, foo, bar, baz) { - Object.keys(foo).forEach(function (key) { exports[key] = foo[key]; }); - Object.keys(bar).forEach(function (key) { exports[key] = bar[key]; }); - Object.keys(baz).forEach(function (key) { exports[key] = baz[key]; }); + Object.keys(foo).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return foo[key]; + } + }); + }); + Object.keys(bar).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return bar[key]; + } + }); + }); + Object.keys(baz).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return baz[key]; + } + }); + }); return exports; diff --git a/test/form/samples/export-all-multiple/_expected/umd.js b/test/form/samples/export-all-multiple/_expected/umd.js index 0fff0ff0f20..0d78421a6ae 100644 --- a/test/form/samples/export-all-multiple/_expected/umd.js +++ b/test/form/samples/export-all-multiple/_expected/umd.js @@ -4,9 +4,30 @@ (global = global || self, factory(global.myBundle = {}, global.foo, global.bar, global.baz)); }(this, function (exports, foo, bar, baz) { 'use strict'; - Object.keys(foo).forEach(function (key) { exports[key] = foo[key]; }); - Object.keys(bar).forEach(function (key) { exports[key] = bar[key]; }); - Object.keys(baz).forEach(function (key) { exports[key] = baz[key]; }); + Object.keys(foo).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return foo[key]; + } + }); + }); + Object.keys(bar).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return bar[key]; + } + }); + }); + Object.keys(baz).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return baz[key]; + } + }); + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/export-default-import/_config.js b/test/form/samples/export-default-import/_config.js index 458f77fb421..86bc7b9c007 100644 --- a/test/form/samples/export-default-import/_config.js +++ b/test/form/samples/export-default-import/_config.js @@ -1,4 +1,7 @@ module.exports = { description: 'correctly exports a default import, even in ES mode (#513)', - options: { output: { name: 'myBundle' } } + options: { + external: ['x'], + output: { name: 'myBundle' } + } }; diff --git a/test/form/samples/external-export-tracing/_config.js b/test/form/samples/external-export-tracing/_config.js index 3a8fc55082b..7f4f4230a9b 100644 --- a/test/form/samples/external-export-tracing/_config.js +++ b/test/form/samples/external-export-tracing/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'Support external namespace reexport', options: { + external: ['external'], output: { name: 'myBundle' } diff --git a/test/form/samples/external-export-tracing/_expected/amd.js b/test/form/samples/external-export-tracing/_expected/amd.js index a8580bc83bf..3feb4663adc 100644 --- a/test/form/samples/external-export-tracing/_expected/amd.js +++ b/test/form/samples/external-export-tracing/_expected/amd.js @@ -2,7 +2,12 @@ define(['exports', 'external'], function (exports, external) { 'use strict'; - exports.s = external.p; + Object.defineProperty(exports, 's', { + enumerable: true, + get: function () { + return external.p; + } + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/external-export-tracing/_expected/cjs.js b/test/form/samples/external-export-tracing/_expected/cjs.js index 3325b182b4a..90e80982b1c 100644 --- a/test/form/samples/external-export-tracing/_expected/cjs.js +++ b/test/form/samples/external-export-tracing/_expected/cjs.js @@ -6,4 +6,9 @@ var external = require('external'); -exports.s = external.p; +Object.defineProperty(exports, 's', { + enumerable: true, + get: function () { + return external.p; + } +}); diff --git a/test/form/samples/external-export-tracing/_expected/iife.js b/test/form/samples/external-export-tracing/_expected/iife.js index 2bb394748a6..a34a5a2bca0 100644 --- a/test/form/samples/external-export-tracing/_expected/iife.js +++ b/test/form/samples/external-export-tracing/_expected/iife.js @@ -3,7 +3,12 @@ var myBundle = (function (exports, external) { - exports.s = external.p; + Object.defineProperty(exports, 's', { + enumerable: true, + get: function () { + return external.p; + } + }); return exports; diff --git a/test/form/samples/external-export-tracing/_expected/umd.js b/test/form/samples/external-export-tracing/_expected/umd.js index 15f54936b5d..3a433b98263 100644 --- a/test/form/samples/external-export-tracing/_expected/umd.js +++ b/test/form/samples/external-export-tracing/_expected/umd.js @@ -4,7 +4,12 @@ (global = global || self, factory(global.myBundle = {}, global.external)); }(this, function (exports, external) { 'use strict'; - exports.s = external.p; + Object.defineProperty(exports, 's', { + enumerable: true, + get: function () { + return external.p; + } + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/external-imports-custom-names-function/_config.js b/test/form/samples/external-imports-custom-names-function/_config.js index f639b281e63..188a8841c56 100644 --- a/test/form/samples/external-imports-custom-names-function/_config.js +++ b/test/form/samples/external-imports-custom-names-function/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'allows globals to be specified as a function', options: { + external: ['a-b-c'], output: { globals(id) { return id.replace(/-/g, '_'); diff --git a/test/form/samples/external-namespace-and-named/_config.js b/test/form/samples/external-namespace-and-named/_config.js index 838b5bc4c4c..4d98af0b52b 100644 --- a/test/form/samples/external-namespace-and-named/_config.js +++ b/test/form/samples/external-namespace-and-named/_config.js @@ -1,3 +1,6 @@ module.exports = { - description: 'Correctly handles external namespace tracing with both namespace and named exports' + description: 'Correctly handles external namespace tracing with both namespace and named exports', + options: { + external: ['foo'] + } }; diff --git a/test/form/samples/external-namespace-reexport/_config.js b/test/form/samples/external-namespace-reexport/_config.js index 3a8fc55082b..d660c4d93ba 100644 --- a/test/form/samples/external-namespace-reexport/_config.js +++ b/test/form/samples/external-namespace-reexport/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'Support external namespace reexport', options: { + external: ['highcharts'], output: { name: 'myBundle' } diff --git a/test/form/samples/import-external-namespace-and-default/_config.js b/test/form/samples/import-external-namespace-and-default/_config.js index 5ee7b482b2e..8e6eaf55816 100644 --- a/test/form/samples/import-external-namespace-and-default/_config.js +++ b/test/form/samples/import-external-namespace-and-default/_config.js @@ -1,3 +1,6 @@ module.exports = { - description: 'disinguishes between external default and namespace (#637)' + description: 'disinguishes between external default and namespace (#637)', + options: { + external: ['foo'] + } }; diff --git a/test/form/samples/import-specifier-deshadowing/_config.js b/test/form/samples/import-specifier-deshadowing/_config.js index 5df0cae1206..7141288646f 100644 --- a/test/form/samples/import-specifier-deshadowing/_config.js +++ b/test/form/samples/import-specifier-deshadowing/_config.js @@ -1,4 +1,7 @@ module.exports = { description: 'deshadows aliased import bindings', - options: { output: { name: 'Sticky' } } + options: { + external: ['react-sticky'], + output: { name: 'Sticky' } + } }; diff --git a/test/form/samples/interop-false-reexport/_config.js b/test/form/samples/interop-false-reexport/_config.js index 81ea15934d1..94fd78a4945 100644 --- a/test/form/samples/interop-false-reexport/_config.js +++ b/test/form/samples/interop-false-reexport/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'reexports with interop: false', options: { - output: { name: 'foo', interop: false }, + external: ['external'], + output: { name: 'foo', interop: false } } }; diff --git a/test/form/samples/interop-false-reexport/_expected/amd.js b/test/form/samples/interop-false-reexport/_expected/amd.js index c17e6b8f1a3..9d40c0b3a93 100644 --- a/test/form/samples/interop-false-reexport/_expected/amd.js +++ b/test/form/samples/interop-false-reexport/_expected/amd.js @@ -3,7 +3,12 @@ define(['exports', 'external'], function (exports, external) { 'use strict'; exports.p = external.default; - exports.q = external.p; + Object.defineProperty(exports, 'q', { + enumerable: true, + get: function () { + return external.p; + } + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/interop-false-reexport/_expected/cjs.js b/test/form/samples/interop-false-reexport/_expected/cjs.js index f4fd8693256..31e5f83b49b 100644 --- a/test/form/samples/interop-false-reexport/_expected/cjs.js +++ b/test/form/samples/interop-false-reexport/_expected/cjs.js @@ -7,4 +7,9 @@ var external = require('external'); exports.p = external.default; -exports.q = external.p; +Object.defineProperty(exports, 'q', { + enumerable: true, + get: function () { + return external.p; + } +}); diff --git a/test/form/samples/interop-false-reexport/_expected/iife.js b/test/form/samples/interop-false-reexport/_expected/iife.js index 2097fb6285c..d392895a2e3 100644 --- a/test/form/samples/interop-false-reexport/_expected/iife.js +++ b/test/form/samples/interop-false-reexport/_expected/iife.js @@ -4,7 +4,12 @@ var foo = (function (exports, external) { exports.p = external.default; - exports.q = external.p; + Object.defineProperty(exports, 'q', { + enumerable: true, + get: function () { + return external.p; + } + }); return exports; diff --git a/test/form/samples/interop-false-reexport/_expected/umd.js b/test/form/samples/interop-false-reexport/_expected/umd.js index 31ef7f3fb57..1cb04081806 100644 --- a/test/form/samples/interop-false-reexport/_expected/umd.js +++ b/test/form/samples/interop-false-reexport/_expected/umd.js @@ -5,7 +5,12 @@ }(this, function (exports, external) { 'use strict'; exports.p = external.default; - exports.q = external.p; + Object.defineProperty(exports, 'q', { + enumerable: true, + get: function () { + return external.p; + } + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/interop-false/_config.js b/test/form/samples/interop-false/_config.js index 71949905d84..5c528384814 100644 --- a/test/form/samples/interop-false/_config.js +++ b/test/form/samples/interop-false/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'getInterop with interop: false', options: { - output: { name: 'foo', interop: false }, + external: ['core/view'], + output: { name: 'foo', interop: false } } }; diff --git a/test/form/samples/namespace-import-reexport/_config.js b/test/form/samples/namespace-import-reexport/_config.js index 7b766fb04cb..d8a7ecc4119 100644 --- a/test/form/samples/namespace-import-reexport/_config.js +++ b/test/form/samples/namespace-import-reexport/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'properly associate or shadow variables in and around functions', options: { + external: ['external-package'], output: { name: 'iife' } diff --git a/test/form/samples/paths-function/_config.js b/test/form/samples/paths-function/_config.js index beba58396e3..c8ee0cdaac7 100644 --- a/test/form/samples/paths-function/_config.js +++ b/test/form/samples/paths-function/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'external paths (#754)', options: { + external: ['foo'], output: { paths: id => `https://unpkg.com/${id}` } } }; diff --git a/test/form/samples/paths/_config.js b/test/form/samples/paths/_config.js index 0dec82d3aa0..88119860563 100644 --- a/test/form/samples/paths/_config.js +++ b/test/form/samples/paths/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'external paths (#754)', options: { + external: ['foo'], output: { paths: { foo: 'https://unpkg.com/foo' } } } }; diff --git a/test/form/samples/prefer-const/_config.js b/test/form/samples/prefer-const/_config.js index c3b1614e518..74ca361f988 100644 --- a/test/form/samples/prefer-const/_config.js +++ b/test/form/samples/prefer-const/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'uses const instead of var if specified (#653)', options: { + external: ['other'], output: { name: 'myBundle', preferConst: true } } }; diff --git a/test/form/samples/re-export-aliasing/_config.js b/test/form/samples/re-export-aliasing/_config.js index 3ee244b45e0..933e7305c2d 100644 --- a/test/form/samples/re-export-aliasing/_config.js +++ b/test/form/samples/re-export-aliasing/_config.js @@ -2,6 +2,6 @@ module.exports = { description: 'external re-exports aliasing', options: { output: { name: 'reexportsAliasingExternal' }, - external: ['external'] + external: ['d'] } }; diff --git a/test/form/samples/re-export-aliasing/_expected/amd.js b/test/form/samples/re-export-aliasing/_expected/amd.js index a16354a6862..18490b4afd7 100644 --- a/test/form/samples/re-export-aliasing/_expected/amd.js +++ b/test/form/samples/re-export-aliasing/_expected/amd.js @@ -2,7 +2,12 @@ define(['exports', 'd'], function (exports, d) { 'use strict'; - exports.b = d.d; + Object.defineProperty(exports, 'b', { + enumerable: true, + get: function () { + return d.d; + } + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/re-export-aliasing/_expected/cjs.js b/test/form/samples/re-export-aliasing/_expected/cjs.js index 1ce3f417ad5..a5824426066 100644 --- a/test/form/samples/re-export-aliasing/_expected/cjs.js +++ b/test/form/samples/re-export-aliasing/_expected/cjs.js @@ -6,4 +6,9 @@ var d = require('d'); -exports.b = d.d; +Object.defineProperty(exports, 'b', { + enumerable: true, + get: function () { + return d.d; + } +}); diff --git a/test/form/samples/re-export-aliasing/_expected/iife.js b/test/form/samples/re-export-aliasing/_expected/iife.js index d56c72b5a38..3ae402e487a 100644 --- a/test/form/samples/re-export-aliasing/_expected/iife.js +++ b/test/form/samples/re-export-aliasing/_expected/iife.js @@ -3,7 +3,12 @@ var reexportsAliasingExternal = (function (exports, d) { - exports.b = d.d; + Object.defineProperty(exports, 'b', { + enumerable: true, + get: function () { + return d.d; + } + }); return exports; diff --git a/test/form/samples/re-export-aliasing/_expected/umd.js b/test/form/samples/re-export-aliasing/_expected/umd.js index 1bb32be30b4..10bfac18f23 100644 --- a/test/form/samples/re-export-aliasing/_expected/umd.js +++ b/test/form/samples/re-export-aliasing/_expected/umd.js @@ -4,7 +4,12 @@ (global = global || self, factory(global.reexportsAliasingExternal = {}, global.d)); }(this, function (exports, d) { 'use strict'; - exports.b = d.d; + Object.defineProperty(exports, 'b', { + enumerable: true, + get: function () { + return d.d; + } + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/re-export-default-external-as-default/_expected/amd.js b/test/form/samples/re-export-default-external-as-default/_expected/amd.js index 8bc157376f8..0938f9c3ec0 100644 --- a/test/form/samples/re-export-default-external-as-default/_expected/amd.js +++ b/test/form/samples/re-export-default-external-as-default/_expected/amd.js @@ -4,7 +4,14 @@ define(['exports', 'external'], function (exports, external) { 'use strict'; - Object.keys(external).forEach(function (key) { exports[key] = external[key]; }); + Object.keys(external).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return external[key]; + } + }); + }); exports.default = external__default; Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/re-export-default-external-as-default/_expected/cjs.js b/test/form/samples/re-export-default-external-as-default/_expected/cjs.js index 1ecf08ad403..2fae2e01703 100644 --- a/test/form/samples/re-export-default-external-as-default/_expected/cjs.js +++ b/test/form/samples/re-export-default-external-as-default/_expected/cjs.js @@ -9,5 +9,12 @@ var external__default = _interopDefault(external); -Object.keys(external).forEach(function (key) { exports[key] = external[key]; }); +Object.keys(external).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return external[key]; + } + }); +}); exports.default = external__default; diff --git a/test/form/samples/re-export-default-external-as-default/_expected/iife.js b/test/form/samples/re-export-default-external-as-default/_expected/iife.js index 13e44bba082..5369633f037 100644 --- a/test/form/samples/re-export-default-external-as-default/_expected/iife.js +++ b/test/form/samples/re-export-default-external-as-default/_expected/iife.js @@ -5,7 +5,14 @@ var reexportsDefaultExternalAsDefault = (function (exports, external) { - Object.keys(external).forEach(function (key) { exports[key] = external[key]; }); + Object.keys(external).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return external[key]; + } + }); + }); exports.default = external__default; return exports; diff --git a/test/form/samples/re-export-default-external-as-default/_expected/umd.js b/test/form/samples/re-export-default-external-as-default/_expected/umd.js index 86629fd6123..2db40955919 100644 --- a/test/form/samples/re-export-default-external-as-default/_expected/umd.js +++ b/test/form/samples/re-export-default-external-as-default/_expected/umd.js @@ -8,7 +8,14 @@ - Object.keys(external).forEach(function (key) { exports[key] = external[key]; }); + Object.keys(external).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return external[key]; + } + }); + }); exports.default = external__default; Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/reexports-from-external/_expected/amd.js b/test/form/samples/reexports-from-external/_expected/amd.js index ae628ce7d7e..b905239cd8b 100644 --- a/test/form/samples/reexports-from-external/_expected/amd.js +++ b/test/form/samples/reexports-from-external/_expected/amd.js @@ -2,7 +2,14 @@ define(['exports', 'external'], function (exports, external) { 'use strict'; - Object.keys(external).forEach(function (key) { exports[key] = external[key]; }); + Object.keys(external).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return external[key]; + } + }); + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/reexports-from-external/_expected/cjs.js b/test/form/samples/reexports-from-external/_expected/cjs.js index 8fe0297bb95..c7651ac316b 100644 --- a/test/form/samples/reexports-from-external/_expected/cjs.js +++ b/test/form/samples/reexports-from-external/_expected/cjs.js @@ -6,4 +6,11 @@ var external = require('external'); -Object.keys(external).forEach(function (key) { exports[key] = external[key]; }); +Object.keys(external).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return external[key]; + } + }); +}); diff --git a/test/form/samples/reexports-from-external/_expected/iife.js b/test/form/samples/reexports-from-external/_expected/iife.js index 830badeb9a1..1549282a6d5 100644 --- a/test/form/samples/reexports-from-external/_expected/iife.js +++ b/test/form/samples/reexports-from-external/_expected/iife.js @@ -3,7 +3,14 @@ var myBundle = (function (exports, external) { - Object.keys(external).forEach(function (key) { exports[key] = external[key]; }); + Object.keys(external).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return external[key]; + } + }); + }); return exports; diff --git a/test/form/samples/reexports-from-external/_expected/umd.js b/test/form/samples/reexports-from-external/_expected/umd.js index 60eb7a0f7cb..ee73c8b8516 100644 --- a/test/form/samples/reexports-from-external/_expected/umd.js +++ b/test/form/samples/reexports-from-external/_expected/umd.js @@ -4,7 +4,14 @@ (global = global || self, factory(global.myBundle = {}, global.external)); }(this, function (exports, external) { 'use strict'; - Object.keys(external).forEach(function (key) { exports[key] = external[key]; }); + Object.keys(external).forEach(function (key) { + Object.defineProperty(exports, key, { + enumerable: true, + get: function () { + return external[key]; + } + }); + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/reexports-name-from-external/_expected/amd.js b/test/form/samples/reexports-name-from-external/_expected/amd.js index 85945ba5c2b..4642bbad96c 100644 --- a/test/form/samples/reexports-name-from-external/_expected/amd.js +++ b/test/form/samples/reexports-name-from-external/_expected/amd.js @@ -2,7 +2,12 @@ define(['exports', 'external'], function (exports, external) { 'use strict'; - exports.foo = external.foo; + Object.defineProperty(exports, 'foo', { + enumerable: true, + get: function () { + return external.foo; + } + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/reexports-name-from-external/_expected/cjs.js b/test/form/samples/reexports-name-from-external/_expected/cjs.js index 7bc73b2ab49..0fd0876a028 100644 --- a/test/form/samples/reexports-name-from-external/_expected/cjs.js +++ b/test/form/samples/reexports-name-from-external/_expected/cjs.js @@ -6,4 +6,9 @@ var external = require('external'); -exports.foo = external.foo; +Object.defineProperty(exports, 'foo', { + enumerable: true, + get: function () { + return external.foo; + } +}); diff --git a/test/form/samples/reexports-name-from-external/_expected/iife.js b/test/form/samples/reexports-name-from-external/_expected/iife.js index acbc785d20a..db9c835d04d 100644 --- a/test/form/samples/reexports-name-from-external/_expected/iife.js +++ b/test/form/samples/reexports-name-from-external/_expected/iife.js @@ -3,7 +3,12 @@ var myBundle = (function (exports, external) { - exports.foo = external.foo; + Object.defineProperty(exports, 'foo', { + enumerable: true, + get: function () { + return external.foo; + } + }); return exports; diff --git a/test/form/samples/reexports-name-from-external/_expected/umd.js b/test/form/samples/reexports-name-from-external/_expected/umd.js index 083302c7fb7..b0d5278f377 100644 --- a/test/form/samples/reexports-name-from-external/_expected/umd.js +++ b/test/form/samples/reexports-name-from-external/_expected/umd.js @@ -4,7 +4,12 @@ (global = global || self, factory(global.myBundle = {}, global.external)); }(this, function (exports, external) { 'use strict'; - exports.foo = external.foo; + Object.defineProperty(exports, 'foo', { + enumerable: true, + get: function () { + return external.foo; + } + }); Object.defineProperty(exports, '__esModule', { value: true }); diff --git a/test/form/samples/unused-import/_config.js b/test/form/samples/unused-import/_config.js index f0f942905ab..9de4055aa24 100644 --- a/test/form/samples/unused-import/_config.js +++ b/test/form/samples/unused-import/_config.js @@ -1,3 +1,6 @@ module.exports = { - description: 'excludes unused imports ([#595])' + description: 'excludes unused imports ([#595])', + options: { + external: ['external'] + } }; diff --git a/test/form/samples/url-external/_config.js b/test/form/samples/url-external/_config.js index 9d60e0a459a..dcceaef3f56 100644 --- a/test/form/samples/url-external/_config.js +++ b/test/form/samples/url-external/_config.js @@ -1,3 +1,6 @@ module.exports = { description: 'supports URL externals', + options: { + external: ['https://external.com/external.js'] + } }; diff --git a/test/function/samples/compact-multiple-imports/_config.js b/test/function/samples/compact-multiple-imports/_config.js new file mode 100644 index 00000000000..b651e98d09d --- /dev/null +++ b/test/function/samples/compact-multiple-imports/_config.js @@ -0,0 +1,16 @@ +module.exports = { + description: 'correctly handles empty external imports in compact mode', + context: { + require(id) { + return { value: id[id.length - 1] }; + } + }, + options: { + external(id) { + return id.startsWith('external'); + }, + output: { + compact: true + } + } +}; diff --git a/test/function/samples/compact-multiple-imports/_expected/amd.js b/test/function/samples/compact-multiple-imports/_expected/amd.js new file mode 100644 index 00000000000..c0c925741aa --- /dev/null +++ b/test/function/samples/compact-multiple-imports/_expected/amd.js @@ -0,0 +1,6 @@ +define(['external'],function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self); +function foo () { + console.log( x ); +} +// trailing comment +return foo;}); diff --git a/test/function/samples/compact-multiple-imports/_expected/cjs.js b/test/function/samples/compact-multiple-imports/_expected/cjs.js new file mode 100644 index 00000000000..c0276a75f26 --- /dev/null +++ b/test/function/samples/compact-multiple-imports/_expected/cjs.js @@ -0,0 +1,6 @@ +'use strict';function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var x=_interopDefault(require('external'));var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self); +function foo () { + console.log( x ); +} +// trailing comment +module.exports=foo; diff --git a/test/function/samples/compact-multiple-imports/_expected/es.js b/test/function/samples/compact-multiple-imports/_expected/es.js new file mode 100644 index 00000000000..16cecd7ccd7 --- /dev/null +++ b/test/function/samples/compact-multiple-imports/_expected/es.js @@ -0,0 +1,6 @@ +import x from'external';var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self); +function foo () { + console.log( x ); +} +// trailing comment +export default foo; diff --git a/test/function/samples/compact-multiple-imports/_expected/iife.js b/test/function/samples/compact-multiple-imports/_expected/iife.js new file mode 100644 index 00000000000..1f8ea4d384d --- /dev/null +++ b/test/function/samples/compact-multiple-imports/_expected/iife.js @@ -0,0 +1,6 @@ +var foo=(function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self); +function foo () { + console.log( x ); +} +// trailing comment +return foo;}(x)); diff --git a/test/function/samples/compact-multiple-imports/_expected/system.js b/test/function/samples/compact-multiple-imports/_expected/system.js new file mode 100644 index 00000000000..a49e388d827 --- /dev/null +++ b/test/function/samples/compact-multiple-imports/_expected/system.js @@ -0,0 +1,6 @@ +System.register('foo',['external'],function(exports,module){'use strict';var x;return{setters:[function(module){x=module.default;}],execute:function(){exports('default',foo);var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self); +function foo () { + console.log( x ); +} +// trailing comment +}}}); diff --git a/test/function/samples/compact-multiple-imports/_expected/umd.js b/test/function/samples/compact-multiple-imports/_expected/umd.js new file mode 100644 index 00000000000..7902ad4e3ff --- /dev/null +++ b/test/function/samples/compact-multiple-imports/_expected/umd.js @@ -0,0 +1,6 @@ +(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=g||self,g.foo=f(g.x));}(this,function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self = {get default(){return foo}};if(typeof Symbol!=='undefined'&&Symbol.toStringTag)Object.defineProperty(self,Symbol.toStringTag,{value:'Module'});else Object.defineProperty(self,'toString',{value:function(){return'[object Module]';}});/*#__PURE__*/Object.freeze(self);console.log(self); +function foo () { + console.log( x ); +} +// trailing comment +return foo;})); diff --git a/test/function/samples/compact-multiple-imports/main.js b/test/function/samples/compact-multiple-imports/main.js new file mode 100644 index 00000000000..0ab38d40533 --- /dev/null +++ b/test/function/samples/compact-multiple-imports/main.js @@ -0,0 +1,8 @@ +import 'external-1'; +import 'external-2'; +import {value as a} from 'external-3'; +import {value as b} from 'external-4'; +import 'external-5'; + +assert.equal(a, '3'); +assert.equal(b, '4'); diff --git a/test/function/samples/cross-chunk-live-binding-compact/_config.js b/test/function/samples/cross-chunk-live-binding-compact/_config.js new file mode 100644 index 00000000000..7e324338e77 --- /dev/null +++ b/test/function/samples/cross-chunk-live-binding-compact/_config.js @@ -0,0 +1,20 @@ +const assert = require('assert'); + +module.exports = { + description: 'handles cross-chunk live-bindings in compact mode', + options: { + input: ['main.js', 'named.js', 'star.js'], + output: { + compact: true + } + }, + exports(exports) { + assert.equal(exports.named, 0, 'named'); + exports.incrementNamed(); + assert.equal(exports.named, 1, 'named'); + + assert.equal(exports.star, 0, 'star'); + exports.incrementStar(); + assert.equal(exports.star, 1, 'star'); + } +}; diff --git a/test/function/samples/cross-chunk-live-binding-compact/main.js b/test/function/samples/cross-chunk-live-binding-compact/main.js new file mode 100644 index 00000000000..8f2402c878b --- /dev/null +++ b/test/function/samples/cross-chunk-live-binding-compact/main.js @@ -0,0 +1,2 @@ +export { named, incrementNamed } from './named.js'; +export * from './star.js'; diff --git a/test/function/samples/cross-chunk-live-binding-compact/named.js b/test/function/samples/cross-chunk-live-binding-compact/named.js new file mode 100644 index 00000000000..411d2dc5f01 --- /dev/null +++ b/test/function/samples/cross-chunk-live-binding-compact/named.js @@ -0,0 +1,2 @@ +export let named = 0; +export const incrementNamed = () => named++; diff --git a/test/function/samples/cross-chunk-live-binding-compact/star.js b/test/function/samples/cross-chunk-live-binding-compact/star.js new file mode 100644 index 00000000000..7be0b462f39 --- /dev/null +++ b/test/function/samples/cross-chunk-live-binding-compact/star.js @@ -0,0 +1,2 @@ +export let star = 0; +export const incrementStar = () => star++; diff --git a/test/function/samples/cross-chunk-live-binding/_config.js b/test/function/samples/cross-chunk-live-binding/_config.js new file mode 100644 index 00000000000..e598590dda3 --- /dev/null +++ b/test/function/samples/cross-chunk-live-binding/_config.js @@ -0,0 +1,17 @@ +const assert = require('assert'); + +module.exports = { + description: 'handles cross-chunk live-bindings', + options: { + input: ['main.js', 'named.js', 'star.js'] + }, + exports(exports) { + assert.equal(exports.named, 0, 'named'); + exports.incrementNamed(); + assert.equal(exports.named, 1, 'named'); + + assert.equal(exports.star, 0, 'star'); + exports.incrementStar(); + assert.equal(exports.star, 1, 'star'); + } +}; diff --git a/test/function/samples/cross-chunk-live-binding/main.js b/test/function/samples/cross-chunk-live-binding/main.js new file mode 100644 index 00000000000..8f2402c878b --- /dev/null +++ b/test/function/samples/cross-chunk-live-binding/main.js @@ -0,0 +1,2 @@ +export { named, incrementNamed } from './named.js'; +export * from './star.js'; diff --git a/test/function/samples/cross-chunk-live-binding/named.js b/test/function/samples/cross-chunk-live-binding/named.js new file mode 100644 index 00000000000..411d2dc5f01 --- /dev/null +++ b/test/function/samples/cross-chunk-live-binding/named.js @@ -0,0 +1,2 @@ +export let named = 0; +export const incrementNamed = () => named++; diff --git a/test/function/samples/cross-chunk-live-binding/star.js b/test/function/samples/cross-chunk-live-binding/star.js new file mode 100644 index 00000000000..7be0b462f39 --- /dev/null +++ b/test/function/samples/cross-chunk-live-binding/star.js @@ -0,0 +1,2 @@ +export let star = 0; +export const incrementStar = () => star++; diff --git a/test/function/samples/external-live-binding-compact/_config.js b/test/function/samples/external-live-binding-compact/_config.js new file mode 100644 index 00000000000..6d373075d51 --- /dev/null +++ b/test/function/samples/external-live-binding-compact/_config.js @@ -0,0 +1,45 @@ +const assert = require('assert'); + +module.exports = { + description: 'handles external live-bindings', + options: { + external: ['named', 'star'], + output: { compact: true } + }, + context: { + require(id) { + switch (id) { + case 'named': { + const exports = { + named: 0, + incrementNamed() { + exports.named++; + } + }; + return exports; + } + case 'star': { + const exports = { + star: 0, + incrementStar() { + exports.star++; + } + }; + return exports; + } + default: { + throw new Error(`Unexpected id ${id}`); + } + } + } + }, + exports(exports) { + assert.equal(exports.named, 0, 'named'); + exports.incrementNamed(); + assert.equal(exports.named, 1, 'named'); + + assert.equal(exports.star, 0, 'star'); + exports.incrementStar(); + assert.equal(exports.star, 1, 'star'); + } +}; diff --git a/test/function/samples/external-live-binding-compact/main.js b/test/function/samples/external-live-binding-compact/main.js new file mode 100644 index 00000000000..6d82e550644 --- /dev/null +++ b/test/function/samples/external-live-binding-compact/main.js @@ -0,0 +1,2 @@ +export { named, incrementNamed } from 'named'; +export * from 'star'; diff --git a/test/function/samples/external-live-binding/_config.js b/test/function/samples/external-live-binding/_config.js new file mode 100644 index 00000000000..485e27366cc --- /dev/null +++ b/test/function/samples/external-live-binding/_config.js @@ -0,0 +1,44 @@ +const assert = require('assert'); + +module.exports = { + description: 'handles external live-bindings', + options: { + external: ['named', 'star'] + }, + context: { + require(id) { + switch (id) { + case 'named': { + const exports = { + named: 0, + incrementNamed() { + exports.named++; + } + }; + return exports; + } + case 'star': { + const exports = { + star: 0, + incrementStar() { + exports.star++; + } + }; + return exports; + } + default: { + throw new Error(`Unexpected id ${id}`); + } + } + } + }, + exports(exports) { + assert.equal(exports.named, 0, 'named'); + exports.incrementNamed(); + assert.equal(exports.named, 1, 'named'); + + assert.equal(exports.star, 0, 'star'); + exports.incrementStar(); + assert.equal(exports.star, 1, 'star'); + } +}; diff --git a/test/function/samples/external-live-binding/main.js b/test/function/samples/external-live-binding/main.js new file mode 100644 index 00000000000..6d82e550644 --- /dev/null +++ b/test/function/samples/external-live-binding/main.js @@ -0,0 +1,2 @@ +export { named, incrementNamed } from 'named'; +export * from 'star';