@polylith/builder 0.0.18 → 0.0.20

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,76 @@ 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
+ this.root = root;
51
+
52
+ var filename = path.posix.join(root, index);
53
+ this.sourcePath = path.posix.dirname(filename);
54
+ this.destination = path.posix.join(root, dest);
30
55
 
31
56
  this.name = name;
32
- this.appEntry = appEntry;
57
+ this.index = index;
33
58
  this.loadables = [];
59
+ this.featureIndexes = [];
34
60
  }
35
61
 
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, '/');
62
+ static fileToPath(filename) {
63
+ filename = fixPath(filename);
64
+ return path.posix.dirname(filename);
65
+ }
42
66
 
43
- return src;
67
+ /**
68
+ * Call this method to generate a path relative to the src directory
69
+ *
70
+ * @param {String} path
71
+ * @returns the path relative to the source root.
72
+ */
73
+ rootPath(path) {
74
+ var idx = path.indexOf(this.sourcePath);
75
+ if (idx === 0) path.splice(0, length(this.sourcePath));
76
+ if (path[0] === '/') path.splice(0, 1);
77
+
78
+ return './' + path;
44
79
  }
45
80
 
46
81
  setHtmlTemplate(template, target) {
@@ -53,24 +88,30 @@ export class App {
53
88
  }
54
89
 
55
90
  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) {;
91
+ var featurePath = path.posix.join(this.sourcePath, src);
92
+ var indexPath = fixPath(path.posix.join(featurePath, 'index.js'))
93
+ var indexExists = await fs.pathExists(indexPath);
94
+ var builderPath = fixPath(path.posix.join(featurePath, 'build/index.js'));
95
+ var builderExists = await fs.pathExists(builderPath);
96
+
97
+ if (builderExists) {
98
+ try {
99
+ var module = await import(builderPath)
61
100
  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);
101
+ await feature.build(this);
102
+ } catch(e) {
103
+ console.error('error while building feature', src);
104
+ console.log(e);
105
+ }
67
106
  }
107
+
108
+ if (indexExists) this.featureIndexes.push(this.rootPath(indexPath));
68
109
  }
69
110
 
70
111
  async buildFeatures() {
71
112
  var features = await this.getFeatures();
72
113
 
73
- for (const feature of features) {
114
+ for (let feature of features) {
74
115
  await(this.loadFeature(feature));
75
116
  }
76
117
  }
@@ -83,21 +124,21 @@ export class App {
83
124
  The start and ready methods will be called on these services
84
125
  */
85
126
  addLoadable(name, src, prefix) {
86
- var dest = path.posix.join(this.basePath, src);
127
+ var dest = path.posix.join(this.sourcePath, src);
87
128
 
88
129
  console.log(dest)
89
130
  this.loadables.push({name, path: dest, prefix});
90
131
  }
91
132
 
92
133
  buildConfiguration() {
93
- var input = [this.appEntry];
134
+ var input = [this.index];
94
135
  this.loadables.forEach(function(path) {
95
136
  input.push(path.path);
96
137
  });
97
138
 
98
139
  this.variables = {
99
140
  pattern: 'main-js',
100
- replacement: `<script type="module" src="${this.appEntry}"></script>`
141
+ replacement: `<script type="module" src="${this.index}"></script>`
101
142
  }
102
143
 
103
144
  var config = {
@@ -111,12 +152,14 @@ export class App {
111
152
  babel({
112
153
  presets: ['@babel/preset-react'],
113
154
  }),
114
- loadables(this.loadables),
155
+ loader(this.loadables),
156
+ features(this.featureIndexes),
115
157
  html({
116
- include: path.join(this.basePath, "**/*.html"),
158
+ include: path.join(this.sourcePath, "**/*.html"),
117
159
  }),
118
160
  styles(),
119
161
  mainHTML({
162
+ root: this.root,
120
163
  template: this.htmlTemplate.template,
121
164
  target: this.htmlTemplate.target,
122
165
  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.20",
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
+
@@ -0,0 +1,34 @@
1
+ function makeSource(features) {
2
+
3
+ var importStatements = '';
4
+ features.forEach(function(feature) {
5
+ importStatements += `import from '${feature}'\n`;
6
+ })
7
+
8
+ var source = `${importStatements}`
9
+
10
+ console.log(source);
11
+ return source;
12
+ }
13
+
14
+
15
+ export default function features(features) {
16
+ return {
17
+ name: 'features',
18
+
19
+ resolveId (source, _, third) {
20
+ if (source === '@polylith/features') {
21
+ return source;
22
+ }
23
+ return null;
24
+ },
25
+
26
+ load (id) {
27
+ if (id === '@polylith/features') {
28
+ return makeSource(features);
29
+ }
30
+ return null;
31
+ }
32
+ };
33
+ }
34
+
@@ -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);