tools: Teach webpack-make.js to get node_modules by itself

This supports the "only work on web-y bits without autotools" workflow.
It avoids having to run `node-modules checkout` as a user from a clean
tree, and also helps after pulling new commits which change
node_modules.

As `argparse`, `webpack`, and our rsync plugin all live in node_modules,
to convert these to dynamic imports.
This commit is contained in:
Martin Pitt 2023-03-18 10:01:51 +01:00 committed by Allison Karlitskaya
parent 55509cb407
commit 15469bb469
3 changed files with 34 additions and 32 deletions

View file

@ -26,7 +26,6 @@ jobs:
git checkout --detach base
git merge --no-edit head
git rev-parse HEAD^{tree} > tree
tools/node-modules make_package_lock_json
NODE_ENV=production tools/webpack-make.js
tar cf webpack-jumpstart.tar dist package-lock.json tree")"

View file

@ -47,10 +47,6 @@ First, install Cockpit on your local machine as described in:
<https://cockpit-project.org/running.html>
Cockpit uses a lot of build and runtime dependencies from [npm](https://www.npmjs.com). Get them with
tools/node-modules checkout
Next, run this command from your top level Cockpit checkout directory, and make
sure to run it as the same user that you'll use to log into Cockpit below.
@ -68,8 +64,8 @@ desktop.
<http://localhost:9090>
After every change to the source files, bundles need to be rebuilt. The
recommended and fastest way is to do that is using the "watch" mode on
the page that you are working on. For example, if you want to work on
recommended and fastest way is to do that is using the "watch" mode on
the page that you are working on. For example, if you want to work on
anything in [pkg/systemd](./pkg/systemd/), run:
tools/webpack-watch systemd
@ -260,7 +256,7 @@ units, cockpit-tls, etc.) use this:
On Debian-based OSes (including Ubuntu), the path will be
`/usr/lib/cockpit/cockpit-ws` instead.
On Fedora, CentOS, Red Hat Enterprise Linux, and related distributions, you also
On Fedora, CentOS, Red Hat Enterprise Linux, and related distributions, you also
need to disable SELinux with:
sudo setenforce 0

View file

@ -1,15 +1,16 @@
#!/usr/bin/env node
import webpack from 'webpack';
import child_process from 'child_process';
import path from 'path';
import argparse from 'argparse';
import process from 'process';
import { CockpitRsyncWebpackPlugin } from '../pkg/lib/cockpit-rsync-plugin.js';
// ensure node_modules is present and up to date
child_process.spawnSync('tools/node-modules', ['make_package_lock_json'], { stdio: 'inherit' });
// argv0 is node
const webpack_watch = process.argv[1].includes('webpack-watch');
const parser = argparse.ArgumentParser();
const parser = (await import('argparse')).default.ArgumentParser();
parser.add_argument('-c', '--config', { help: "Path to webpack.config.js", default: "webpack.config.js" });
parser.add_argument('-r', '--rsync', { help: "rsync webpack to ssh target after build", metavar: "HOST" });
parser.add_argument('-w', '--watch', { action: 'store_true', help: "Enable webpack watch mode", default: webpack_watch });
@ -40,26 +41,6 @@ if (args.onlydir)
const cwd = process.cwd();
const config_path = path.resolve(cwd, args.config);
import(config_path).then(module => {
const config = module.default;
if (args.rsync) {
process.env.RSYNC = args.rsync;
config.plugins.push(new CockpitRsyncWebpackPlugin({ source: "dist/" + (args.onlydir || "") }));
}
const compiler = webpack(config);
if (args.watch) {
compiler.hooks.watchRun.tap("WebpackInfo", compilation => {
const time = new Date().toTimeString().split(' ')[0];
process.stdout.write(`${time} Build started\n`);
});
compiler.watch(config.watchOptions, process_result);
} else {
compiler.run(process_result);
}
});
function process_result(err, stats) {
// process.stdout.write(stats.toString({colors: true}) + "\n");
@ -83,3 +64,29 @@ function process_result(err, stats) {
process.exit(1);
}
}
async function build() {
// dynamic imports which need node_modules
const config = (await import(config_path)).default;
const webpack = (await import('webpack')).default;
const cockpit_rsync = (await import('../pkg/lib/cockpit-rsync-plugin.js'));
if (args.rsync) {
process.env.RSYNC = args.rsync;
config.plugins.push(new cockpit_rsync.CockpitRsyncWebpackPlugin({ source: "dist/" + (args.onlydir || "") }));
}
const compiler = webpack(config);
if (args.watch) {
compiler.hooks.watchRun.tap("WebpackInfo", compilation => {
const time = new Date().toTimeString().split(' ')[0];
process.stdout.write(`${time} Build started\n`);
});
compiler.watch(config.watchOptions, process_result);
} else {
compiler.run(process_result);
}
}
build();