Skip to content

Commit 02af1b5

Browse files
authored
Initialize Webpack assets without Amphora. (#195)
* Remove default entrypoints. * Always generate asset manifest. * Use Webpack to initialize the client. * Fix import paths. * Factor async/await into Promises. * Update ESLint to support import functions. * Remove unused glob function, polish config. * Export component mounting function. * Remove entrypoint globs. * Reduce "clay pack" to a single command. * Enable hot module reloading. * Expose getWebpackConfig. * Fix watch params. * Don't build Node modules. * Build assets into "js" path. * Delint. * Remove redundant CLI args. * Enable prod build based on environment variable. * Fix docs.
1 parent 0cf1969 commit 02af1b5

14 files changed

+263
-419
lines changed

.eslintrc

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
{
2+
"parser": "@babel/eslint-parser",
23
"parserOptions": {
3-
"ecmaVersion": 2019
4+
"ecmaVersion": 2015,
5+
"requireConfigFile": false,
6+
"sourceType": "script",
7+
"babelOptions": {
8+
"plugins": [
9+
"@babel/plugin-syntax-dynamic-import"
10+
]
11+
}
412
},
513
"env": {
614
"browser": false,

cli/pack.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'use strict';
2+
3+
const { getWebpackConfig } = require('../lib/cmd/pack');
4+
const log = require('./log').setup({ file: __filename });
5+
const webpack = require('webpack');
6+
7+
function builder(yargs) {
8+
return yargs
9+
.usage('Usage: $0')
10+
.example('$0', 'Compile the entrypoints configured in Webpack.');
11+
}
12+
13+
/**
14+
* Run a one-off Webpack build.
15+
*
16+
* @param {webpack.Compiler} webpackCompiler - A configured Webpack compiler
17+
* instance.
18+
* @returns {Promise} - A Promise that resolves when the compilation is
19+
* complete.
20+
*/
21+
function handleAssetBuild(webpackCompiler) {
22+
return new Promise((resolve, reject) => {
23+
webpackCompiler.run((err, stats) => {
24+
if (err) {
25+
return reject(err);
26+
}
27+
28+
if (stats.hasErrors()) {
29+
const msg = stats.toJson('errors-only');
30+
31+
return reject(msg);
32+
}
33+
34+
resolve(webpackCompiler);
35+
});
36+
}).then(compiler => {
37+
compiler.close(error => {
38+
if (error) {
39+
throw error;
40+
}
41+
});
42+
}).catch(error => {
43+
log('error', 'Webpack compilation failed', {
44+
error
45+
});
46+
});
47+
}
48+
49+
/**
50+
* Run a Webpack build.
51+
*
52+
* @param {webpack.Compiler} webpackCompiler - A configured Webpack compiler
53+
* instance.
54+
* @returns {Promise} - A Promise that resolves when the live compilation is
55+
* terminated.
56+
*/
57+
function handleAssetWatch(webpackCompiler) {
58+
return new Promise((resolve, reject) => {
59+
const watchingInstance = webpackCompiler.watch(
60+
{
61+
ignored: /node_modules/
62+
},
63+
(err, stats) => {
64+
if (err) {
65+
return reject(err);
66+
}
67+
68+
if (stats.hasErrors()) {
69+
const msg = stats.toJson('errors-only');
70+
71+
return reject(msg);
72+
}
73+
}
74+
);
75+
76+
resolve(watchingInstance);
77+
}).then(watching => {
78+
process.on('exit', () => {
79+
watching.close(error => {
80+
if (error) {
81+
throw error;
82+
}
83+
});
84+
});
85+
}).catch(error => {
86+
log('error', 'Webpack compilation failed', {
87+
message: error.message,
88+
stack: error.stack
89+
});
90+
});
91+
}
92+
93+
function handler(argv) {
94+
const config = getWebpackConfig(argv).toConfig();
95+
const compiler = webpack(config);
96+
const builder = argv.watch ? handleAssetWatch.bind(null, compiler) : handleAssetBuild.bind(null, compiler);
97+
98+
return builder();
99+
}
100+
101+
exports.aliases = ['p'];
102+
exports.builder = builder;
103+
exports.command = 'pack';
104+
exports.describe = 'Compile Webpack assets';
105+
exports.handler = handler;

cli/pack/cli.js

-30
This file was deleted.

cli/pack/index.js

-3
This file was deleted.

cli/pack/options.js

-14
This file was deleted.

cli/pack/scripts.js

-52
This file was deleted.

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ module.exports.import = require('./lib/cmd/import');
88
module.exports.export = require('./lib/cmd/export');
99
module.exports.compile = require('./lib/cmd/compile');
1010
module.exports.gulp = require('gulp'); // A reference to the Gulp instance so that external tasks can reference a common package
11+
module.exports.mountComponentModules = require('./lib/cmd/pack/mount-component-modules');
12+
module.exports.getWebpackConfig = require('./lib/cmd/pack/get-webpack-config');

lib/cmd/compile/scripts.js

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ function getModuleId(file, legacyFiles) {
164164
**/
165165
function idGenerator({ cachedIds, legacyFiles }) {
166166
const generatedIds = _.assign({}, cachedIds);
167+
167168
// set to the highest number in the generateIds, or 1
168169
let i = _.max(_.values(generatedIds).filter(_.isFinite)) + 1 || 1;
169170

lib/cmd/pack/_client-init.js

-57
This file was deleted.

0 commit comments

Comments
 (0)