Skip to content

Commit

Permalink
chore: bundle @eslint/eslintrc
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Oct 7, 2021
1 parent 9e7cee9 commit a53147c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 53 deletions.
3 changes: 2 additions & 1 deletion @recommended/eslint/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ bin
conf
lib
messages
__bundled__
.babelrc
__bundled__
__rc__
15 changes: 4 additions & 11 deletions @recommended/eslint/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -11,7 +11,8 @@
"conf",
"lib",
"messages",
"__bundled__"
"__bundled__",
"__rc__"
],
"license": "MIT",
"engines": {
Expand All @@ -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": {
Expand Down
130 changes: 89 additions & 41 deletions @recommended/eslint/setup.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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
Expand All @@ -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'}
Expand All @@ -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'),
Expand All @@ -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)
})

0 comments on commit a53147c

Please sign in to comment.