From ce4a6c860f926fc85d52ecf824ca0573c5466ffd Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Fri, 20 Dec 2019 18:24:47 +0100 Subject: [PATCH] fix(compiler): fix circular dependency with @app-globals --- package.json | 1 + scripts/bundles/helpers/jest/jest-preset.js | 1 + scripts/bundles/plugins/alias-plugin.ts | 1 + src/app-data/index.ts | 2 -- src/app-globals/index.ts | 1 + src/client/index.ts | 2 +- src/compiler/component-lazy/generate-lazy-app.ts | 7 +++++-- src/compiler_next/bundle/app-data-plugin.ts | 10 +++++++--- src/compiler_next/bundle/entry-alias-ids.ts | 1 + .../dist-custom-elements-bundle/index.ts | 5 +++-- .../output-targets/dist-lazy/lazy-output.ts | 7 ++++--- src/hydrate/platform/hydrate-app.ts | 2 +- src/hydrate/platform/index.ts | 2 +- src/runtime/vdom/test/tsconfig.json | 3 +++ tsconfig.json | 4 ++++ 15 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 src/app-globals/index.ts diff --git a/package.json b/package.json index 9ee9b927bed..ef9e84c1e2e 100644 --- a/package.json +++ b/package.json @@ -152,6 +152,7 @@ "preset": "./testing/jest-preset.js", "moduleNameMapper": { "@app-data": "/internal/app-data/index.js", + "@app-globals": "/internal/app-globals/index.js", "@compiler": "/compiler/index.js", "@mock-doc": "/mock-doc/index.js", "@platform": "/internal/testing/index.js", diff --git a/scripts/bundles/helpers/jest/jest-preset.js b/scripts/bundles/helpers/jest/jest-preset.js index 4b6e4b4668d..16291d562a9 100644 --- a/scripts/bundles/helpers/jest/jest-preset.js +++ b/scripts/bundles/helpers/jest/jest-preset.js @@ -15,6 +15,7 @@ module.exports = { "^@stencil/core/cli$": path.join(rootDir, 'cli', 'index_legacy.js'), "^@stencil/core/compiler$": path.join(rootDir, 'compiler', 'index.js'), "^@stencil/core/internal/app-data$": path.join(internalDir, 'app-data', 'index.js'), + "^@stencil/core/internal/app-globals$": path.join(internalDir, 'app-globals', 'index.js'), "^@stencil/core/internal/platform$": path.join(internalDir, 'testing', 'index.js'), "^@stencil/core/internal/runtime$": path.join(internalDir, 'runtime', 'index.js'), "^@stencil/core/mock-doc$": path.join(rootDir, 'mock-doc', 'index.js'), diff --git a/scripts/bundles/plugins/alias-plugin.ts b/scripts/bundles/plugins/alias-plugin.ts index 236d88ada5d..8788aafbd2f 100644 --- a/scripts/bundles/plugins/alias-plugin.ts +++ b/scripts/bundles/plugins/alias-plugin.ts @@ -7,6 +7,7 @@ export function aliasPlugin(opts: BuildOptions): Plugin { const alias = new Map([ ['@app-data', '@stencil/core/internal/app-data'], + ['@app-globals', '@stencil/core/internal/app-globals'], ['@hydrate-factory', '@stencil/core/hydrate-factory'], ['@mock-doc', '@stencil/core/mock-doc'], ['@platform', '@stencil/core/internal/platform'], diff --git a/src/app-data/index.ts b/src/app-data/index.ts index 923a6a7cf6a..224d043aabd 100644 --- a/src/app-data/index.ts +++ b/src/app-data/index.ts @@ -62,6 +62,4 @@ export const BUILD: BuildConditionals = /* default */ { cloneNodeFix: false, }; -export const globalScripts = /* default */ () => {/**/}; - export const NAMESPACE = /* default */ 'app' as string; diff --git a/src/app-globals/index.ts b/src/app-globals/index.ts new file mode 100644 index 00000000000..37b291fd6dd --- /dev/null +++ b/src/app-globals/index.ts @@ -0,0 +1 @@ +export const globalScripts = /* default */ () => {/**/}; diff --git a/src/client/index.ts b/src/client/index.ts index db6e29844d0..4e35d98cc49 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -9,4 +9,4 @@ export * from './client-task-queue'; export * from './client-build'; export * from './import-shims'; export * from '@runtime'; -export { BUILD, NAMESPACE, globalScripts } from '@app-data'; +export { BUILD, NAMESPACE } from '@app-data'; diff --git a/src/compiler/component-lazy/generate-lazy-app.ts b/src/compiler/component-lazy/generate-lazy-app.ts index 4b2e59ef4b4..c935061e71b 100644 --- a/src/compiler/component-lazy/generate-lazy-app.ts +++ b/src/compiler/component-lazy/generate-lazy-app.ts @@ -91,7 +91,9 @@ async function bundleLazyApp(config: d.Config, compilerCtx: d.CompilerCtx, build } const BROWSER_ENTRY = ` -import { bootstrapLazy, globalScripts, patchBrowser } from '@stencil/core/internal/client'; +import { bootstrapLazy, patchBrowser } from '@stencil/core/internal/client'; +import { globalScripts } from '@stencil/core/internal/app-globals'; + patchBrowser().then(options => { globalScripts(); return bootstrapLazy([/*!__STENCIL_LAZY_DATA__*/], options); @@ -100,7 +102,8 @@ patchBrowser().then(options => { // This is for webpack const EXTERNAL_ENTRY = ` -import { bootstrapLazy, globalScripts, patchEsm } from '@stencil/core/internal/client'; +import { bootstrapLazy, patchEsm } from '@stencil/core/internal/client'; +import { globalScripts } from '@stencil/core/internal/app-globals'; export const defineCustomElements = (win, options) => { return patchEsm().then(() => { diff --git a/src/compiler_next/bundle/app-data-plugin.ts b/src/compiler_next/bundle/app-data-plugin.ts index 8794e5e3dac..773ddc57a4c 100644 --- a/src/compiler_next/bundle/app-data-plugin.ts +++ b/src/compiler_next/bundle/app-data-plugin.ts @@ -2,7 +2,7 @@ import * as d from '../../declarations'; import MagicString from 'magic-string'; import { normalizePath } from '@utils'; import { Plugin } from 'rollup'; -import { STENCIL_APP_DATA_ID, STENCIL_INTERNAL_CLIENT_ID, STENCIL_INTERNAL_HYDRATE_ID } from './entry-alias-ids'; +import { STENCIL_APP_DATA_ID, STENCIL_INTERNAL_CLIENT_ID, STENCIL_INTERNAL_HYDRATE_ID, STENCIL_APP_GLOBALS_ID } from './entry-alias-ids'; export const appDataPlugin = (config: d.Config, compilerCtx: d.CompilerCtx, build: d.BuildConditionals, platform: 'client' | 'hydrate' | 'worker'): Plugin => { @@ -17,7 +17,7 @@ export const appDataPlugin = (config: d.Config, compilerCtx: d.CompilerCtx, buil name: 'appDataPlugin', resolveId(id) { - if (id === STENCIL_APP_DATA_ID) { + if (id === STENCIL_APP_DATA_ID || id === STENCIL_APP_GLOBALS_ID) { if (platform === 'worker') { this.error('@stencil/core packages cannot be imported from a worker.'); } @@ -27,11 +27,15 @@ export const appDataPlugin = (config: d.Config, compilerCtx: d.CompilerCtx, buil }, load(id) { + if (id === STENCIL_APP_GLOBALS_ID) { + const s = new MagicString(``); + appendGlobalScripts(globalPaths, s); + return s.toString(); + } if (id === STENCIL_APP_DATA_ID) { const s = new MagicString(``); appendNamespace(config, s); appendBuildConditionals(config, build, s); - appendGlobalScripts(globalPaths, s); return s.toString(); } return null; diff --git a/src/compiler_next/bundle/entry-alias-ids.ts b/src/compiler_next/bundle/entry-alias-ids.ts index 6be7cf13669..16ecd7aaf36 100644 --- a/src/compiler_next/bundle/entry-alias-ids.ts +++ b/src/compiler_next/bundle/entry-alias-ids.ts @@ -1,6 +1,7 @@ export const STENCIL_CORE_ID = '@stencil/core'; export const STENCIL_APP_DATA_ID = '@stencil/core/internal/app-data'; +export const STENCIL_APP_GLOBALS_ID = '@stencil/core/internal/app-globals'; export const STENCIL_HYDRATE_FACTORY_ID = '@stencil/core/hydrate-factory'; export const STENCIL_INTERNAL_CLIENT_ID = '@stencil/core/internal/client'; export const STENCIL_INTERNAL_HYDRATE_ID = '@stencil/core/internal/hydrate'; diff --git a/src/compiler_next/output-targets/dist-custom-elements-bundle/index.ts b/src/compiler_next/output-targets/dist-custom-elements-bundle/index.ts index 5743c423695..24fd3731d48 100644 --- a/src/compiler_next/output-targets/dist-custom-elements-bundle/index.ts +++ b/src/compiler_next/output-targets/dist-custom-elements-bundle/index.ts @@ -5,7 +5,7 @@ import { catchError, dashToPascalCase, hasError } from '@utils'; import { getBuildFeatures, updateBuildConditionals } from '../../build/app-data'; import { isOutputTargetDistCustomElementsBundle } from '../../../compiler/output-targets/output-utils'; import { nativeComponentTransform } from '../../../compiler/transformers/component-native/tranform-to-native-component'; -import { STENCIL_INTERNAL_CLIENT_ID, USER_INDEX_ENTRY_ID } from '../../bundle/entry-alias-ids'; +import { STENCIL_INTERNAL_CLIENT_ID, USER_INDEX_ENTRY_ID, STENCIL_APP_GLOBALS_ID } from '../../bundle/entry-alias-ids'; import { updateStencilCoreImports } from '../../../compiler/transformers/update-stencil-core-import'; import path from 'path'; import { formatComponentRuntimeMeta, stringifyRuntimeData } from '../../../compiler/app-core/format-component-runtime-meta'; @@ -80,8 +80,9 @@ function generateEntryPoint(_config: d.Config, _compilerCtx: d.CompilerCtx, buil const imports: string[] = []; const exports: string[] = []; imports.push( - `import { proxyNative, globalScripts } from '${STENCIL_INTERNAL_CLIENT_ID}';`, + `import { proxyNative } from '${STENCIL_INTERNAL_CLIENT_ID}';`, `export * from '${USER_INDEX_ENTRY_ID}';`, + `import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';`, 'globalScripts();', ); diff --git a/src/compiler_next/output-targets/dist-lazy/lazy-output.ts b/src/compiler_next/output-targets/dist-lazy/lazy-output.ts index 9272fa3d70b..263f87e0e3f 100644 --- a/src/compiler_next/output-targets/dist-lazy/lazy-output.ts +++ b/src/compiler_next/output-targets/dist-lazy/lazy-output.ts @@ -4,7 +4,7 @@ import { bundleOutput } from '../../bundle/bundle-output'; import { catchError } from '@utils'; import { generateEntryModules } from '../../../compiler/entries/entry-modules'; import { getBuildFeatures, updateBuildConditionals } from '../../build/app-data'; -import { LAZY_BROWSER_ENTRY_ID, LAZY_EXTERNAL_ENTRY_ID, STENCIL_INTERNAL_CLIENT_ID, USER_INDEX_ENTRY_ID } from '../../bundle/entry-alias-ids'; +import { LAZY_BROWSER_ENTRY_ID, LAZY_EXTERNAL_ENTRY_ID, STENCIL_INTERNAL_CLIENT_ID, USER_INDEX_ENTRY_ID, STENCIL_APP_GLOBALS_ID } from '../../bundle/entry-alias-ids'; import { isOutputTargetDistLazy, isOutputTargetHydrate } from '../../../compiler/output-targets/output-utils'; import { lazyComponentTransform } from '../../transformers/component-lazy/transform-lazy-component'; import { updateStencilCoreImports } from '../../../compiler/transformers/update-stencil-core-import'; @@ -119,11 +119,11 @@ const getCustomTransformer = (compilerCtx: d.CompilerCtx) => { const getLazyEntry = (isBrowser: boolean) => { const s = new MagicString(``); - s.append(`import { bootstrapLazy, globalScripts } from '${STENCIL_INTERNAL_CLIENT_ID}';\n`); - + s.append(`import { bootstrapLazy } from '${STENCIL_INTERNAL_CLIENT_ID}';\n`); if (isBrowser) { s.append(`import { patchBrowser } from '${STENCIL_INTERNAL_CLIENT_ID}';\n`); + s.append(`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';\n`); s.append(`patchBrowser().then(options => {\n`); s.append(` globalScripts();\n`); s.append(` return bootstrapLazy([/*!__STENCIL_LAZY_DATA__*/], options);\n`); @@ -131,6 +131,7 @@ const getLazyEntry = (isBrowser: boolean) => { } else { s.append(`import { patchEsm } from '${STENCIL_INTERNAL_CLIENT_ID}';\n`); + s.append(`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';\n`); s.append(`export const defineCustomElements = (win, options) => patchEsm().then(() => {\n`); s.append(` globalScripts();\n`); s.append(` return bootstrapLazy([/*!__STENCIL_LAZY_DATA__*/], options);\n`); diff --git a/src/hydrate/platform/hydrate-app.ts b/src/hydrate/platform/hydrate-app.ts index 9815b97b72e..39536b34865 100644 --- a/src/hydrate/platform/hydrate-app.ts +++ b/src/hydrate/platform/hydrate-app.ts @@ -1,8 +1,8 @@ import * as d from '../../declarations'; import { connectedCallback, insertVdomAnnotations } from '@runtime'; import { doc, getHostRef, loadModule, plt, registerHost } from '@platform'; -import { globalScripts } from '@app-data'; import { proxyHostElement } from './proxy-host-element'; +import { globalScripts } from '@app-globals'; export function hydrateApp( diff --git a/src/hydrate/platform/index.ts b/src/hydrate/platform/index.ts index 4555da73f88..cd1032eaa06 100644 --- a/src/hydrate/platform/index.ts +++ b/src/hydrate/platform/index.ts @@ -131,7 +131,7 @@ export const Build: d.UserBuildConditionals = { export const styles: d.StyleMap = new Map(); -export { BUILD, NAMESPACE, globalScripts } from '@app-data'; +export { BUILD, NAMESPACE } from '@app-data'; export { hydrateApp } from './hydrate-app'; export * from '@runtime'; diff --git a/src/runtime/vdom/test/tsconfig.json b/src/runtime/vdom/test/tsconfig.json index 52c20134558..3ca311ffd76 100644 --- a/src/runtime/vdom/test/tsconfig.json +++ b/src/runtime/vdom/test/tsconfig.json @@ -32,6 +32,9 @@ "@app-data": [ "../../../app-data/index.ts" ], + "@app-globals": [ + "../../../app-globals/index.ts" + ], "@utils": [ "../../../utils/index.ts" ] diff --git a/tsconfig.json b/tsconfig.json index f687304725d..b8c89092fa5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -27,6 +27,9 @@ "@app-data": [ "src/app-data/index.ts" ], + "@app-globals": [ + "src/app-globals/index.ts" + ], "@compiler": [ "src/compiler_next/index.ts" ], @@ -80,6 +83,7 @@ "files": [ "src/app-data/index.ts", + "src/app-globals/index.ts", "src/client/index.ts", "src/client/polyfills/css-shim/index.ts", "src/cli/index.ts",