Skip to content

Commit

Permalink
refactor(compiler): update compiler sys
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jun 30, 2020
1 parent bd45a16 commit 4408ec1
Show file tree
Hide file tree
Showing 57 changed files with 1,656 additions and 869 deletions.
82 changes: 46 additions & 36 deletions bin/stencil
@@ -1,46 +1,56 @@
#!/usr/bin/env node
'use strict';

try {
var minimumVersion = '10.13';
var recommendedVersion = '12.17';
var currentVersion = process.versions.node;

function isNodeLT(v) {
var check = v.split('.').map(Number);
var node = currentVersion.split('.').map(Number);
return node[0] < check[0] || (node[0] === check[0] && node[1] < check[1]);
}

if (isNodeLT(minimumVersion)) {
console.error(
'\nYour current version of Node is v' +
currentVersion +
', however Stencil requires v' +
minimumVersion +
'.0 or greater. It is recommended to install latest version of Node (https://github.com/nodejs/Release).\n',
);
process.exit(1);
}

if (isNodeLT(recommendedVersion)) {
console.warn(
'\nYour current version of Node is v' +
currentVersion +
', however Stencil\'s recommendation is v' +
recommendedVersion +
'.0 or greater. Note that future versions of Stencil will eventually remove support for non-LTS Node versions (https://github.com/nodejs/Release).\n',
);
}
} catch (e) {}
var minimumVersion = '10.13';
var recommendedVersion = '12.17';
var currentVersion = process.versions.node;

function isNodeLT(v) {
var check = v.split('.').map(Number);
var node = currentVersion.split('.').map(Number);
return node[0] < check[0] || (node[0] === check[0] && node[1] < check[1]);
}

if (isNodeLT(minimumVersion)) {
console.error(
'\nYour current version of Node is v' +
currentVersion +
', however Stencil requires v' +
minimumVersion +
'.0 or greater. It is recommended to install latest version of Node (https://github.com/nodejs/Release).\n',
);
process.exit(1);
}

if (isNodeLT(recommendedVersion)) {
console.warn(
'\nYour current version of Node is v' +
currentVersion +
", however Stencil's recommendation is v" +
recommendedVersion +
'.0 or greater. Note that future versions of Stencil will eventually remove support for non-LTS Node versions (https://github.com/nodejs/Release).\n',
);
}

if (process.argv.indexOf('--next') > -1) {
console.warn('\nThe next compiler is now the default and the --next flag is no longer needed.\n');
}

var cli = require('../cli/index.js');
if (typeof globalThis === 'undefined') {
// globalThis not added until node 12
global.globalThis = global;
}

var cli = require('../cli/index.cjs.js');
var nodeApi = require('../sys/node/index.js');
var nodeLogger = nodeApi.createNodeLogger({ process: process });
var nodeSys = nodeApi.createNodeSysWithWatch({ process: process, logger: nodeLogger });

nodeApi.setupNodeProcess({ process: process, logger: nodeLogger });

cli.run({
process: process,
logger: cli.createNodeLogger(process),
sys: cli.createNodeSystem(process)
args: process.argv.slice(2),
logger: nodeLogger,
sys: nodeSys,
checkVersion: nodeApi.checkVersion
});
9 changes: 2 additions & 7 deletions src/compiler/build/build-hmr.ts
@@ -1,8 +1,8 @@
import * as d from '../../declarations';
import { isGlob, normalizePath, sortBy } from '@utils';
import { getScopeId } from '../style/scope-css';
import { isOutputTargetWww } from '../output-targets/output-utils';
import minimatch from 'minimatch';
import { isGlob, normalizePath, sortBy } from '@utils';
import { basename } from 'path';

