@cmmn/tools 1.2.0 → 1.3.0
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/bin.js +8 -2
- package/bundle/bundle.js +1 -1
- package/bundle/rollup.config.js +46 -22
- package/compile/compile.js +0 -5
- package/compile/tsconfig.json +2 -1
- package/compile/typings.d.ts +4 -0
- package/gen/component.ts.tpl +16 -0
- package/gen/gen.js +27 -0
- package/gen/style.less.tpl +3 -0
- package/gen/template.ts.tpl +8 -0
- package/package.json +11 -9
- package/plugins/absolute-plugin.cjs +56 -10
package/bin.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import {bundle} from "./bundle/bundle.js";
|
|
4
4
|
import {compile} from "./compile/compile.js";
|
|
@@ -10,4 +10,10 @@ const actions = {
|
|
|
10
10
|
bundle, compile, gen
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
actions
|
|
13
|
+
if (action in actions) {
|
|
14
|
+
actions[action](...args);
|
|
15
|
+
} else {
|
|
16
|
+
console.log(`cmmn bundle [-b] [index.ts] [--watch] [--run] [--prod]`);
|
|
17
|
+
console.log(`cmmn compile [-b] [--watch]`);
|
|
18
|
+
console.log(`cmmn gen AppRoot . [--nested]`);
|
|
19
|
+
}
|
package/bundle/bundle.js
CHANGED
|
@@ -53,7 +53,7 @@ function getConfigs(options) {
|
|
|
53
53
|
}
|
|
54
54
|
return getPackageConfigs(process.cwd(), options);
|
|
55
55
|
}
|
|
56
|
-
if (options.input
|
|
56
|
+
if (!options.input.includes('.') || !fs.existsSync(options.input)) {
|
|
57
57
|
return getPackageConfigs(process.cwd(), options, options.input);
|
|
58
58
|
}
|
|
59
59
|
const creator = new ConfigCreator(options);
|
package/bundle/rollup.config.js
CHANGED
|
@@ -3,13 +3,15 @@ import nodeResolve from '@rollup/plugin-node-resolve';
|
|
|
3
3
|
import {terser} from "rollup-plugin-terser"
|
|
4
4
|
import {visualizer} from 'rollup-plugin-visualizer';
|
|
5
5
|
import styles from "rollup-plugin-styles";
|
|
6
|
-
import builtins from "rollup-plugin-node-builtins";
|
|
7
6
|
import {string} from "rollup-plugin-string";
|
|
8
7
|
import serve from 'rollup-plugin-serve'
|
|
9
8
|
import livereload from 'rollup-plugin-livereload'
|
|
10
9
|
import fs from "fs";
|
|
11
10
|
import path from "path";
|
|
12
|
-
import html
|
|
11
|
+
import html from '@open-wc/rollup-plugin-html';
|
|
12
|
+
import json from '@rollup/plugin-json';
|
|
13
|
+
import alias from '@rollup/plugin-alias';
|
|
14
|
+
|
|
13
15
|
/**
|
|
14
16
|
* @typedef {import(rollup).RollupOptions} RollupOptions
|
|
15
17
|
* @typedef {import(rollup).OutputOptions} OutputOptions
|
|
@@ -28,6 +30,7 @@ export class ConfigCreator {
|
|
|
28
30
|
* name: string,
|
|
29
31
|
* outDir: string,
|
|
30
32
|
* html: string,
|
|
33
|
+
* browser: boolean,
|
|
31
34
|
* dedupe: string[]
|
|
32
35
|
* }}
|
|
33
36
|
*/
|
|
@@ -45,47 +48,59 @@ export class ConfigCreator {
|
|
|
45
48
|
};
|
|
46
49
|
}
|
|
47
50
|
|
|
48
|
-
setRootDir(rootDir){
|
|
51
|
+
setRootDir(rootDir) {
|
|
49
52
|
this.root = rootDir;
|
|
50
53
|
}
|
|
51
54
|
|
|
52
|
-
get outDir(){
|
|
55
|
+
get outDir() {
|
|
53
56
|
return path.join(this.root, this.options.outDir);
|
|
54
57
|
}
|
|
55
58
|
|
|
59
|
+
getOutputFileName(module, minify){
|
|
60
|
+
switch (module){
|
|
61
|
+
case "cjs":
|
|
62
|
+
return `[name]${minify ? '.min' : ''}.cjs`;
|
|
63
|
+
case "es":
|
|
64
|
+
return `[name]${minify ? '.min' : ''}.js`;
|
|
65
|
+
default:
|
|
66
|
+
return `[name]-${module}${minify ? '.min' : ''}.js`;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
56
70
|
/**
|
|
57
71
|
*
|
|
58
72
|
* @returns {OutputOptions}
|
|
59
73
|
*/
|
|
60
74
|
get output() {
|
|
61
75
|
// const output = `${this.options.name ?? 'index'}-${this.options.module}${this.options.minify ? '.min' : ''}.js`;
|
|
62
|
-
return {
|
|
63
|
-
entryFileNames:
|
|
76
|
+
return this.options.module.split(',').map(module => ({
|
|
77
|
+
entryFileNames: this.getOutputFileName(module, this.options.minify),
|
|
64
78
|
// file: output,
|
|
65
79
|
dir: this.outDir,
|
|
66
|
-
sourcemap:
|
|
67
|
-
format:
|
|
80
|
+
sourcemap: true,
|
|
81
|
+
format: module,
|
|
68
82
|
name: 'global',
|
|
69
|
-
};
|
|
83
|
+
}));
|
|
70
84
|
}
|
|
71
85
|
|
|
72
|
-
get html(){
|
|
86
|
+
get html() {
|
|
73
87
|
return html({
|
|
74
88
|
publicPath: '/',
|
|
75
89
|
dir: this.outDir,
|
|
76
90
|
template: () => fs.readFileSync(path.join(this.root, this.options.html), 'utf8')
|
|
77
91
|
});
|
|
78
92
|
}
|
|
79
|
-
|
|
93
|
+
|
|
94
|
+
get devServer() {
|
|
80
95
|
return serve({
|
|
81
96
|
open: false,
|
|
82
97
|
contentBase: [this.outDir, path.join(this.root, 'assets')],
|
|
83
|
-
port:
|
|
98
|
+
port: this.options.port ?? 3000,
|
|
84
99
|
historyApiFallback: true
|
|
85
100
|
});
|
|
86
101
|
}
|
|
87
102
|
|
|
88
|
-
get livereload(){
|
|
103
|
+
get livereload() {
|
|
89
104
|
return livereload({
|
|
90
105
|
watch: [this.outDir, path.join(this.root, 'assets')],
|
|
91
106
|
verbose: false, // Disable console output
|
|
@@ -95,25 +110,26 @@ export class ConfigCreator {
|
|
|
95
110
|
})
|
|
96
111
|
}
|
|
97
112
|
|
|
98
|
-
get visualizer(){
|
|
113
|
+
get visualizer() {
|
|
99
114
|
return visualizer({
|
|
100
115
|
open: true,
|
|
101
116
|
sourcemap: true,
|
|
102
117
|
template: 'treemap',
|
|
103
118
|
brotliSize: true,
|
|
119
|
+
|
|
104
120
|
filename: path.join(this.outDir, '/stats.html')
|
|
105
121
|
})
|
|
106
122
|
}
|
|
107
123
|
|
|
108
124
|
get plugins() {
|
|
109
125
|
const result = [
|
|
110
|
-
builtins(),
|
|
126
|
+
// builtins(),
|
|
111
127
|
nodeResolve({
|
|
112
|
-
browser:
|
|
128
|
+
browser: this.options.browser,
|
|
113
129
|
dedupe: this.options.dedupe || []
|
|
114
130
|
}),
|
|
115
131
|
commonjs({
|
|
116
|
-
|
|
132
|
+
requireReturnsDefault: "namespace",
|
|
117
133
|
}),
|
|
118
134
|
styles({
|
|
119
135
|
mode: "emit",
|
|
@@ -121,8 +137,16 @@ export class ConfigCreator {
|
|
|
121
137
|
string({
|
|
122
138
|
include: /\.(html|svg|less|css)$/,
|
|
123
139
|
}),
|
|
140
|
+
json(),
|
|
141
|
+
|
|
124
142
|
];
|
|
125
|
-
if (this.options.
|
|
143
|
+
if (this.options.alias) {
|
|
144
|
+
result.unshift(alias({
|
|
145
|
+
entries: this.options.alias
|
|
146
|
+
}));
|
|
147
|
+
console.log(this.options.alias)
|
|
148
|
+
}
|
|
149
|
+
if (this.options.html || this.options.input.endsWith('.html')) {
|
|
126
150
|
result.push(this.html);
|
|
127
151
|
}
|
|
128
152
|
if (this.options.minify) {
|
|
@@ -141,7 +165,7 @@ export class ConfigCreator {
|
|
|
141
165
|
if (this.options.devServer) {
|
|
142
166
|
result.push(this.devServer, this.livereload);
|
|
143
167
|
}
|
|
144
|
-
if (this.options.stats){
|
|
168
|
+
if (this.options.stats) {
|
|
145
169
|
result.push(this.visualizer);
|
|
146
170
|
}
|
|
147
171
|
return result;
|
|
@@ -151,7 +175,7 @@ export class ConfigCreator {
|
|
|
151
175
|
* @returns {RollupOptions[]}
|
|
152
176
|
*/
|
|
153
177
|
getConfig() {
|
|
154
|
-
Object.assign(this.options,{
|
|
178
|
+
Object.assign(this.options, {
|
|
155
179
|
module: this.options.module || 'es',
|
|
156
180
|
external: this.options.external || [],
|
|
157
181
|
name: this.options.name || 'index',
|
|
@@ -162,7 +186,7 @@ export class ConfigCreator {
|
|
|
162
186
|
console.log(this.options);
|
|
163
187
|
return [{
|
|
164
188
|
input: {
|
|
165
|
-
[this.options.name]: path.join(this.root,this.options.input)
|
|
189
|
+
[this.options.name]: path.join(this.root, this.options.input)
|
|
166
190
|
},
|
|
167
191
|
output: this.output,
|
|
168
192
|
external: (this.options.external || []).map(s => new RegExp(s)),
|
|
@@ -177,7 +201,7 @@ export class ConfigCreator {
|
|
|
177
201
|
console.warn(`(!) ${warning.message}`)
|
|
178
202
|
},
|
|
179
203
|
plugins: this.plugins,
|
|
180
|
-
treeshake: this.options.minify ? "smallest" : "recommended"
|
|
204
|
+
treeshake: this.options.minify ? "smallest" : "recommended",
|
|
181
205
|
}]
|
|
182
206
|
}
|
|
183
207
|
}
|
package/compile/compile.js
CHANGED
|
@@ -18,13 +18,8 @@ export function compile(...flags) {
|
|
|
18
18
|
}, {
|
|
19
19
|
excludeDirectories: ["node_modules", "dist"]
|
|
20
20
|
});
|
|
21
|
-
if (flags.includes('-b')) {
|
|
22
|
-
builder.cleanReferences(rootDir);
|
|
23
|
-
builder.buildReferences(rootDir);
|
|
24
|
-
} else {
|
|
25
21
|
builder.clean(rootDir);
|
|
26
22
|
builder.build(rootDir);
|
|
27
|
-
}
|
|
28
23
|
}
|
|
29
24
|
|
|
30
25
|
function createProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences) {
|
package/compile/tsconfig.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"module": "
|
|
3
|
+
"module": "esnext",
|
|
4
4
|
"moduleResolution": "Node",
|
|
5
5
|
"target": "ES2019",
|
|
6
6
|
"composite": true,
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"declarationDir": "./dist/typings",
|
|
19
19
|
"declaration": true,
|
|
20
20
|
"types": [
|
|
21
|
+
"@cmmn/tools"
|
|
21
22
|
],
|
|
22
23
|
"lib": [
|
|
23
24
|
"ES2020.BigInt",
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {component, HtmlComponent, property} from "@cmmn/ui";
|
|
2
|
+
import {template, IState, IEvents} from "./$name$.template";
|
|
3
|
+
import style from "./$name$.style.less";
|
|
4
|
+
import {Injectable} from "@cmmn/core";
|
|
5
|
+
|
|
6
|
+
@Injectable(true)
|
|
7
|
+
@component({name: '$name$', template, style})
|
|
8
|
+
export class $Name$Component extends HtmlComponent<IState, IEvents> {
|
|
9
|
+
|
|
10
|
+
@property()
|
|
11
|
+
private property!: any;
|
|
12
|
+
|
|
13
|
+
get State() {
|
|
14
|
+
return this.property;
|
|
15
|
+
}
|
|
16
|
+
}
|
package/gen/gen.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import {fileURLToPath} from 'url';
|
|
4
|
+
import {execSync} from "child_process";
|
|
5
|
+
|
|
6
|
+
const currentDir = '__dirname' in global ? __dirname : path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
|
|
8
|
+
const templateTpl = fs.readFileSync(path.join(currentDir, './template.ts.tpl'), 'utf8');
|
|
9
|
+
const componentTpl = fs.readFileSync(path.join(currentDir, './component.ts.tpl'), 'utf8');
|
|
10
|
+
const styleTpl = fs.readFileSync(path.join(currentDir, './style.less.tpl'), 'utf8');
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export function gen(name, directory, nested = false) {
|
|
14
|
+
const Name = name.replace(/^./, c => c.toUpperCase());
|
|
15
|
+
name = Name.replace(/[A-Z]/g, (c, i) => (i ? '-' : '') + c.toLowerCase());
|
|
16
|
+
process.chdir(directory);
|
|
17
|
+
if (nested) {
|
|
18
|
+
fs.mkdirSync(name);
|
|
19
|
+
process.chdir(name);
|
|
20
|
+
}
|
|
21
|
+
fs.writeFileSync(name + '.component.ts', componentTpl.replace(/\$Name\$/g, Name).replace(/\$name\$/g, name), 'utf8');
|
|
22
|
+
fs.writeFileSync(name + '.template.ts', templateTpl.replace(/\$Name\$/g, Name).replace(/\$name\$/g, name), 'utf8');
|
|
23
|
+
fs.writeFileSync(name + '.style.less', styleTpl.replace(/\$Name\$/g, Name).replace(/\$name\$/g, name), 'utf8');
|
|
24
|
+
execSync(`git add ${name}.component.ts`);
|
|
25
|
+
execSync(`git add ${name}.template.ts`);
|
|
26
|
+
execSync(`git add ${name}.style.less`);
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cmmn/tools",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "di, base extensions, useful functions",
|
|
5
5
|
"main": "dist/rollup.config.js",
|
|
6
6
|
"type": "module",
|
|
@@ -11,25 +11,24 @@
|
|
|
11
11
|
"bin": {
|
|
12
12
|
"cmmn": "bin.js"
|
|
13
13
|
},
|
|
14
|
+
"typings": "compile/typings.d.ts",
|
|
14
15
|
"files": [
|
|
15
16
|
"bin.js",
|
|
17
|
+
"gen/*",
|
|
16
18
|
"compile/*",
|
|
17
19
|
"bundle/*",
|
|
18
20
|
"plugins/*"
|
|
19
21
|
],
|
|
20
22
|
"dependencies": {
|
|
21
|
-
"less": "^4",
|
|
22
|
-
"ttypescript": "1.5.13",
|
|
23
|
-
"typescript": "4.4.3",
|
|
24
|
-
"typescript-transform-paths": "^3.3.1"
|
|
25
|
-
},
|
|
26
|
-
"devDependencies": {
|
|
27
23
|
"@open-wc/rollup-plugin-html": "^1.2.5",
|
|
24
|
+
"@rollup/plugin-alias": "3",
|
|
28
25
|
"@rollup/plugin-commonjs": "^21",
|
|
26
|
+
"@rollup/plugin-json": "4",
|
|
29
27
|
"@rollup/plugin-node-resolve": "^13",
|
|
30
28
|
"@rollup/plugin-typescript": "^8",
|
|
31
29
|
"@web/rollup-plugin-html": "^1.10.1",
|
|
32
30
|
"fast-glob": "^3.2.11",
|
|
31
|
+
"less": "^4",
|
|
33
32
|
"rollup": "^2",
|
|
34
33
|
"rollup-plugin-livereload": "^2.0.5",
|
|
35
34
|
"rollup-plugin-node-builtins": "^2.1.2",
|
|
@@ -38,9 +37,12 @@
|
|
|
38
37
|
"rollup-plugin-string": "^3.0.0",
|
|
39
38
|
"rollup-plugin-styles": "^4",
|
|
40
39
|
"rollup-plugin-terser": "^7",
|
|
41
|
-
"rollup-plugin-visualizer": "^5.5.4"
|
|
40
|
+
"rollup-plugin-visualizer": "^5.5.4",
|
|
41
|
+
"ttypescript": "1.5.13",
|
|
42
|
+
"typescript": "4.4.3",
|
|
43
|
+
"typescript-transform-paths": "^3.3.1"
|
|
42
44
|
},
|
|
43
45
|
"author": "",
|
|
44
46
|
"license": "ISC",
|
|
45
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "76478b70f43752f9bfe6cb96b4e540b1ff1e52e3"
|
|
46
48
|
}
|
|
@@ -1,13 +1,49 @@
|
|
|
1
1
|
const ts = require("typescript");
|
|
2
2
|
const path = require("path");
|
|
3
|
+
const fs = require("fs");
|
|
3
4
|
|
|
4
|
-
function
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
function visitExportNode(exportNode, sourceFile) {
|
|
6
|
+
if (exportNode.typeOnly){
|
|
7
|
+
console.log('type olnly')
|
|
8
|
+
return ;
|
|
9
|
+
}
|
|
10
|
+
const file = exportNode.moduleSpecifier?.text ?? exportNode.test;
|
|
11
|
+
if (!file)
|
|
12
|
+
return;
|
|
13
|
+
const sourceFileDir = path.dirname(sourceFile.path);
|
|
14
|
+
const abs = path.resolve(sourceFileDir, file);
|
|
15
|
+
if (/\.(less|css|scss|sass|svg|png|html)$/.test(file)) {
|
|
16
|
+
const absSource = path.join(options.outDir, path.relative(options.baseUrl, sourceFile.path));
|
|
17
|
+
const relFile = path.relative(absSource, abs)
|
|
18
|
+
return ts.updateExportDeclaration(exportNode, exportNode.decorators, exportNode.modifiers, exportNode.exportClause, ts.createStringLiteral(relFile), exportNode.typeOnly);
|
|
19
|
+
}
|
|
20
|
+
if (fs.existsSync(abs + '.ts')) {
|
|
21
|
+
return ts.updateExportDeclaration(exportNode, exportNode.decorators, exportNode.modifiers, exportNode.exportClause, ts.createStringLiteral(file + '.js'), exportNode.typeOnly);
|
|
22
|
+
}
|
|
23
|
+
if (fs.existsSync(abs + '/')) {
|
|
24
|
+
const indexFile = `${file}/index.js`;
|
|
25
|
+
return ts.updateExportDeclaration(exportNode, exportNode.decorators, exportNode.modifiers, exportNode.exportClause, ts.createStringLiteral(indexFile), exportNode.typeOnly);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function visitImportNode(importNode, sourceFile, options) {
|
|
30
|
+
const file = importNode.moduleSpecifier?.text;
|
|
31
|
+
if (!file)
|
|
7
32
|
return;
|
|
8
33
|
const sourceFileDir = path.dirname(sourceFile.path);
|
|
9
|
-
const
|
|
10
|
-
|
|
34
|
+
const abs = path.resolve(sourceFileDir, file);
|
|
35
|
+
if (/\.(less|css|scss|sass|svg|png|html)$/.test(file)) {
|
|
36
|
+
const absSource = path.join(options.outDir, path.relative(options.baseUrl, sourceFile.path));
|
|
37
|
+
const relFile = path.relative(absSource, abs)
|
|
38
|
+
return ts.updateImportDeclaration(importNode, importNode.decorators, importNode.modifiers, importNode.importClause, ts.createStringLiteral(relFile));
|
|
39
|
+
}
|
|
40
|
+
if (fs.existsSync(abs + '.ts')) {
|
|
41
|
+
return ts.updateImportDeclaration(importNode, importNode.decorators, importNode.modifiers, importNode.importClause, ts.createStringLiteral(file + '.js'));
|
|
42
|
+
}
|
|
43
|
+
if (fs.existsSync(abs + '/')) {
|
|
44
|
+
const indexFile = `${file}/index.js`;
|
|
45
|
+
return ts.updateImportDeclaration(importNode, importNode.decorators, importNode.modifiers, importNode.importClause, ts.createStringLiteral(indexFile));
|
|
46
|
+
}
|
|
11
47
|
}
|
|
12
48
|
|
|
13
49
|
function visitRequireNode(importNode, sourceFile) {
|
|
@@ -18,24 +54,34 @@ function visitRequireNode(importNode, sourceFile) {
|
|
|
18
54
|
const file = importNode.arguments[0].text;
|
|
19
55
|
if (/\.(less|css|scss|sass|svg|png|html)/.test(file)) {
|
|
20
56
|
const sourceFileDir = path.dirname(sourceFile.path);
|
|
21
|
-
const
|
|
22
|
-
|
|
57
|
+
const abs = path.join(sourceFileDir, file);
|
|
58
|
+
const absSource = path.join(options.outDir, path.relative(options.baseUrl, sourceFile.path));
|
|
59
|
+
const relFile = path.relative(absSource, abs)
|
|
60
|
+
return ts.updateCall(importNode, importNode.expression, undefined, [ts.createStringLiteral(relFile)]);
|
|
23
61
|
}
|
|
24
62
|
}
|
|
25
63
|
|
|
26
64
|
const lessToStringTransformer = function (context) {
|
|
65
|
+
const options = context.getCompilerOptions();
|
|
27
66
|
return (sourceFile) => {
|
|
28
67
|
function visitor(node) {
|
|
29
68
|
// if (node && node.kind == ts.SyntaxKind.ImportDeclaration) {
|
|
30
69
|
// return visitImportNode(node as ts.ImportDeclaration);
|
|
31
70
|
// }
|
|
32
|
-
if (node
|
|
71
|
+
if (!node)
|
|
72
|
+
return ts.visitEachChild(node, visitor, context);
|
|
73
|
+
if (ts.isCallExpression(node)) {
|
|
33
74
|
const result = visitRequireNode(node, sourceFile);
|
|
34
75
|
if (result)
|
|
35
76
|
return result;
|
|
36
77
|
}
|
|
37
|
-
if (
|
|
38
|
-
const result = visitImportNode(node, sourceFile);
|
|
78
|
+
if (ts.isImportDeclaration(node)) {
|
|
79
|
+
const result = visitImportNode(node, sourceFile, options);
|
|
80
|
+
if (result)
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
if (ts.isExportDeclaration(node)) {
|
|
84
|
+
const result = visitExportNode(node, sourceFile);
|
|
39
85
|
if (result)
|
|
40
86
|
return result;
|
|
41
87
|
}
|