@polylith/builder 0.0.18 → 0.0.19

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
@@ -6,41 +6,69 @@ import * as resolve from '@rollup/plugin-node-resolve';
6
6
  import commonjs from '@rollup/plugin-commonjs';
7
7
  import html from "rollup-plugin-html";
8
8
 
9
- import loadables from './plugin-loadables.js';
9
+ import loader from './plugin-loader.js';
10
10
  import mainHTML from './plugin-main-html.js';
11
+ import features from './plugin-feature.js';
11
12
  import styles from "rollup-plugin-styles";
12
13
 
13
- export class App {
14
- /*
15
- srcDir - the path to the calling app. This is necessary to resolve file paths
16
- name - a name for the app
17
- appEntry - the path to the main source file for the app
18
- destination - the path to the destination folder for the rolled up files
19
-
20
- All paths are relative to srcDir
21
- */
14
+ /**
15
+ * call this method to get the path from the a filename, and force the file path
16
+ * to use posix notation and remove all drive information. This will likely
17
+ * be used by parent App implementations in their constructors
18
+ *
19
+ *
20
+ * @param {String} src the filename wqe
21
+ * @returns {String} the new path
22
+ */
23
+ function fixPath(src) {
24
+ src = src.replace('file:', '');
25
+ src = src.replace('///', '');
26
+ src = src.replace(/.:/, '');
27
+ src = src.replace(/\\/g, '/');
28
+
29
+ return src;
30
+ }
22
31
 
23
- constructor(srcDir, name, appEntry, destination) {
24
- this.srcDir = this.fixPath(srcDir);
25
- var parts = path.posix.parse(this.srcDir);
26
- var root = parts.dir;
27
- var filename = path.posix.join(root, appEntry);
28
- this.basePath = path.posix.dirname(filename);
29
- this.destination = path.posix.join(root, destination);
32
+ /**
33
+ * The base class for applications. Applications inherit from this class
34
+ */
35
+ export class App {
36
+ /**
37
+ * Construct the app object.
38
+ *
39
+ * @param {String} name a name for the app
40
+ * @param {String} root the root directory of the project. All other
41
+ * paths will be relative to this path.
42
+ * @param {String} index the path to the main source file for the app. all
43
+ * source paths will be assumed to be relative to this path.
44
+ * @param {String} dest the path to the destination folder for the
45
+ * rolled up files
46
+ */
47
+
48
+ constructor(name, root, index , dest) {
49
+ root = fixPath(root);
50
+ var filename = path.posix.join(root, index);
51
+ this.sourcePath = path.posix.dirname(filename);
52
+ this.destination = path.posix.join(root, dest);
30
53
 
31
54
  this.name = name;
32
- this.appEntry = appEntry;
55
+ this.index = index;
33
56
  this.loadables = [];
57
+ this.root = root;
34
58
  }
35
59
 
36
- // make all paths on the same drive, this is to clean up windows paths
37
- fixPath(src) {
38
- src = src.replace('file:', '');
39
- src = src.replace('///', '');
40
- src = src.replace(/.:/, '');
41
- src = src.replace(/\\/g, '/');
60
+ static fileToPath(filename) {
61
+ filename = fixPath(filename);
62
+ return path.posix.dirname(filename);
63
+ }
64
+
65
+ /**
66
+ * Call this method to generate a path relative to the src directory
67
+ *
68
+ * @param {String} path
69
+ */
70
+ rootPath(path) {
42
71
 
43
- return src;
44
72
  }
45
73
 
46
74
  setHtmlTemplate(template, target) {
@@ -53,24 +81,30 @@ export class App {
53
81
  }
54
82
 
55
83
  async loadFeature(src) {
56
- var featurePath = path.posix.join(this.basePath, src);
57
- var featureModule = this.fixPath(path.posix.join(featurePath, 'build/index.js'));
58
-
59
- try {
60
- return import(featureModule).then(function(module) {;
84
+ var featurePath = path.posix.join(this.sourcePath, src);
85
+ var indexPath = fixPath(path.posix.join(featurePath, 'index.js'))
86
+ var indexExists = await fs.pathExists(indexPath);
87
+ var builderPath = fixPath(path.posix.join(featurePath, 'build/index.js'));
88
+ var builderExists = await fs.pathExists(builderPath);
89
+
90
+ if (builderExists) {
91
+ try {
92
+ var module = await import(builderPath)
61
93
  var feature = module.default;
62
- feature.build(this);
63
- }.bind(this))
64
- } catch(e) {
65
- console.error('error while building feature', src);
66
- console.log(e);
94
+ await feature.build(this);
95
+ } catch(e) {
96
+ console.error('error while building feature', src);
97
+ console.log(e);
98
+ }
67
99
  }
100
+
101
+ if (indexExists) this.featureIndexes.push(indexPath);
68
102
  }
69
103
 
70
104
  async buildFeatures() {
71
105
  var features = await this.getFeatures();
72
106
 
73
- for (const feature of features) {
107
+ for (let feature of features) {
74
108
  await(this.loadFeature(feature));
75
109
  }
76
110
  }
@@ -83,21 +117,21 @@ export class App {
83
117
  The start and ready methods will be called on these services
84
118
  */
85
119
  addLoadable(name, src, prefix) {
86
- var dest = path.posix.join(this.basePath, src);
120
+ var dest = path.posix.join(this.sourcePath, src);
87
121
 
88
122
  console.log(dest)
89
123
  this.loadables.push({name, path: dest, prefix});
90
124
  }
91
125
 
92
126
  buildConfiguration() {
93
- var input = [this.appEntry];
127
+ var input = [this.index];
94
128
  this.loadables.forEach(function(path) {
95
129
  input.push(path.path);
96
130
  });
97
131
 
98
132
  this.variables = {
99
133
  pattern: 'main-js',
100
- replacement: `<script type="module" src="${this.appEntry}"></script>`
134
+ replacement: `<script type="module" src="${this.index}"></script>`
101
135
  }
102
136
 
103
137
  var config = {
@@ -111,12 +145,13 @@ export class App {
111
145
  babel({
112
146
  presets: ['@babel/preset-react'],
113
147
  }),
114
- loadables(this.loadables),
148
+ loader(this.loadables),
115
149
  html({
116
- include: path.join(this.basePath, "**/*.html"),
150
+ include: path.join(this.sourcePath, "**/*.html"),
117
151
  }),
118
152
  styles(),
119
153
  mainHTML({
154
+ root: this.root,
120
155
  template: this.htmlTemplate.template,
121
156
  target: this.htmlTemplate.target,
122
157
  replaceVars: {},
package/Feature.js CHANGED
@@ -2,7 +2,7 @@ export class Feature {
2
2
  constructor () {
3
3
  }
4
4
 
5
- build(app) {
5
+ async build(app) {
6
6
  this.app = app;
7
7
  }
8
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polylith/builder",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "The polylith builder",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -0,0 +1,23 @@
1
+
2
+ import rollupPluginutils from ('rollup-pluginutils');
3
+
4
+
5
+ export default function config() {
6
+ var opts = {};
7
+
8
+ opts.include = '**/*.cfg'
9
+
10
+ var filter = rollupPluginutils.createFilter(opts.include, opts.exclude);
11
+
12
+ return {
13
+ name: 'config',
14
+
15
+ async load(id) {
16
+ if (filter(id)) {
17
+ console.log(id);
18
+ return source;
19
+ }
20
+ }
21
+ };
22
+ }
23
+
File without changes
@@ -40,19 +40,19 @@ ${loadableSwitches}
40
40
  }
41
41
 
42
42
 
43
- export default function features(loadables) {
43
+ export default function loader(loadables) {
44
44
  return {
45
- name: 'features',
45
+ name: 'loader',
46
46
 
47
- resolveId ( source, _, third ) {
48
- if (source === '@polylith/loadables') {
47
+ resolveId (source, _, third) {
48
+ if (source === '@polylith/loader') {
49
49
  return source;
50
50
  }
51
51
  return null;
52
52
  },
53
53
 
54
- load ( id ) {
55
- if (id === '@polylith/loadables') {
54
+ load (id) {
55
+ if (id === '@polylith/loader') {
56
56
  return makeSource(loadables);
57
57
  }
58
58
  return null;
@@ -1,12 +1,6 @@
1
- /*
2
- var escapeStringRegexp = require('escape-string-regexp');
3
- var fs = require('fs-extra');
4
- var path = require('path');
5
- */
6
-
7
1
  import escapeStringRegexp from 'escape-string-regexp';
8
2
  import fs from 'fs-extra';
9
- import path from 'path';
3
+ import path from 'path/posix';
10
4
 
11
5
  const INVALID_ARGS_ERROR =
12
6
  "[plugin-main-html] You did not provide a template or target!";
@@ -28,7 +22,8 @@ function createScriptTags(scripts) {
28
22
  }
29
23
 
30
24
  export default function(options = {}) {
31
- var { template, target, replaceVars } = options;
25
+ var { root, template, target, replaceVars } = options;
26
+
32
27
  return {
33
28
  name: "main-html-template",
34
29
 
@@ -42,11 +37,6 @@ export default function(options = {}) {
42
37
 
43
38
  if (!target && !template) throw new Error(INVALID_ARGS_ERROR);
44
39
 
45
- var outputDir = outputOptions.dir || path.dirname(outputOptions.file);
46
-
47
- var targetDir = outputDir;
48
- var bundleDirString = "";
49
-
50
40
  names.forEach(function(name) {
51
41
  var entry = bundleInfo[name];
52
42
  if (!entry.isDynamicEntry) {
@@ -57,40 +47,30 @@ export default function(options = {}) {
57
47
  scripts = createScriptTags(includes);
58
48
  replaceVars["scripts"] = scripts;
59
49
 
60
- if (target && path.dirname(target) !== ".") {
61
- targetDir = path.dirname(target);
62
- var bundleDir = path.relative(targetDir, outputDir);
63
- bundleDirString = bundleDir && `${bundleDir}/`;
64
- }
65
-
66
- // Get the target file name.
67
- var targetName = path.basename(target || template);
68
-
69
- // Add the file suffix if it isn't there.
70
- var targetFile =
71
- targetName.indexOf(".html") < 0 ? `${targetName}.html` : targetName;
50
+ var templateFilePath = path.join(root, template);
51
+ var targetFilePath = path.join(root, target);
72
52
 
73
53
  // Read the file
74
- var buffer = await fs.readFile(template);
54
+ var buffer = await fs.readFile(templateFilePath);
75
55
 
76
- // Convert buffer to a string and get the </body> index
77
- var tmpl = buffer.toString("utf8");
56
+ // Convert buffer to a string and replace the replaceable
57
+ // variables
58
+ var content = buffer.toString("utf8");
78
59
  if (replaceVars) {
79
- var patterns = Object.keys(replaceVars);
80
- patterns.forEach(function(pattern) {
81
- var replacement = replaceVars[pattern]
82
- var escapedPattern = escapeStringRegexp('${' + pattern + '}');
83
- var regex = new RegExp(`${escapedPattern}`, 'g');
84
- tmpl = tmpl.replace(regex, replacement);
60
+ var varNames = Object.keys(replaceVars);
61
+ varNames.forEach(function(name) {
62
+ var replacement = replaceVars[name]
63
+ var escapedName = escapeStringRegexp('${' + name + '}');
64
+ var regex = new RegExp(escapedName, 'g');
65
+ content = content.replace(regex, replacement);
85
66
  });
86
67
  }
87
68
 
88
- var injected = tmpl;
69
+ var injected = content;
89
70
 
90
71
  // write the injected template to a file
91
- var finalTarget = path.join(targetDir, targetFile);
92
- await fs.ensureFile(finalTarget);
93
- await fs.writeFile(finalTarget, injected);
72
+ await fs.ensureFile(targetFilePath);
73
+ await fs.writeFile(targetFilePath, injected);
94
74
  resolve();
95
75
  } catch (e) {
96
76
  reject(e);