Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adamdbradley committed Aug 16, 2020
1 parent abae5d1 commit c9306b9
Show file tree
Hide file tree
Showing 20 changed files with 32 additions and 22 deletions.
3 changes: 2 additions & 1 deletion 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.
Expand Down
2 changes: 1 addition & 1 deletion 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) {
Expand Down
3 changes: 2 additions & 1 deletion 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) => {
Expand Down
3 changes: 2 additions & 1 deletion 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';
Expand Down
3 changes: 2 additions & 1 deletion 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';
Expand Down
2 changes: 1 addition & 1 deletion 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) {
Expand Down
2 changes: 1 addition & 1 deletion 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;

Expand Down
3 changes: 2 additions & 1 deletion 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) => {
Expand Down
3 changes: 2 additions & 1 deletion 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<string, string[]>,
Expand Down
4 changes: 1 addition & 3 deletions src/utils/environment.ts → 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 =
Expand All @@ -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 : () => '/';

Expand Down
2 changes: 1 addition & 1 deletion 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;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/sys/modules/os.ts
@@ -1,4 +1,4 @@
import { OS_PLATFORM } from '@utils';
import { OS_PLATFORM } from '../environment';

export const EOL = '\n';

Expand Down
3 changes: 2 additions & 1 deletion 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;
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/sys/resolve/resolve-module-sync.ts
Expand Up @@ -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';

Expand Down
3 changes: 2 additions & 1 deletion 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'];

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/sys/stencil-sys.ts
Expand Up @@ -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';

Expand Down
3 changes: 2 additions & 1 deletion 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';
Expand Down
3 changes: 2 additions & 1 deletion 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';

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transpile/transpile-module.ts
Expand Up @@ -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';
Expand Down
1 change: 0 additions & 1 deletion 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';
Expand Down

0 comments on commit c9306b9

Please sign in to comment.