@polylith/builder 0.1.16 → 0.1.18

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 CHANGED
@@ -32,14 +32,22 @@ export default class App {
32
32
  * Construct the app object.
33
33
  *
34
34
  * @param {String} name a name for the app
35
- * @param {String} root the full path to the root directory of the project.
35
+ * @param {Object} config
36
+ * @property {String} root the full path to the root directory of the project.
36
37
  * All other paths will be relative to this path.
37
- * @param {String} index the relative path to the main source file from the
38
+ * @proprty {String} index the relative path to the main source file from the
38
39
  * root. All source paths will be assumed to be relative to this path.
39
- * @param {String} dest the relative path to the destination folder from the
40
- * root for rolled up files
40
+ * @property {String} dest the relative path to the destination folder from
41
+ * the root for rolled up files
42
+ * @property {String} spec the relative path to the testing source file from
43
+ * the root. All source paths will be assumed to be relative to this
44
+ * path.
45
+ * @property {String} testDest the relative path to the testing destination
46
+ * folder from the root for rolled up files
41
47
  */
42
- constructor(name, root, index, dest) {
48
+ constructor(name, config) {
49
+ var {root, index, dest, spec, testDest} = config;
50
+
43
51
  root = forceToPosix(root);
44
52
  this.root = root;
45
53
  this.routerRoot = '/' + name;
@@ -47,10 +55,13 @@ export default class App {
47
55
  var filename = path.posix.join(root, index);
48
56
  this.sourcePath = path.posix.dirname(filename);
49
57
  this.destPath = path.posix.join(root, dest);
58
+ this.testPath = path.posix.join(root, testDest);
50
59
 
51
60
  this.name = name;
52
61
  this.index = index;
62
+ this.spec = spec;
53
63
  this.fullIndexPath = path.posix.join(root, index);
64
+ this.fullSpecPath = path.posix.join(root, spec);
54
65
 
55
66
  this.loadables = [];
56
67
  this.features = [];
@@ -417,12 +428,13 @@ export default class App {
417
428
  *
418
429
  * @param {String} root path relative to the source directory for the
419
430
  * feature to load.
431
+ * @param {Boolean} forTest true if building test features
420
432
  */
421
- async loadFeature(root) {
433
+ async loadFeature(root, forTest) {
422
434
  var featurePath = path.posix.join(this.sourcePath, root);
423
- var indexPath = path.posix.join(featurePath, 'index.js')
424
- var builderPath = path.posix.join(featurePath, 'build.js');
425
- var jsonPath = path.posix.join(featurePath, 'build.json');
435
+ var indexPath = path.posix.join(featurePath, forTest ? 'spec.js' : 'index.js')
436
+ var builderPath = path.posix.join(featurePath, forTest ? 'test.js' : 'build.js');
437
+ var jsonPath = path.posix.join(featurePath, forTest ? 'test.json' : 'build.json');
426
438
 
427
439
  var indexExists = await fileExists(indexPath);
428
440
  var builderExists = await fileExists(builderPath);
@@ -445,7 +457,7 @@ export default class App {
445
457
  let content = JSON.parse(await readFile(jsonPath));
446
458
 
447
459
  let builder = new ConfigFeature(content, root);
448
- await builder.build(this)
460
+ await builder.build(this, forTest)
449
461
  } catch (e) {
450
462
  console.error(e);
451
463
  throw e;
@@ -463,6 +475,15 @@ export default class App {
463
475
  }
464
476
  }
465
477
 
478
+ async testFeatures() {
479
+ var features = this.features;
480
+
481
+ for (let feature of features) {
482
+ await(this.loadFeature(feature, true));
483
+ }
484
+ }
485
+
486
+
466
487
  /**
467
488
  *
468
489
  * @param {String} root the relative path from the source root to the
@@ -529,12 +550,95 @@ export default class App {
529
550
  }
530
551
 
531
552
  /**
532
- * The build method calls this to create the rollup configuration object
553
+ * Call this method to create the configuration for testing the app
533
554
  *
534
555
  * @returns {Object} rollup configuration object
535
556
  */
536
557
 
537
- async buildConfiguration() {
558
+ async generateTestConfiguration() {
559
+ var input = [this.fullSpecPath];
560
+
561
+ // using a for loop because we are making an async call
562
+ for (let loadable of this.loadables) {
563
+ // find all the css files for the loadables
564
+ await this.findLoadableCss(loadable);
565
+ input.push(loadable.index);
566
+ }
567
+
568
+ var manualChunks = this.getManualChunks();
569
+
570
+ var plugins = [
571
+ resolve.nodeResolve({
572
+ preferBuiltins: true,
573
+ browser: true,
574
+ extensions: ['.js', '.jsx']
575
+ }),
576
+ commonjs(),
577
+ globals(),
578
+ builtins(),
579
+ rollupJson(),
580
+ jsconfig(this.root),
581
+ babel({
582
+ presets: ['@babel/preset-react'],
583
+ babelHelpers: 'bundled',
584
+ }),
585
+ svgr({svgo: false}),
586
+ loadConfigs(this.configs),
587
+ loader(this.loadables),
588
+ features(this.featureIndexes),
589
+ styles(),
590
+ resources(this.name, this.files)
591
+ ];
592
+
593
+ var config = {
594
+ input : {
595
+ input: input,
596
+ plugins: plugins,
597
+ },
598
+ output : {
599
+ output : {
600
+ sourcemap: true,
601
+ dir : this.testPath,
602
+ format: 'es',
603
+ assetFileNames: function(chunkInfo) {
604
+ return '[name]-[hash][extname]';
605
+ },
606
+ entryFileNames: function(chunkInfo) {
607
+ var entry = forceToPosix(chunkInfo.facadeModuleId);
608
+ var found = this.loadables.find(function(loadable) {
609
+ return loadable.index === entry;
610
+ }, this);
611
+ var exists = Boolean(found);
612
+
613
+ if (exists) return (`${found.name}.js`);
614
+ if (entry === this.fullSpecPath) {
615
+ return `${this.name}.js`;
616
+ }
617
+ return '[name].js';
618
+ }.bind(this),
619
+ manualChunks: manualChunks,
620
+ },
621
+ },
622
+ watch : {
623
+ watch: {
624
+ buildDelay: 250,
625
+ exclude: 'node_modules/**',
626
+ clearScreen: true,
627
+ }
628
+ }
629
+ };
630
+
631
+ return config;
632
+
633
+ }
634
+
635
+ /**
636
+ * Call this method to create the configuration for building the app
637
+ *
638
+ * @returns {Object} rollup configuration object
639
+ */
640
+
641
+ async generateBuildConfiguration() {
538
642
  var input = [this.fullIndexPath];
539
643
 
540
644
  // using a for loop because we are making an async call
@@ -566,7 +670,6 @@ export default class App {
566
670
  presets: ['@babel/preset-react'],
567
671
  babelHelpers: 'bundled',
568
672
  }),
569
- url(),
570
673
  svgr({svgo: false}),
571
674
  loadConfigs(this.configs),
572
675
  loader(this.loadables),
@@ -631,14 +734,35 @@ export default class App {
631
734
  return config;
632
735
  }
633
736
 
737
+ async test() {
738
+ this.testing = true;
739
+ if (!this.testPath || !this.spec) {
740
+ console.error('Building tests is not properly configured.')
741
+ return false;
742
+ }
743
+
744
+ await this.testFeatures();
745
+ this.config = await this.generateTestConfiguration();
746
+
747
+ const bundle = await rollup.rollup(this.config.input);
748
+ await bundle.generate(this.config.output);
749
+ await bundle.write(this.config.output);
750
+ await bundle.close();
751
+
752
+ return true;
753
+ }
754
+
634
755
  async build() {
756
+ this.testing = false;
635
757
  await this.buildFeatures();
636
- this.config = await this.buildConfiguration();
758
+ this.config = await this.generateBuildConfiguration();
637
759
 
638
760
  const bundle = await rollup.rollup(this.config.input);
639
761
  await bundle.generate(this.config.output);
640
762
  await bundle.write(this.config.output);
641
763
  await bundle.close();
764
+
765
+ return true;
642
766
  }
643
767
 
644
768
  watch() {
@@ -667,6 +791,7 @@ export default class App {
667
791
  // console.log(event);
668
792
  }
669
793
  }.bind(this));
794
+ return true;
670
795
  }
671
796
 
672
797
  }
package/ConfigApp.js CHANGED
@@ -11,8 +11,10 @@ export default class ConfigApp extends App {
11
11
  var name = config.name || 'unnamed';
12
12
  var index = config.index || 'src/index.js';
13
13
  var dest = config.dest || 'dist';
14
+ var spec = config.spec;
15
+ var testDest = config.testDest;
14
16
 
15
- super(name, root, index, dest);
17
+ super(name, {root, index, dest, spec, testDest});
16
18
  this.config = config;
17
19
 
18
20
  if (!config.template || !config.template.source) throw new Error('html source not defined in config file');
package/ConfigFeature.js CHANGED
@@ -19,13 +19,15 @@ export default class ConfigFeature extends Feature {
19
19
  *
20
20
  * @param {App} app the application for the feature
21
21
  */
22
- async build(app) {
22
+ async build(app, forTest) {
23
23
  var config = this.config;
24
24
 
25
- if (config.loadables && Array.isArray(config.loadables)) {
26
- for (let loadable of config.loadables){
27
- if (loadable.name && loadable.index) {
28
- await app.addLoadable(this.root, loadable.name, path.join(this.root, loadable.index), loadable.prefix, loadable.css);
25
+ if (!forTest) {
26
+ if (config.loadables && Array.isArray(config.loadables)) {
27
+ for (let loadable of config.loadables) {
28
+ if (loadable.name && loadable.index) {
29
+ await app.addLoadable(this.root, loadable.name, path.join(this.root, loadable.index), loadable.prefix, loadable.css);
30
+ }
29
31
  }
30
32
  }
31
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polylith/builder",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "description": "The polylith builder",
5
5
  "main": "index.js",
6
6
  "type": "module",