diff --git a/package.json b/package.json index f6138aa3360c..e29a403b3234 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "babel-loader": "^9.0.0", "camelcase": "^6.2.0", "chalk": "^4.0.0", - "chokidar": "^3.3.0", "dedent": "^1.0.0", "eslint": "^8.8.0", "eslint-config-prettier": "^9.0.0", @@ -118,7 +117,7 @@ "typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,create-jest,diff-sequences,expect,expect-utils,jest-circus,jest-cli,jest-config,jest-console,jest-snapshot,jest-util,jest-validate,jest-watcher,jest-worker,pretty-format}/**/__tests__", "verify-old-ts": "node ./scripts/verifyOldTs.mjs", "verify-pnp": "node ./scripts/verifyPnP.mjs", - "watch": "yarn build:js && node ./scripts/watch.mjs", + "watch": "node ./scripts/watch.mjs", "watch:ts": "yarn build:ts --watch" }, "workspaces": [ diff --git a/scripts/build.mjs b/scripts/build.mjs index 2cf4a6aa4f18..d38e8eab2f94 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -10,18 +10,12 @@ import * as path from 'path'; import util from 'util'; import chalk from 'chalk'; import fs from 'graceful-fs'; -import webpack from 'webpack'; -import {ERROR, OK, createWebpackConfigs} from './buildUtils.mjs'; +import {ERROR, OK, createWebpackCompiler} from './buildUtils.mjs'; async function buildNodePackages() { process.stdout.write(chalk.inverse(' Bundling packages \n')); - const webpackConfigs = createWebpackConfigs(); - const compiler = webpack( - webpackConfigs - .map(({webpackConfig}) => webpackConfig) - .filter(config => config != null), - ); + const compiler = createWebpackCompiler(); let stats; try { diff --git a/scripts/buildUtils.mjs b/scripts/buildUtils.mjs index e4cf23490d19..9140a8b1247a 100644 --- a/scripts/buildUtils.mjs +++ b/scripts/buildUtils.mjs @@ -146,7 +146,7 @@ export const copyrightSnippet = ` */ `.trim(); -export function createWebpackConfigs() { +function createWebpackConfigs() { const packages = getPackages(); return packages.map(({packageDir, pkg}) => { @@ -292,6 +292,15 @@ export function createWebpackConfigs() { }); } +export function createWebpackCompiler() { + const webpackConfigs = createWebpackConfigs(); + return webpack( + webpackConfigs + .map(({webpackConfig}) => webpackConfig) + .filter(config => config != null), + ); +} + // inspired by https://framagit.org/Glandos/webpack-ignore-dynamic-require class IgnoreDynamicRequire { constructor(extraEntries) { diff --git a/scripts/watch.mjs b/scripts/watch.mjs index 7d834f0d3621..a3e40bfd4229 100644 --- a/scripts/watch.mjs +++ b/scripts/watch.mjs @@ -9,75 +9,38 @@ * Watch files for changes and rebuild (copy from 'src/' to `build/`) if changed */ -import {execSync} from 'child_process'; -import * as path from 'path'; -import {fileURLToPath} from 'url'; import chalk from 'chalk'; -import chokidar from 'chokidar'; -import fs from 'graceful-fs'; -import {PACKAGES_DIR, getPackages} from './buildUtils.mjs'; +import {createWebpackCompiler} from './buildUtils.mjs'; -const BUILD_CMD = `node ${path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - 'build.mjs', -)}`; +const compiler = createWebpackCompiler(); -let filesToBuild = new Map(); +let hasBuilt = false; -const exists = filename => { - try { - return fs.statSync(filename).isFile(); - } catch {} - return false; -}; -const rebuild = filename => filesToBuild.set(filename, true); +console.log(chalk.cyan('Building packages…')); -chokidar - .watch( - getPackages().map(p => path.resolve(p.packageDir, 'src')), - { - ignoreInitial: true, - ignored: /(^|[/\\])\../, // ignore dotfiles - }, - ) - .on('all', (event, filePath) => { - if ( - (event === 'change' || event === 'rename' || event === 'add') && - exists(filePath) - ) { - console.log( - chalk.green('->'), - `${event}: ${path.relative(PACKAGES_DIR, filePath)}`, - ); - rebuild(filePath); - } else { - filePath.split(path.join(path.sep, 'src', path.sep)); - const buildFile = filePath - .replace( - path.join(path.sep, 'src', path.sep), - path.join(path.sep, 'build', path.sep), - ) - .replace(/\.ts$/, '.js'); - try { - fs.unlinkSync(buildFile); - process.stdout.write( - `${chalk.red(' \u2022 ')}${path.relative( - PACKAGES_DIR, - buildFile, - )} (deleted)\n`, - ); - } catch {} - } - }); +compiler.watch({}, (error, stats) => { + if (!hasBuilt) { + hasBuilt = true; + + console.log(chalk.red('->'), chalk.cyan('Watching for changes…')); + } -setInterval(() => { - const files = Array.from(filesToBuild.keys()); - if (files.length > 0) { - filesToBuild = new Map(); - try { - execSync(`${BUILD_CMD} ${files.join(' ')}`, {stdio: [0, 1, 2]}); - } catch {} + if (error) { + console.error('Got error from watch mode', error); } -}, 100); -console.log(chalk.red('->'), chalk.cyan('Watching for changes...')); + if (stats) { + const info = stats.toJson(); + + if (stats.hasErrors() || stats.hasWarnings()) { + for (const error of info.errors) { + console.error('error', error.message); + } + for (const warning of info.warnings) { + console.warn('warning', warning.message); + } + } else { + console.log(chalk.red('->'), chalk.green('Rebuilt packages')); + } + } +}); diff --git a/yarn.lock b/yarn.lock index f2f0fa3d9aa7..08e473775afb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2948,7 +2948,6 @@ __metadata: babel-loader: ^9.0.0 camelcase: ^6.2.0 chalk: ^4.0.0 - chokidar: ^3.3.0 dedent: ^1.0.0 eslint: ^8.8.0 eslint-config-prettier: ^9.0.0 @@ -7296,7 +7295,7 @@ __metadata: languageName: node linkType: hard -"chokidar@npm:^3.3.0, chokidar@npm:^3.4.2, chokidar@npm:^3.5.3": +"chokidar@npm:^3.4.2, chokidar@npm:^3.5.3": version: 3.5.3 resolution: "chokidar@npm:3.5.3" dependencies: