From a53147ced89afefbdbe0623c39174544616688d6 Mon Sep 17 00:00:00 2001 From: ambar Date: Thu, 7 Oct 2021 09:43:56 +0800 Subject: [PATCH] chore: bundle @eslint/eslintrc --- @recommended/eslint/.gitignore | 3 +- @recommended/eslint/package.json | 15 +--- @recommended/eslint/setup.js | 130 +++++++++++++++++++++---------- 3 files changed, 95 insertions(+), 53 deletions(-) diff --git a/@recommended/eslint/.gitignore b/@recommended/eslint/.gitignore index e2fb8f5..da3c529 100644 --- a/@recommended/eslint/.gitignore +++ b/@recommended/eslint/.gitignore @@ -3,5 +3,6 @@ bin conf lib messages -__bundled__ .babelrc +__bundled__ +__rc__ diff --git a/@recommended/eslint/package.json b/@recommended/eslint/package.json index bfe54bb..7aec3f4 100644 --- a/@recommended/eslint/package.json +++ b/@recommended/eslint/package.json @@ -1,6 +1,6 @@ { "name": "@recommended/eslint", - "version": "7.32.1-0", + "version": "7.32.1-1", "main": "./lib/api.js", "bin": { "eslint": "./bin/eslint.js" @@ -11,7 +11,8 @@ "conf", "lib", "messages", - "__bundled__" + "__bundled__", + "__rc__" ], "license": "MIT", "engines": { @@ -23,18 +24,10 @@ }, "dependencies": { "doctrine": "^3.0.0", - "@eslint/eslintrc": "^0.4.3", "ajv": "^6.10.0", - "debug": "^4.0.1", - "espree": "^7.3.1", - "globals": "^13.6.0", - "ignore": "^4.0.6", - "import-fresh": "^3.0.0", - "js-yaml": "^3.13.1", - "minimatch": "^3.0.4", - "strip-json-comments": "^3.1.0", "chalk": "^4.0.0", "semver": "^7.2.1", + "debug": "^4.0.1", "enquirer": "^2.3.5" }, "devDependencies": { diff --git a/@recommended/eslint/setup.js b/@recommended/eslint/setup.js index 554189c..9c81476 100644 --- a/@recommended/eslint/setup.js +++ b/@recommended/eslint/setup.js @@ -1,5 +1,6 @@ /* eslint-disable no-console */ const fsp = require('fs').promises +const assert = require('assert') const path = require('path') const globby = require('globby') const semver = require('semver') @@ -12,44 +13,75 @@ if (dep.name === pkg.name) { throw new Error('Do not link to the bundled package') } -const externals = [ - ...new Set([ - // shared by plugins - 'doctrine', - // not using entry point - '@eslint/eslintrc', - // shared by eslint & @eslint/eslintrc, ajv, espree, globals, js-yaml, minimatch etc. - ...Object.keys(rcPkg.dependencies).filter((x) => - Object.keys(dep.dependencies).includes(x) - ), - // lightweight, or zero dependency - 'chalk', - 'semver', - 'debug', - 'enquirer', - ]), -] +const externals = new Set([ + // shared by plugins + 'doctrine', + // not using entry point + 'ajv', + // lightweight, or zero dependency + 'chalk', + 'semver', + 'debug', + 'enquirer', +]) console.info('externals:', externals) const bundleDir = '__bundled__' +const rcDir = '__rc__' const babelrc = '.babelrc' const files = dep.files.filter((x) => x !== 'README.md') -const gitignore = files.concat(bundleDir, babelrc) -const eslintDir = path.dirname(require.resolve('eslint/package.json')) +const gitignore = files.concat(babelrc, bundleDir, rcDir) + +const copyPkg = async (name, {dir, files}) => { + const target = `${name}/package.json` + const fromPkg = require(target) + const from = path.dirname(require.resolve(target)) + await fsp.mkdir(dir, {recursive: true}) + + // Copy files + files = files || fromPkg.files + await Promise.all( + files.map(async (x) => { + const dest = path.resolve(__dirname, dir, x) + await fsp.rmdir(dest, {recursive: true, force: true}) + }) + ) + const filesToCopy = await globby(files, {cwd: from}) + console.info('Coping %s files from %s %s', filesToCopy.length, name, files) + await Promise.all( + filesToCopy.map(async (x) => { + const dest = path.resolve(__dirname, dir, x) + await fsp.mkdir(path.dirname(dest), {recursive: true}) + await fsp.copyFile(path.resolve(from, x), dest) + }) + ) + + const pkgFile = path.resolve(dir, 'package.json') + if (!(await fsp.stat(pkgFile).catch(() => null))) { + await fsp.writeFile( + pkgFile, + JSON.stringify({ + private: true, + main: fromPkg.main, + bin: fromPkg.bin, + }) + ) + } +} const main = async () => { // Setup package.json Object.assign(pkg, { bin: dep.bin, main: dep.main, - files: files.concat(bundleDir), + files: files.concat(bundleDir, rcDir), license: dep.license, engines: dep.engines, // reset dependencies: {}, }) // NOTE: increase this number before publish - const ver = 1 + const ver = 2 pkg.version = [...Array(ver).keys()].reduce( (acc) => semver.inc(acc, 'prerelease'), dep.version @@ -65,33 +97,23 @@ const main = async () => { }) // Copy files - await Promise.all( - files.map(async (x) => { - await fsp.rmdir(x, {recursive: true, force: true}) - }) - ) - const filesToCopy = await globby(files, {cwd: eslintDir}) - console.info('Coping %s files from %s', filesToCopy.length, files) - await Promise.all( - filesToCopy.map(async (x) => { - const dest = path.resolve(__dirname, x) - await fsp.mkdir(path.dirname(dest), {recursive: true}) - await fsp.copyFile(path.resolve(eslintDir, x), dest) - }) - ) + await copyPkg('eslint', {dir: '.', files}) // Bundle dependencies const aliasConfig = {} await fsp.rmdir(bundleDir, {recursive: true, force: true}) await fsp.mkdir(bundleDir) + const itsDeps = Object.keys(dep.dependencies) + const rcDeps = Object.keys(rcPkg.dependencies) + const theExternals = ['@eslint/eslintrc', ...externals] await Promise.all( - Object.keys(dep.dependencies).map(async (name) => { - if (!externals.includes(name)) { + [...new Set(itsDeps.concat(rcDeps))].map(async (name) => { + if (!theExternals.includes(name)) { const outfile = path.format({dir: bundleDir, name, ext: '.js'}) aliasConfig[name] = `./${outfile}` console.info('bundle:', name) await command( - `esbuild --platform=node --bundle --outfile=${outfile} ${name} ${externals + `esbuild --platform=node --bundle --outfile=${outfile} ${name} ${theExternals .map((x) => `--external:${x}`) .join(' ')}`, {stdio: 'inherit'} @@ -100,6 +122,12 @@ const main = async () => { }) ) + // Setup `@eslint/eslintrc` + await fsp.rmdir(rcDir, {recursive: true, force: true}) + await fsp.mkdir(rcDir) + await copyPkg('@eslint/eslintrc', {dir: rcDir}) + aliasConfig['@eslint/eslintrc'] = `./${rcDir}` + const babelConfig = {plugins: [['module-resolver', {alias: aliasConfig}]]} await Promise.all([ fsp.writeFile(babelrc, JSON.stringify(babelConfig, null, ' ') + '\n'), @@ -108,15 +136,35 @@ const main = async () => { ]) // Transform alias, overwrite existing files + const pathToAlias = [ + ...files, + // Transform rc + ...rcPkg.files.map((x) => path.join(rcDir, x)), + ] await Promise.all( - files.map(async (x) => { + pathToAlias.map(async (x) => { const stat = await fsp.stat(x) if (stat.isDirectory()) { console.info('transform:', x) - await command(`babel --out-dir ${x} ${x}`, {stdio: 'inherit'}) + await command(`babel --config-file ./.babelrc --out-dir ${x} ${x}`, { + stdio: 'inherit', + }) } }) ) + + await command(`node ${pkg.bin.eslint} -v`, {stdio: 'inherit'}) + assert.ok('doctrine' in pkg.dependencies) + assert.ok(!('@eslint/eslintrc' in pkg.dependencies)) + assert.ok(String(await fsp.readFile(pkg.bin.eslint)).includes(bundleDir)) + assert.ok( + String( + await fsp.readFile(path.resolve(rcDir, 'conf/environments.js')) + ).includes(bundleDir) + ) } -main() +main().catch((e) => { + console.error(e) + process.exit(1) +})