From 32d6a46474e8efc2d2810d533ca68ddadf3a3416 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 13 Feb 2020 18:50:36 -0500 Subject: [PATCH] build: avoid runtime wildcard import in global build --- jest.config.js | 1 + packages/global.d.ts | 1 + packages/vue/src/index.ts | 8 +++++++- rollup.config.js | 3 +++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/jest.config.js b/jest.config.js index ebc6e76e..aeda0802 100644 --- a/jest.config.js +++ b/jest.config.js @@ -7,6 +7,7 @@ module.exports = { __BROWSER__: false, __BUNDLER__: true, __RUNTIME_COMPILE__: true, + __GLOBAL__: false, __NODE_JS__: true, __FEATURE_OPTIONS__: true, __FEATURE_SUSPENSE__: true diff --git a/packages/global.d.ts b/packages/global.d.ts index 7d180553..3f644c9f 100644 --- a/packages/global.d.ts +++ b/packages/global.d.ts @@ -4,6 +4,7 @@ declare var __TEST__: boolean declare var __BROWSER__: boolean declare var __BUNDLER__: boolean declare var __RUNTIME_COMPILE__: boolean +declare var __GLOBAL__: boolean declare var __NODE_JS__: boolean declare var __COMMIT__: string declare var __VERSION__: string diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts index 085a5b34..056fcc90 100644 --- a/packages/vue/src/index.ts +++ b/packages/vue/src/index.ts @@ -58,7 +58,13 @@ function compileToFunction( ...options }) - const render = new Function('Vue', code)(runtimeDom) as RenderFunction + // The wildcard import results in a huge object with every export + // with keys that cannot be mangled, and can be quite heavy size-wise. + // In the global build we know `Vue` is available globally so we can avoid + // the wildcard object. + const render = (__GLOBAL__ + ? new Function(code)() + : new Function('Vue', code)(runtimeDom)) as RenderFunction return (compileCache[key] = render) } diff --git a/rollup.config.js b/rollup.config.js index 0934ba8b..18fc0e5a 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -136,6 +136,7 @@ function createConfig(format, output, plugins = []) { (isGlobalBuild || isRawESMBuild || isBundlerESMBuild) && !packageOptions.enableNonBrowserBranches, isRuntimeCompileBuild, + isGlobalBuild, isNodeBuild ), ...plugins @@ -154,6 +155,7 @@ function createReplacePlugin( isBundlerESMBuild, isBrowserBuild, isRuntimeCompileBuild, + isGlobalBuild, isNodeBuild ) { const replacements = { @@ -172,6 +174,7 @@ function createReplacePlugin( __BUNDLER__: isBundlerESMBuild, // support compile in browser? __RUNTIME_COMPILE__: isRuntimeCompileBuild, + __GLOBAL__: isGlobalBuild, // is targeting Node (SSR)? __NODE_JS__: isNodeBuild, // support options?