1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | import * as path from 'path';
|
45 | import { program as commander } from 'commander';
|
46 | import webpack from 'webpack';
|
47 | import generateConfig from './extensionConfig';
|
48 | import { stdout as colors } from 'supports-color';
|
49 |
|
50 | commander
|
51 | .description('Build an extension')
|
52 | .option('--development', 'build in development mode (implies --source-map)')
|
53 | .option('--source-map', 'generate source maps')
|
54 | .requiredOption('--core-path <path>', 'the core package directory')
|
55 | .option(
|
56 | '--static-url <url>',
|
57 | 'url for build assets, if hosted outside the built extension'
|
58 | )
|
59 | .option('--watch')
|
60 | .action(async (options, command) => {
|
61 | const mode = options.development ? 'development' : 'production';
|
62 | const corePath = path.resolve(options.corePath || process.cwd());
|
63 | const packagePath = path.resolve(command.args[0]);
|
64 | const devtool = options.sourceMap ? 'source-map' : undefined;
|
65 |
|
66 | const config = generateConfig({
|
67 | packagePath,
|
68 | mode,
|
69 | corePath,
|
70 | staticUrl: options.staticUrl,
|
71 | devtool,
|
72 | watchMode: options.watch
|
73 | });
|
74 | const compiler = webpack(config);
|
75 |
|
76 | let lastHash: string | null = null;
|
77 |
|
78 | function compilerCallback(err: any, stats: any) {
|
79 | if (!options.watch || err) {
|
80 |
|
81 | compiler.purgeInputFileSystem();
|
82 | }
|
83 |
|
84 | if (err) {
|
85 | console.error(err.stack || err);
|
86 | if (err.details) {
|
87 | console.error(err.details);
|
88 | }
|
89 | throw new Error(err.details);
|
90 | }
|
91 |
|
92 | const info = stats.toJson();
|
93 |
|
94 | if (stats.hasErrors()) {
|
95 | console.error(info.errors);
|
96 | if (!options.watch) {
|
97 | process.exit(2);
|
98 | }
|
99 | }
|
100 |
|
101 | if (stats.hash !== lastHash) {
|
102 | lastHash = stats.hash;
|
103 | const statsString = stats.toString({ colors });
|
104 | const delimiter = '';
|
105 | if (statsString) {
|
106 | process.stdout.write(`${statsString}\n${delimiter}`);
|
107 | }
|
108 | }
|
109 | }
|
110 |
|
111 | if (options.watch) {
|
112 | compiler.hooks.watchRun.tap('WebpackInfo', () => {
|
113 | console.error('\nWatch Compilation starting…\n');
|
114 | });
|
115 | compiler.hooks.done.tap('WebpackInfo', () => {
|
116 | console.error('\nWatch Compilation finished\n');
|
117 | });
|
118 | } else {
|
119 | compiler.hooks.run.tap('WebpackInfo', () => {
|
120 | console.error('\nCompilation starting…\n');
|
121 | });
|
122 | compiler.hooks.done.tap('WebpackInfo', () => {
|
123 | console.error('\nCompilation finished\n');
|
124 | });
|
125 | }
|
126 |
|
127 | if (options.watch) {
|
128 | compiler.watch(config[0].watchOptions || {}, compilerCallback);
|
129 | console.error('\nwebpack is watching the files…\n');
|
130 | } else {
|
131 | compiler.run((err: any, stats: any) => {
|
132 | if (compiler.close) {
|
133 | compiler.close((err2: any) => {
|
134 | compilerCallback(err || err2, stats);
|
135 | });
|
136 | } else {
|
137 | compilerCallback(err, stats);
|
138 | }
|
139 | });
|
140 | }
|
141 | });
|
142 |
|
143 | commander.parse(process.argv);
|
144 |
|
145 |
|
146 | if (!process.argv.slice(2).length) {
|
147 | commander.outputHelp();
|
148 | process.exit(1);
|
149 | }
|