Skip to content

Commit

Permalink
Rename tools/webpack-make.js to ./build.js
Browse files Browse the repository at this point in the history
This is our primary and only interaction point with building bundles, so it
should be in the top-level directory. Also, we are going to switch to esbuild
soon, so naming it "webpack" will be confusing. Move it to ./build.js like
cockpit-podman already did.

Keep tools/webpack-{make.js,watch} as a shell shim for a little while, which
points to the new tool and the documentation.
  • Loading branch information
martinpitt authored and allisonkarlitskaya committed Mar 21, 2023
1 parent 54d54f3 commit 4b97d35
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/webpack-jumpstart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
git checkout --detach base
git merge --no-edit head
git rev-parse HEAD^{tree} > tree
NODE_ENV=production tools/webpack-make.js
NODE_ENV=production ./build.js
tar cf webpack-jumpstart.tar dist package-lock.json tree")"
docker container start --attach "${id}" >&2
Expand Down
16 changes: 7 additions & 9 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ 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
anything in [pkg/systemd](./pkg/systemd/), run:
recommended and fastest way is to do that is using the "watch" mode (`-w` or
`--watch`) 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
./build.js -w systemd

See [pkg/](./pkg/) for a list of all pages.

If you work on a change that affects multiple pages (such as a file in
pkg/lib/), you can also build all pages:

tools/webpack-watch
./build.js -w

Note that this enables eslint and stylelint by default -- if you want to
disable them, run it with `-e`/`--no-eslint` and/or `-s`/`--no-stylelint`.
Expand All @@ -89,10 +89,8 @@ option for copying the built page into the given SSH target's
SSH `c` alias as described in [test/README.md](./test/README.md), you can use
one of these commands:

tools/webpack-make.js -r c kdump
tools/webpack-make.js -r c
tools/webpack-watch -r c kdump
tools/webpack-watch -r c
./build.js -w -r c kdump
./build.js -w -r c

To make Cockpit use system packages again, instead of your checkout directory,
remove the symlink with the following command and log back into Cockpit:
Expand Down
89 changes: 89 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env node

import child_process from 'child_process';
import path from 'path';
import process from 'process';

// ensure node_modules is present and up to date
child_process.spawnSync('tools/node-modules', ['make_package_lock_json'], { stdio: 'inherit' });

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" });
parser.add_argument('-e', '--no-eslint', { action: 'store_true', help: "Disable eslint linting" });
parser.add_argument('-s', '--no-stylelint', { action: 'store_true', help: "Disable stylelint linting" });
parser.add_argument('onlydir', { nargs: '?', help: "The pkg/<DIRECTORY> to build (eg. base1, shell, ...)", metavar: "DIRECTORY" });
const args = parser.parse_args();

if (args.no_eslint) {
process.env.ESLINT = "0";
} else if (args.watch) {
process.env.ESLINT = "1";
}

if (args.no_stylelint) {
process.env.STYLELINT = "0";
} else if (args.watch) {
process.env.STYLELINT = "1";
}

if (args.onlydir?.includes('/')) {
parser.error("Directory must not contain '/'");
}

if (args.onlydir)
process.env.ONLYDIR = args.onlydir;

const cwd = process.cwd();
const config_path = path.resolve(cwd, args.config);

function process_result(err, stats) {
// process.stdout.write(stats.toString({colors: true}) + "\n");

if (err) {
console.log(JSON.stringify(err));
process.exit(1);
}

if (args.watch) {
const info = stats.toJson();
const time = new Date().toTimeString().split(' ')[0];
process.stdout.write(`${time} Build succeeded, took ${info.time / 1000}s\n`);
}

// Failure exit code when compilation fails
if (stats.hasErrors() || stats.hasWarnings())
console.log(stats.toString("normal"));

if (stats.hasErrors()) {
if (!args.watch)
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();
2 changes: 1 addition & 1 deletion pkg/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ V_BUNDLE_0 = @echo " BUNDLE dist";
# but this is just a representative for all of dist/*
$(DIST_STAMP): $(srcdir)/package-lock.json $(PKG_INPUTS)
@rm -f $(DIST_STAMP)
$(V_BUNDLE) cd $(srcdir) && NODE_ENV='$(NODE_ENV)' tools/termschutz tools/webpack-make.js
$(V_BUNDLE) cd $(srcdir) && NODE_ENV='$(NODE_ENV)' tools/termschutz ./build.js

EXTRA_DIST += webpack.config.js package.json package-lock.json

Expand Down
95 changes: 3 additions & 92 deletions tools/webpack-make.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,3 @@
#!/usr/bin/env node

import child_process from 'child_process';
import path from 'path';
import process from 'process';

// 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 = (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 });
parser.add_argument('-e', '--no-eslint', { action: 'store_true', help: "Disable eslint linting" });
parser.add_argument('-s', '--no-stylelint', { action: 'store_true', help: "Disable stylelint linting" });
parser.add_argument('onlydir', { nargs: '?', help: "The pkg/<DIRECTORY> to build (eg. base1, shell, ...)", metavar: "DIRECTORY" });
const args = parser.parse_args();

if (args.no_eslint) {
process.env.ESLINT = "0";
} else if (args.watch) {
process.env.ESLINT = "1";
}

if (args.no_stylelint) {
process.env.STYLELINT = "0";
} else if (args.watch) {
process.env.STYLELINT = "1";
}

if (args.onlydir?.includes('/')) {
parser.error("Directory must not contain '/'");
}

if (args.onlydir)
process.env.ONLYDIR = args.onlydir;

const cwd = process.cwd();
const config_path = path.resolve(cwd, args.config);

function process_result(err, stats) {
// process.stdout.write(stats.toString({colors: true}) + "\n");

if (err) {
console.log(JSON.stringify(err));
process.exit(1);
}

if (args.watch) {
const info = stats.toJson();
const time = new Date().toTimeString().split(' ')[0];
process.stdout.write(`${time} Build succeeded, took ${info.time / 1000}s\n`);
}

// Failure exit code when compilation fails
if (stats.hasErrors() || stats.hasWarnings())
console.log(stats.toString("normal"));

if (stats.hasErrors()) {
if (!args.watch)
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();
#!/bin/sh
echo "This got renamed to ./build.js. Please see HACKING.md"
exit 1

0 comments on commit 4b97d35

Please sign in to comment.