Skip to content

chore(website): update build script to use esbuild instead of rollup #6716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions .github/renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@
'@nrwl/jest',
'@nrwl/nx-cloud',
'@nrwl/tao',
// TODO - once we bump pass the major, we can remove these. Currently renovate is creating broken, immortal PRs
'@rollup/plugin-babel',
'@rollup/plugin-commonjs',
'@rollup/plugin-json',
'@rollup/plugin-node-resolve',
'@rollup/plugin-replace',
'@rollup/plugin-terser',
'@rollup/pluginutils',
'rollup-plugin-terser',
'rollup',
],
ignorePaths: [
// integration test package.json's should never be updated as they're purposely fixed tests
Expand Down Expand Up @@ -85,11 +75,6 @@
matchPackagePrefixes: ['@types/jest', 'jest-', '@jest/'],
groupName: 'jest',
},
{
matchPackagePrefixes: ['@rollup', 'rollup-'],
matchPackageNames: ['rollup'],
groupName: 'rollup',
},
],
postUpdateOptions: [
// run yarn dedupe to cleanup the lockfile after updates
Expand Down
6 changes: 1 addition & 5 deletions packages/scope-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json",
"./use-at-your-own-risk/analyze": {
"types": "./dist/analyze.d.ts",
"default": "./dist/analyze.js"
}
"./package.json": "./package.json"
},
"engines": {
"node": "^14.18.0 || ^16.0.0 || >=18.0.0"
Expand Down
14 changes: 3 additions & 11 deletions packages/typescript-estree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@
"default": "./dist/index.js"
},
"./package.json": "./package.json",
"./use-at-your-own-risk/ast-converter": {
"types": "./dist/ast-converter.d.ts",
"default": "./dist/ast-converter.js"
},
"./use-at-your-own-risk/parseSettings": {
"types": "./dist/parseSettings/index.d.ts",
"default": "./dist/parseSettings/index.js"
},
"./use-at-your-own-risk/getScriptKind": {
"types": "./dist/create-program/getScriptKind.d.ts",
"default": "./dist/create-program/getScriptKind.js"
"./use-at-your-own-risk": {
"types": "./dist/use-at-your-own-risk.d.ts",
"default": "./dist/use-at-your-own-risk.js"
}
},
"engines": {
Expand Down
8 changes: 8 additions & 0 deletions packages/typescript-estree/src/use-at-your-own-risk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// required by website
export * from './create-program/getScriptKind';
export * from './ast-converter';
export type { ParseSettings } from './parseSettings';

// required by packages/utils/src/ts-estree.ts
export * from './getModifiers';
export { typescriptVersionIsAtLeast } from './version-check';
6 changes: 1 addition & 5 deletions packages/visitor-keys/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json",
"./use-at-your-own-risk/visitor-keys": {
"types": "./dist/visitor-keys.d.ts",
"default": "./dist/visitor-keys.js"
}
"./package.json": "./package.json"
},
"engines": {
"node": "^14.18.0 || ^16.0.0 || >=18.0.0"
Expand Down
167 changes: 167 additions & 0 deletions packages/website-eslint/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/* eslint-disable no-process-exit, no-console */

import * as fs from 'node:fs/promises';
import { createRequire } from 'node:module';
import * as path from 'node:path';

import * as esbuild from 'esbuild';

function requireResolved(targetPath: string): string {
return createRequire(__filename).resolve(targetPath);
}

function normalizePath(filePath: string): string {
return filePath.replace(/\\/g, '/');
}

function requireMock(targetPath: string): Promise<string> {
return fs.readFile(requireResolved(targetPath), 'utf8');
}

function makeFilter(filePath: string | string[]): { filter: RegExp } {
const paths = Array.isArray(filePath) ? filePath : [filePath];
const norm = paths.map(item =>
normalizePath(item).replace(/\//g, '[\\\\/]').replace(/\./g, '\\.'),
);
return { filter: new RegExp('(' + norm.join('|') + ')$') };
}

function createResolve(
targetPath: string,
join: string,
): esbuild.OnResolveResult {
const resolvedPackage = requireResolved(targetPath + '/package.json');
return {
path: path.join(resolvedPackage, '../src/', join),
};
}

async function buildPackage(name: string, file: string): Promise<void> {
const eslintRoot = requireResolved('eslint/package.json');
const linterPath = path.join(eslintRoot, '../lib/linter/linter.js');
const rulesPath = path.join(eslintRoot, '../lib/rules/index.js');

await esbuild.build({
entryPoints: {
[name]: requireResolved(file),
},
format: 'cjs',
platform: 'browser',
bundle: true,
external: [],
minify: true,
treeShaking: true,
write: true,
target: 'es2020',
sourcemap: 'linked',
outdir: './dist/',
supported: {},
banner: {
// https://github.com/evanw/esbuild/issues/819
js: `define(['exports', 'vs/language/typescript/tsWorker'], function (exports) {`,
},
footer: {
// https://github.com/evanw/esbuild/issues/819
js: `});`,
},
define: {
'process.env.NODE_ENV': '"production"',
'process.env.NODE_DEBUG': 'false',
'process.env.IGNORE_TEST_WIN32': 'true',
'process.env.DEBUG': 'false',
'process.emitWarning': 'console.warn',
'process.platform': '"browser"',
'process.env.TIMING': 'undefined',
'define.amd': 'false',
global: 'window',
},
alias: {
util: requireResolved('./src/mock/util.js'),
assert: requireResolved('./src/mock/assert.js'),
path: requireResolved('./src/mock/path.js'),
typescript: requireResolved('./src/mock/typescript.js'),
'lru-cache': requireResolved('./src/mock/lru-cache.js'),
},
plugins: [
{
name: 'replace-plugin',
setup(build): void {
build.onLoad(
makeFilter([
'/eslint-utils/rule-tester/RuleTester.ts',
'/ts-eslint/ESLint.ts',
'/ts-eslint/RuleTester.ts',
'/ts-eslint/CLIEngine.ts',
]),
async args => {
console.log('onLoad:replace', args.path);
const contents = await requireMock('./src/mock/empty.js');
return { contents, loader: 'js' };
},
);
build.onLoad(
makeFilter('/eslint/lib/unsupported-api.js'),
async args => {
console.log('onLoad:eslint:unsupported-api', args.path);
let contents = await requireMock('./src/mock/eslint-rules.js');
// this is needed to bypass system module resolver
contents = contents.replace(
'vt:eslint/rules',
normalizePath(rulesPath),
);
return { contents, loader: 'js' };
},
);
build.onLoad(makeFilter('/eslint/lib/api.js'), async args => {
console.log('onLoad:eslint', args.path);
let text = await requireMock('./src/mock/eslint.js');
// this is needed to bypass system module resolver
text = text.replace('vt:eslint/linter', normalizePath(linterPath));
return { contents: text, loader: 'js' };
});
build.onResolve(
makeFilter([
'@typescript-eslint/typescript-estree',
'@typescript-eslint/typescript-estree/use-at-your-own-risk',
]),
() =>
createResolve(
'@typescript-eslint/typescript-estree',
'use-at-your-own-risk.ts',
),
);
const anyAlias = /^(@typescript-eslint\/[a-z-]+)\/([a-z-]+)$/;
build.onResolve({ filter: anyAlias }, args => {
const parts = args.path.match(anyAlias);
if (parts) {
return createResolve(parts[1], `${parts[2]}/index.ts`);
}
return null;
});
build.onResolve(makeFilter('@typescript-eslint/[a-z-]+'), args =>
createResolve(args.path, 'index.ts'),
);
build.onEnd(e => {
for (const error of e.errors) {
console.error(error);
}
for (const warning of e.warnings) {
console.warn(warning);
}
});
},
},
],
});
}

console.time('building eslint for web');

buildPackage('index', './src/index.js')
.then(() => {
console.timeEnd('building eslint for web');
})
.catch((e: unknown) => {
console.error(String(e));
process.exit(1);
});
16 changes: 4 additions & 12 deletions packages/website-eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,22 @@
"node": "^14.18.0 || ^16.0.0 || >=18.0.0"
},
"scripts": {
"build": "rollup --config=rollup.config.js",
"build": "yarn tsx ./build.ts",
"format": "prettier --write \"./**/*.{ts,mts,cts,tsx,js,mjs,cjs,jsx,json,md,css}\" --ignore-path ../../.prettierignore",
"lint": "nx lint",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@typescript-eslint/types": "5.56.0",
"@typescript-eslint/utils": "5.56.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^23.0.0",
"@rollup/plugin-json": "^5.0.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@rollup/plugin-terser": "^0.4.0",
"@rollup/pluginutils": "^5.0.0",
"@typescript-eslint/eslint-plugin": "5.56.0",
"@typescript-eslint/parser": "5.56.0",
"@typescript-eslint/scope-manager": "5.56.0",
"@typescript-eslint/typescript-estree": "5.56.0",
"@typescript-eslint/visitor-keys": "5.56.0",
"@typescript-eslint/types": "5.56.0",
"@typescript-eslint/utils": "5.56.0",
"eslint": "*",
"esbuild": "~0.17.12",
"esquery": "*",
"magic-string": "0.25.9",
"rollup": "^2.75.4",
"semver": "^7.3.7"
}
}
96 changes: 0 additions & 96 deletions packages/website-eslint/rollup-plugin/replace.js

This file was deleted.

Loading