@polylith/builder 0.0.25 → 0.0.27

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 +89 -5
  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,81 @@ 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 the filename being processed.
160
+ *
161
+ * @returns {String} the chunk name if there is a match
162
+ */
163
+ defaultManualChunkCallback(id) {
164
+ if (id.includes('node_modules')) {
165
+ return 'vendor';
166
+ }
167
+ }
168
+
169
+ manualChunksArray(id) {
170
+ var fixId = this.fixPath(id);
171
+ var result = this.manualChunks.find(function(spec) {
172
+ return fixId.includes(spec.includes);
173
+ })
174
+
175
+ if (result) return result.name;
176
+ }
177
+
178
+ manualChunksCallback(id) {
179
+ var fixId = this.fixPath(fixId);
180
+ if (this.manualChunks.length === 0) {
181
+ return this.defaultManualChunks(fixId);
182
+ } else {
183
+ for (let idx = 0; idx < this.manualChunks.length; idx++) {
184
+ var result = this.manualChunks[id](fixId);
185
+ if (result) return result;
186
+ }
187
+ }
188
+ }
189
+
190
+ getManualChunks() {
191
+ if (this.manualChunksType === 'object') return this.manualChunks;
192
+ if (this.manualChunksType === 'array') return this.manualChunksArray.bind(this);
193
+ if (this.manualChunksType === 'function') return this.manualChunksCallback.bind(this);
194
+ }
195
+
119
196
  /**
120
197
  * Call this method to add the feature to the app. Features are isolated
121
198
  * pieces of code that are not a direct dependency of the main application.
@@ -183,6 +260,12 @@ export default class App {
183
260
  this.loadables.push({name, path: dest, prefix});
184
261
  }
185
262
 
263
+ /**
264
+ * build calls this method to create the rollup configuration object
265
+ *
266
+ * @returns {Object} rollup configuration object
267
+ */
268
+
186
269
  buildConfiguration() {
187
270
  var input = [this.fullIndexPath];
188
271
  this.loadables.forEach(function(path) {
@@ -194,6 +277,8 @@ export default class App {
194
277
  replacement: `<script type="module" src="${this.index}"></script>`
195
278
  }
196
279
 
280
+ var manualChunks = this.getManualChunks();
281
+
197
282
  var config = {
198
283
  input : {
199
284
  input: input,
@@ -236,14 +321,13 @@ export default class App {
236
321
 
237
322
  if (exists) return (`${found.name}.js`);
238
323
  if (entry === this.fullIndexPath) {
239
- return `${this.name}.js`
324
+ return `${this.name}.js`;
240
325
  }
241
326
  return '[name].js';
242
327
  }.bind(this),
243
- manualChunks: function(id) {
244
- if (id.includes('node_modules')) {
245
- return 'vendor';
246
- }
328
+ manualChunks: manualChunks,
329
+ treeshake: {
330
+ moduleSideEffects: true
247
331
  }
248
332
  },
249
333
  }
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.25",
3
+ "version": "0.0.27",
4
4
  "description": "The polylith builder",
5
5
  "main": "index.js",
6
6
  "type": "module",