@cmmn/tools 1.9.6 → 1.9.9
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 +6 -87
- package/bundle/esbuild.config.js +5 -3
- package/bundle/getConfigs.js +22 -20
- package/helpers/getTSConfig.js +19 -0
- package/package.json +6 -5
- package/test/jest.config.js +2 -16
package/bundle/bundle.js
CHANGED
|
@@ -6,10 +6,8 @@ import path, {relative} from "path";
|
|
|
6
6
|
|
|
7
7
|
export async function bundle(...options) {
|
|
8
8
|
const configOptions = await getConfigOptions({
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
minify: options.includes('--prod'),
|
|
12
|
-
devServer: options.includes('--run'),
|
|
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());
|
|
@@ -26,8 +24,11 @@ export async function bundle(...options) {
|
|
|
26
24
|
}else {
|
|
27
25
|
const logs = [];
|
|
28
26
|
for (let [config, context] of contexts) {
|
|
29
|
-
const result = await context.rebuild();
|
|
30
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
|
+
});
|
|
31
32
|
const name = config.entryPoints[0].out;
|
|
32
33
|
let log = logs.find(x => x.project === project && x.name === name);
|
|
33
34
|
if (!log){
|
|
@@ -41,86 +42,4 @@ export async function bundle(...options) {
|
|
|
41
42
|
}
|
|
42
43
|
console.table(logs);
|
|
43
44
|
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function runWatching(configs){
|
|
47
|
-
let counter = 0;
|
|
48
|
-
while(true){
|
|
49
|
-
console.log('Check input existence');
|
|
50
|
-
const missed = await checkMissed(configs);
|
|
51
|
-
if (!missed.length)
|
|
52
|
-
break;
|
|
53
|
-
console.log('missed files:');
|
|
54
|
-
missed.forEach(x => console.log('\t', relative(process.cwd(), x)));
|
|
55
|
-
counter = Math.min(++counter, 5);
|
|
56
|
-
console.log(`wait ${counter} sec...`);
|
|
57
|
-
await new Promise(resolve => setTimeout(resolve, 1000 * counter));
|
|
58
|
-
console.clear();
|
|
59
|
-
}
|
|
60
|
-
const watcher = watch(configs);
|
|
61
|
-
watcher.on('event', (event) => {
|
|
62
|
-
switch (event.code) {
|
|
63
|
-
case 'START':
|
|
64
|
-
console.log(`START BUNDLING at ${new Date().toTimeString().substring(0,8)}`);
|
|
65
|
-
break;
|
|
66
|
-
case 'END':
|
|
67
|
-
console.log(`FINISH at ${new Date().toTimeString().substring(0,8)}`);
|
|
68
|
-
break;
|
|
69
|
-
case 'BUNDLE_START':
|
|
70
|
-
for (let key in event.input){
|
|
71
|
-
console.log(`\t${key} -> ${event.output}`);
|
|
72
|
-
}
|
|
73
|
-
break;
|
|
74
|
-
case 'BUNDLE_END':
|
|
75
|
-
for (let key in event.input){
|
|
76
|
-
console.log(`\t\t(${event.duration / 1000}s)`);
|
|
77
|
-
}
|
|
78
|
-
break;
|
|
79
|
-
|
|
80
|
-
case 'ERROR':
|
|
81
|
-
switch (event.error.code) {
|
|
82
|
-
case 'PARSE_ERROR':
|
|
83
|
-
console.warn('Error parsing files:');
|
|
84
|
-
console.log(`\t${event.error.parserError.message}`);
|
|
85
|
-
console.log(`\tat: ${event.error.id}`);
|
|
86
|
-
console.log(`\tline: ${event.error.frame}`);
|
|
87
|
-
break;
|
|
88
|
-
case 'UNRESOLVED_IMPORT':
|
|
89
|
-
console.warn('UNRESOLVED_IMPORT:\t',event.error.message);
|
|
90
|
-
break;
|
|
91
|
-
case 'MISSING_EXPORT':
|
|
92
|
-
console.warn('MISSING_EXPORT: \t', event.error.message);
|
|
93
|
-
break;
|
|
94
|
-
case 'UNRESOLVED_ENTRY':
|
|
95
|
-
console.warn('UNRESOLVED_ENTRY:\t',event.error.message);
|
|
96
|
-
watcher.close();
|
|
97
|
-
runWatching(configs);
|
|
98
|
-
break;
|
|
99
|
-
default:
|
|
100
|
-
console.warn('Unknown error:', event.error.code);
|
|
101
|
-
console.error(event.error);
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
break;
|
|
105
|
-
default:
|
|
106
|
-
console.warn('WARNING:', event)
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* @param configs {RollupOptions[]}
|
|
113
|
-
*/
|
|
114
|
-
async function checkMissed(configs) {
|
|
115
|
-
const missed = [];
|
|
116
|
-
for (let config of configs) {
|
|
117
|
-
for (let key in config.input) {
|
|
118
|
-
try {
|
|
119
|
-
await fs.promises.stat(config.input[key]);
|
|
120
|
-
}catch (e) {
|
|
121
|
-
missed.push(config.input[key]);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return missed;
|
|
126
45
|
}
|
package/bundle/esbuild.config.js
CHANGED
|
@@ -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
|
*/
|
|
@@ -123,8 +124,8 @@ export class ConfigCreator {
|
|
|
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.
|
|
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,
|
|
@@ -143,7 +144,8 @@ export class ConfigCreator {
|
|
|
143
144
|
...(platform !== "node" && this.options.minify ? [] : this.options.external)
|
|
144
145
|
],
|
|
145
146
|
define: {
|
|
146
|
-
'process.env.NODE_ENV': '"production"'
|
|
147
|
+
'process.env.NODE_ENV': '"production"',
|
|
148
|
+
PRODUCTION: this.options.prod ? "true": "false"
|
|
147
149
|
},
|
|
148
150
|
publicPath: '/',
|
|
149
151
|
alias: this.options.alias,
|
package/bundle/getConfigs.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
|
-
import
|
|
4
|
-
import { dependencyOrder } from "dependency-order";
|
|
3
|
+
import {getTSConfig} from "../helpers/getTSConfig.js";
|
|
5
4
|
|
|
6
5
|
function getProjectConfig(rootDir, cmmn, options) {
|
|
7
6
|
return {
|
|
@@ -11,15 +10,27 @@ function getProjectConfig(rootDir, cmmn, options) {
|
|
|
11
10
|
};
|
|
12
11
|
}
|
|
13
12
|
|
|
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
|
+
|
|
14
28
|
async function getPackageConfigs(rootDir, options, name = null, visited = []) {
|
|
15
29
|
const pckPath = path.join(rootDir, 'package.json');
|
|
16
30
|
if (!fs.existsSync(pckPath))
|
|
17
31
|
return [];
|
|
18
32
|
const results = [];
|
|
19
|
-
const pkg = JSON.parse(fs.
|
|
20
|
-
const packageInfos = await dependencyOrder({
|
|
21
|
-
cwd: rootDir
|
|
22
|
-
});
|
|
33
|
+
const pkg = JSON.parse(await fs.promises.readFile(pckPath));
|
|
23
34
|
if (pkg.cmmn) {
|
|
24
35
|
if (name) {
|
|
25
36
|
results.push(getProjectConfig(rootDir, pkg.cmmn[name], {
|
|
@@ -37,24 +48,15 @@ async function getPackageConfigs(rootDir, options, name = null, visited = []) {
|
|
|
37
48
|
}
|
|
38
49
|
}
|
|
39
50
|
}
|
|
40
|
-
for (let packageInfo of packageInfos) {
|
|
41
|
-
const root = packageInfo.packageMeta.directory;
|
|
42
|
-
if (visited.includes(root))
|
|
43
|
-
continue;
|
|
44
|
-
visited.push(root)
|
|
45
|
-
const configs = await getPackageConfigs(root, options, name, visited);
|
|
46
|
-
results.push(...configs);
|
|
47
|
-
}
|
|
48
51
|
return results;
|
|
49
52
|
}
|
|
50
53
|
|
|
51
54
|
|
|
52
55
|
export async function getConfigOptions(options) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return await getPackageConfigs(process.cwd(), options, options.input);
|
|
56
|
+
const result = []
|
|
57
|
+
for await (let project of getDependencyOrder(process.cwd())){
|
|
58
|
+
const configs = await getPackageConfigs(project, options);
|
|
59
|
+
result.push(...configs);
|
|
58
60
|
}
|
|
59
|
-
return
|
|
61
|
+
return result;
|
|
60
62
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export function getTSConfig(directory = process.cwd()) {
|
|
4
|
+
const configPath = ts.findConfigFile(directory, ts.sys.fileExists, 'tsconfig.json');
|
|
5
|
+
const readConfigFileResult = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
6
|
+
if (readConfigFileResult.error) {
|
|
7
|
+
throw readConfigFileResult.error;
|
|
8
|
+
}
|
|
9
|
+
return readConfigFileResult.config;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function getTSCompilerOptions(directory = process.cwd()) {
|
|
13
|
+
const jsonConfig = getTSConfig(directory);
|
|
14
|
+
const convertResult = ts.convertCompilerOptionsFromJson(jsonConfig.compilerOptions, './');
|
|
15
|
+
if (convertResult.errors && convertResult.errors.length > 0) {
|
|
16
|
+
throw convertResult.errors;
|
|
17
|
+
}
|
|
18
|
+
return convertResult.options;
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cmmn/tools",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.9",
|
|
4
4
|
"description": "Compilation, bundling, code generator, testing.",
|
|
5
5
|
"main": "dist/rollup.config.js",
|
|
6
6
|
"type": "module",
|
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"scripts": {},
|
|
14
13
|
"bin": {
|
|
15
14
|
"cmmn": "bin.js"
|
|
16
15
|
},
|
|
@@ -38,7 +37,8 @@
|
|
|
38
37
|
"serve/*",
|
|
39
38
|
"plugins/*",
|
|
40
39
|
"spawn/*",
|
|
41
|
-
"test/*"
|
|
40
|
+
"test/*",
|
|
41
|
+
"helpers/*"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@jest/globals": "27.x.x",
|
|
@@ -63,5 +63,6 @@
|
|
|
63
63
|
},
|
|
64
64
|
"author": "",
|
|
65
65
|
"license": "ISC",
|
|
66
|
-
"gitHead": "2755b333808bf97f1d9454601c2a2c9a88e4a021"
|
|
67
|
-
}
|
|
66
|
+
"gitHead": "2755b333808bf97f1d9454601c2a2c9a88e4a021",
|
|
67
|
+
"scripts": {}
|
|
68
|
+
}
|
package/test/jest.config.js
CHANGED
|
@@ -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 =
|
|
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
|
-
}
|