export const generateHmr = (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) => {
Expand All @@ -12,12 +12,7 @@ export const generateHmr = (config: d.Config, compilerCtx: d.CompilerCtx, buildC

const hmr: d.HotModuleReplacement = {
reloadStrategy: config.devServer.reloadStrategy,
versionId:
Date.now()
.toString()
.substring(6) +
'' +
Math.round(Math.random() * 89999 + 10000),
versionId: Date.now().toString().substring(6) + '' + Math.round(Math.random() * 89999 + 10000),
};

if (buildCtx.scriptsAdded.length > 0) {
Expand Down
11 changes: 5 additions & 6 deletions src/compiler/bundle/core-resolve-plugin.ts
@@ -1,7 +1,6 @@
import * as d from '../../declarations';
import { dirname, join } from 'path';
import { fetchModuleAsync } from '../sys/fetch/fetch-module-async';
import { getNodeModulePath } from '../sys/resolve/resolve-utils';
import { HYDRATED_CSS } from '../../runtime/runtime-constants';
import { isExternalUrl, getStencilModuleUrl, packageVersions } from '../sys/fetch/fetch-utils';
import { normalizePath, normalizeFsPath } from '@utils';
Expand All @@ -28,9 +27,9 @@ export const coreResolvePlugin = (config: d.Config, compilerCtx: d.CompilerCtx,
};
}
const compilerExe = config.sys.getCompilerExecutingPath();
const internalClient = getStencilInternalModule(config.rootDir, compilerExe, 'client/index.js');
const internalClientPatch = getStencilInternalModule(config.rootDir, compilerExe, 'client/patch.js');
const internalHydrate = getStencilInternalModule(config.rootDir, compilerExe, 'hydrate/index.js');
const internalClient = getStencilInternalModule(config, compilerExe, 'client/index.js');
const internalClientPatch = getStencilInternalModule(config, compilerExe, 'client/patch.js');
const internalHydrate = getStencilInternalModule(config, compilerExe, 'hydrate/index.js');

return {
name: 'coreResolvePlugin',
Expand Down Expand Up @@ -118,9 +117,9 @@ export const coreResolvePlugin = (config: d.Config, compilerCtx: d.CompilerCtx,
};
};

export const getStencilInternalModule = (rootDir: string, compilerExe: string, internalModule: string) => {
export const getStencilInternalModule = (config: d.Config, compilerExe: string, internalModule: string) => {
if (isExternalUrl(compilerExe)) {
return getNodeModulePath(rootDir, '@stencil', 'core', 'internal', internalModule);
return normalizePath(config.sys.getLocalModulePath({ rootDir: config.rootDir, moduleId: '@stencil/core', path: 'internal/' + internalModule }));
}

const compilerExeDir = dirname(compilerExe);
Expand Down
11 changes: 8 additions & 3 deletions src/compiler/bundle/test/core-resolve-plugin.spec.ts
@@ -1,19 +1,24 @@
import * as d from '../../../declarations';
import { createSystem } from '../../../compiler/sys/stencil-sys';
import { getHydratedFlagHead, getStencilInternalModule } from '../core-resolve-plugin';

describe('core resolve plugin', () => {
const rootDir = '/';
const config: d.Config = {
rootDir: '/',
sys: createSystem(),
};

it('http localhost with port url path', () => {
const compilerExe = 'http://localhost:3333/@stencil/core/compiler/stencil.js?v=1.2.3';
const internalModule = 'hydrate/index.js';
const m = getStencilInternalModule(rootDir, compilerExe, internalModule);
const m = getStencilInternalModule(config, compilerExe, internalModule);
expect(m).toBe('/node_modules/@stencil/core/internal/hydrate/index.js');
});

it('node path', () => {
const compilerExe = '/Users/me/node_modules/stencil/compiler/stencil.js';
const internalModule = 'client/index.js';
const m = getStencilInternalModule(rootDir, compilerExe, internalModule);
const m = getStencilInternalModule(config, compilerExe, internalModule);
expect(m).toBe('/Users/me/node_modules/stencil/internal/client/index.js');
});

Expand Down
11 changes: 7 additions & 4 deletions src/compiler/compiler.ts
Expand Up @@ -5,11 +5,12 @@ import { createFullBuild } from './build/full-build';
import { createInMemoryFs } from './sys/in-memory-fs';
import { createSysWorker } from './sys/worker/sys-worker';
import { createWatchBuild } from './build/watch-build';
import { fetchPreloadFs } from './sys/fetch/fetch-preload';
import { dependencies } from './sys/dependencies';
import { getConfig } from './sys/config';
import { patchFs } from './sys/fs-patch';
import { patchTypescript } from './sys/typescript/typescript-patch';
import { resolveModuleIdAsync } from './sys/resolve/resolve-module-async';
import { isFunction } from '@utils';

export const createCompiler = async (config: Config) => {
// actual compiler code
Expand All @@ -22,14 +23,16 @@ export const createCompiler = async (config: Config) => {

patchFs(sys);

if (isFunction(sys.ensureDependencies)) {
await sys.ensureDependencies({ rootDir: config.rootDir, dependencies });
}

compilerCtx.fs = createInMemoryFs(sys);
compilerCtx.cache = new Cache(config, createInMemoryFs(sys));
await compilerCtx.cache.initCacheDir();

sys.resolveModuleId = opts => resolveModuleIdAsync(sys, compilerCtx.fs, opts);
compilerCtx.worker = createSysWorker(sys, config.maxConcurrentWorkers);

await fetchPreloadFs(config, compilerCtx.fs);
compilerCtx.worker = createSysWorker(config);

if (sys.events) {
// Pipe events from sys.events to compilerCtx
Expand Down
34 changes: 15 additions & 19 deletions src/compiler/config/load-config.ts
@@ -1,12 +1,12 @@
import type { CompilerSystem, Config, Diagnostic, LoadConfigInit, LoadConfigResults } from '../../declarations';
import type TypeScript from 'typescript';
import { buildError, catchError, isString, normalizePath, hasError, IS_NODE_ENV } from '@utils';
import { CompilerSystem, Config, Diagnostic, LoadConfigInit, LoadConfigResults } from '../../declarations';
import { createLogger } from '../sys/logger';
import { createLogger } from '../sys/logger/console-logger';
import { createSystem } from '../sys/stencil-sys';
import { dirname, isAbsolute, join, resolve } from 'path';
import { loadTypescript } from '../sys/typescript/typescript-load';
import { validateConfig } from './validate-config';
import { validateTsConfig } from '../sys/typescript/typescript-config';
import tsTypes from 'typescript';

export const loadConfig = async (init: LoadConfigInit = {}) => {
const results: LoadConfigResults = {
Expand All @@ -21,7 +21,6 @@ export const loadConfig = async (init: LoadConfigInit = {}) => {
const sys = init.sys || createSystem();
const config = init.config || {};
const cwd = sys.getCurrentDirectory();
const tsPromise = loadTypescript(sys, results.diagnostics, init.typescriptPath);
let configPath = init.configPath || config.configPath;
let isDefaultConfigPath = true;

Expand Down Expand Up @@ -54,13 +53,12 @@ export const loadConfig = async (init: LoadConfigInit = {}) => {
} else {
// no stencil.config.ts or .js file, which is fine
// #0CJS ¯\_(ツ)_/¯
results.config = Object.assign({}, config);
results.config = { ...config };
results.config.configPath = null;
results.config.rootDir = normalizePath(cwd);
}

results.config.sys = sys;
results.config.cwd = normalizePath(cwd);

const validated = validateConfig(results.config);
results.diagnostics.push(...validated.diagnostics);
Expand All @@ -79,9 +77,9 @@ export const loadConfig = async (init: LoadConfigInit = {}) => {
}

results.config.logger = init.logger || results.config.logger || createLogger();
results.config.logger.level = results.config.logLevel;
results.config.logger.setLevel(results.config.logLevel);

const loadedTs = await tsPromise;
const loadedTs = await loadTypescript(sys, results.config.rootDir, init.typescriptPath, false);
if (!hasError(results.diagnostics)) {
const tsConfigResults = await validateTsConfig(loadedTs, results.config, sys, init);
results.diagnostics.push(...tsConfigResults.diagnostics);
Expand All @@ -101,7 +99,7 @@ export const loadConfig = async (init: LoadConfigInit = {}) => {
return results;
};

const loadConfigFile = async (sys: CompilerSystem, diagnostics: Diagnostic[], configPath: string, isDefaultConfigPath: boolean, typescriptPath: string) => {
const loadConfigFile = async (sys: CompilerSystem, diagnostics: Diagnostic[], configPath: string, isDefaultConfigPath: boolean, typeScriptPath: string) => {
let config: Config = null;

let hasConfigFile = false;
Expand All @@ -121,7 +119,7 @@ const loadConfigFile = async (sys: CompilerSystem, diagnostics: Diagnostic[], co
hasConfigFile = true;
} else if (stat.isDirectory()) {
// this is only a directory, so let's make some assumptions
for (const configName of CONFIG_FILENAMES) {
for (const configName of ['stencil.config.ts', 'stencil.config.js']) {
const testConfigFilePath = join(configPath, configName);
const stat = await sys.stat(testConfigFilePath);
if (stat && stat.isFile()) {
Expand All @@ -137,7 +135,7 @@ const loadConfigFile = async (sys: CompilerSystem, diagnostics: Diagnostic[], co
if (hasConfigFile) {
// the passed in config was a string, so it's probably a path to the config we need to load
// first clear the require cache so we don't get the same file
const configFileData = await evaluateConfigFile(sys, diagnostics, configPath, typescriptPath);
const configFileData = await evaluateConfigFile(sys, diagnostics, configPath, typeScriptPath);
if (hasError(diagnostics)) {
return config;
}
Expand All @@ -155,14 +153,12 @@ const loadConfigFile = async (sys: CompilerSystem, diagnostics: Diagnostic[], co
return config;
};

const CONFIG_FILENAMES = ['stencil.config.ts', 'stencil.config.js'];

const evaluateConfigFile = async (sys: CompilerSystem, diagnostics: Diagnostic[], configFilePath: string, typescriptPath: string) => {
const evaluateConfigFile = async (sys: CompilerSystem, diagnostics: Diagnostic[], configFilePath: string, typeScriptPath: string) => {
let configFileData: { config?: Config } = null;

try {
// TODO: this should use sys for resolving
const ts = await loadTypescript(sys, diagnostics, typescriptPath);
const rootDir = sys.platformPath.dirname(configFilePath);
const ts = await loadTypescript(sys, rootDir, typeScriptPath, false);

if (IS_NODE_ENV) {
// ensure we cleared out node's internal require() cache for this file
Expand Down Expand Up @@ -193,7 +189,7 @@ const evaluateConfigFile = async (sys: CompilerSystem, diagnostics: Diagnostic[]
require.extensions['.ts'] = undefined;
} else {
// browser environment, can't use node's require() to evaluate
let sourceText = sys.readFileSync(configFilePath, 'utf8');
let sourceText = await sys.readFile(configFilePath);
sourceText = transpileTypedConfig(ts, diagnostics, sourceText, configFilePath);
if (hasError(diagnostics)) {
return configFileData;
Expand All @@ -209,14 +205,14 @@ const evaluateConfigFile = async (sys: CompilerSystem, diagnostics: Diagnostic[]
return configFileData;
};

const transpileTypedConfig = (ts: typeof tsTypes, diagnostics: Diagnostic[], sourceText: string, filePath: string) => {
const transpileTypedConfig = (ts: typeof TypeScript, diagnostics: Diagnostic[], sourceText: string, filePath: string) => {
// let's transpile an awesome stencil.config.ts file into
// a boring stencil.config.js file
if (hasError(diagnostics)) {
return sourceText;
}

const opts: tsTypes.TranspileOptions = {
const opts: TypeScript.TranspileOptions = {
fileName: filePath,
compilerOptions: {
module: ts.ModuleKind.CommonJS,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/config/transpile-options.ts
Expand Up @@ -86,6 +86,7 @@ export const getTranspileConfig = (input: TranspileOptions) => {
componentExport: compileOpts.componentExport as any,
componentMetadata: compileOpts.componentMetadata as any,
currentDirectory: compileOpts.currentDirectory,
isolatedModules: true,
module: compileOpts.module as any,
proxy: compileOpts.proxy as any,
file: compileOpts.file,
Expand All @@ -94,7 +95,6 @@ export const getTranspileConfig = (input: TranspileOptions) => {
};

const config: Config = {
cwd: compileOpts.currentDirectory,
rootDir: compileOpts.currentDirectory,
srcDir: compileOpts.currentDirectory,
devMode: true,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/config/validate-service-worker.ts
@@ -1,5 +1,5 @@
import * as d from '../../declarations';
import { HOST_CONFIG_FILENAME } from '../../prerender/host-config';
import { HOST_CONFIG_FILENAME } from '../prerender/host-config';
import { isAbsolute, join } from 'path';
import { isString } from '@utils';

Expand Down
18 changes: 12 additions & 6 deletions src/compiler/index.ts
@@ -1,15 +1,21 @@
import { initWorkerThread } from './worker/worker-thread';
import path from './sys/modules/path';
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';

if (IS_WEB_WORKER_ENV) {
initWebWorkerThread(createWorkerMessageHandler(createSystem()));
}

export { compile, compileSync, transpile, transpileSync } from './transpile';
export { createCompiler } from './compiler';
export { createPrerenderer } from './prerender/prerender-main';
export { createSystem } from './sys/stencil-sys';
export { createWorkerContext } from './worker/worker-thread';
export { createWorkerMessageHandler } from './worker/worker-thread';
export { dependencies } from './sys/dependencies';
export { loadConfig } from './config/load-config';
export { optimizeCss } from './optimize/optimize-css';
export { optimizeJs } from './optimize/optimize-js';
export { path };
export { version } from '../version';

initWorkerThread(globalThis);
export { path } from './sys/modules/path';
export { version, versions, vermoji, buildId } from '../version';

0 comments on commit 4408ec1

Please sign in to comment.