forked from steveblue/angular2-rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.dev.js
403 lines (293 loc) · 10.7 KB
/
build.dev.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"use strict";
require('shelljs/global');
const env = 'dev';
const path = require('path');
const fs = require('fs');
const utils = require('./build.utils.js');
const chokidar = require('chokidar');
const sass = require('node-sass');
const postcss = require('./postcss.' + env + '.js');
const spawn = require('child_process').spawn;
/* References to shared tools are found in build.utils.js */
const console = utils.console;
const colors = utils.colors;
const scripts = utils.scripts;
const config = utils.config;
const log = utils.log;
const warn = utils.warn;
const alert = utils.alert;
const clean = utils.clean;
let allowPostCSS = false;
let canWatch = false;
let isCompiling = false;
let hasInit = false;
let styleFiles = [];
let hasCompletedFirstStylePass = false;
let postcssConfig = ' -u';
let canServe = false;
let isVerbose = false;
let startElectron = false;
/* Test for arguments the ngr cli spits out */
process.argv.forEach((arg) => {
if (arg.includes('watch')) {
canWatch = arg.split('=')[1].trim() === 'true' ? true : false;
}
if (arg.includes('serve')) {
canServe = arg.split('=')[1].trim() === 'true' ? true : false;
}
if (arg.includes('verbose')) {
isVerbose = arg.split('=')[1].trim() === 'true' ? true : false;
}
if (arg.includes('electron')) {
startElectron = arg.split('=')[1].trim() === 'true' ? true : false;
}
if (arg.includes('postcss')) {
allowPostCSS = arg.split('=')[1].trim() === 'true' ? true : false;
}
});
if (!config.style || !config.style.sass || !config.style.sass.dev) {
config.style = {
sass: {
dev: {
includePaths: ['src/style/'],
outputStyle: 'expanded',
sourceComments: true
}
}
}
}
/*
Copy Tasks
- public: Copies the contents of the src/public folder
- file: Copies a file to /build
- lib: Copies files and folders from /node_modules to /build/lib
*/
const copy = {
public: (filePath) => {
cp('-R', path.normalize(config.src + '/public/') + '.', path.normalize(path.join(config.build, '/')));
exec(path.join(config.cliRoot, path.normalize('node_modules/.bin/htmlprocessor')) +
' ' + path.normalize(path.join(config.build, '/') + 'index.html') +
' -o ' + path.normalize(path.join(config.build, '/') + 'index.html') +
' -e ' + env, { silent: true }, function (code, output, error) {
alert('htmlprocessor', 'formatted index.html');
});
if (isVerbose) log(filePath || path.join(config.src, 'public/'), 'copied to', path.join(config.build, '/'));
if (config && config.clean) {
clean.paths(config);
}
},
file: (filePath, dest) => {
cp('-R', filePath, dest || path.join(config.build, '/'));
if (isVerbose) log(filePath, 'copied to', dest || path.join(config.build, '/'));
},
lib: () => {
for (var i = 0; i < config.dep.lib.length; i++) {
if (config.dep.lib[i].split('/').pop().split('.').length > 1) { // file
let filePath = path.join(config.dep.dist, config.dep.lib[i]);
if (!fs.existsSync(path.normalize(filePath.substring(0, filePath.replace(/\\/g, "/").lastIndexOf('/'))))) {
mkdir('-p', path.normalize(filePath.substring(0, filePath.replace(/\\/g, "/").lastIndexOf('/'))));
} // catch folders
cp('-R', path.join(path.normalize(config.dep.src), path.normalize(config.dep.lib[i])), path.join(path.normalize(config.dep.dist), path.normalize(config.dep.lib[i])));
} else { // folder
cp('-R', path.join(path.normalize(config.dep.src), path.normalize(config.dep.lib[i])), path.join(path.normalize(config.dep.dist), path.normalize(config.dep.lib[i])));
}
if (isVerbose) log(config.dep.lib[i], 'copied to', path.join(path.normalize(config.dep.dist), path.normalize(config.dep.lib[i])));
if (i === config.dep.lib.length - 1) {
alert(config.dep.src.replace('./', ''), 'copied to', config.dep.dist.replace('./', ''));
}
}
}
};
/*
Compile Tasks
- clean: Removes source code comments
- ts: Compiles AOT for development using ngc
*/
const compile = {
clean: (filePath) => {
const outFile = filePath ? filePath : path.join(config.projectRoot, config.build, 'bundle.js');
fs.readFile(outFile, 'utf8', function (err, contents) {
if (!err) {
contents = contents.replace(utils.multilineComment, '');
contents = contents.replace(utils.singleLineComment, '');
fs.writeFile(outFile, contents, function (err) {
if (!err) {
// log('Cleaned up', 'comments', 'from', outFile);
} else {
warn(err);
}
});
} else {
warn(err);
}
});
},
main: () => {
const outFile = path.join(config.projectRoot, config.build, 'main.ts');
fs.readFile(path.join(config.projectRoot, 'main.prod.js'), 'utf8', function (err, contents) {
if (!err) {
contents = contents.replace("./ngfactory/src/app/app.module.ngfactory", "src/app/app.module.ngfactory");
contents = contents.replace('import { enableProdMode } from "@angular/core";', '');
contents = contents.replace("enableProdMode();", "");
fs.writeFile(outFile, contents, function (err) {
if (!err) {
let transpile = exec(path.join(config.projectRoot, 'node_modules/.bin/tsc') +
' ' + outFile + ' --target es5 --module commonjs' +
' --emitDecoratorMetadata true --experimentalDecorators true' +
' --noImplicitAny false --sourceMap true --moduleResolution node' +
' --typeRoots node --lib dom,es2017', { silent: true },
function (code, output, error) {
if (isVerbose) alert('typescript compiled', outFile);
});
} else {
warn(err);
}
});
} else {
warn(err);
}
});
},
src: () => {
isCompiling = true;
let readyMessage = function(isAOTWatch) {
if (!isAOTWatch) {
alert(colors.green('Build is ready'));
}
if (canServe === true) {
alert(colors.green('Ready to serve'));
utils.serve(canWatch);
} else if (startElectron === true) {
alert(colors.green('Ready to serve'));
utils.electron(canWatch);
}
}
let clean = exec(scripts['clean:ngfactory'], function (code, output, error) {
if (canWatch === true) {
utils.alert('@angular/compiler started');
readyMessage(true);
spawn(path.normalize(config.projectRoot + '/node_modules/.bin/ngc') + ' -p ' +
path.normalize('./tsconfig.dev.json') +
' --watch', { shell: true, stdio: 'inherit' });
} else {
utils.alert('@angular/compiler started');
exec(path.normalize(config.projectRoot + '/node_modules/.bin/ngc') +
' -p ' + path.normalize('./tsconfig.dev.json'), { shell: true, stdio: 'inherit' }, function(){
utils.alert('@angular/compiler compiled');
readyMessage();
});
}
hasInit = true;
});
}
}
/*
Style Tasks
- file: Styles a single file.
- If the file is in the /src/styles folder it will compile /src/styles/style.scss
- If the file is elsewhere, like part of a Component, it will compile into the
appropriate folder in the /src directory, then ngc will run and compile for AOT
- src: Compiles the global styles
SASS render method is called and fs writes the files to appropriate folder
PostCSS processes the file in place, using the --replace argument
*/
let style = utils.style;
/*
Init Tasks
A sequence of commands needed to clean and start the prod build
*/
let init = () => {
const initProcesses = () => {
copy.lib();
copy.public();
allowPostCSS ? alert('libsass and postcss', 'started') : alert('libsass', 'started');
style.src({
sassConfig: config.style.sass.dev,
env: env,
allowPostCSS: allowPostCSS,
src: config.src,
dist: config.build,
styleSrcOnInit: false,
sourceMap: true,
isVerbose: isVerbose
},
function (filePath) {
if (utils.style.files.indexOf(filePath) === utils.style.files.length - 1 && hasCompletedFirstStylePass === false) {
allowPostCSS ? alert('libsass and postcss', 'compiled') : alert('libsass', 'compiled');
hasCompletedFirstStylePass = true;
setTimeout(compile.src, 1000);
}
},
function (filePath, outFile, err) {
if (utils.style.files.indexOf(filePath) === utils.style.files.length - 1 && hasCompletedFirstStylePass === false) {
if (!err) {
//alert('libsass', 'compiled');
allowPostCSS ? alert('libsass and postcss', 'compiled') : alert('libsass', 'compiled');
hasCompletedFirstStylePass = true;
setTimeout(compile.src, 1000);
}
}
});
compile.main();
}
rm('-rf', path.normalize(path.join('./', config.build)));
clean.tmp();
mkdir(path.normalize('./' + config.build));
mkdir(path.normalize('./' + config.build + '/lib'));
if (config.buildHooks && config.buildHooks[env] && config.buildHooks[env].pre) {
config.buildHooks[env].pre(process.argv).then(() => {
initProcesses();
if (canWatch) {
watch();
}
});
} else {
initProcesses();
if (canWatch) {
watch();
}
}
};
/*
Watcher
Chokidar is used to watch files, run the above methods.
*/
let watch = () => {
let watcher = chokidar.watch(path.normalize('./' + config.src + '/**/*.*'), {
ignored: /[\/\\]\./,
persistent: canWatch
}).on('change', filePath => {
if (hasInit === false) {
return;
}
if (filePath.includes(path.join(config.src, 'public'))) {
if (filePath.includes(path.join(config.src, 'public', 'index.html'))) {
copy.public();
} else {
copy.file(filePath, filePath.replace(path.normalize('src/public/'), path.normalize(config.build + '/')));
}
}
else if (filePath.indexOf('.scss') > -1) {
//alert('change', filePath.replace(/^.*[\\\/]/, ''), 'triggered libsass and postcss');
hasCompletedFirstStylePass = true;
style.file(filePath, {
sassConfig: config.style.sass.dev,
env: env,
allowPostCSS: allowPostCSS,
src: config.src,
dist: config.build,
styleSrcOnInit: false,
isVerbose: isVerbose
},
function (filePath, outFile) {
if (utils.style.files.indexOf(filePath) === utils.style.files.length - 1 && hasCompletedFirstStylePass === false) {
allowPostCSS ? alert('libsass and postcss', 'compiled') : alert('libsass', 'compiled');
}
});
}
})
.on('unlink', filePath => log(filePath, 'has been removed'));
watcher
.on('error', error => warn('ERROR:', error));
}
init();