@cmmn/tools 1.9.4 → 1.9.7

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/bundle/bundle.js CHANGED
@@ -5,27 +5,30 @@ import fs from "fs";
5
5
  import path, {relative} from "path";
6
6
 
7
7
  export async function bundle(...options) {
8
- const configOptions = getConfigOptions({
9
- input: options.filter(x => !x.startsWith('-'))[0],
10
- project: options.includes('-b'),
11
- minify: options.includes('--prod'),
12
- devServer: options.includes('--run'),
8
+ const configOptions = await getConfigOptions({
9
+ minify: options.includes('--minify'),
10
+ prod: options.includes('--prod'),
13
11
  stats: options.includes('--stats'),
14
12
  });
15
13
  const configs = configOptions.flatMap(x => new ConfigCreator(x).getConfig());
16
- const contexts = await Promise.all(configs.map(async x =>
17
- [x, await esbuild.context(x)]
18
- ));
14
+ const contexts = [];
15
+ for (let config of configs){
16
+ contexts.push([config, await esbuild.context(config)]);
17
+ }
19
18
 
20
19
  if (options.includes('--watch')) {
21
- for (let [name, context] of contexts) {
20
+ for (let [config, context] of contexts) {
22
21
  await context.watch();
23
22
  }
23
+ console.log('bundled. Continue watching...');
24
24
  }else {
25
25
  const logs = [];
26
26
  for (let [config, context] of contexts) {
27
- const result = await context.rebuild();
28
27
  const project = path.relative(process.cwd(), config.absWorkingDir);
28
+ const result = await context.rebuild().catch(err => {
29
+ err.message = project + ": " + err.message;
30
+ throw err;
31
+ });
29
32
  const name = config.entryPoints[0].out;
30
33
  let log = logs.find(x => x.project === project && x.name === name);
31
34
  if (!log){
@@ -39,86 +42,4 @@ export async function bundle(...options) {
39
42
  }
40
43
  console.table(logs);
41
44
  }
42
- }
43
-
44
- async function runWatching(configs){
45
- let counter = 0;
46
- while(true){
47
- console.log('Check input existence');
48
- const missed = await checkMissed(configs);
49
- if (!missed.length)
50
- break;
51
- console.log('missed files:');
52
- missed.forEach(x => console.log('\t', relative(process.cwd(), x)));
53
- counter = Math.min(++counter, 5);
54
- console.log(`wait ${counter} sec...`);
55
- await new Promise(resolve => setTimeout(resolve, 1000 * counter));
56
- console.clear();
57
- }
58
- const watcher = watch(configs);
59
- watcher.on('event', (event) => {
60
- switch (event.code) {
61
- case 'START':
62
- console.log(`START BUNDLING at ${new Date().toTimeString().substring(0,8)}`);
63
- break;
64
- case 'END':
65
- console.log(`FINISH at ${new Date().toTimeString().substring(0,8)}`);
66
- break;
67
- case 'BUNDLE_START':
68
- for (let key in event.input){
69
- console.log(`\t${key} -> ${event.output}`);
70
- }
71
- break;
72
- case 'BUNDLE_END':
73
- for (let key in event.input){
74
- console.log(`\t\t(${event.duration / 1000}s)`);
75
- }
76
- break;
77
-
78
- case 'ERROR':
79
- switch (event.error.code) {
80
- case 'PARSE_ERROR':
81
- console.warn('Error parsing files:');
82
- console.log(`\t${event.error.parserError.message}`);
83
- console.log(`\tat: ${event.error.id}`);
84
- console.log(`\tline: ${event.error.frame}`);
85
- break;
86
- case 'UNRESOLVED_IMPORT':
87
- console.warn('UNRESOLVED_IMPORT:\t',event.error.message);
88
- break;
89
- case 'MISSING_EXPORT':
90
- console.warn('MISSING_EXPORT: \t', event.error.message);
91
- break;
92
- case 'UNRESOLVED_ENTRY':
93
- console.warn('UNRESOLVED_ENTRY:\t',event.error.message);
94
- watcher.close();
95
- runWatching(configs);
96
- break;
97
- default:
98
- console.warn('Unknown error:', event.error.code);
99
- console.error(event.error);
100
- break;
101
- }
102
- break;
103
- default:
104
- console.warn('WARNING:', event)
105
- }
106
- });
107
- }
108
-
109
- /**
110
- * @param configs {RollupOptions[]}
111
- */
112
- async function checkMissed(configs) {
113
- const missed = [];
114
- for (let config of configs) {
115
- for (let key in config.input) {
116
- try {
117
- await fs.promises.stat(config.input[key]);
118
- }catch (e) {
119
- missed.push(config.input[key]);
120
- }
121
- }
122
- }
123
- return missed;
124
45
  }
@@ -26,6 +26,7 @@ export class ConfigCreator {
26
26
  * platform: string,
27
27
  * dedupe: string[],
28
28
  * target: string
29
+ * prod: boolean,
29
30
  * inject: string
30
31
  * }}
31
32
  */
@@ -117,14 +118,14 @@ export class ConfigCreator {
117
118
  getConfig() {
118
119
  if (this.options.external && typeof this.options.external === "string")
119
120
  this.options.external = [this.options.external]
120
- console.log(this.options.name, this.options);
121
+ // console.log(this.options.name, this.options);
121
122
  return this.modules.flatMap(format => this.platforms.map(platform => ({
122
123
  entryPoints: [
123
124
  { out: this.options.name, in: this.options.input }
124
125
  ],
125
126
  bundle: true,
126
- minify: this.options.minify,
127
- sourcemap: this.options.minify ? false : 'external',
127
+ minify: this.options.minify || this.options.prod,
128
+ sourcemap: this.options.prod ? false : 'external',
128
129
  target: ['chrome88', 'safari14', 'firefox88'],
129
130
  outdir: 'dist/bundle',
130
131
  metafile: true,
@@ -138,9 +139,13 @@ export class ConfigCreator {
138
139
  },
139
140
  platform: platform,
140
141
  tsconfig: 'tsconfig.json',
141
- external: ["*.woff2", "*.woff", ...this.options.external],
142
+ external: [
143
+ "*.woff2", "*.woff",
144
+ ...(platform !== "node" && this.options.minify ? [] : this.options.external)
145
+ ],
142
146
  define: {
143
- 'process.env.NODE_ENV': '"production"'
147
+ 'process.env.NODE_ENV': '"production"',
148
+ PRODUCTION: this.options.prod ? "true": "false"
144
149
  },
145
150
  publicPath: '/',
146
151
  alias: this.options.alias,
@@ -1,6 +1,6 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
- import fg from "fast-glob";
3
+ import {getTSConfig} from "../helpers/getTSConfig.js";
4
4
 
5
5
  function getProjectConfig(rootDir, cmmn, options) {
6
6
  return {
@@ -10,21 +10,27 @@ function getProjectConfig(rootDir, cmmn, options) {
10
10
  };
11
11
  }
12
12
 
13
- function getPackageConfigs(rootDir, options, name = null) {
13
+ async function *getDependencyOrder(rootDir, visited = []) {
14
+ const tsConfig = getTSConfig(rootDir)
15
+ for (let reference of tsConfig.references ?? []){
16
+ const refRoot = path.resolve(rootDir, reference.path);
17
+ if (visited.includes(refRoot))
18
+ continue;
19
+ visited.push(refRoot)
20
+ for await (let dep of await getDependencyOrder(refRoot, visited)){
21
+ yield dep;
22
+ }
23
+ }
24
+ yield rootDir;
25
+ }
26
+
27
+
28
+ async function getPackageConfigs(rootDir, options, name = null, visited = []) {
14
29
  const pckPath = path.join(rootDir, 'package.json');
15
30
  if (!fs.existsSync(pckPath))
16
31
  return [];
17
32
  const results = [];
18
- const pkg = JSON.parse(fs.readFileSync(pckPath));
19
- if (pkg.workspaces){
20
- const dirs = pkg.workspaces.flatMap(pkg => fg.sync([pkg], {
21
- absolute: true,
22
- globstar: true,
23
- onlyDirectories: true,
24
- cwd: rootDir
25
- }));
26
- dirs.forEach(d => results.push(...getPackageConfigs(d, options, name)));
27
- }
33
+ const pkg = JSON.parse(await fs.promises.readFile(pckPath));
28
34
  if (pkg.cmmn) {
29
35
  if (name) {
30
36
  results.push(getProjectConfig(rootDir, pkg.cmmn[name], {
@@ -45,29 +51,12 @@ function getPackageConfigs(rootDir, options, name = null) {
45
51
  return results;
46
52
  }
47
53
 
48
- function getLernaSubPackages(lernaFile, options) {
49
- const config = JSON.parse(fs.readFileSync(lernaFile, 'utf8'));
50
- const packages = config.packages;
51
- const dirs = packages.flatMap(pkg => fg.sync([pkg], {
52
- absolute: true,
53
- globstar: true,
54
- onlyDirectories: true,
55
- cwd: path.dirname(lernaFile)
56
- }));
57
- return dirs.flatMap(dir => getPackageConfigs(dir, options));
58
- }
59
54
 
60
- export function getConfigOptions(options) {
61
- if (!options.input || options.project) {
62
- const rootDir = process.cwd();
63
- const lernaPath = path.join(rootDir, 'lerna.json');
64
- if (fs.existsSync(lernaPath)) {
65
- return getLernaSubPackages(lernaPath, options);
66
- }
67
- return getPackageConfigs(process.cwd(), options);
68
- }
69
- if (!options.input.includes('.') || !fs.existsSync(options.input)) {
70
- return getPackageConfigs(process.cwd(), options, options.input);
55
+ export async function getConfigOptions(options) {
56
+ const result = []
57
+ for await (let project of getDependencyOrder(process.cwd())){
58
+ const configs = await getPackageConfigs(project, options);
59
+ result.push(...configs);
71
60
  }
72
- return [options];
61
+ return result;
73
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmmn/tools",
3
- "version": "1.9.4",
3
+ "version": "1.9.7",
4
4
  "description": "Compilation, bundling, code generator, testing.",
5
5
  "main": "dist/rollup.config.js",
6
6
  "type": "module",
@@ -47,6 +47,7 @@
47
47
  "@types/jest": "27.x.x",
48
48
  "@types/sinon": "10.x.x",
49
49
  "@zerollup/ts-helpers": "1.7.18",
50
+ "dependency-order": "1.1.6",
50
51
  "esbuild": "0.19.5",
51
52
  "esbuild-plugin-less": "1.3.1",
52
53
  "esbuild-register": "3.5.0",
@@ -62,5 +63,5 @@
62
63
  },
63
64
  "author": "",
64
65
  "license": "ISC",
65
- "gitHead": "1602adb5285c0b8a7bd52d5a5d32860cdad4a434"
66
+ "gitHead": "2755b333808bf97f1d9454601c2a2c9a88e4a021"
66
67
  }
package/serve/serve.js CHANGED
@@ -5,8 +5,8 @@ import liveServer from "live-server";
5
5
  import {resolve, moduleResolve} from 'import-meta-resolve';
6
6
  import uri2path from "file-uri-to-path";
7
7
 
8
- export function serve(...options) {
9
- const configs = getConfigOptions({
8
+ export async function serve(...options) {
9
+ const configs = await getConfigOptions({
10
10
  project: options.includes('-b'),
11
11
  });
12
12
  configs.filter(x => x.port).forEach(async (x,i) => {
@@ -1,7 +1,7 @@
1
- import ts from "typescript";
2
1
  import {pathsToModuleNameMapper} from "ts-jest";
2
+ import {getTSCompilerOptions} from "../helpers/getTSConfig.js";
3
3
 
4
- const options = getTSConfig();
4
+ const options = getTSCompilerOptions(process.cwd());
5
5
 
6
6
  export default {
7
7
  transform: {
@@ -30,17 +30,3 @@ export default {
30
30
  }
31
31
 
32
32
 
33
- function getTSConfig() {
34
- const configPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists, 'tsconfig.json');
35
- const readConfigFileResult = ts.readConfigFile(configPath, ts.sys.readFile);
36
- if (readConfigFileResult.error) {
37
- throw new Error(ts.formatDiagnostic(readConfigFileResult.error, formatHost));
38
- }
39
- const jsonConfig = readConfigFileResult.config;
40
- const convertResult = ts.convertCompilerOptionsFromJson(jsonConfig.compilerOptions, './');
41
- if (convertResult.errors && convertResult.errors.length > 0) {
42
- throw new Error(ts.formatDiagnostics(convertResult.errors, formatHost));
43
- }
44
- const compilerOptions = convertResult.options;
45
- return compilerOptions;
46
- }