Skip to content

Commit b042d8b

Browse files
alicewriteswrongsrwaskiewicz
authored andcommitted
refactor(many): remove in-browser compilation support (#4317)
This removes in-browser compilation support from Stencil. Doing so involves a number of changes, including: - removing the rollup plugin which polyfills node.js built-in modules (fs, path, etc) for use in the browser - deleting the code for the polyfills themselves (found in src/compiler/sys/modules) - miscellaneous refactors related to tests for browser vs node environment as well as testing There will be more follow-up work to fully address this stuff, but this makes the minimal change to remove browser support for Stencil.
1 parent 4a8cf97 commit b042d8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+279
-622
lines changed

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ module.exports = {
33
moduleNameMapper: {
44
'@app-data': '<rootDir>/internal/app-data/index.cjs',
55
'@app-globals': '<rootDir>/internal/app-globals/index.cjs',
6-
'@compiler-deps': '<rootDir>/src/compiler/sys/modules/compiler-deps.ts',
76
'@platform': '<rootDir>/internal/testing/index.js',
87
'@runtime': '<rootDir>/internal/testing/index.js',
98
'@stencil/core/cli': '<rootDir>/cli/index.js',
109
'@stencil/core/compiler': '<rootDir>/compiler/stencil.js',
1110
'@stencil/core/mock-doc': '<rootDir>/mock-doc/index.cjs',
1211
'@stencil/core/testing': '<rootDir>/testing/index.js',
12+
'@sys-api-node': '<rootDir>/sys/node/index.js',
1313
'@utils': '<rootDir>/src/utils',
1414
'^typescript$': '<rootDir>/scripts/build/typescript-modified-for-jest.js',
1515
},

package-lock.json

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"@types/inquirer": "^7.3.1",
7171
"@types/jest": "^27.0.3",
7272
"@types/listr": "^0.14.4",
73+
"@types/mock-fs": "^4.13.1",
7374
"@types/node": "^20.1.1",
7475
"@types/pixelmatch": "^5.2.4",
7576
"@types/pngjs": "^6.0.1",
@@ -106,6 +107,7 @@
106107
"merge-source-map": "^1.1.0",
107108
"mime-db": "^1.46.0",
108109
"minimatch": "5.1.6",
110+
"mock-fs": "^5.2.0",
109111
"node-fetch": "3.3.1",
110112
"open": "^9.0.0",
111113
"open-in-editor": "2.2.0",

scripts/bundles/compiler.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import type { OutputChunk, RollupOptions, RollupWarning, TransformResult } from
88
import sourcemaps from 'rollup-plugin-sourcemaps';
99

1010
import { getBanner } from '../utils/banner';
11+
import { NODE_BUILTINS } from '../utils/constants';
1112
import type { BuildOptions } from '../utils/options';
1213
import { writePkgJson } from '../utils/write-pkg-json';
1314
import { aliasPlugin } from './plugins/alias-plugin';
14-
import { inlinedCompilerDepsPlugin } from './plugins/inlined-compiler-deps-plugin';
1515
import { parse5Plugin } from './plugins/parse5-plugin';
1616
import { replacePlugin } from './plugins/replace-plugin';
1717
import { sizzlePlugin } from './plugins/sizzle-plugin';
18-
import { sysModulesPlugin } from './plugins/sys-modules-plugin';
1918
import { terserPlugin } from './plugins/terser-plugin';
2019
import { typescriptSourcePlugin } from './plugins/typescript-source-plugin';
2120

@@ -67,6 +66,7 @@ export async function compiler(opts: BuildOptions) {
6766
const rollupWatchPath = join(opts.nodeModulesDir, 'rollup', 'dist', 'es', 'shared', 'watch.js');
6867
const compilerBundle: RollupOptions = {
6968
input: join(inputDir, 'index.js'),
69+
external: NODE_BUILTINS,
7070
output: {
7171
format: 'cjs',
7272
file: join(opts.output.compilerDir, compilerFileName),
@@ -154,14 +154,11 @@ export async function compiler(opts: BuildOptions) {
154154
};
155155
},
156156
},
157-
inlinedCompilerDepsPlugin(opts, inputDir),
158157
parse5Plugin(opts),
159158
sizzlePlugin(opts),
160159
aliasPlugin(opts),
161-
sysModulesPlugin(inputDir),
162160
rollupNodeResolve({
163161
mainFields: ['module', 'main'],
164-
preferBuiltins: false,
165162
}),
166163
rollupCommonjs({
167164
transformMixedEsModules: false,

scripts/bundles/plugins/alias-plugin.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export function aliasPlugin(opts: BuildOptions): Plugin {
1515
['@hydrate-factory', '@stencil/core/hydrate-factory'],
1616
['@stencil/core/mock-doc', '@stencil/core/mock-doc'],
1717
['@stencil/core/testing', '@stencil/core/testing'],
18-
['@sys-api-node', './index.js'],
1918
['@dev-server-process', './server-process.js'],
2019
]);
2120

@@ -54,6 +53,9 @@ export function aliasPlugin(opts: BuildOptions): Plugin {
5453
if (id === '@environment') {
5554
return join(opts.buildDir, 'compiler', 'sys', 'environment.js');
5655
}
56+
if (id === '@sys-api-node') {
57+
return join(opts.buildDir, 'sys', 'node', 'index.js');
58+
}
5759
if (helperResolvers.has(id)) {
5860
return join(opts.bundleHelpersDir, `${id}.js`);
5961
}

scripts/bundles/plugins/inlined-compiler-deps-plugin.ts

-77
This file was deleted.

scripts/bundles/plugins/sys-modules-plugin.ts

-31
This file was deleted.

scripts/bundles/sys-node.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { RollupOptions } from 'rollup';
66
import webpack, { Configuration } from 'webpack';
77

88
import { getBanner } from '../utils/banner';
9+
import { NODE_BUILTINS } from '../utils/constants';
910
import type { BuildOptions } from '../utils/options';
1011
import { writePkgJson } from '../utils/write-pkg-json';
1112
import { aliasPlugin } from './plugins/alias-plugin';
@@ -38,7 +39,7 @@ export async function sysNode(opts: BuildOptions) {
3839
preferConst: true,
3940
freeze: false,
4041
},
41-
external: ['child_process', 'crypto', 'events', 'https', 'path', 'readline', 'os', 'util'],
42+
external: NODE_BUILTINS,
4243
plugins: [
4344
relativePathPlugin('glob', './glob.js'),
4445
relativePathPlugin('graceful-fs', './graceful-fs.js'),
@@ -69,7 +70,7 @@ export async function sysNode(opts: BuildOptions) {
6970
preferConst: true,
7071
freeze: false,
7172
},
72-
external: ['child_process', 'crypto', 'events', 'https', 'path', 'readline', 'os', 'util'],
73+
external: NODE_BUILTINS,
7374
plugins: [
7475
{
7576
name: 'sysNodeWorkerAlias',
@@ -82,6 +83,7 @@ export async function sysNode(opts: BuildOptions) {
8283
}
8384
},
8485
},
86+
relativePathPlugin('@sys-api-node', './index.js'),
8587
rollupResolve({
8688
preferBuiltins: true,
8789
}),

scripts/test/validate-build.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { dirname, join, relative } from 'path';
33
import { rollup } from 'rollup';
44
import ts, { ModuleResolutionKind, ScriptTarget } from 'typescript';
55

6+
import { NODE_BUILTINS } from '../utils/constants';
67
import { BuildOptions, getOptions } from '../utils/options';
78
import { PackageData } from '../utils/write-pkg-json';
89

@@ -335,8 +336,11 @@ async function validateModuleTreeshake(opts: BuildOptions, moduleName: string, e
335336
const outputFile = join(opts.scriptsBuildDir, `treeshake_${moduleName}.js`);
336337

337338
const bundle = await rollup({
339+
external: NODE_BUILTINS,
338340
input: virtualInputId,
339-
treeshake: true,
341+
treeshake: {
342+
moduleSideEffects: false,
343+
},
340344
plugins: [
341345
{
342346
name: 'stencilResolver',

src/compiler/sys/modules/module.ts scripts/utils/constants.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Node builtin modules as of v14.5.0
2+
* Node built-ins that we mark as external when building Stencil
33
*/
44
export const NODE_BUILTINS = [
55
'_http_agent',
@@ -58,9 +58,3 @@ export const NODE_BUILTINS = [
5858
'worker_threads',
5959
'zlib',
6060
];
61-
62-
export default class Module {
63-
static get builtinModules() {
64-
return NODE_BUILTINS;
65-
}
66-
}

scripts/utils/test/options.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { path } from '../../../compiler';
1+
import path from 'path';
2+
23
import { BuildOptions, getOptions } from '../options';
34
import * as Vermoji from '../vermoji';
45

src/cli/run.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { hasError, isFunction, shouldIgnoreError } from '@utils';
22

3-
import { createLogger } from '../compiler/sys/logger/console-logger';
43
import type * as d from '../declarations';
54
import { ValidatedConfig } from '../declarations';
65
import { createConfigFlags } from './config-flags';
@@ -114,13 +113,14 @@ export const runTask = async (
114113
coreCompiler: CoreCompiler,
115114
config: d.Config,
116115
task: d.TaskCommand,
117-
sys?: d.CompilerSystem
116+
sys: d.CompilerSystem
118117
): Promise<void> => {
119-
const logger = config.logger ?? createLogger();
120118
const flags = createConfigFlags(config.flags ?? { task });
121-
config.logger = logger;
122119
config.flags = flags;
123-
config.sys = sys ?? config.sys ?? coreCompiler.createSystem({ logger });
120+
121+
if (!config.sys) {
122+
config.sys = sys;
123+
}
124124
const strictConfig: ValidatedConfig = coreCompiler.validateConfig(config, {}).config;
125125

126126
switch (task) {
@@ -134,7 +134,7 @@ export const runTask = async (
134134

135135
case 'generate':
136136
case 'g':
137-
await taskGenerate(coreCompiler, strictConfig);
137+
await taskGenerate(strictConfig);
138138
break;
139139

140140
case 'help':

0 commit comments

Comments
 (0)