From 4e593c13d618831c865e7fc06f6b1f92881a35fa Mon Sep 17 00:00:00 2001 From: Alice Pote Date: Fri, 7 Jul 2023 09:59:10 -0500 Subject: [PATCH] refactor(environment): remove IS_NODE_ENV and delete dead code This removes the `IS_NODE_ENV` variable from our environment module which previously was used to switch between browser-specific and node-specific approaches to various things in the Stencil compilation process. Following the merge of #4317 we only support one runtime, node, for compiling Stencil components, so we can assume that `IS_NODE_ENV` is `true` and delete code accordingly. --- src/compiler/build/build-finish.ts | 3 +- src/compiler/config/load-config.ts | 99 +++++-------------- src/compiler/optimize/autoprefixer.ts | 4 - src/compiler/sys/environment.ts | 11 +-- src/compiler/sys/node-require.ts | 93 +++++++++-------- .../typescript/typescript-resolve-module.ts | 27 +---- src/compiler/sys/typescript/typescript-sys.ts | 2 - 7 files changed, 70 insertions(+), 169 deletions(-) diff --git a/src/compiler/build/build-finish.ts b/src/compiler/build/build-finish.ts index 91c0016b5cf..cbd44487756 100644 --- a/src/compiler/build/build-finish.ts +++ b/src/compiler/build/build-finish.ts @@ -2,7 +2,6 @@ import { isFunction, isRemoteUrl } from '@utils'; import { relative } from 'path'; import type * as d from '../../declarations'; -import { IS_NODE_ENV } from '../sys/environment'; import { generateBuildResults } from './build-results'; import { generateBuildStats, writeBuildStats } from './build-stats'; @@ -122,7 +121,7 @@ const buildDone = async ( if (!config.watch) { compilerCtx.reset(); - if (IS_NODE_ENV && global.gc) { + if (global.gc) { buildCtx.debug(`triggering forced gc`); global.gc(); buildCtx.debug(`forced gc finished`); diff --git a/src/compiler/config/load-config.ts b/src/compiler/config/load-config.ts index 46e3040d810..e0f0929854a 100644 --- a/src/compiler/config/load-config.ts +++ b/src/compiler/config/load-config.ts @@ -1,16 +1,8 @@ import { createNodeSys } from '@sys-api-node'; import { buildError, catchError, hasError, isString, normalizePath } from '@utils'; import { dirname } from 'path'; -import ts from 'typescript'; - -import type { - CompilerSystem, - Diagnostic, - LoadConfigInit, - LoadConfigResults, - UnvalidatedConfig, -} from '../../declarations'; -import { IS_NODE_ENV } from '../sys/environment'; + +import type { Diagnostic, LoadConfigInit, LoadConfigResults, UnvalidatedConfig } from '../../declarations'; import { nodeRequire } from '../sys/node-require'; import { validateTsConfig } from '../sys/typescript/typescript-config'; import { validateConfig } from './validate-config'; @@ -55,7 +47,7 @@ export const loadConfig = async (init: LoadConfigInit = {}): Promise => { +const loadConfigFile = async (diagnostics: Diagnostic[], configPath: string): Promise => { let config: UnvalidatedConfig | null = null; if (isString(configPath)) { // the passed in config was a string, so it's probably a path to the config we need to load - const configFileData = await evaluateConfigFile(sys, diagnostics, configPath); + const configFileData = await evaluateConfigFile(diagnostics, configPath); if (hasError(diagnostics)) { return config; } @@ -152,73 +142,28 @@ const loadConfigFile = async ( /** * Load the configuration file, based on the environment that Stencil is being run in - * @param sys the underlying System entity to use to interact with the operating system - * @param diagnostics a series of diagnostics used to track errors & warnings throughout the loading process. Entries - * may be added to this list in the event of an error. + * + * @param diagnostics a series of diagnostics used to track errors & warnings + * throughout the loading process. Entries may be added to this list in the + * event of an error. * @param configFilePath the path to the configuration file to load - * @returns an unvalidated configuration. In the event of an error, additional diagnostics may be pushed to the - * provided `diagnostics` argument and `null` will be returned. + * @returns an unvalidated configuration. In the event of an error, additional + * diagnostics may be pushed to the provided `diagnostics` argument and `null` + * will be returned. */ const evaluateConfigFile = async ( - sys: CompilerSystem, diagnostics: Diagnostic[], configFilePath: string ): Promise<{ config?: UnvalidatedConfig } | null> => { let configFileData: { config?: UnvalidatedConfig } | null = null; try { - if (IS_NODE_ENV) { - const results = nodeRequire(configFilePath); - diagnostics.push(...results.diagnostics); - configFileData = results.module; - } else { - // browser environment, can't use node's require() to evaluate - let sourceText = await sys.readFile(configFilePath); - sourceText = transpileTypedConfig(diagnostics, sourceText, configFilePath); - if (hasError(diagnostics)) { - return configFileData; - } - - const evalConfig = new Function(`const exports = {}; ${sourceText}; return exports;`); - configFileData = evalConfig(); - } + const results = nodeRequire(configFilePath); + diagnostics.push(...results.diagnostics); + configFileData = results.module; } catch (e: any) { catchError(diagnostics, e); } return configFileData; }; - -/** - * Transpiles the provided TypeScript source text into JavaScript. - * - * This function is intended to be used on a `stencil.config.ts` file - * - * @param diagnostics a collection of compiler diagnostics to check as a part of the compilation process - * @param sourceText the text to transpile - * @param filePath the name of the file to transpile - * @returns the transpiled text. If there are any diagnostics in the provided collection, the provided source is returned - */ -const transpileTypedConfig = (diagnostics: Diagnostic[], sourceText: string, filePath: string): string => { - // let's transpile an awesome stencil.config.ts file into - // a boring stencil.config.js file - if (hasError(diagnostics)) { - return sourceText; - } - - const opts: ts.TranspileOptions = { - fileName: filePath, - compilerOptions: { - module: ts.ModuleKind.CommonJS, - moduleResolution: ts.ModuleResolutionKind.NodeJs, - esModuleInterop: true, - target: ts.ScriptTarget.ES2015, - allowJs: true, - }, - reportDiagnostics: false, - }; - - const output = ts.transpileModule(sourceText, opts); - - return output.outputText; -}; diff --git a/src/compiler/optimize/autoprefixer.ts b/src/compiler/optimize/autoprefixer.ts index a01a253c43c..22cd48183e0 100644 --- a/src/compiler/optimize/autoprefixer.ts +++ b/src/compiler/optimize/autoprefixer.ts @@ -1,7 +1,6 @@ import { Postcss } from 'postcss'; import type * as d from '../../declarations'; -import { IS_NODE_ENV } from '../sys/environment'; type CssProcessor = ReturnType; let cssProcessor: CssProcessor; @@ -22,9 +21,6 @@ export const autoprefixCss = async (cssText: string, opts: boolean | null | d.Au output: cssText, diagnostics: [], }; - if (!IS_NODE_ENV) { - return output; - } try { const autoprefixerOpts = opts != null && typeof opts === 'object' ? opts : DEFAULT_AUTOPREFIX_OPTIONS; diff --git a/src/compiler/sys/environment.ts b/src/compiler/sys/environment.ts index 2f05fe7b569..821946400e6 100644 --- a/src/compiler/sys/environment.ts +++ b/src/compiler/sys/environment.ts @@ -1,13 +1,4 @@ -export const IS_NODE_ENV = - typeof global !== 'undefined' && - typeof require === 'function' && - !!global.process && - typeof __filename === 'string' && - (!(global as any as Window).origin || typeof (global as any as Window).origin !== 'string'); - -export const OS_PLATFORM = IS_NODE_ENV ? process.platform : ''; - -export const IS_WINDOWS_ENV = OS_PLATFORM === 'win32'; +export const IS_WINDOWS_ENV = process.platform === 'win32'; export const IS_CASE_SENSITIVE_FILE_NAMES = !IS_WINDOWS_ENV; diff --git a/src/compiler/sys/node-require.ts b/src/compiler/sys/node-require.ts index 589c0b4e458..5282a5e024c 100644 --- a/src/compiler/sys/node-require.ts +++ b/src/compiler/sys/node-require.ts @@ -2,7 +2,6 @@ import { catchError, loadTypeScriptDiagnostic } from '@utils'; import ts from 'typescript'; import type { Diagnostic } from '../../declarations'; -import { IS_NODE_ENV } from './environment'; export const nodeRequire = (id: string) => { const results = { @@ -11,61 +10,59 @@ export const nodeRequire = (id: string) => { diagnostics: [] as Diagnostic[], }; - if (IS_NODE_ENV) { - try { - const fs: typeof import('fs') = require('fs'); - const path: typeof import('path') = require('path'); + try { + const fs: typeof import('fs') = require('fs'); + const path: typeof import('path') = require('path'); - results.id = path.resolve(id); + results.id = path.resolve(id); - // ensure we cleared out node's internal require() cache for this file - delete require.cache[results.id]; + // ensure we cleared out node's internal require() cache for this file + delete require.cache[results.id]; - // let's override node's require for a second - // don't worry, we'll revert this when we're done - require.extensions['.ts'] = (module: NodeJS.Module, fileName: string) => { - let sourceText = fs.readFileSync(fileName, 'utf8'); + // let's override node's require for a second + // don't worry, we'll revert this when we're done + require.extensions['.ts'] = (module: NodeJS.Module, fileName: string) => { + let sourceText = fs.readFileSync(fileName, 'utf8'); - if (fileName.endsWith('.ts')) { - // looks like we've got a typed config file - // let's transpile it to .js quick - const tsResults = ts.transpileModule(sourceText, { - fileName, - compilerOptions: { - module: ts.ModuleKind.CommonJS, - moduleResolution: ts.ModuleResolutionKind.NodeJs, - esModuleInterop: true, - target: ts.ScriptTarget.ES2017, - allowJs: true, - }, - }); - sourceText = tsResults.outputText; + if (fileName.endsWith('.ts')) { + // looks like we've got a typed config file + // let's transpile it to .js quick + const tsResults = ts.transpileModule(sourceText, { + fileName, + compilerOptions: { + module: ts.ModuleKind.CommonJS, + moduleResolution: ts.ModuleResolutionKind.NodeJs, + esModuleInterop: true, + target: ts.ScriptTarget.ES2017, + allowJs: true, + }, + }); + sourceText = tsResults.outputText; - results.diagnostics.push(...tsResults.diagnostics.map(loadTypeScriptDiagnostic)); - } else { - // quick hack to turn a modern es module - // into and old school commonjs module - sourceText = sourceText.replace(/export\s+\w+\s+(\w+)/gm, 'exports.$1'); - } + results.diagnostics.push(...tsResults.diagnostics.map(loadTypeScriptDiagnostic)); + } else { + // quick hack to turn a modern es module + // into and old school commonjs module + sourceText = sourceText.replace(/export\s+\w+\s+(\w+)/gm, 'exports.$1'); + } - try { - // we need to coerce because of the requirements for the arguments to - // this function. It's safe enough since it's already wrapped in a - // `try { } catch`. - (module as NodeModuleWithCompile)._compile(sourceText, fileName); - } catch (e: any) { - catchError(results.diagnostics, e); - } - }; + try { + // we need to coerce because of the requirements for the arguments to + // this function. It's safe enough since it's already wrapped in a + // `try { } catch`. + (module as NodeModuleWithCompile)._compile(sourceText, fileName); + } catch (e: any) { + catchError(results.diagnostics, e); + } + }; - // let's do this! - results.module = require(results.id); + // let's do this! + results.module = require(results.id); - // all set, let's go ahead and reset the require back to the default - require.extensions['.ts'] = undefined; - } catch (e: any) { - catchError(results.diagnostics, e); - } + // all set, let's go ahead and reset the require back to the default + require.extensions['.ts'] = undefined; + } catch (e: any) { + catchError(results.diagnostics, e); } return results; diff --git a/src/compiler/sys/typescript/typescript-resolve-module.ts b/src/compiler/sys/typescript/typescript-resolve-module.ts index 6e495a36222..90400f4eb3f 100644 --- a/src/compiler/sys/typescript/typescript-resolve-module.ts +++ b/src/compiler/sys/typescript/typescript-resolve-module.ts @@ -1,10 +1,9 @@ -import { isRemoteUrl, isString, normalizePath } from '@utils'; +import { isString, normalizePath } from '@utils'; import { basename, dirname, isAbsolute, join, resolve } from 'path'; import ts from 'typescript'; import type * as d from '../../../declarations'; import { version } from '../../../version'; -import { IS_BROWSER_ENV, IS_NODE_ENV } from '../environment'; import { InMemoryFileSystem } from '../in-memory-fs'; import { resolveRemoteModuleIdSync } from '../resolve/resolve-module-sync'; import { @@ -19,28 +18,6 @@ import { } from '../resolve/resolve-utils'; import { patchTsSystemFileSystem } from './typescript-sys'; -// TODO(STENCIL-728): fix typing of `inMemoryFs` parameter in `patchTypescript`, related functions -export const patchTypeScriptResolveModule = (config: d.Config, inMemoryFs: InMemoryFileSystem) => { - let compilerExe: string; - if (config.sys) { - compilerExe = config.sys.getCompilerExecutingPath(); - } else if (IS_BROWSER_ENV) { - compilerExe = location.href; - } - - if (shouldPatchRemoteTypeScript(compilerExe)) { - const resolveModuleName = ((ts as any).__resolveModuleName = ts.resolveModuleName); - - ts.resolveModuleName = (moduleName, containingFile, compilerOptions, host, cache, redirectedReference) => { - const resolvedModule = patchedTsResolveModule(config, inMemoryFs, moduleName, containingFile); - if (resolvedModule) { - return resolvedModule; - } - return resolveModuleName(moduleName, containingFile, compilerOptions, host, cache, redirectedReference); - }; - } -}; - export const tsResolveModuleName = ( config: d.Config, compilerCtx: d.CompilerCtx, @@ -235,5 +212,3 @@ const getTsResolveExtension = (p: string) => { } return ts.Extension.Ts; }; - -const shouldPatchRemoteTypeScript = (compilerExe: string) => !IS_NODE_ENV && isRemoteUrl(compilerExe); diff --git a/src/compiler/sys/typescript/typescript-sys.ts b/src/compiler/sys/typescript/typescript-sys.ts index 6cf1b2c23d5..3e39d4fde44 100644 --- a/src/compiler/sys/typescript/typescript-sys.ts +++ b/src/compiler/sys/typescript/typescript-sys.ts @@ -6,7 +6,6 @@ import type * as d from '../../../declarations'; import { IS_CASE_SENSITIVE_FILE_NAMES, IS_WEB_WORKER_ENV } from '../environment'; import { fetchUrlSync } from '../fetch/fetch-module-sync'; import { InMemoryFileSystem } from '../in-memory-fs'; -import { patchTypeScriptResolveModule } from './typescript-resolve-module'; // TODO(STENCIL-728): fix typing of `inMemoryFs` parameter in `patchTypescript`, related functions export const patchTsSystemFileSystem = ( @@ -197,7 +196,6 @@ export const patchTypescript = (config: d.Config, inMemoryFs: InMemoryFileSystem patchTsSystemFileSystem(config, config.sys, inMemoryFs, ts.sys); patchTsSystemWatch(config.sys, ts.sys); } - patchTypeScriptResolveModule(config, inMemoryFs); (ts as any).__patched = true; } };