-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathindex.ts
More file actions
168 lines (141 loc) · 3.87 KB
/
index.ts
File metadata and controls
168 lines (141 loc) · 3.87 KB
1
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env node
import { redBright, green, greenBright, yellow } from 'ansi-colors';
import { program } from 'commander';
import dedent from 'ts-dedent';
import webpack from 'webpack';
import path from 'path';
import fs from 'fs';
import { parseEnvFlags } from '../cli/parseEnvFlags';
const defaultConfig = path.resolve(
__dirname,
'../stubs/default.config.stub.js',
);
const tag = `[${green('@nativescript/webpack')}]`;
function error(message: string) {
console.error(`${tag} ${redBright(dedent(message))}`);
}
function info(message: string) {
console.info(`${tag} ${greenBright(dedent(message))}`);
}
program.enablePositionalOptions();
program
.command('init')
.description('Initialize a new webpack.config.js in the current directory.')
.action(() => {
const targetPath = path.resolve(process.cwd(), 'webpack.config.js');
if (fs.existsSync(targetPath)) {
return error(`File Already Exists: ${targetPath}`);
}
fs.copyFileSync(defaultConfig, targetPath);
info('Initialized config.');
});
program
.command('build')
.description('Build...')
.option('--env [name]', 'environment name')
.option('--config [path]', 'config path')
.option('--watch', 'watch for changes')
.allowUnknownOption()
.allowExcessArguments()
.action((options, command) => {
const env = parseEnvFlags(command.args);
// add --env <val> into the env object
// for example if we use --env prod
// we'd have env.env = 'prod'
if (options.env) {
env['env'] = options.env;
}
env['stats'] ??= true;
env['watch'] ??= options.watch;
// if --env.config is passed, we'll set an environment
// variable to it's value so that the config Util
// reads from the correct config file.
process.env.NATIVESCRIPT_CONFIG_NAME ??= env['config'];
const configPath = (() => {
if (options.config) {
return path.resolve(options.config);
}
return path.resolve(process.cwd(), 'webpack.config.js');
})();
// todo: validate config exists
// todo: guard against invalid config
let configuration: webpack.Configuration;
try {
configuration = require(configPath)(env);
} catch (err) {
console.log(err);
}
if (!configuration) {
console.log('No configuration!');
process.exitCode = 1;
return;
}
const compiler = webpack(configuration);
const webpackCompilationCallback = (
err: webpack.WebpackError,
stats: webpack.Stats,
) => {
if (err) {
// Do not keep cache anymore
compiler.purgeInputFileSystem();
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
process.exitCode = 1;
return;
}
if (stats) {
// Set the process exit code depending on errors
process.exitCode = stats.hasErrors() ? 1 : 0;
if (env.stats) {
console.log(
stats.toString({
chunks: false,
colors: true,
errorDetails: env.verbose,
}),
);
}
// if webpack profile is enabled we write the stats to a JSON file
if (configuration.profile || env.profile) {
console.log(
[
'',
'|',
`| The build profile has been written to ${yellow(
'webpack.stats.json',
)}`,
`| You can analyse the stats at ${green(
'https://webpack.github.io/analyse/',
)}`,
'|',
'',
].join('\n'),
);
fs.writeFileSync(
path.join(process.cwd(), 'webpack.stats.json'),
JSON.stringify(stats.toJson()),
);
}
}
};
if (options.watch) {
env.stats && console.log('webpack is watching the files...');
compiler.watch(
configuration.watchOptions ?? {},
webpackCompilationCallback,
);
} else {
compiler.run((err, status) => {
compiler.close((err2) =>
webpackCompilationCallback(
(err || err2) as webpack.WebpackError,
status,
),
);
});
}
});
program.version(require('../../package.json').version, '-v, --version');
program.parse(process.argv);