UNPKG

5.03 kBPlain TextView Raw
1#!/usr/bin/env node
2/* -----------------------------------------------------------------------------
3| Copyright (c) Jupyter Development Team.
4| Distributed under the terms of the Modified BSD License.
5|----------------------------------------------------------------------------*/
6
7// Build an extension
8
9// Inputs:
10// Path to extension (required)
11// Dev vs prod (dev is default)
12// Output path (defaults to <extension>/build)
13
14// Outputs
15// Webpack build assets
16
17///////////////////////////////////////////////////////
18// Portions of the below code handling watch mode and displaying output were
19// adapted from the https://github.com/webpack/webpack-cli project, which has
20// an MIT license (https://github.com/webpack/webpack-cli/blob/4dc6dfbf29da16e61745770f7b48638963fb05c5/LICENSE):
21//
22// Copyright JS Foundation and other contributors
23//
24// Permission is hereby granted, free of charge, to any person obtaining
25// a copy of this software and associated documentation files (the
26// 'Software'), to deal in the Software without restriction, including
27// without limitation the rights to use, copy, modify, merge, publish,
28// distribute, sublicense, and/or sell copies of the Software, and to
29// permit persons to whom the Software is furnished to do so, subject to
30// the following conditions:
31//
32// The above copyright notice and this permission notice shall be
33// included in all copies or substantial portions of the Software.
34//
35// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
36// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
38// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
39// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
40// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
41// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42///////////////////////////////////////////////////////
43
44import * as path from 'path';
45import { program as commander } from 'commander';
46import webpack from 'webpack';
47import generateConfig from './extensionConfig';
48import { stdout as colors } from 'supports-color';
49
50commander
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 // Do not keep cache anymore
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
143commander.parse(process.argv);
144
145// If no arguments supplied
146if (!process.argv.slice(2).length) {
147 commander.outputHelp();
148 process.exit(1);
149}