@polylith/builder 0.0.26 → 0.0.28

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.
Files changed (3) hide show
  1. package/App.js +149 -27
  2. package/ConfigApp.js +3 -1
  3. package/package.json +1 -1
package/App.js CHANGED
@@ -61,6 +61,8 @@ export default class App {
61
61
  this.loadables = [];
62
62
  this.featureIndexes = [];
63
63
  this.configs = {};
64
+ this.manualChunkType = 'function';
65
+ this.manualChunks = [];
64
66
  }
65
67
 
66
68
  static fileToPath(filename) {
@@ -116,6 +118,107 @@ export default class App {
116
118
  this.featureIndexes.push(main);
117
119
  }
118
120
 
121
+ /**
122
+ * Call this method to add a new manual chunk specifier. The type of the
123
+ * parameter passed will determine how the specifier will be processed. If
124
+ * the type differs from previously added types, then the previous
125
+ * specifiers will be removed.
126
+ *
127
+ * @param {Object|Function|Array} spec the chunk name specifier to be added.
128
+ */
129
+ addManualChunk(spec) {
130
+ var type = typeof spec === 'function' ? 'function' : 'object';
131
+ type = Array.isArray(spec) ? 'array' : type;
132
+
133
+ if (this.manualChunkType != type) {
134
+ console.warn(`addManualChunk of type ${type} will override previously set of type ${this.manualChunkType}`);
135
+ if (type === 'object') this.manualChunks = {}
136
+ else this.manualChunks === [];
137
+ this.manualChunkType = type;
138
+ }
139
+
140
+ if (type === 'function') {
141
+ this.manualChunks.unshift(spec);
142
+ } else if (type === 'array') {
143
+ var keys = Object.keys(spec);
144
+ spec.forEach(function(one) {
145
+ this.manualChunks.unshift(one);
146
+ }.bind(this));
147
+ } else if (type === 'object') {
148
+ var keys = Object.keys(spec);
149
+ keys.forEach(function(key) {
150
+ this.manualChunks[key] = spec[key];
151
+ }.bind(this));
152
+ }
153
+ }
154
+
155
+ /**
156
+ * If no manual chunk specifiers are added, then this will be used as the
157
+ * default.
158
+ *
159
+ * @param {String} id this is the filename of the current file being
160
+ * processed by rollup
161
+ *
162
+ * @returns {String} the chunk name if there is a match
163
+ */
164
+ defaultManualChunks(id) {
165
+ if (id.includes('node_modules')) {
166
+ return 'vendor';
167
+ }
168
+ }
169
+
170
+ /**
171
+ * This is called when manual chunk specifications have been added as an
172
+ * array.
173
+ *
174
+ * @param {String} id this is the filename of the current file being
175
+ * processed by rollup
176
+ * @returns {String} the name fo the chunk if there is a matching chunk
177
+ * name specifier
178
+ */
179
+ handleManualChunksArray(id) {
180
+ var fixId = App.fixPath(id);
181
+ var result = this.manualChunks.find(function(spec) {
182
+ return fixId.includes(spec.includes);
183
+ })
184
+
185
+ if (result) return result.name;
186
+ }
187
+
188
+ /**
189
+ * This is called when the manual chunk specifiers are functions. Each
190
+ * registered function is called in the reverse order to how they were
191
+ * added.
192
+ *
193
+ * @param {String} id this is the filename of the current file being
194
+ * processed by rollup
195
+ * @returns the chunk name if any of the callbacks return one
196
+ */
197
+ handleManualChunksCallbacks(id) {
198
+ var fixId = App.fixPath(id);
199
+ if (this.manualChunks.length === 0) {
200
+ return this.defaultManualChunks(fixId);
201
+ } else {
202
+ for (let idx = 0; idx < this.manualChunks.length; idx++) {
203
+ var result = this.manualChunks[idx](fixId);
204
+ if (result) return result;
205
+ }
206
+ }
207
+ }
208
+
209
+ /**
210
+ * This is called by the builder to get the method to handle manual chunks
211
+ * based on how they have been setup. The value returned from this funtion
212
+ * will be the value set in the rollup options object.
213
+ *
214
+ * @returns {Object|Function} the value to assign to the manualChunk field.
215
+ */
216
+ getManualChunks() {
217
+ if (this.manualChunkType === 'object') return this.manualChunks;
218
+ if (this.manualChunkType === 'array') return this.handleManualChunksArray.bind(this);
219
+ if (this.manualChunkType === 'function') return this.handleManualChunksCallbacks.bind(this);
220
+ }
221
+
119
222
  /**
120
223
  * Call this method to add the feature to the app. Features are isolated
121
224
  * pieces of code that are not a direct dependency of the main application.
@@ -168,21 +271,27 @@ export default class App {
168
271
  }
169
272
  }
170
273
 
171
- /* features will call this method if they want to add loadables
172
-
173
- name - name of the loadable that we be passed to the load method
174
- main - the relative path from the source folder to the entry point of the
175
- loadable.
176
- prefix - if given, the prefix on services created in this loadable.
177
- The start and ready methods will be called on these services
178
- */
274
+ /**
275
+ *
276
+ * @param {String} name unique name of the loadable that will be passed to
277
+ * the load method
278
+ * @param {String} main the relative path from the source folder to the entry
279
+ * point of the loadable.
280
+ * @param {String} [prefix] if given, the prefix on services created in
281
+ * this loadable. When the loadable has been loaded, the start and ready
282
+ * methods will be called on all services starting with this prefix.
283
+ */
179
284
  addLoadable(name, main, prefix) {
180
285
  var dest = path.posix.join(this.sourcePath, main);
181
-
182
- console.log('addLoadable', {name, path: dest, prefix})
183
286
  this.loadables.push({name, path: dest, prefix});
184
287
  }
185
288
 
289
+ /**
290
+ * build calls this method to create the rollup configuration object
291
+ *
292
+ * @returns {Object} rollup configuration object
293
+ */
294
+
186
295
  buildConfiguration() {
187
296
  var input = [this.fullIndexPath];
188
297
  this.loadables.forEach(function(path) {
@@ -194,6 +303,8 @@ export default class App {
194
303
  replacement: `<script type="module" src="${this.index}"></script>`
195
304
  }
196
305
 
306
+ var manualChunks = this.getManualChunks();
307
+
197
308
  var config = {
198
309
  input : {
199
310
  input: input,
@@ -240,20 +351,15 @@ export default class App {
240
351
  }
241
352
  return '[name].js';
242
353
  }.bind(this),
243
- manualChunks: function(id) {
244
- var fixId = App.fixPath(id);
245
- if (fixId.includes('init.js')) {
246
- return 'init.js';
247
- }
248
- if (id.includes('node_modules')) {
249
- return 'vendor';
250
- }
251
- }.bind(this),
252
- treeshake: {
253
- moduleSideEffects: true
254
- }
354
+ manualChunks: manualChunks,
255
355
  },
256
- }
356
+ },
357
+ watch : {
358
+ watch: {
359
+ buildDelay: 250,
360
+ exclude: 'node_modules/**',
361
+ }
362
+ }
257
363
  };
258
364
 
259
365
  return config;
@@ -261,11 +367,27 @@ export default class App {
261
367
 
262
368
  async build() {
263
369
  await this.buildFeatures();
264
- var config = this.buildConfiguration();
370
+ this.config = this.buildConfiguration();
265
371
 
266
- const bundle = await rollup.rollup(config.input);
267
- await bundle.generate(config.output);
268
- await bundle.write(config.output);
372
+ const bundle = await rollup.rollup(this.config.input);
373
+ await bundle.generate(this.config.output);
374
+ await bundle.write(this.config.output);
269
375
  await bundle.close();
270
376
  }
377
+
378
+ watch() {
379
+ var watchConfig = {
380
+ input: this.config.input,
381
+ output: this.config.output,
382
+ watch: this.config.watch,
383
+ }
384
+
385
+ const watcher = rollup.watch(watchConfig);
386
+
387
+ watcher.on('event', function({result}) {
388
+ if (result) {
389
+ result.close();
390
+ }
391
+ }.bind(this));
392
+ }
271
393
  }
package/ConfigApp.js CHANGED
@@ -15,7 +15,9 @@ export default class ConfigApp extends App {
15
15
  var source = index.template.source;
16
16
  var sourceFilename = path.basename(source);
17
17
  var destination = index.template.destination || path.join(dest, sourceFilename)
18
- this.setHtmlTemplate(source, destination)
18
+ this.setHtmlTemplate(source, destination);
19
+
20
+ if (config.manualChunks) this.addManualChunks(config.manualChunks);
19
21
  }
20
22
 
21
23
  async getFeatures() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polylith/builder",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "description": "The polylith builder",
5
5
  "main": "index.js",
6
6
  "type": "module",