From c9306b9f3d359620f10b5a756bd717b1a210a576 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Sun, 16 Aug 2020 14:48:12 -0500 Subject: [PATCH] fix(treeshaking): move environment helpers out of utils Node and Deno globals are not easily treeshaken, and client imports utils. Move all environment helpers to just the compiler, not shared utils. --- src/cli/task-generate.ts | 3 ++- src/cli/task-test.ts | 2 +- src/compiler/build/build-finish.ts | 3 ++- src/compiler/config/load-config.ts | 3 ++- src/compiler/config/transpile-options.ts | 3 ++- src/compiler/index.ts | 2 +- src/compiler/optimize/autoprefixer.ts | 2 +- src/compiler/prerender/prerender-config.ts | 3 ++- src/compiler/prerender/prerender-worker.ts | 3 ++- src/{utils => compiler/sys}/environment.ts | 4 +--- src/compiler/sys/logger/console-logger.ts | 2 +- src/compiler/sys/modules/os.ts | 2 +- src/compiler/sys/modules/path.ts | 3 ++- src/compiler/sys/resolve/resolve-module-sync.ts | 3 ++- src/compiler/sys/resolve/resolve-utils.ts | 3 ++- src/compiler/sys/stencil-sys.ts | 3 ++- src/compiler/sys/typescript/typescript-resolve-module.ts | 3 ++- src/compiler/sys/typescript/typescript-sys.ts | 3 ++- src/compiler/transpile/transpile-module.ts | 3 ++- src/utils/index.ts | 1 - 20 files changed, 32 insertions(+), 22 deletions(-) rename src/{utils => compiler/sys}/environment.ts (92%) diff --git a/src/cli/task-generate.ts b/src/cli/task-generate.ts index e2ee18748f2..f6bdea45880 100644 --- a/src/cli/task-generate.ts +++ b/src/cli/task-generate.ts @@ -1,6 +1,7 @@ import type { Config } from '../declarations'; import type { CoreCompiler } from './load-compiler'; -import { IS_NODE_ENV, validateComponentTag } from '@utils'; +import { IS_NODE_ENV } from '../compiler/sys/environment'; +import { validateComponentTag } from '@utils'; /** * Task to generate component boilerplate. diff --git a/src/cli/task-test.ts b/src/cli/task-test.ts index ff6f0a38bad..bf610c6e8ca 100644 --- a/src/cli/task-test.ts +++ b/src/cli/task-test.ts @@ -1,5 +1,5 @@ import type { Config, TestingRunOptions } from '../declarations'; -import { IS_NODE_ENV } from '@utils'; +import { IS_NODE_ENV } from '../compiler/sys/environment'; export const taskTest = async (config: Config) => { if (!IS_NODE_ENV) { diff --git a/src/compiler/build/build-finish.ts b/src/compiler/build/build-finish.ts index 5ca7add66c8..f9dbb127c23 100644 --- a/src/compiler/build/build-finish.ts +++ b/src/compiler/build/build-finish.ts @@ -1,6 +1,7 @@ import type * as d from '../../declarations'; import { generateBuildResults } from './build-results'; -import { IS_NODE_ENV, isFunction, isRemoteUrl } from '@utils'; +import { isFunction, isRemoteUrl } from '@utils'; +import { IS_NODE_ENV } from '../sys/environment'; import { relative } from 'path'; export const buildFinish = async (buildCtx: d.BuildCtx) => { diff --git a/src/compiler/config/load-config.ts b/src/compiler/config/load-config.ts index c8539124d78..104012df14d 100644 --- a/src/compiler/config/load-config.ts +++ b/src/compiler/config/load-config.ts @@ -1,8 +1,9 @@ import type { CompilerSystem, Config, Diagnostic, LoadConfigInit, LoadConfigResults } from '../../declarations'; -import { buildError, catchError, isString, normalizePath, hasError, IS_NODE_ENV } from '@utils'; +import { buildError, catchError, isString, normalizePath, hasError } from '@utils'; import { createLogger } from '../sys/logger/console-logger'; import { createSystem } from '../sys/stencil-sys'; import { dirname, resolve } from 'path'; +import { IS_NODE_ENV } from '../sys/environment'; import { validateConfig } from './validate-config'; import { validateTsConfig } from '../sys/typescript/typescript-config'; import ts from 'typescript'; diff --git a/src/compiler/config/transpile-options.ts b/src/compiler/config/transpile-options.ts index 2ea51df31ee..2ef89392a4b 100644 --- a/src/compiler/config/transpile-options.ts +++ b/src/compiler/config/transpile-options.ts @@ -1,6 +1,7 @@ import type { TranspileOptions, TranspileResults, Config, TransformOptions, TransformCssToEsmInput, ImportData, CompilerSystem } from '../../declarations'; import { createSystem } from '../sys/stencil-sys'; -import { isString, IS_NODE_ENV, requireFunc, IS_DENO_ENV } from '@utils'; +import { IS_DENO_ENV, IS_NODE_ENV, requireFunc } from '../sys/environment'; +import { isString } from '@utils'; import { parseImportPath } from '../transformers/stencil-import-path'; import { STENCIL_INTERNAL_CLIENT_ID } from '../bundle/entry-alias-ids'; import type { CompilerOptions } from 'typescript'; diff --git a/src/compiler/index.ts b/src/compiler/index.ts index 13ff4d4522c..83e00b5b973 100644 --- a/src/compiler/index.ts +++ b/src/compiler/index.ts @@ -1,7 +1,7 @@ -import { IS_WEB_WORKER_ENV } from '@utils'; import { createSystem } from './sys/stencil-sys'; import { createWorkerMessageHandler } from './worker/worker-thread'; import { initWebWorkerThread } from './sys/worker/web-worker-thread'; +import { IS_WEB_WORKER_ENV } from './sys/environment'; import ts from 'typescript'; if (IS_WEB_WORKER_ENV) { diff --git a/src/compiler/optimize/autoprefixer.ts b/src/compiler/optimize/autoprefixer.ts index de58cb110ae..f1c7ac47320 100644 --- a/src/compiler/optimize/autoprefixer.ts +++ b/src/compiler/optimize/autoprefixer.ts @@ -1,5 +1,5 @@ import type * as d from '../../declarations'; -import { IS_NODE_ENV, requireFunc } from '@utils'; +import { IS_NODE_ENV, requireFunc } from '../sys/environment'; let cssProcessor: any; diff --git a/src/compiler/prerender/prerender-config.ts b/src/compiler/prerender/prerender-config.ts index 78abd511a28..d5120c16275 100644 --- a/src/compiler/prerender/prerender-config.ts +++ b/src/compiler/prerender/prerender-config.ts @@ -1,5 +1,6 @@ import type * as d from '../../declarations'; -import { catchError, requireFunc, loadTypeScriptDiagnostics, IS_NODE_ENV } from '@utils'; +import { catchError, loadTypeScriptDiagnostics } from '@utils'; +import { IS_NODE_ENV, requireFunc } from '../sys/environment'; import { resolve } from 'path'; export const getPrerenderConfig = (diagnostics: d.Diagnostic[], prerenderConfigPath: string) => { diff --git a/src/compiler/prerender/prerender-worker.ts b/src/compiler/prerender/prerender-worker.ts index 22ccf1afc1f..ee9b82ec9e6 100644 --- a/src/compiler/prerender/prerender-worker.ts +++ b/src/compiler/prerender/prerender-worker.ts @@ -1,9 +1,10 @@ import type * as d from '../../declarations'; import { addModulePreloads, excludeStaticComponents, minifyScriptElements, minifyStyleElements, removeModulePreloads, removeStencilScripts } from './prerender-optimize'; -import { catchError, isPromise, isRootPath, normalizePath, requireFunc, isFunction } from '@utils'; +import { catchError, isPromise, isRootPath, normalizePath, isFunction } from '@utils'; import { crawlAnchorsForNextUrls } from './crawl-urls'; import { getHydrateOptions } from './prerender-hydrate-options'; import { getPrerenderConfig } from './prerender-config'; +import { requireFunc } from '../sys/environment'; const prerenderCtx = { componentGraph: null as Map, diff --git a/src/utils/environment.ts b/src/compiler/sys/environment.ts similarity index 92% rename from src/utils/environment.ts rename to src/compiler/sys/environment.ts index 5fd4264a84d..03319a531b0 100644 --- a/src/utils/environment.ts +++ b/src/compiler/sys/environment.ts @@ -1,5 +1,3 @@ -import { noop } from './helpers'; - export const IS_DENO_ENV = typeof Deno !== 'undefined'; export const IS_NODE_ENV = @@ -24,7 +22,7 @@ export const HAS_WEB_WORKER = IS_BROWSER_ENV && typeof Worker === 'function'; export const IS_FETCH_ENV = typeof fetch === 'function'; -export const requireFunc = IS_NODE_ENV ? require : noop; +export const requireFunc = IS_NODE_ENV ? require : () => {}; export const getCurrentDirectory: () => string = IS_NODE_ENV ? process.cwd : IS_DENO_ENV ? Deno.cwd : () => '/'; diff --git a/src/compiler/sys/logger/console-logger.ts b/src/compiler/sys/logger/console-logger.ts index 2a1af85c7aa..edc9cb9ca26 100644 --- a/src/compiler/sys/logger/console-logger.ts +++ b/src/compiler/sys/logger/console-logger.ts @@ -1,5 +1,5 @@ import type * as d from '../../../declarations'; -import { IS_BROWSER_ENV } from '@utils'; +import { IS_BROWSER_ENV } from '../environment'; export const createLogger = () => { let useColors = IS_BROWSER_ENV; diff --git a/src/compiler/sys/modules/os.ts b/src/compiler/sys/modules/os.ts index 24366703ca4..2c2341e484c 100644 --- a/src/compiler/sys/modules/os.ts +++ b/src/compiler/sys/modules/os.ts @@ -1,4 +1,4 @@ -import { OS_PLATFORM } from '@utils'; +import { OS_PLATFORM } from '../environment'; export const EOL = '\n'; diff --git a/src/compiler/sys/modules/path.ts b/src/compiler/sys/modules/path.ts index 88e3d970011..065ee08b4a5 100644 --- a/src/compiler/sys/modules/path.ts +++ b/src/compiler/sys/modules/path.ts @@ -1,5 +1,6 @@ import type * as d from '../../../declarations'; -import { normalizePath, IS_NODE_ENV, requireFunc } from '@utils'; +import { IS_NODE_ENV, requireFunc } from '../environment'; +import { normalizePath } from '@utils'; import pathBrowserify from 'path-browserify'; export let basename: any; diff --git a/src/compiler/sys/resolve/resolve-module-sync.ts b/src/compiler/sys/resolve/resolve-module-sync.ts index d32353269a5..3a7e6a86f82 100644 --- a/src/compiler/sys/resolve/resolve-module-sync.ts +++ b/src/compiler/sys/resolve/resolve-module-sync.ts @@ -2,7 +2,8 @@ import type * as d from '../../../declarations'; import { COMMON_DIR_FILENAMES, getCommonDirName, isCommonDirModuleFile, shouldFetchModule } from './resolve-utils'; import { fetchModuleSync } from '../fetch/fetch-module-sync'; import { getCommonDirUrl, getNodeModuleFetchUrl, packageVersions } from '../fetch/fetch-utils'; -import { isString, IS_WEB_WORKER_ENV, normalizeFsPath, normalizePath } from '@utils'; +import { isString, normalizeFsPath, normalizePath } from '@utils'; +import { IS_WEB_WORKER_ENV } from '../environment'; import { basename, dirname } from 'path'; import resolve, { SyncOpts } from 'resolve'; diff --git a/src/compiler/sys/resolve/resolve-utils.ts b/src/compiler/sys/resolve/resolve-utils.ts index 1411623c54a..73eb716bfaf 100644 --- a/src/compiler/sys/resolve/resolve-utils.ts +++ b/src/compiler/sys/resolve/resolve-utils.ts @@ -1,5 +1,6 @@ import type * as d from '../../../declarations'; -import { IS_BROWSER_ENV, IS_FETCH_ENV, normalizePath } from '@utils'; +import { IS_BROWSER_ENV, IS_FETCH_ENV } from '../environment'; +import { normalizePath } from '@utils'; const COMMON_DIR_MODULE_EXTS = ['.tsx', '.ts', '.mjs', '.js', '.jsx', '.json', '.md']; diff --git a/src/compiler/sys/stencil-sys.ts b/src/compiler/sys/stencil-sys.ts index ad2f6fe675a..dae9e5618a7 100644 --- a/src/compiler/sys/stencil-sys.ts +++ b/src/compiler/sys/stencil-sys.ts @@ -19,7 +19,8 @@ import { basename, dirname, join } from 'path'; import { buildEvents } from '../events'; import { createLogger } from './logger/console-logger'; import { createWebWorkerMainController } from './worker/web-worker-main'; -import { HAS_WEB_WORKER, IS_BROWSER_ENV, IS_WEB_WORKER_ENV, isRootPath, normalizePath } from '@utils'; +import { HAS_WEB_WORKER, IS_BROWSER_ENV, IS_WEB_WORKER_ENV } from './environment'; +import { isRootPath, normalizePath } from '@utils'; import { resolveModuleIdAsync } from './resolve/resolve-module-async'; import { version } from '../../version'; diff --git a/src/compiler/sys/typescript/typescript-resolve-module.ts b/src/compiler/sys/typescript/typescript-resolve-module.ts index baa9575116d..5edebd704e1 100644 --- a/src/compiler/sys/typescript/typescript-resolve-module.ts +++ b/src/compiler/sys/typescript/typescript-resolve-module.ts @@ -1,7 +1,8 @@ import type * as d from '../../../declarations'; import { basename, dirname, isAbsolute, join, resolve } from 'path'; import { isDtsFile, isJsFile, isJsxFile, isLocalModule, isStencilCoreImport, isTsxFile, isTsFile, isJsonFile } from '../resolve/resolve-utils'; -import { isRemoteUrl, isString, IS_BROWSER_ENV, IS_NODE_ENV, normalizePath } from '@utils'; +import { IS_BROWSER_ENV, IS_NODE_ENV } from '../environment'; +import { isRemoteUrl, isString, normalizePath } from '@utils'; import { patchTsSystemFileSystem } from './typescript-sys'; import { resolveRemoteModuleIdSync } from '../resolve/resolve-module-sync'; import { version } from '../../../version'; diff --git a/src/compiler/sys/typescript/typescript-sys.ts b/src/compiler/sys/typescript/typescript-sys.ts index f3150f74cca..1644b511a09 100644 --- a/src/compiler/sys/typescript/typescript-sys.ts +++ b/src/compiler/sys/typescript/typescript-sys.ts @@ -1,7 +1,8 @@ import type * as d from '../../../declarations'; import { basename, resolve } from 'path'; -import { getCurrentDirectory, IS_CASE_SENSITIVE_FILE_NAMES, IS_WEB_WORKER_ENV, isRemoteUrl, isString, normalizePath, noop } from '@utils'; +import { isRemoteUrl, isString, normalizePath, noop } from '@utils'; import { fetchUrlSync } from '../fetch/fetch-module-sync'; +import { getCurrentDirectory, IS_CASE_SENSITIVE_FILE_NAMES, IS_WEB_WORKER_ENV } from '../environment'; import { patchTypeScriptResolveModule } from './typescript-resolve-module'; import ts from 'typescript'; diff --git a/src/compiler/transpile/transpile-module.ts b/src/compiler/transpile/transpile-module.ts index c0c831e8c21..0deb4f81e70 100644 --- a/src/compiler/transpile/transpile-module.ts +++ b/src/compiler/transpile/transpile-module.ts @@ -4,7 +4,8 @@ import { CompilerContext } from '../build/compiler-ctx'; import { convertDecoratorsToStatic } from '../transformers/decorators-to-static/convert-decorators'; import { convertStaticToMeta } from '../transformers/static-to-meta/visitor'; import { createLogger } from '../sys/logger/console-logger'; -import { isNumber, isString, loadTypeScriptDiagnostics, normalizePath, getCurrentDirectory } from '@utils'; +import { getCurrentDirectory } from '../sys/environment'; +import { isNumber, isString, loadTypeScriptDiagnostics, normalizePath } from '@utils'; import { lazyComponentTransform } from '../transformers/component-lazy/transform-lazy-component'; import { nativeComponentTransform } from '../transformers/component-native/tranform-to-native-component'; import { updateStencilCoreImports } from '../transformers/update-stencil-core-import'; diff --git a/src/utils/index.ts b/src/utils/index.ts index b4fe0903de2..56ad68c91ba 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,4 @@ export * from './constants'; -export * from './environment'; export * from './format-component-runtime-meta'; export * from './helpers'; export * from './is-glob';