Skip to content

Commit

Permalink
refactor(environment): remove IS_NODE_ENV and delete dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduy1407 committed Dec 1, 2023
1 parent 8a506bc commit 90984b3
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 355 deletions.
3 changes: 1 addition & 2 deletions src/compiler/build/build-finish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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`);
Expand Down
99 changes: 22 additions & 77 deletions src/compiler/config/load-config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -55,7 +47,7 @@ export const loadConfig = async (init: LoadConfigInit = {}): Promise<LoadConfigR
// attached to a configuration entity, validated or otherwise)
const sys = init.sys ?? createNodeSys();

const loadedConfigFile = await loadConfigFile(sys, results.diagnostics, configPath);
const loadedConfigFile = await loadConfigFile(results.diagnostics, configPath);
if (hasError(results.diagnostics)) {
return results;
}
Expand Down Expand Up @@ -116,23 +108,21 @@ export const loadConfig = async (init: LoadConfigInit = {}): Promise<LoadConfigR

/**
* Load a Rindo configuration file from disk
* @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 configPath 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 loadConfigFile = async (
sys: CompilerSystem,
diagnostics: Diagnostic[],
configPath: string
): Promise<UnvalidatedConfig | null> => {
const loadConfigFile = async (diagnostics: Diagnostic[], configPath: string): Promise<UnvalidatedConfig | null> => {
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;
}
Expand All @@ -152,73 +142,28 @@ const loadConfigFile = async (

/**
* Load the configuration file, based on the environment that Rindo 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 `rindo.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 rindo.config.ts file into
// a boring rindo.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;
};
4 changes: 0 additions & 4 deletions src/compiler/optimize/autoprefixer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Postcss } from 'postcss';

import type * as d from '../../declarations';
import { IS_NODE_ENV } from '../sys/environment';

type CssProcessor = ReturnType<Postcss>;
let cssProcessor: CssProcessor;
Expand All @@ -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;
Expand Down
21 changes: 1 addition & 20 deletions src/compiler/sys/environment.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
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;

export const IS_BROWSER_ENV =
typeof location !== 'undefined' && typeof navigator !== 'undefined' && typeof XMLHttpRequest !== 'undefined';

export const IS_WEB_WORKER_ENV =
IS_BROWSER_ENV && typeof self !== 'undefined' && typeof (self as any).importScripts === 'function';

export const HAS_WEB_WORKER = IS_BROWSER_ENV && typeof Worker === 'function';

export const IS_FETCH_ENV = typeof fetch === 'function';
38 changes: 0 additions & 38 deletions src/compiler/sys/fetch/fetch-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,6 @@ export const getRindoModuleUrl = (compilerExe: string, path: string): string =>
return new URL('./' + path, rindoRootUrl).href;
};

export const getCommonDirUrl = (
sys: d.CompilerSystem,
pkgVersions: Map<string, string>,
dirPath: string,
fileName: string
) => getNodeModuleFetchUrl(sys, pkgVersions, dirPath) + '/' + fileName;

export const getNodeModuleFetchUrl = (sys: d.CompilerSystem, pkgVersions: Map<string, string>, filePath: string) => {
// /node_modules/lodash/package.json
filePath = normalizePath(filePath);

// ["node_modules", "lodash", "package.json"]
let pathParts = filePath.split('/').filter((p) => p.length);

const nmIndex = pathParts.lastIndexOf('node_modules');
if (nmIndex > -1 && nmIndex < pathParts.length - 1) {
pathParts = pathParts.slice(nmIndex + 1);
}

let moduleId = pathParts.shift();

if (moduleId.startsWith('@')) {
moduleId += '/' + pathParts.shift();
}

const path = pathParts.join('/');
if (moduleId === '@rindo/core') {
const compilerExe = sys.getCompilerExecutingPath();
return getRindoModuleUrl(compilerExe, path);
}

return sys.getRemoteModuleUrl({
moduleId,
version: pkgVersions.get(moduleId),
path,
});
};

export const skipFilePathFetch = (filePath: string) => {
if (isTsFile(filePath) || isTsxFile(filePath)) {
// don't bother trying to resolve node_module packages w/ typescript files
Expand Down
93 changes: 45 additions & 48 deletions src/compiler/sys/node-require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 90984b3

Please sign in to comment.