From ae2d4247f69ee3892b31a7594d14b21d13e9b092 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 3 Dec 2018 06:30:27 +0100 Subject: [PATCH] Make sure tree-shaken dynamic imports do not lead to the creation of namespace objects when inlined --- src/Chunk.ts | 10 ++++++---- .../_expected/amd/entryB.js | 2 +- .../_expected/amd/main1.js | 2 +- .../form/samples/default-identifier-deshadowing/dep.js | 2 +- .../inlined-treeshaken-dynamic-import/_config.js | 6 ++++++ .../inlined-treeshaken-dynamic-import/_expected/amd.js | 5 +++++ .../inlined-treeshaken-dynamic-import/_expected/cjs.js | 3 +++ .../inlined-treeshaken-dynamic-import/_expected/es.js | 1 + .../_expected/iife.js | 6 ++++++ .../_expected/system.js | 10 ++++++++++ .../inlined-treeshaken-dynamic-import/_expected/umd.js | 9 +++++++++ .../inlined-treeshaken-dynamic-import/dynamic.js | 2 ++ .../samples/inlined-treeshaken-dynamic-import/main.js | 2 ++ 13 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 test/form/samples/inlined-treeshaken-dynamic-import/_config.js create mode 100644 test/form/samples/inlined-treeshaken-dynamic-import/_expected/amd.js create mode 100644 test/form/samples/inlined-treeshaken-dynamic-import/_expected/cjs.js create mode 100644 test/form/samples/inlined-treeshaken-dynamic-import/_expected/es.js create mode 100644 test/form/samples/inlined-treeshaken-dynamic-import/_expected/iife.js create mode 100644 test/form/samples/inlined-treeshaken-dynamic-import/_expected/system.js create mode 100644 test/form/samples/inlined-treeshaken-dynamic-import/_expected/umd.js create mode 100644 test/form/samples/inlined-treeshaken-dynamic-import/dynamic.js create mode 100644 test/form/samples/inlined-treeshaken-dynamic-import/main.js diff --git a/src/Chunk.ts b/src/Chunk.ts index 6fb382d3280..a1e52a87fa6 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -248,10 +248,12 @@ export default class Chunk { const declaration = module.imports[importName]; this.traceAndInitializeImport(declaration.name, declaration.module); } - for (const { resolution } of module.dynamicImports) { - this.hasDynamicImport = true; - if (resolution instanceof Module && resolution.chunk === this) - resolution.getOrCreateNamespace().include(); + for (const { node, resolution } of module.dynamicImports) { + if (node.included) { + this.hasDynamicImport = true; + if (resolution instanceof Module && resolution.chunk === this) + resolution.getOrCreateNamespace().include(); + } } } diff --git a/test/chunking-form/samples/dynamic-import-tree-shaking-1/_expected/amd/entryB.js b/test/chunking-form/samples/dynamic-import-tree-shaking-1/_expected/amd/entryB.js index ece2de9a3ae..b58286ca58e 100644 --- a/test/chunking-form/samples/dynamic-import-tree-shaking-1/_expected/amd/entryB.js +++ b/test/chunking-form/samples/dynamic-import-tree-shaking-1/_expected/amd/entryB.js @@ -1,4 +1,4 @@ -define(['require'], function (require) { 'use strict'; +define(function () { 'use strict'; console.log('main2'); diff --git a/test/chunking-form/samples/dynamic-import-tree-shaking-2/_expected/amd/main1.js b/test/chunking-form/samples/dynamic-import-tree-shaking-2/_expected/amd/main1.js index a2d15717cea..34f4f818070 100644 --- a/test/chunking-form/samples/dynamic-import-tree-shaking-2/_expected/amd/main1.js +++ b/test/chunking-form/samples/dynamic-import-tree-shaking-2/_expected/amd/main1.js @@ -1,4 +1,4 @@ -define(['require'], function (require) { 'use strict'; +define(function () { 'use strict'; console.log('main1'); diff --git a/test/form/samples/default-identifier-deshadowing/dep.js b/test/form/samples/default-identifier-deshadowing/dep.js index 8349860b51a..1e617709bd7 100644 --- a/test/form/samples/default-identifier-deshadowing/dep.js +++ b/test/form/samples/default-identifier-deshadowing/dep.js @@ -1,4 +1,4 @@ export default function a() { a = someGlobal; return a(); -} \ No newline at end of file +} diff --git a/test/form/samples/inlined-treeshaken-dynamic-import/_config.js b/test/form/samples/inlined-treeshaken-dynamic-import/_config.js new file mode 100644 index 00000000000..b5257d549c5 --- /dev/null +++ b/test/form/samples/inlined-treeshaken-dynamic-import/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'completely removes tree-shaken dynamic imports ', + options: { + inlineDynamicImports: true + } +}; diff --git a/test/form/samples/inlined-treeshaken-dynamic-import/_expected/amd.js b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/amd.js new file mode 100644 index 00000000000..126aa22ee83 --- /dev/null +++ b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/amd.js @@ -0,0 +1,5 @@ +define(function () { 'use strict'; + + console.log('main'); + +}); diff --git a/test/form/samples/inlined-treeshaken-dynamic-import/_expected/cjs.js b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/cjs.js new file mode 100644 index 00000000000..d0ed06d8c90 --- /dev/null +++ b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/cjs.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('main'); diff --git a/test/form/samples/inlined-treeshaken-dynamic-import/_expected/es.js b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/es.js new file mode 100644 index 00000000000..c0b933d7b56 --- /dev/null +++ b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/es.js @@ -0,0 +1 @@ +console.log('main'); diff --git a/test/form/samples/inlined-treeshaken-dynamic-import/_expected/iife.js b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/iife.js new file mode 100644 index 00000000000..d283cbce8ba --- /dev/null +++ b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/iife.js @@ -0,0 +1,6 @@ +(function () { + 'use strict'; + + console.log('main'); + +}()); diff --git a/test/form/samples/inlined-treeshaken-dynamic-import/_expected/system.js b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/system.js new file mode 100644 index 00000000000..ba798b19101 --- /dev/null +++ b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/system.js @@ -0,0 +1,10 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + console.log('main'); + + } + }; +}); diff --git a/test/form/samples/inlined-treeshaken-dynamic-import/_expected/umd.js b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/umd.js new file mode 100644 index 00000000000..41674034421 --- /dev/null +++ b/test/form/samples/inlined-treeshaken-dynamic-import/_expected/umd.js @@ -0,0 +1,9 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : + typeof define === 'function' && define.amd ? define(factory) : + (factory()); +}(this, (function () { 'use strict'; + + console.log('main'); + +}))); diff --git a/test/form/samples/inlined-treeshaken-dynamic-import/dynamic.js b/test/form/samples/inlined-treeshaken-dynamic-import/dynamic.js new file mode 100644 index 00000000000..1d7a1acc1b8 --- /dev/null +++ b/test/form/samples/inlined-treeshaken-dynamic-import/dynamic.js @@ -0,0 +1,2 @@ +console.log('dynamic'); +export var dynamic = 42; diff --git a/test/form/samples/inlined-treeshaken-dynamic-import/main.js b/test/form/samples/inlined-treeshaken-dynamic-import/main.js new file mode 100644 index 00000000000..9b0aad2c210 --- /dev/null +++ b/test/form/samples/inlined-treeshaken-dynamic-import/main.js @@ -0,0 +1,2 @@ +console.log('main'); +const getFoo = () => import('./dynamic.js');