@polylith/builder 0.0.27 → 0.0.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/App.js +147 -41
- package/ConfigApp.js +24 -17
- package/ConfigFeature.js +28 -17
- package/Feature.js +6 -5
- package/Files.js +156 -0
- package/index.js +3 -1
- package/package.json +2 -1
- package/plugin-copy-resources.js +22 -0
- package/plugin-features.js +4 -5
- package/plugin-main-html.js +3 -5
package/App.js
CHANGED
|
@@ -13,6 +13,7 @@ import features from './plugin-features.js';
|
|
|
13
13
|
import styles from "rollup-plugin-styles";
|
|
14
14
|
|
|
15
15
|
import ConfigFeature from './ConfigFeature.js';
|
|
16
|
+
import Files from './Files.js';
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* call this function to check if the given file exists
|
|
@@ -39,11 +40,11 @@ export default class App {
|
|
|
39
40
|
*
|
|
40
41
|
* @param {String} name a name for the app
|
|
41
42
|
* @param {String} root the root directory of the project. All other
|
|
42
|
-
*
|
|
43
|
+
* paths will be relative to this path.
|
|
43
44
|
* @param {String} index the path to the main source file for the app. all
|
|
44
|
-
*
|
|
45
|
+
* source paths will be assumed to be relative to this path.
|
|
45
46
|
* @param {String} dest the path to the destination folder for the
|
|
46
|
-
*
|
|
47
|
+
* rolled up files
|
|
47
48
|
*/
|
|
48
49
|
|
|
49
50
|
constructor(name, root, index , dest) {
|
|
@@ -52,17 +53,19 @@ export default class App {
|
|
|
52
53
|
|
|
53
54
|
var filename = path.posix.join(root, index);
|
|
54
55
|
this.sourcePath = path.posix.dirname(filename);
|
|
55
|
-
this.
|
|
56
|
+
this.destPath = path.posix.join(root, dest);
|
|
56
57
|
|
|
57
58
|
this.name = name;
|
|
58
59
|
this.index = index;
|
|
59
60
|
this.fullIndexPath = path.posix.join(root, index);
|
|
60
61
|
|
|
61
62
|
this.loadables = [];
|
|
63
|
+
this.features = [];
|
|
62
64
|
this.featureIndexes = [];
|
|
63
65
|
this.configs = {};
|
|
64
66
|
this.manualChunkType = 'function';
|
|
65
67
|
this.manualChunks = [];
|
|
68
|
+
this.files = new Files(sourcePath, destination)
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
static fileToPath(filename) {
|
|
@@ -109,13 +112,53 @@ export default class App {
|
|
|
109
112
|
this.configs[root] = config;
|
|
110
113
|
}
|
|
111
114
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Call this method to add a list of resources that will be moved to the
|
|
117
|
+
* destination path when the application is built. This will either be a
|
|
118
|
+
* feature root, or the source path
|
|
119
|
+
*
|
|
120
|
+
* @param {Array<import('./Files.js').CopySpec} resourceSpecs the copy specs
|
|
121
|
+
* for all the resources being added.
|
|
122
|
+
* @param {String} root the path to the origin of the caller. Paths in
|
|
123
|
+
* the spec are assumed to be relative to this.
|
|
124
|
+
*/
|
|
125
|
+
addResources(resourceSpecs, root) {
|
|
126
|
+
resourceSpecs.forEach(function(spec) {
|
|
127
|
+
this.files.addCopySpec(root, spec);
|
|
128
|
+
}, this)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Call this method to add a feature to the application. This method is
|
|
133
|
+
* given a path to the root of the feature. At build time the builder will
|
|
134
|
+
* look directory for a file named build.js and if found import it and
|
|
135
|
+
* call the default function passing it a pointner to this object.
|
|
136
|
+
*
|
|
137
|
+
* If there is no build.js file the builder will look for a build.json file.
|
|
138
|
+
* If present that json will be loaded and used to build the feature.
|
|
139
|
+
*
|
|
140
|
+
* If that is not found it will look for an index.js file. and if found it
|
|
141
|
+
* will add that to the list on feature index files which will be
|
|
142
|
+
* automatically imported when the built code imports the @polylith/features
|
|
143
|
+
* module
|
|
144
|
+
*
|
|
145
|
+
* @param {String} feature the relative path from the application source
|
|
146
|
+
* directory to the feature root.
|
|
147
|
+
*/
|
|
148
|
+
addFeature(feature) {
|
|
149
|
+
this.features.push(feature);
|
|
115
150
|
}
|
|
116
151
|
|
|
117
|
-
|
|
118
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Call this method to add a feature index file to the application. These
|
|
154
|
+
* features will be imported when the application imports
|
|
155
|
+
* "@polylith/features"
|
|
156
|
+
*
|
|
157
|
+
* @param {String} index the relative path to the feature index file from
|
|
158
|
+
* the application source folder.
|
|
159
|
+
*/
|
|
160
|
+
addFeatureIndex(index) {
|
|
161
|
+
this.featureIndexes.push(index);
|
|
119
162
|
}
|
|
120
163
|
|
|
121
164
|
/**
|
|
@@ -156,18 +199,28 @@ export default class App {
|
|
|
156
199
|
* If no manual chunk specifiers are added, then this will be used as the
|
|
157
200
|
* default.
|
|
158
201
|
*
|
|
159
|
-
* @param {String} id the filename being
|
|
202
|
+
* @param {String} id this is the filename of the current file being
|
|
203
|
+
* processed by rollup
|
|
160
204
|
*
|
|
161
205
|
* @returns {String} the chunk name if there is a match
|
|
162
206
|
*/
|
|
163
|
-
|
|
207
|
+
defaultManualChunks(id) {
|
|
164
208
|
if (id.includes('node_modules')) {
|
|
165
209
|
return 'vendor';
|
|
166
210
|
}
|
|
167
211
|
}
|
|
168
212
|
|
|
169
|
-
|
|
170
|
-
|
|
213
|
+
/**
|
|
214
|
+
* This is called when manual chunk specifications have been added as an
|
|
215
|
+
* array.
|
|
216
|
+
*
|
|
217
|
+
* @param {String} id this is the filename of the current file being
|
|
218
|
+
* processed by rollup
|
|
219
|
+
* @returns {String} the name fo the chunk if there is a matching chunk
|
|
220
|
+
* name specifier
|
|
221
|
+
*/
|
|
222
|
+
handleManualChunksArray(id) {
|
|
223
|
+
var fixId = App.fixPath(id);
|
|
171
224
|
var result = this.manualChunks.find(function(spec) {
|
|
172
225
|
return fixId.includes(spec.includes);
|
|
173
226
|
})
|
|
@@ -175,22 +228,38 @@ export default class App {
|
|
|
175
228
|
if (result) return result.name;
|
|
176
229
|
}
|
|
177
230
|
|
|
178
|
-
|
|
179
|
-
|
|
231
|
+
/**
|
|
232
|
+
* This is called when the manual chunk specifiers are functions. Each
|
|
233
|
+
* registered function is called in the reverse order to how they were
|
|
234
|
+
* added.
|
|
235
|
+
*
|
|
236
|
+
* @param {String} id this is the filename of the current file being
|
|
237
|
+
* processed by rollup
|
|
238
|
+
* @returns the chunk name if any of the callbacks return one
|
|
239
|
+
*/
|
|
240
|
+
handleManualChunksCallbacks(id) {
|
|
241
|
+
var fixId = App.fixPath(id);
|
|
180
242
|
if (this.manualChunks.length === 0) {
|
|
181
243
|
return this.defaultManualChunks(fixId);
|
|
182
244
|
} else {
|
|
183
245
|
for (let idx = 0; idx < this.manualChunks.length; idx++) {
|
|
184
|
-
var result = this.manualChunks[
|
|
246
|
+
var result = this.manualChunks[idx](fixId);
|
|
185
247
|
if (result) return result;
|
|
186
248
|
}
|
|
187
249
|
}
|
|
188
250
|
}
|
|
189
251
|
|
|
252
|
+
/**
|
|
253
|
+
* This is called by the builder to get the method to handle manual chunks
|
|
254
|
+
* based on how they have been setup. The value returned from this funtion
|
|
255
|
+
* will be the value set in the rollup options object.
|
|
256
|
+
*
|
|
257
|
+
* @returns {Object|Function} the value to assign to the manualChunk field.
|
|
258
|
+
*/
|
|
190
259
|
getManualChunks() {
|
|
191
|
-
if (this.
|
|
192
|
-
if (this.
|
|
193
|
-
if (this.
|
|
260
|
+
if (this.manualChunkType === 'object') return this.manualChunks;
|
|
261
|
+
if (this.manualChunkType === 'array') return this.handleManualChunksArray.bind(this);
|
|
262
|
+
if (this.manualChunkType === 'function') return this.handleManualChunksCallbacks.bind(this);
|
|
194
263
|
}
|
|
195
264
|
|
|
196
265
|
/**
|
|
@@ -198,7 +267,7 @@ export default class App {
|
|
|
198
267
|
* pieces of code that are not a direct dependency of the main application.
|
|
199
268
|
*
|
|
200
269
|
* @param {String} root path relative to the source directory for the
|
|
201
|
-
*
|
|
270
|
+
* feature to load.
|
|
202
271
|
*/
|
|
203
272
|
async loadFeature(root) {
|
|
204
273
|
var featurePath = path.posix.join(this.sourcePath, root);
|
|
@@ -226,7 +295,7 @@ export default class App {
|
|
|
226
295
|
try {
|
|
227
296
|
let content = JSON.parse(await readFile(jsonPath));
|
|
228
297
|
|
|
229
|
-
let builder = new ConfigFeature(content,
|
|
298
|
+
let builder = new ConfigFeature(content, root);
|
|
230
299
|
await builder.build(this)
|
|
231
300
|
} catch (e) {
|
|
232
301
|
console.error(e);
|
|
@@ -238,30 +307,32 @@ export default class App {
|
|
|
238
307
|
}
|
|
239
308
|
|
|
240
309
|
async buildFeatures() {
|
|
241
|
-
var features =
|
|
310
|
+
var features = this.features;
|
|
242
311
|
|
|
243
312
|
for (let feature of features) {
|
|
244
313
|
await(this.loadFeature(feature));
|
|
245
314
|
}
|
|
246
315
|
}
|
|
247
316
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
317
|
+
/**
|
|
318
|
+
*
|
|
319
|
+
* @param {String} name unique name of the loadable that will be passed to
|
|
320
|
+
* the load method
|
|
321
|
+
* @param {String} main the relative path from the source folder to the entry
|
|
322
|
+
* point of the loadable.
|
|
323
|
+
* @param {String} [prefix] if given, the prefix on services created in
|
|
324
|
+
* this loadable. When the loadable has been loaded, the start and
|
|
325
|
+
* ready methods will be called on all services starting with this
|
|
326
|
+
* prefix.
|
|
327
|
+
*/
|
|
256
328
|
addLoadable(name, main, prefix) {
|
|
257
329
|
var dest = path.posix.join(this.sourcePath, main);
|
|
258
|
-
|
|
259
|
-
console.log('addLoadable', {name, path: dest, prefix})
|
|
260
330
|
this.loadables.push({name, path: dest, prefix});
|
|
261
331
|
}
|
|
262
332
|
|
|
263
333
|
/**
|
|
264
|
-
* build calls this method to create the rollup configuration
|
|
334
|
+
* The build method calls this method to create the rollup configuration
|
|
335
|
+
* object
|
|
265
336
|
*
|
|
266
337
|
* @returns {Object} rollup configuration object
|
|
267
338
|
*/
|
|
@@ -289,6 +360,7 @@ export default class App {
|
|
|
289
360
|
commonjs(),
|
|
290
361
|
babel({
|
|
291
362
|
presets: ['@babel/preset-react'],
|
|
363
|
+
babelHelpers: 'bundled',
|
|
292
364
|
}),
|
|
293
365
|
loader(this.loadables),
|
|
294
366
|
features(this.featureIndexes),
|
|
@@ -307,7 +379,7 @@ export default class App {
|
|
|
307
379
|
output : {
|
|
308
380
|
output : {
|
|
309
381
|
sourcemap: true,
|
|
310
|
-
dir : this.
|
|
382
|
+
dir : this.destPath,
|
|
311
383
|
format: 'es',
|
|
312
384
|
assetFileNames: function(chunkInfo) {
|
|
313
385
|
return '[name]-[hash][extname]';
|
|
@@ -326,10 +398,14 @@ export default class App {
|
|
|
326
398
|
return '[name].js';
|
|
327
399
|
}.bind(this),
|
|
328
400
|
manualChunks: manualChunks,
|
|
329
|
-
treeshake: {
|
|
330
|
-
moduleSideEffects: true
|
|
331
|
-
}
|
|
332
401
|
},
|
|
402
|
+
},
|
|
403
|
+
watch : {
|
|
404
|
+
watch: {
|
|
405
|
+
buildDelay: 250,
|
|
406
|
+
exclude: 'node_modules/**',
|
|
407
|
+
clearScreen: true,
|
|
408
|
+
}
|
|
333
409
|
}
|
|
334
410
|
};
|
|
335
411
|
|
|
@@ -338,11 +414,41 @@ export default class App {
|
|
|
338
414
|
|
|
339
415
|
async build() {
|
|
340
416
|
await this.buildFeatures();
|
|
341
|
-
|
|
417
|
+
this.config = this.buildConfiguration();
|
|
342
418
|
|
|
343
|
-
const bundle = await rollup.rollup(config.input);
|
|
344
|
-
await bundle.generate(config.output);
|
|
345
|
-
await bundle.write(config.output);
|
|
419
|
+
const bundle = await rollup.rollup(this.config.input);
|
|
420
|
+
await bundle.generate(this.config.output);
|
|
421
|
+
await bundle.write(this.config.output);
|
|
346
422
|
await bundle.close();
|
|
347
423
|
}
|
|
424
|
+
|
|
425
|
+
watch() {
|
|
426
|
+
var watchConfig = {
|
|
427
|
+
...this.config.input,
|
|
428
|
+
...this.config.output,
|
|
429
|
+
...this.config.watch,
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const watcher = rollup.watch(watchConfig);
|
|
433
|
+
watcher.on('event', function(event) {
|
|
434
|
+
console.log(event.code);
|
|
435
|
+
if (event.result) {
|
|
436
|
+
event.result.close();
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (event.code === 'ERROR') {
|
|
440
|
+
console.error(event.error)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (event.code === 'BUNDLE_START') {
|
|
444
|
+
process.stdout.write("\u001b[2J\u001b[0;0H");
|
|
445
|
+
console.log(event);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (event.code === 'BUNDLE_END') {
|
|
449
|
+
process.stdout.write("\u001b[2J\u001b[0;0H");
|
|
450
|
+
console.log(event);
|
|
451
|
+
}
|
|
452
|
+
}.bind(this));
|
|
453
|
+
}
|
|
348
454
|
}
|
package/ConfigApp.js
CHANGED
|
@@ -2,25 +2,32 @@ import App from './App';
|
|
|
2
2
|
import path from 'node:path/posix';
|
|
3
3
|
|
|
4
4
|
export default class ConfigApp extends App {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
constructor (config, root) {
|
|
6
|
+
App.fixPath(root);
|
|
7
|
+
var name = config.name || 'unnamed';
|
|
8
|
+
var index = config.index || path.join(root, 'src', 'index.js');
|
|
9
|
+
var dest = config.dest || path.join(root, 'dist');
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
super(name, root, index, dest);
|
|
12
|
+
this.config = config;
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
if (!index.template || !index.template.source) throw new Error('html source not defined in config file');
|
|
15
|
+
var source = index.template.source;
|
|
16
|
+
var sourceFilename = path.basename(source);
|
|
17
|
+
var destination = index.template.destination || path.join(dest, sourceFilename)
|
|
18
|
+
this.setHtmlTemplate(source, destination);
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
if (config.manualChunks) this.addManualChunks(config.manualChunks);
|
|
21
|
+
if (this.config.features) {
|
|
22
|
+
this.config.features.forEach(function(feature) {
|
|
23
|
+
this.addFeature(feature);
|
|
24
|
+
}.bind(this))
|
|
25
|
+
}
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
if (config.resources) ;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async getFeatures() {
|
|
31
|
+
return this.config.features || [];
|
|
32
|
+
}
|
|
26
33
|
}
|
package/ConfigFeature.js
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
1
1
|
import Feature from './Feature.js';
|
|
2
2
|
|
|
3
3
|
export default class ConfigFeature extends Feature {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Constrctor for the ConfigFeature class
|
|
6
|
+
*
|
|
7
|
+
* @param {Object} config the contents of the config file
|
|
8
|
+
* @param {String} root the relative path of the feature from the app source directory
|
|
9
|
+
*/
|
|
10
|
+
constructor (config, root) {
|
|
11
|
+
super(root);
|
|
12
|
+
this.config = config;
|
|
13
|
+
}
|
|
9
14
|
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
/**
|
|
16
|
+
* The application calls the method to build the feature.
|
|
17
|
+
* @param {App} app the application for th
|
|
18
|
+
*/
|
|
19
|
+
async build(app) {
|
|
20
|
+
var config = this.config;
|
|
12
21
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
if (config.loadables && Array.isArray(config.loadables)) {
|
|
23
|
+
config.loadables.forEach(function(loadable) {
|
|
24
|
+
if (loadable.name && loadable.index) {
|
|
25
|
+
app.addLoadable(loadable.name, loadable.index, loadable.prefix);
|
|
26
|
+
}
|
|
27
|
+
}, this);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (config.index) app.addFeatureIndex(config.index);
|
|
31
|
+
if (config.config) app.addConfig(config.config, this.root);
|
|
32
|
+
if (config.resources && Array.isArray(config.resources)) app.addResources(config.resources, this.root);
|
|
33
|
+
}
|
|
23
34
|
}
|
package/Feature.js
CHANGED
package/Files.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import fg from 'fast-glob';
|
|
2
|
+
import path from 'node:path/posix'
|
|
3
|
+
import {ensureDir} from 'fs-extra';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} CopySpec
|
|
7
|
+
* @property {String} dest the relative path of the copy destination from the
|
|
8
|
+
* application's destination path.
|
|
9
|
+
* @property {String} cwd the relative path from the spec root to search for
|
|
10
|
+
* files
|
|
11
|
+
* @property {String} glob the search expression for the files to copy, in glob
|
|
12
|
+
* format
|
|
13
|
+
* @property {Boolean} [keepNest] if true then the nesting of the found file
|
|
14
|
+
* relative to the cwd property will be retained when the file is copied.
|
|
15
|
+
* Defaults to false
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {Object} FileSpec
|
|
20
|
+
* @property {String} name the full path to the file being copied
|
|
21
|
+
* @property {String} searchRoot the absolute path to where the search started
|
|
22
|
+
* @property {CopySpec} spec the specifier for how to find and copy files
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* create an instance of this class to specify and and copy files from source
|
|
27
|
+
* to destination.
|
|
28
|
+
*/
|
|
29
|
+
export default class Files {
|
|
30
|
+
/**
|
|
31
|
+
* Constructor for the Files class
|
|
32
|
+
*
|
|
33
|
+
* @param {String} dest the absolute filepath to the application's
|
|
34
|
+
* destination directory
|
|
35
|
+
* @param {String} src the abolute path to the applications source
|
|
36
|
+
* directory
|
|
37
|
+
*/
|
|
38
|
+
constructor(dest, src) {
|
|
39
|
+
this.dest = dest;
|
|
40
|
+
this.src = src;
|
|
41
|
+
this.files = {};
|
|
42
|
+
this.specs = [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Call this method to add a copy specifier for resources to be copied. This
|
|
47
|
+
* will be called from either the application or from a feature build
|
|
48
|
+
* configuration or js file
|
|
49
|
+
*
|
|
50
|
+
* @param {String} root the originating path of the specifier. This is a
|
|
51
|
+
* relative path from the project src path. This is probably the location
|
|
52
|
+
* of the feature, or empty for the src path itself.
|
|
53
|
+
* @param {CopySpec} spec the specification for how to find and copy files
|
|
54
|
+
*/
|
|
55
|
+
addCopySpec(root, spec) {
|
|
56
|
+
spec.root = root;
|
|
57
|
+
this.specs.push(spec);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Call this method once per spec to add a list of files to the complete
|
|
62
|
+
* list of all the files to be copied. If multiple files are found through
|
|
63
|
+
* different specs then the spec with the longest search root will take
|
|
64
|
+
* presidence, since it is the spec with the greatest specificity
|
|
65
|
+
*
|
|
66
|
+
* @param {Array<String>} files absolute filepaths of files that have been found
|
|
67
|
+
* @param {String} searchRoot the absolute path of the spec root
|
|
68
|
+
* @param {CopySpec} spec the spec that was used to find these files.
|
|
69
|
+
*/
|
|
70
|
+
addFiles(searchRoot, files, spec) {
|
|
71
|
+
// the rule here is that files that are found by multiple specs will be
|
|
72
|
+
// controlled according to the spec with the deepest nested search path.
|
|
73
|
+
// Since file paths here are absolute tthis will always be based on the
|
|
74
|
+
// string length.
|
|
75
|
+
files.forEach(function(file) {
|
|
76
|
+
// reconcile conflicts
|
|
77
|
+
if (this.files[file]) {
|
|
78
|
+
copyInfo = this.files[file];
|
|
79
|
+
if (copyInfo.searchRoot.length > searchRoot.length) return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.files[file] = {
|
|
83
|
+
name: file,
|
|
84
|
+
searchRoot: searchRoot,
|
|
85
|
+
spec: spec,
|
|
86
|
+
}
|
|
87
|
+
}, this)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Call this method to locate all the files to be found from all the copy
|
|
92
|
+
* specs that have been added
|
|
93
|
+
*
|
|
94
|
+
* @returns {Promise<Array<FileSpec>>} the list of all files. This is also
|
|
95
|
+
* stored in the object variable this.files
|
|
96
|
+
*/
|
|
97
|
+
async findAllFiles() {
|
|
98
|
+
// using a for loop here because we are making async calls
|
|
99
|
+
for (let idx = 0; idx < this.specs.length; idx++) {
|
|
100
|
+
let spec = this.specs[idx];
|
|
101
|
+
let searchRoot = path.join(this.src, spec.root, spec.cwd);
|
|
102
|
+
let options = {
|
|
103
|
+
cwd: searchRoot,
|
|
104
|
+
ignore: ['**/node_modules'],
|
|
105
|
+
absolute: true,
|
|
106
|
+
onlyFiles: true,
|
|
107
|
+
unique: true,
|
|
108
|
+
dot: true,
|
|
109
|
+
}
|
|
110
|
+
let fullGlob = path.join(searchPath, spec.glob);
|
|
111
|
+
let files = await fg(fullGlob, options);
|
|
112
|
+
|
|
113
|
+
this.addFiles(searchRoot, files, spec);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return this.files
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Call this method to copy all the files that have been specified through
|
|
121
|
+
* addCopySpec
|
|
122
|
+
*/
|
|
123
|
+
async copyFiles() {
|
|
124
|
+
await this.findAllFiles();
|
|
125
|
+
|
|
126
|
+
var filenames = Object.keys(this.files);
|
|
127
|
+
|
|
128
|
+
// using a for loop because we are making async calls
|
|
129
|
+
for (let idx = 0 ; idx < filenames.length; idx++) {
|
|
130
|
+
let srcFilename = filenames[idx];
|
|
131
|
+
let spec = this.files[srcFilename].spec;
|
|
132
|
+
let relativePath = this.srcFilename.slice(this.files[srcFilename].searchRoot.length);
|
|
133
|
+
let destFilename = '';
|
|
134
|
+
|
|
135
|
+
if (spec.keepNest) {
|
|
136
|
+
destFilename = path.join(this.dest, spec.dest, relativePath);
|
|
137
|
+
} else {
|
|
138
|
+
destFilename = path.join(this.dest, spec.dest, path.basename(srcFilename));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// we will override existing destination files. This could have
|
|
142
|
+
// unintended consequences
|
|
143
|
+
try {
|
|
144
|
+
console.log(`copying ${srcFilename} to ${destFilename}`);
|
|
145
|
+
/*
|
|
146
|
+
await ensureDir(path.dirname(destinationFilePath));
|
|
147
|
+
await fs.copyFiles(srcFilename, destFilename);
|
|
148
|
+
*/
|
|
149
|
+
} catch (e) {
|
|
150
|
+
console.error(`Error copying file ${srcFilename} to ${destFilename}`);
|
|
151
|
+
throw e;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
}
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polylith/builder",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.29",
|
|
4
4
|
"description": "The polylith builder",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"@rollup/plugin-node-resolve": "^13.0.5",
|
|
18
18
|
"babel": "^6.23.0",
|
|
19
19
|
"escape-string-regexp": "^5.0.0",
|
|
20
|
+
"fast-glob": "^3.2.12",
|
|
20
21
|
"fs-extra": "^10.0.0",
|
|
21
22
|
"less": "^4.1.2",
|
|
22
23
|
"rollup": "^2.58.0",
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @param {Files} files the files object that specifies the resource files to
|
|
4
|
+
* copy
|
|
5
|
+
* @returns {Object} the plugin
|
|
6
|
+
*/
|
|
7
|
+
export default function(files) {
|
|
8
|
+
return {
|
|
9
|
+
name: "main-html-template",
|
|
10
|
+
|
|
11
|
+
async generateBundle(outputOptions, bundleInfo) {
|
|
12
|
+
return new Promise(async function (resolve, reject) {
|
|
13
|
+
try {
|
|
14
|
+
await files.copyFiles();
|
|
15
|
+
resolve(true)
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e)
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
package/plugin-features.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
function makeSource(features) {
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
var importStatements = '';
|
|
4
4
|
features.forEach(function(feature) {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
importStatements += `import '${feature}'\n`;
|
|
6
|
+
})
|
|
7
7
|
|
|
8
8
|
var source = `${importStatements}`
|
|
9
9
|
|
|
@@ -13,7 +13,7 @@ function makeSource(features) {
|
|
|
13
13
|
|
|
14
14
|
export default function features(features) {
|
|
15
15
|
return {
|
|
16
|
-
name: 'features',
|
|
16
|
+
name: 'features',
|
|
17
17
|
|
|
18
18
|
resolveId (source, _, third) {
|
|
19
19
|
if (source === '@polylith/features') {
|
|
@@ -30,4 +30,3 @@ export default function features(features) {
|
|
|
30
30
|
}
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
|
-
|
package/plugin-main-html.js
CHANGED
|
@@ -15,7 +15,7 @@ const INVALID_ARGS_ERROR =
|
|
|
15
15
|
function createScriptTags(scripts) {
|
|
16
16
|
var tags = '';
|
|
17
17
|
scripts.forEach(function(script) {
|
|
18
|
-
var oneTag = `<script type="module" src="${script}"></script
|
|
18
|
+
var oneTag = `<script type="module" src="${script}"></script>\n`;
|
|
19
19
|
tags += oneTag;
|
|
20
20
|
});
|
|
21
21
|
|
|
@@ -24,7 +24,7 @@ function createScriptTags(scripts) {
|
|
|
24
24
|
|
|
25
25
|
export default function(options = {}) {
|
|
26
26
|
var { root, source, destination, replaceVars } = options;
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
return {
|
|
29
29
|
name: "main-html-template",
|
|
30
30
|
|
|
@@ -34,7 +34,7 @@ export default function(options = {}) {
|
|
|
34
34
|
var includes = [];
|
|
35
35
|
var names = Object.keys(bundleInfo);
|
|
36
36
|
var scripts;
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
if (!destination && !source) throw new Error(INVALID_ARGS_ERROR);
|
|
39
39
|
|
|
40
40
|
names.forEach(function(name) {
|
|
@@ -73,5 +73,3 @@ export default function(options = {}) {
|
|
|
73
73
|
},
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
|
-
|
|
77
|
-
|