@polylith/builder 0.0.22 → 0.0.24

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
@@ -1,33 +1,33 @@
1
- import path from 'path';
2
- import fs from 'fs-extra';
1
+ import path from 'node:path';
2
+ import {readFile, writeFile, stat} from 'node:fs/promises';
3
3
 
4
4
  import * as rollup from 'rollup';
5
5
  import { babel } from '@rollup/plugin-babel';
6
6
  import * as resolve from '@rollup/plugin-node-resolve';
7
7
  import commonjs from '@rollup/plugin-commonjs';
8
- import html from "rollup-plugin-html";
8
+ import html from 'rollup-plugin-html';
9
9
 
10
10
  import loader from './plugin-loader.js';
11
11
  import mainHTML from './plugin-main-html.js';
12
12
  import features from './plugin-features.js';
13
13
  import styles from "rollup-plugin-styles";
14
14
 
15
+ import ConfigFeature from './ConfigFeature';
16
+
15
17
  /**
16
- * call this method to get the path from the a filename, and force the file path
17
- * to use posix notation and remove all drive information. This will likely
18
- * be used by parent App implementations in their constructors
19
- *
20
- *
21
- * @param {String} src the filename wqe
22
- * @returns {String} the new path
18
+ * call this function to check if the given file exists
19
+ *
20
+ * @param {String} path the name of the file
21
+ * @returns {Promise<Boolean>} true if the file exists
23
22
  */
24
- function fixPath(src) {
25
- src = src.replace('file:', '');
26
- src = src.replace('///', '');
27
- src = src.replace(/.:/, '');
28
- src = src.replace(/\\/g, '/');
29
23
 
30
- return src;
24
+ async function fileExists(path) {
25
+ try {
26
+ await stat(path)
27
+ return true;
28
+ } catch (e) {
29
+ return false;
30
+ }
31
31
  }
32
32
 
33
33
  /**
@@ -36,18 +36,18 @@ function fixPath(src) {
36
36
  export class App {
37
37
  /**
38
38
  * Construct the app object.
39
- *
39
+ *
40
40
  * @param {String} name a name for the app
41
- * @param {String} root the root directory of the project. All other
42
- * paths will be relative to this path.
43
- * @param {String} index the path to the main source file for the app. all
41
+ * @param {String} root the root directory of the project. All other
42
+ * paths will be relative to this path.
43
+ * @param {String} index the path to the main source file for the app. all
44
44
  * source paths will be assumed to be relative to this path.
45
- * @param {String} dest the path to the destination folder for the
45
+ * @param {String} dest the path to the destination folder for the
46
46
  * rolled up files
47
47
  */
48
48
 
49
49
  constructor(name, root, index , dest) {
50
- root = fixPath(root);
50
+ root = this.fixPath(root);
51
51
  this.root = root;
52
52
 
53
53
  var filename = path.posix.join(root, index);
@@ -56,31 +56,55 @@ export class App {
56
56
 
57
57
  this.name = name;
58
58
  this.index = index;
59
+ this.fullIndexPath = path.posix.join(root, index);
60
+
59
61
  this.loadables = [];
60
62
  this.featureIndexes = [];
63
+ this.configs = {};
61
64
  }
62
65
 
63
66
  static fileToPath(filename) {
64
- filename = fixPath(filename);
67
+ filename = this.fixPath(filename);
65
68
  return path.posix.dirname(filename);
66
69
  }
67
70
 
71
+ /**
72
+ * call this method to force the file path to use posix notation and remove
73
+ * all drive information.
74
+ *
75
+ *
76
+ * @param {String} src the filename wqe
77
+ * @returns {String} the new path
78
+ */
79
+ static fixPath(src) {
80
+ src = src.replace('file:', '');
81
+ src = src.replace('///', '');
82
+ src = src.replace(/.:/, '');
83
+ src = src.replace(/\\/g, '/');
84
+
85
+ return src;
86
+ }
87
+
68
88
  /**
69
89
  * Call this method to generate a path relative to the src directory
70
- *
71
- * @param {String} path
90
+ *
91
+ * @param {String} fully qualified path
72
92
  * @returns the path relative to the source root.
73
93
  */
74
94
  rootPath(path) {
75
95
  var idx = path.indexOf(this.sourcePath);
76
- if (idx === 0) path.splice(0, length(this.sourcePath));
77
- if (path[0] === '/') path.splice(0, 1);
96
+ if (idx === 0) path = path.slice(this.sourcePath.length);
97
+ if (path[0] === '/') path = path.slice(1);
78
98
 
79
99
  return './' + path;
80
100
  }
81
101
 
82
- setHtmlTemplate(template, target) {
83
- this.htmlTemplate = {template, target};
102
+ setHtmlTemplate(source, destination) {
103
+ this.htmlTemplate = {source: source, destination: destination};
104
+ }
105
+
106
+ addConfig(config, root) {
107
+ this.configs[root] = config;
84
108
  }
85
109
 
86
110
  // default implementation, applications will implement this to specify the loadable features to be used
@@ -88,25 +112,46 @@ export class App {
88
112
  return [];
89
113
  }
90
114
 
91
- async loadFeature(src) {
92
- var featurePath = path.posix.join(this.sourcePath, src);
93
- var indexPath = fixPath(path.posix.join(featurePath, 'index.js'))
94
- var indexExists = await fs.pathExists(indexPath);
95
- var builderPath = fixPath(path.posix.join(featurePath, 'build/index.js'));
96
- var builderExists = await fs.pathExists(builderPath);
115
+ addFeatureMain(main) {
116
+ this.featureIndexes.push(main);
117
+ }
97
118
 
119
+ /**
120
+ * Call this method to add the feature to the app. Features are isolated
121
+ * pieces of code that are not a direct dependency of the main application.
122
+ *
123
+ * @param {String} root path relative to the source directory for the
124
+ * feature to load.
125
+ */
126
+ async loadFeature(root) {
127
+ var featurePath = path.posix.join(this.sourcePath, root);
128
+ var indexPath = path.posix.join(featurePath, 'index.js')
129
+ var builderPath = path.posix.join(featurePath, 'build.js');
130
+ var jsonPath = path.posix.join(featurePath, 'build.json');
131
+
132
+ var indexExists = await fileExists(indexPath);
133
+ var builderExists = await fileExists(builderPath);
134
+ var jsonExists = await fileExists(jsonPath);
135
+
136
+ // If there is a builder, we will use this
98
137
  if (builderExists) {
99
138
  try {
100
- var module = await import(builderPath)
101
- var feature = module.default;
139
+ let module = await import(builderPath)
140
+ let feature = module.default;
102
141
  await feature.build(this);
103
142
  } catch(e) {
104
- console.error('error while building feature', src);
143
+ console.error('error while building feature', root);
105
144
  console.log(e);
106
145
  }
107
- }
108
146
 
109
- if (indexExists) this.featureIndexes.push(this.rootPath(indexPath));
147
+ // second priority is a build configuration file.
148
+ } else if (jsonExists) {
149
+ let content = readFile(jsonPath);
150
+ let builder = new ConfigFeature(content, featurePath);
151
+ await feature.build
152
+
153
+ // if neither exist, assume the index should be added
154
+ } else if (indexExists) this.featureIndexes.push(indexPath);
110
155
  }
111
156
 
112
157
  async buildFeatures() {
@@ -114,25 +159,26 @@ export class App {
114
159
 
115
160
  for (let feature of features) {
116
161
  await(this.loadFeature(feature));
117
- }
162
+ }
118
163
  }
119
164
 
120
165
  /* features will call this method if they want to add loadables
121
166
 
122
167
  name - name of the loadable that we be passed to the load method
123
- src - the path to the entry point of the loadable
124
- prefix - if given, the prefix on services created in this loadable.
168
+ main - the relative path from the source folder to the entry point of the
169
+ loadable.
170
+ prefix - if given, the prefix on services created in this loadable.
125
171
  The start and ready methods will be called on these services
126
172
  */
127
- addLoadable(name, src, prefix) {
128
- var dest = path.posix.join(this.sourcePath, src);
173
+ addLoadable(name, main, prefix) {
174
+ var dest = path.posix.join(this.sourcePath, main);
129
175
 
130
176
  console.log(dest)
131
177
  this.loadables.push({name, path: dest, prefix});
132
178
  }
133
-
179
+
134
180
  buildConfiguration() {
135
- var input = [this.index];
181
+ var input = [this.fullIndexPath];
136
182
  this.loadables.forEach(function(path) {
137
183
  input.push(path.path);
138
184
  });
@@ -148,28 +194,28 @@ export class App {
148
194
  plugins: [
149
195
  resolve.nodeResolve({
150
196
  extensions: ['.js', '.jsx']
151
- }),
152
- commonjs(),
197
+ }),
198
+ commonjs(),
153
199
  babel({
154
200
  presets: ['@babel/preset-react'],
155
201
  }),
156
202
  loader(this.loadables),
157
203
  features(this.featureIndexes),
158
204
  html({
159
- include: path.join(this.sourcePath, "**/*.html"),
205
+ include: path.join(this.sourcePath, "**/*.html"),
160
206
  }),
161
- styles(),
207
+ styles(),
162
208
  mainHTML({
163
209
  root: this.root,
164
- template: this.htmlTemplate.template,
165
- target: this.htmlTemplate.target,
210
+ source: this.htmlTemplate.source,
211
+ destination: this.htmlTemplate.destination,
166
212
  replaceVars: {},
167
213
  })
168
214
  ],
169
215
  },
170
216
  output : {
171
217
  output : {
172
- sourcemap: true,
218
+ sourcemap: true,
173
219
  dir : this.destination,
174
220
  format: 'es',
175
221
  },
@@ -184,10 +230,11 @@ export class App {
184
230
  await this.buildFeatures();
185
231
  var config = this.buildConfiguration();
186
232
 
233
+ console.log(JSON.stringify(config, null, ' '));
234
+
187
235
  const bundle = await rollup.rollup(config.input);
188
236
  await bundle.generate(config.output);
189
237
  await bundle.write(config.output);
190
238
  await bundle.close();
191
239
  }
192
240
  }
193
-
package/ConfigApp.js ADDED
@@ -0,0 +1,24 @@
1
+ import {App} from './App';
2
+ import path from 'node:path/posix';
3
+
4
+ export default class ConfigApp extends App {
5
+ constructor (config, root) {
6
+ App.fixPath(root);
7
+ var name = config.name || 'unnamed';
8
+ var index = config.main || path.join(root, 'src', 'index.js');
9
+ var dest = config.dest || path.join(root, 'dist');
10
+
11
+ super(name, root, index, dest);
12
+ this.config = config;
13
+
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
+ }
20
+
21
+ async getFeatures() {
22
+ return this.config.features || [];
23
+ }
24
+ }
@@ -0,0 +1,21 @@
1
+ import Feature from './Feature';
2
+
3
+ export default class Feature {
4
+ constructor (config, root) {
5
+ super();
6
+ this.config = config;
7
+ this.root = root;
8
+ }
9
+
10
+ async build(app) {
11
+ if (config.loadables && Array.isArray(config.loadables)) {
12
+ config.loadables.forEach(function(loadable) {
13
+ if (loadable.name && loadable.main) {
14
+ app.addLoadable(loadable.name, loadable.main, loadable.prefix);
15
+ }
16
+ }, this);
17
+ }
18
+ if (config.main) app.addFeatureMain(config.main);
19
+ if (config.config) app.addConfig(config.config, this.root);
20
+ }
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polylith/builder",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "description": "The polylith builder",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -22,7 +22,7 @@
22
22
  "rollup": "^2.58.0",
23
23
  "rollup-plugin-html": "^0.2.1",
24
24
  "rollup-plugin-jsx": "^1.0.3",
25
- "rollup-plugin-styles": "^3.14.1",
25
+ "rollup-plugin-styles": "^4.0.0",
26
26
  "sass": "^1.43.4"
27
27
  }
28
28
  }
@@ -2,12 +2,11 @@ function makeSource(features) {
2
2
 
3
3
  var importStatements = '';
4
4
  features.forEach(function(feature) {
5
- importStatements += `import from '${feature}'\n`;
5
+ importStatements += `import '${feature}'\n`;
6
6
  })
7
7
 
8
8
  var source = `${importStatements}`
9
9
 
10
- console.log(source);
11
10
  return source;
12
11
  }
13
12
 
@@ -1,6 +1,7 @@
1
1
  import escapeStringRegexp from 'escape-string-regexp';
2
- import fs from 'fs-extra';
2
+ import {ensureFile} from 'fs-extra';
3
3
  import path from 'path/posix';
4
+ import {readFile, writeFile} from 'node:fs/promises';
4
5
 
5
6
  const INVALID_ARGS_ERROR =
6
7
  "[plugin-main-html] You did not provide a template or target!";
@@ -22,20 +23,19 @@ function createScriptTags(scripts) {
22
23
  }
23
24
 
24
25
  export default function(options = {}) {
25
- var { root, template, target, replaceVars } = options;
26
+ var { root, source, destination, replaceVars } = options;
26
27
 
27
28
  return {
28
29
  name: "main-html-template",
29
30
 
30
31
  async generateBundle(outputOptions, bundleInfo) {
31
- // console.log(JSON.stringify(bundleInfo, null, ' '));
32
32
  return new Promise(async function (resolve, reject) {
33
33
  try {
34
34
  var includes = [];
35
35
  var names = Object.keys(bundleInfo);
36
36
  var scripts;
37
37
 
38
- if (!target && !template) throw new Error(INVALID_ARGS_ERROR);
38
+ if (!destination && !source) throw new Error(INVALID_ARGS_ERROR);
39
39
 
40
40
  names.forEach(function(name) {
41
41
  var entry = bundleInfo[name];
@@ -47,15 +47,11 @@ export default function(options = {}) {
47
47
  scripts = createScriptTags(includes);
48
48
  replaceVars["scripts"] = scripts;
49
49
 
50
- var templateFilePath = path.join(root, template);
51
- var targetFilePath = path.join(root, target);
50
+ var sourceFilePath = path.join(root, source);
51
+ var destinationFilePath = path.join(root, destination);
52
52
 
53
53
  // Read the file
54
- var buffer = await fs.readFile(templateFilePath);
55
-
56
- // Convert buffer to a string and replace the replaceable
57
- // variables
58
- var content = buffer.toString("utf8");
54
+ var content = await readFile(sourceFilePath, 'utf-8');
59
55
  if (replaceVars) {
60
56
  var varNames = Object.keys(replaceVars);
61
57
  varNames.forEach(function(name) {
@@ -66,11 +62,9 @@ export default function(options = {}) {
66
62
  });
67
63
  }
68
64
 
69
- var injected = content;
70
-
71
65
  // write the injected template to a file
72
- await fs.ensureFile(targetFilePath);
73
- await fs.writeFile(targetFilePath, injected);
66
+ await ensureFile(destinationFilePath);
67
+ writeFile(destinationFilePath, content, 'utf-8');
74
68
  resolve();
75
69
  } catch (e) {
76
70
  reject(e);
package/plugin-config.js DELETED
@@ -1,23 +0,0 @@
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
-