@polylith/builder 0.0.32 → 0.0.33

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
@@ -12,6 +12,7 @@ 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
  import resources from "./plugin-copy-resources.js";
15
+ import jsconfig from "./plugin-jsconfig.js";
15
16
 
16
17
  import ConfigFeature from './ConfigFeature.js';
17
18
  import Files from './Files.js';
@@ -159,7 +160,7 @@ export default class App {
159
160
  * the application source folder.
160
161
  */
161
162
  addFeatureIndex(index) {
162
- this.featureIndexes.push(index);
163
+ this.featureIndexes.push(path.join(this.sourcePath, index));
163
164
  }
164
165
 
165
166
  /**
@@ -326,7 +327,20 @@ export default class App {
326
327
  * ready methods will be called on all services starting with this
327
328
  * prefix.
328
329
  */
329
- addLoadable(name, main, prefix) {
330
+ async addLoadable(name, main, prefix, css) {
331
+ // expand css specs
332
+ var files = new Files(this.sourcePath, this.destPath)
333
+ resourceSpecs.forEach(function(spec) {
334
+ files.addCopySpec(root, spec);
335
+ }, this)
336
+ var cssUris = [];
337
+ var expanded = await files.findAllFiles();
338
+ var keys = Object.keys(expanded);
339
+ keys.forEach(function(key){
340
+ var file = expanded[key];
341
+ cssUris.push(file.uri)
342
+ }, this)
343
+
330
344
  var dest = path.posix.join(this.sourcePath, main);
331
345
  this.loadables.push({name, path: dest, prefix});
332
346
  }
@@ -365,6 +379,7 @@ export default class App {
365
379
  }),
366
380
  loader(this.loadables),
367
381
  features(this.featureIndexes),
382
+ jsconfig(this.root),
368
383
  html({
369
384
  include: path.join(this.sourcePath, "**/*.html"),
370
385
  }),
@@ -427,7 +442,7 @@ export default class App {
427
442
  watch() {
428
443
  var watchConfig = {
429
444
  ...this.config.input,
430
- ...this.config.output,
445
+ output: [this.config.output.output],
431
446
  ...this.config.watch,
432
447
  }
433
448
 
package/ConfigApp.js CHANGED
@@ -3,7 +3,7 @@ import path from 'node:path/posix';
3
3
 
4
4
  export default class ConfigApp extends App {
5
5
  constructor (config, root) {
6
- App.fixPath(root);
6
+ root = App.fixPath(root);
7
7
  var name = config.name || 'unnamed';
8
8
  var index = config.index || path.join(root, 'src', 'index.js');
9
9
  var dest = config.dest || path.join(root, 'dist');
package/ConfigFeature.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import Feature from './Feature.js';
2
+ import path from 'node:path/posix';
2
3
 
3
4
  export default class ConfigFeature extends Feature {
4
5
  /**
@@ -22,12 +23,12 @@ export default class ConfigFeature extends Feature {
22
23
  if (config.loadables && Array.isArray(config.loadables)) {
23
24
  config.loadables.forEach(function(loadable) {
24
25
  if (loadable.name && loadable.index) {
25
- app.addLoadable(loadable.name, loadable.index, loadable.prefix);
26
+ app.addLoadable(loadable.name, path.join(this.root, loadable.index), loadable.prefix, loadable.css);
26
27
  }
27
28
  }, this);
28
29
  }
29
30
 
30
- if (config.index) app.addFeatureIndex(config.index);
31
+ if (config.index) app.addFeatureIndex(path.join(this.root, config.index));
31
32
  if (config.config) app.addConfig(config.config, this.root);
32
33
  if (config.resources && Array.isArray(config.resources)) app.addResources(config.resources, this.root);
33
34
  }
package/Files.js CHANGED
@@ -59,6 +59,28 @@ export default class Files {
59
59
  this.specs.push(spec);
60
60
  }
61
61
 
62
+ /**
63
+ * call this methid to generate the destination filename from the spec and
64
+ * the source filename
65
+ *
66
+ * @param {CopySpec} spec the spec that generate the filename
67
+ * @param {String} srcFilename the source filename
68
+ * @returns
69
+ */
70
+ makeDestination(searchRoot, spec, srcFilename) {
71
+ var fullPath = searchRoot;
72
+ var relativePath = srcFilename.slice(fullPath.length + 1);
73
+ var destFilename = '';
74
+
75
+ if (spec.keepNest) {
76
+ destFilename = path.join(this.dest, spec.dest, relativePath);
77
+ } else {
78
+ destFilename = path.join(this.dest, spec.dest, path.basename(srcFilename));
79
+ }
80
+
81
+ return destFilename;
82
+ }
83
+
62
84
  /**
63
85
  * Call this method once per spec to add a list of files to the complete
64
86
  * list of all the files to be copied. If multiple files are found through
@@ -81,15 +103,37 @@ export default class Files {
81
103
  var copyInfo = this.files[file];
82
104
  if (copyInfo.searchRoot.length > searchRoot.length) return;
83
105
  }
106
+ var destination = this.makeDestination(searchRoot, spec, file);
107
+ var uri = destination.slice(this.dest.length + 1);
84
108
 
85
109
  this.files[file] = {
86
110
  name: file,
111
+ destFilename: destination,
112
+ uri: uri,
87
113
  searchRoot: searchRoot,
88
114
  spec: spec,
89
115
  }
90
116
  }, this)
91
117
  }
92
118
 
119
+ async findFiles(spec) {
120
+ var searchRoot = path.join(this.src, spec.root, spec.cwd);
121
+ var options = {
122
+ cwd: searchRoot,
123
+ ignore: ['**/node_modules'],
124
+ absolute: true,
125
+ onlyFiles: true,
126
+ unique: true,
127
+ dot: true,
128
+ }
129
+ var fullGlob = path.join(searchRoot, spec.glob);
130
+ var files = await fg(fullGlob, options);
131
+
132
+ this.addFiles(searchRoot, files, spec);
133
+
134
+ return this.files;
135
+ }
136
+
93
137
  /**
94
138
  * Call this method to locate all the files to be found from all the copy
95
139
  * specs that have been added
@@ -101,22 +145,10 @@ export default class Files {
101
145
  // using a for loop here because we are making async calls
102
146
  for (let idx = 0; idx < this.specs.length; idx++) {
103
147
  let spec = this.specs[idx];
104
- let searchRoot = path.join(this.src, spec.root, spec.cwd);
105
- let options = {
106
- cwd: searchRoot,
107
- ignore: ['**/node_modules'],
108
- absolute: true,
109
- onlyFiles: true,
110
- unique: true,
111
- dot: true,
112
- }
113
- let fullGlob = path.join(searchRoot, spec.glob);
114
- let files = await fg(fullGlob, options);
115
-
116
- this.addFiles(searchRoot, files, spec);
148
+ await this.findFiles(spec);
117
149
  }
118
150
 
119
- return this.files
151
+ return this.files;
120
152
  }
121
153
 
122
154
  /**
@@ -126,30 +158,18 @@ export default class Files {
126
158
  async copyFiles() {
127
159
  await this.findAllFiles();
128
160
 
129
-
130
161
  var filenames = Object.keys(this.files);
131
162
  // using a for loop because we are making async calls
132
163
  for (let idx = 0 ; idx < filenames.length; idx++) {
133
164
  let srcFilename = filenames[idx];
134
- let spec = this.files[srcFilename].spec;
135
- let searchRoot = this.files[srcFilename].searchRoot;
136
- let fullPath = searchRoot;
137
- let relativePath = srcFilename.slice(fullPath.length + 1);
138
- let destFilename = '';
139
-
140
- if (spec.keepNest) {
141
- destFilename = path.join(this.dest, spec.dest, relativePath);
142
- } else {
143
- destFilename = path.join(this.dest, spec.dest, path.basename(srcFilename));
144
- }
145
-
165
+ let destFilename = this.files[srcFilename].destFilename;
146
166
  var destFilePath = path.dirname(destFilename);
147
167
  // we will override existing destination files. This could have
148
168
  // unintended consequences
149
169
  try {
150
- console.log(`copying ${srcFilename} to ${destFilename}`);
151
- await ensureDir(destFilePath);
152
- await copyFile(srcFilename, destFilename);
170
+ // console.log(`copying ${srcFilename} to ${destFilename}`);
171
+ await ensureDir(destFilePath);
172
+ await copyFile(srcFilename, destFilename);
153
173
  } catch (e) {
154
174
  console.error(`Error copying file ${srcFilename} to ${destFilename}`);
155
175
  throw e;
@@ -0,0 +1,12 @@
1
+ import {App, registry} from '@polylith/core';
2
+
3
+ var loaded = {}
4
+
5
+ export async function load(name) {
6
+ if (loaded[name]) return loaded[name]
7
+ var promise;
8
+
9
+ {{SWITCHES}}
10
+
11
+ loaded[name] = promise;
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polylith/builder",
3
- "version": "0.0.32",
3
+ "version": "0.0.33",
4
4
  "description": "The polylith builder",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -2,11 +2,13 @@ function makeSource(features) {
2
2
 
3
3
  var importStatements = '';
4
4
  features.forEach(function(feature) {
5
- importStatements += `import '${feature}'\n`;
5
+ importStatements += `import ${JSON.stringify(feature)}\n`;
6
6
  })
7
7
 
8
8
  var source = `${importStatements}`
9
9
 
10
+ console.log('@polylith/features');
11
+ console.log(source);
10
12
  return source;
11
13
  }
12
14
 
@@ -0,0 +1,147 @@
1
+ import path from 'node:path/posix';
2
+ import {readFile, writeFile, stat} from 'node:fs/promises';
3
+
4
+ async function fileExists(path) {
5
+ try {
6
+ await stat(path)
7
+ return true;
8
+ } catch (e) {
9
+ return false;
10
+ }
11
+ }
12
+
13
+ function fixPath(src) {
14
+ src = src.replace('file:', '');
15
+ src = src.replace('///', '');
16
+ src = src.replace(/.:/, '');
17
+ src = src.replace(/\\/g, '/');
18
+
19
+ return src;
20
+ }
21
+
22
+
23
+
24
+ /**
25
+ * Matches pattern with a single star against search.
26
+ * Star must match at least one character to be considered a match.
27
+ *
28
+ * @param patttern for example "foo*"
29
+ * @param search for example "fooawesomebar"
30
+ * @returns the part of search that * matches, or undefined if no match.
31
+ */
32
+ function matchStar(pattern, search) {
33
+ if (search.length < pattern.length) {
34
+ return;
35
+ }
36
+ if (pattern === "*") {
37
+ return search;
38
+ }
39
+ const star = pattern.indexOf("*");
40
+
41
+ if (star === -1) {
42
+ return;
43
+ }
44
+
45
+ const part1 = pattern.substring(0, star);
46
+ const part2 = pattern.substring(star + 1);
47
+
48
+ if (search.substr(0, star) !== part1) {
49
+ return;
50
+ }
51
+
52
+ if (search.substr(search.length - part2.length) !== part2) {
53
+ return;
54
+ }
55
+
56
+ return search.substr(star, search.length - part2.length);
57
+ }
58
+
59
+ async function findPathMatch(base, source, paths) {
60
+ var patterns = Object.keys(paths);
61
+ var source = fixPath(source);
62
+
63
+ if (source.indexOf(base) === 0) return
64
+ // source = source.slice(base.length);
65
+
66
+ console.log('>>>', source)
67
+ for(let patternIdx = 0; patternIdx < patterns.length; patternIdx++) {
68
+ let pattern = patterns[patternIdx];
69
+ let searches = paths[pattern];
70
+ let capture = matchStar(pattern, source);
71
+ if (!capture) continue;
72
+
73
+ if (!Array.isArray(searches)) continue;
74
+ for (let searchIdx = 0; searchIdx < searches.length; searchIdx++) {
75
+ var tryName = path.join(base, searches[searchIdx].replace('*', capture));
76
+ console.log('\t..', searches[searchIdx], tryName);
77
+
78
+ if (await fileExists(tryName)) {
79
+ return tryName;
80
+ }
81
+ }
82
+ }
83
+ }
84
+
85
+
86
+ export default function features(root) {
87
+ var jsConfig;
88
+ var basePath;
89
+ var paths;
90
+ var previouslyMatched = {};
91
+
92
+ console.log('====root', root);
93
+ async function readJsConfig() {
94
+ var jsConfigPath = path.join(root, 'jsconfig.json');
95
+ var exists = await fileExists(jsConfigPath);
96
+ if (!exists) {
97
+ jsConfig === false;
98
+ return;
99
+ }
100
+
101
+ try {
102
+ let content = JSON.parse(await readFile(jsConfigPath));
103
+ basePath = content.compilerOptions.baseUrl;
104
+ paths = content.compilerOptions.paths;
105
+ jsConfig = content;
106
+ } catch (e) {
107
+ console.err(e)
108
+ jsConfig = false;
109
+ }
110
+ }
111
+
112
+
113
+ return {
114
+ name: 'features',
115
+
116
+ async resolveId (source, importer, options) {
117
+ source = fixPath(source);
118
+
119
+ // console.log('---', source);
120
+ if (previouslyMatched[source] !== undefined) return previouslyMatched[source];
121
+ previouslyMatched[source] === null;
122
+
123
+ if (jsConfig === undefined) await readJsConfig();
124
+ if (!jsConfig) return null;
125
+
126
+ if (basePath) {
127
+ var tryName = path.join(root, basePath, source);
128
+ if (await fileExists(tryName)) {
129
+ previouslyMatched[source] = tryName;
130
+ return tryName;
131
+ }
132
+ }
133
+ if (paths) {
134
+ if (source.indexOf(root) === 0) return null;
135
+
136
+ var matchedPath = await findPathMatch(path.join(root, basePath), source, paths);
137
+
138
+ if (matchedPath) {
139
+ previouslyMatched[source] = matchedPath;
140
+ return matchedPath;
141
+ }
142
+ }
143
+
144
+ return null;
145
+ }
146
+ }
147
+ }
package/plugin-loader.js CHANGED
@@ -1,15 +1,45 @@
1
- function makeSource(loadables) {
1
+ import path from 'node:path/posix';
2
+ import {readFile, writeFile, stat} from 'node:fs/promises';
3
+ var templateSource;
4
+
5
+ async function fileExists(path) {
6
+ try {
7
+ await stat(path)
8
+ return true;
9
+ } catch (e) {
10
+ return false;
11
+ }
12
+ }
2
13
 
14
+ function makeSource(loadables) {
3
15
  var loadableSwitches = '';
4
16
 
17
+ function makeCss() {
18
+ if (!loadable.css) return '';
19
+ var jsonCss = JSON.stringify(loadable.css);
20
+ return (
21
+ ` App.loadCss(${jsonCss});`
22
+ )
23
+
24
+ }
25
+
26
+ function makeServices() {
27
+ if (!loadable.prefix) return '';
28
+ return (
29
+ ` await registry.start('${loadable.prefix}');`
30
+ )
31
+ }
32
+
5
33
  loadables.forEach(function(loadable) {
6
34
  var prefixStr = ';';
35
+ var cssStr = makeCss();
36
+ var serviceStr = makeServices();
7
37
 
8
- if (loadable.prefix) {
38
+ if (loadable.prefix || loadable.css) {
9
39
  prefixStr = `
10
40
  .then(async function(result) {
11
- await registry.start('${loadable.prefix}');
12
- return result;
41
+ ${cssStr}
42
+ ${serviceStr}
13
43
  });
14
44
  `
15
45
  }
@@ -22,19 +52,9 @@ function makeSource(loadables) {
22
52
  loadableSwitches += switchStr;
23
53
  })
24
54
 
25
- var source =
26
- `
27
- import {registry} from '@polylith/core';
28
-
29
- export async function load(name) {
30
- var promise;
55
+ var source = templateSource;
56
+ var source = source.replace('{{SWITCHES}}', loadableSwitches);
31
57
 
32
- switch (name) {
33
- ${loadableSwitches}
34
- }
35
- return promise;
36
- }
37
- `
38
58
  return source;
39
59
  }
40
60
 
@@ -49,7 +69,10 @@ export default function loader(loadables) {
49
69
  return null;
50
70
  },
51
71
 
52
- load (id) {
72
+ async load (id) {
73
+ if (templateSource) {
74
+ templateSource = await readFile('./loaderTemplate.js');
75
+ }
53
76
  if (id === '@polylith/loader') {
54
77
  return makeSource(loadables);
55
78
  }