@cmmn/tools 1.9.3 → 1.9.4

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.
@@ -54,6 +54,13 @@ export class ConfigCreator {
54
54
  return path.join(this.root, this.options.outDir);
55
55
  }
56
56
 
57
+ get importMaps(){
58
+ return JSON.stringify({
59
+ imports: Object.fromEntries(this.options.external
60
+ .map(key => key.replace('*', '/'))
61
+ .map(key => [key, `/external/${this.options.alias?.[key] ?? key}`]))
62
+ })
63
+ }
57
64
  getHtmlPlugin(){
58
65
  if (!this.options.html)
59
66
  return [];
@@ -61,21 +68,26 @@ export class ConfigCreator {
61
68
  {
62
69
  name: 'esbuild-html-plugin',
63
70
  setup: (build) => { build.onEnd(async result => {
64
- const inject = [
71
+ if (!result.metafile)
72
+ return;
73
+ const injectStyles = [];
74
+ const injectScripts = [
65
75
  `<script type="importmap">${this.importMaps}</script>`
66
76
  ];
67
77
  for (let [key, value] of Object.entries(result.metafile.outputs)){
68
78
  if (value.entryPoint !== this.options.input) continue;
69
79
  const file = path.relative(this.outDir, path.join(this.root, key));
70
- inject.push(`<script type="module" src="/${file}"/>`)
80
+ injectScripts.push(`<script type="module" src="/${file}"></script>`)
71
81
  if (value.cssBundle) {
72
82
  const file = path.relative(this.outDir, path.join(this.root, value.cssBundle));
73
- inject.push(`<style rel="stylesheet" href="/${file}"/>`)
83
+ injectStyles.push(`<link rel="stylesheet" href="/${file}"></link>`)
74
84
  }
75
85
  const html = await fs.promises.readFile(path.join(this.root, this.options.html));
76
86
  await fs.promises.writeFile(
77
87
  path.join(this.outDir, 'index.html'),
78
- html.toString().replace('</head>', `${inject.map(x => `\t${x}\n`).join('')}</head>`)
88
+ html.toString()
89
+ .replace('</head>', `${injectStyles.map(x => `\t${x}\n`).join('')}</head>`)
90
+ .replace('</body>', `${injectScripts.map(x => `\t${x}\n`).join('')}</body>`)
79
91
  );
80
92
  }
81
93
  }); }
@@ -112,7 +124,7 @@ export class ConfigCreator {
112
124
  ],
113
125
  bundle: true,
114
126
  minify: this.options.minify,
115
- sourcemap: true,
127
+ sourcemap: this.options.minify ? false : 'external',
116
128
  target: ['chrome88', 'safari14', 'firefox88'],
117
129
  outdir: 'dist/bundle',
118
130
  metafile: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmmn/tools",
3
- "version": "1.9.3",
3
+ "version": "1.9.4",
4
4
  "description": "Compilation, bundling, code generator, testing.",
5
5
  "main": "dist/rollup.config.js",
6
6
  "type": "module",
@@ -62,5 +62,5 @@
62
62
  },
63
63
  "author": "",
64
64
  "license": "ISC",
65
- "gitHead": "5b83506f4132e1876b5573f7245d8bc759fcb92d"
65
+ "gitHead": "1602adb5285c0b8a7bd52d5a5d32860cdad4a434"
66
66
  }
package/serve/serve.js CHANGED
@@ -17,10 +17,11 @@ export function serve(...options) {
17
17
  file: 'index.html',
18
18
  port: process.env.PORT ? (+process.env.PORT + i) : x.port,
19
19
  open: false,
20
+ watch: configs.map(x => path.join(x.rootDir, x.outDir ?? 'dist/bundle')).join(','),
20
21
  mount: x.mount && Object.entries(x.mount)
21
- .map(([from, to]) => [from, path.resolve(x.rootDir, to)])
22
- .concat(configs.map(x => [`/external/${x.package}`,
23
- path.join(x.rootDir, x.outDir ?? `dist/bundle/${x.name}.js`)])),
22
+ .map(([from, to]) => [from, path.resolve(x.rootDir, to)]),
23
+ // .concat(configs.map(x => [`/external/${x.package}`,
24
+ // path.join(x.rootDir, x.outDir ?? `dist/bundle/${x.name}.js`)])),
24
25
  proxy: Object.entries(x.proxy ?? {}),
25
26
  middleware: [resolveESModule(x.rootDir, configs)].filter(x => x)
26
27
  });
@@ -36,6 +37,16 @@ function getModuleName(path) {
36
37
 
37
38
  const mappingCache = {};
38
39
 
40
+ async function resolveModule(module, root){
41
+ const conditions = ['browser', 'main', 'module', 'import', 'node', 'default'];
42
+ for (let condition of conditions) {
43
+ try {
44
+ return moduleResolve(module, root, new Set([condition]));
45
+ }catch (e){
46
+ }
47
+ }
48
+ throw new Error(`Failed resolve ${module} from ${root}`);
49
+ }
39
50
  // for resolve modules and files inside modules
40
51
  async function getFileName(moduleName, root) {
41
52
  // console.log(moduleName, root);
@@ -44,21 +55,22 @@ async function getFileName(moduleName, root) {
44
55
  let file;
45
56
  if (moduleName.match('\.[cm]?js')) {
46
57
  const module = moduleName.match(/^(@[^/]+\/)?[^/]+/)[0];
47
- const main = (await moduleResolve(module, root, new Set(['module', 'import', 'browser', 'main', 'node'])));
58
+ const main = await resolveModule(module, root);
48
59
  const file = main.href.replace(new RegExp('node_modules/' + module + '.*$'), 'node_modules/' + moduleName);
49
- console.log(file)
50
60
  return mappingCache[moduleName] = file;
51
61
  } else {
52
- const file = (await moduleResolve(moduleName, root, new Set(['module', 'import', 'browser', 'main', 'node'])));
62
+ const file = await resolveModule(moduleName, root);
53
63
  return mappingCache[moduleName] = file.href;
54
64
  }
55
65
  }
56
66
  const resolveESModule = (rootDir, configs) => async function (req, res, next) {
57
67
  const name = getModuleName(req.url);
58
- if (!name)
59
- return next();
60
- if (configs.some(x => x.package === name))
68
+ if (!name) {
69
+ console.log(`skip ${req.url}`)
61
70
  return next();
71
+ }
72
+ // if (configs.some(x => x.package === name))
73
+ // return next();
62
74
  const referer = req.headers.referer?.substring(req.headers.origin.length);
63
75
  const refererModule = referer && getModuleName(referer);
64
76
  const root = refererModule
@@ -67,7 +79,7 @@ const resolveESModule = (rootDir, configs) => async function (req, res, next) {
67
79
  try {
68
80
  const file = uri2path(await getFileName(name, root));
69
81
  var stat = fs.statSync(file);
70
-
82
+ console.info(`Resolve ${name} to ${file}`)
71
83
  res.writeHead(200, {
72
84
  'Content-Type': 'application/javascript',
73
85
  'Content-Length': stat.size