@cmmn/tools 1.0.3 → 1.1.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 +10 -10
- package/bundle/bundle.js +101 -53
- package/bundle/rollup.config.js +61 -78
- package/package.json +14 -11
- package/absolute-plugin.cjs +0 -36
- package/compile/compile.js +0 -47
package/bin.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import {bundle} from "./bundle/bundle.js";
|
|
4
|
-
import {compile} from "./compile/compile.js";
|
|
5
|
-
|
|
6
|
-
const [action, ...args] = process.argv.slice(2);
|
|
7
|
-
|
|
8
|
-
const actions = {
|
|
9
|
-
bundle, compile
|
|
10
|
-
}
|
|
11
|
-
|
|
2
|
+
|
|
3
|
+
import {bundle} from "./bundle/bundle.js";
|
|
4
|
+
import {compile} from "./compile/compile.js";
|
|
5
|
+
|
|
6
|
+
const [action, ...args] = process.argv.slice(2);
|
|
7
|
+
|
|
8
|
+
const actions = {
|
|
9
|
+
bundle, compile
|
|
10
|
+
}
|
|
11
|
+
|
|
12
12
|
actions[action](...args);
|
package/bundle/bundle.js
CHANGED
|
@@ -1,53 +1,101 @@
|
|
|
1
|
-
import {getConfig} from "./rollup.config.js";
|
|
2
|
-
import {rollup, watch} from "rollup";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
1
|
+
import {getConfig} from "./rollup.config.js";
|
|
2
|
+
import {rollup, watch} from "rollup";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import fg from "fast-glob";
|
|
6
|
+
|
|
7
|
+
function getPackageConfigs(rootDir, options) {
|
|
8
|
+
const results = [];
|
|
9
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(rootDir, 'package.json')));
|
|
10
|
+
for (let name in pkg.cmmn) {
|
|
11
|
+
results.push(...getConfig({
|
|
12
|
+
name,
|
|
13
|
+
outDir: path.join(rootDir, pkg.cmmn[name].output ?? 'dist'),
|
|
14
|
+
input: path.join(rootDir, pkg.cmmn[name].input ?? 'index.ts'),
|
|
15
|
+
minify: options.includes('--prod'),
|
|
16
|
+
devServer: options.includes('--run'),
|
|
17
|
+
stats: options.includes('--stats'),
|
|
18
|
+
module: pkg.cmmn[name].module ?? 'es'
|
|
19
|
+
}))
|
|
20
|
+
}
|
|
21
|
+
return results;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function getLernaSubPackages(lernaFile, options) {
|
|
25
|
+
const config = JSON.parse(fs.readFileSync(lernaFile, 'utf8'));
|
|
26
|
+
const packages = config.packages;
|
|
27
|
+
const dirs = packages.flatMap(pkg => fg.sync([pkg], {
|
|
28
|
+
absolute: true,
|
|
29
|
+
globstar: true,
|
|
30
|
+
onlyDirectories: true,
|
|
31
|
+
cwd: path.dirname(lernaFile)
|
|
32
|
+
}));
|
|
33
|
+
return dirs.flatMap(dir => getPackageConfigs(dir, options));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getConfigs(options) {
|
|
37
|
+
if (options.includes('-b')) {
|
|
38
|
+
const rootDir = process.cwd();
|
|
39
|
+
const lernaPath = path.join(rootDir, 'lerna.json');
|
|
40
|
+
if (fs.existsSync(lernaPath)) {
|
|
41
|
+
return getLernaSubPackages(lernaPath, options);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const input = options.filter(x => !x.startsWith('-'))[0];
|
|
45
|
+
return getConfig({
|
|
46
|
+
input,
|
|
47
|
+
minify: options.includes('--prod'),
|
|
48
|
+
devServer: options.includes('--run'),
|
|
49
|
+
stats: options.includes('--stats'),
|
|
50
|
+
module: 'es'
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function bundle(...options) {
|
|
55
|
+
const configs = getConfigs(options);
|
|
56
|
+
if (!options.includes('--watch')) {
|
|
57
|
+
for (let config of configs) {
|
|
58
|
+
console.log(`1. ${config.input} -> ${config.output.file}`);
|
|
59
|
+
const build = await rollup(config);
|
|
60
|
+
await build.write(config.output);
|
|
61
|
+
console.log('SUCCESS');
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const watcher = watch(configs);
|
|
66
|
+
watcher.on('event', (event) => {
|
|
67
|
+
switch (event.code) {
|
|
68
|
+
case 'START':
|
|
69
|
+
console.clear();
|
|
70
|
+
console.log('START BUNDLING');
|
|
71
|
+
break;
|
|
72
|
+
case 'END':
|
|
73
|
+
console.log('FINISH');
|
|
74
|
+
break;
|
|
75
|
+
case 'BUNDLE_START':
|
|
76
|
+
console.log(`1. ${event.input} -> ${event.output}`);
|
|
77
|
+
break;
|
|
78
|
+
case 'BUNDLE_END':
|
|
79
|
+
console.log(`1. ${event.input} -> ${event.output}, (${event.duration / 1000}s)`);
|
|
80
|
+
break;
|
|
81
|
+
|
|
82
|
+
case 'ERROR':
|
|
83
|
+
switch (event.error.code) {
|
|
84
|
+
case 'PARSE_ERROR':
|
|
85
|
+
console.warn('Error parsing files:');
|
|
86
|
+
console.log(`\t${event.error.parserError.message}`);
|
|
87
|
+
console.log(`\tat: ${event.error.id}`);
|
|
88
|
+
console.log(`\tline: ${event.error.frame}`);
|
|
89
|
+
break;
|
|
90
|
+
case 'UNRESOLVED_IMPORT':
|
|
91
|
+
console.error(event.error.message);
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
console.warn('Unknown error:', event.error.code);
|
|
95
|
+
console.error(event.error);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
package/bundle/rollup.config.js
CHANGED
|
@@ -1,78 +1,61 @@
|
|
|
1
|
-
import commonjs from '@rollup/plugin-commonjs';
|
|
2
|
-
import nodeResolve from '@rollup/plugin-node-resolve';
|
|
3
|
-
import {terser} from "rollup-plugin-terser"
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import serve from 'rollup-plugin-serve'
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}),
|
|
64
|
-
less({
|
|
65
|
-
insert: true
|
|
66
|
-
}),
|
|
67
|
-
string({
|
|
68
|
-
include: /\.(html|svg)$/,
|
|
69
|
-
}),
|
|
70
|
-
...[devServer ? serve({
|
|
71
|
-
open: false,
|
|
72
|
-
contentBase: ['dist', 'assets'],
|
|
73
|
-
port: 3001,
|
|
74
|
-
historyApiFallback: true
|
|
75
|
-
}) : '']
|
|
76
|
-
]
|
|
77
|
-
});
|
|
78
|
-
};
|
|
1
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
2
|
+
import nodeResolve from '@rollup/plugin-node-resolve';
|
|
3
|
+
import {terser} from "rollup-plugin-terser"
|
|
4
|
+
import {visualizer} from 'rollup-plugin-visualizer';
|
|
5
|
+
import styles from "rollup-plugin-styles";
|
|
6
|
+
import {string} from "rollup-plugin-string";
|
|
7
|
+
import serve from 'rollup-plugin-serve'
|
|
8
|
+
import livereload from 'rollup-plugin-livereload'
|
|
9
|
+
|
|
10
|
+
export const getConfig = ({minify, input, devServer, module, stats, name, outDir}) => {
|
|
11
|
+
const output = `${outDir ?? 'dist'}/${name ?? 'index'}-${module}${minify ? '.min' : ''}.js`;
|
|
12
|
+
return [{
|
|
13
|
+
input,
|
|
14
|
+
output: {
|
|
15
|
+
file: output,
|
|
16
|
+
sourcemap: !minify,
|
|
17
|
+
format: module,
|
|
18
|
+
exports: 'auto',
|
|
19
|
+
name: 'global'
|
|
20
|
+
},
|
|
21
|
+
onwarn: function () {
|
|
22
|
+
|
|
23
|
+
},
|
|
24
|
+
// prettier-ignore
|
|
25
|
+
plugins: [
|
|
26
|
+
nodeResolve({
|
|
27
|
+
browser: true,
|
|
28
|
+
dedupe: ['lib0']
|
|
29
|
+
}),
|
|
30
|
+
commonjs({}),
|
|
31
|
+
styles({mode: "emit"}),
|
|
32
|
+
string({
|
|
33
|
+
include: /\.(html|svg|less|css)$/,
|
|
34
|
+
}),
|
|
35
|
+
...(minify ? [terser({})] : []),
|
|
36
|
+
...(devServer ? [
|
|
37
|
+
serve({
|
|
38
|
+
open: false,
|
|
39
|
+
contentBase: ['dist', 'assets'],
|
|
40
|
+
port: 3001,
|
|
41
|
+
historyApiFallback: true
|
|
42
|
+
}), livereload({
|
|
43
|
+
watch: ['dist', 'assets'],
|
|
44
|
+
verbose: false, // Disable console output
|
|
45
|
+
|
|
46
|
+
// other livereload options
|
|
47
|
+
port: 12345,
|
|
48
|
+
delay: 300,
|
|
49
|
+
})] : []),
|
|
50
|
+
...(stats ? [
|
|
51
|
+
visualizer({
|
|
52
|
+
open: true,
|
|
53
|
+
sourcemap: true,
|
|
54
|
+
template: 'treemap',
|
|
55
|
+
brotliSize: true,
|
|
56
|
+
filename: 'dist/stats.html'
|
|
57
|
+
}),
|
|
58
|
+
] : [])
|
|
59
|
+
]
|
|
60
|
+
}];
|
|
61
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cmmn/tools",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "di, base extensions, useful functions",
|
|
5
5
|
"main": "dist/rollup.config.js",
|
|
6
6
|
"type": "module",
|
|
@@ -13,26 +13,29 @@
|
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
15
|
"bin.js",
|
|
16
|
-
"
|
|
17
|
-
"bundle/*",
|
|
18
|
-
"compile/*"
|
|
16
|
+
"bundle/*"
|
|
19
17
|
],
|
|
20
|
-
"peerDependencies": {
|
|
21
|
-
"typescript": "4.4.3"
|
|
22
|
-
},
|
|
23
18
|
"dependencies": {
|
|
19
|
+
"less": "^4",
|
|
20
|
+
"ttypescript": "1.5.13",
|
|
21
|
+
"typescript": "4.4.3",
|
|
22
|
+
"typescript-transform-paths": "^3.3.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
24
25
|
"@rollup/plugin-commonjs": "^21",
|
|
25
26
|
"@rollup/plugin-node-resolve": "^13",
|
|
26
27
|
"@rollup/plugin-typescript": "^8",
|
|
28
|
+
"@web/rollup-plugin-html": "^1.10.1",
|
|
29
|
+
"fast-glob": "^3.2.11",
|
|
27
30
|
"rollup": "^2",
|
|
28
|
-
"rollup-plugin-
|
|
31
|
+
"rollup-plugin-livereload": "^2.0.5",
|
|
29
32
|
"rollup-plugin-serve": "^1.1.0",
|
|
30
33
|
"rollup-plugin-string": "^3.0.0",
|
|
34
|
+
"rollup-plugin-styles": "^4",
|
|
31
35
|
"rollup-plugin-terser": "^7",
|
|
32
|
-
"rollup-plugin-visualizer": "^5.5.4"
|
|
33
|
-
"typescript": "4.4.3"
|
|
36
|
+
"rollup-plugin-visualizer": "^5.5.4"
|
|
34
37
|
},
|
|
35
38
|
"author": "",
|
|
36
39
|
"license": "ISC",
|
|
37
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "5309832cf3397001967465f0508d947cbab2540d"
|
|
38
41
|
}
|
package/absolute-plugin.cjs
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
const ts = require("typescript");
|
|
2
|
-
const path_1 = require("path");
|
|
3
|
-
|
|
4
|
-
function visitRequireNode(importNode) {
|
|
5
|
-
if (importNode.expression.kind == ts.SyntaxKind.Identifier &&
|
|
6
|
-
importNode.expression.escapedText == "require") {
|
|
7
|
-
const file = importNode.arguments[0].text;
|
|
8
|
-
if (/\.(less|css|scss|sass|svg|png|html)/.test(file)) {
|
|
9
|
-
const currentFileName = importNode.getSourceFile().fileName;
|
|
10
|
-
const absolute = path_1.join(path_1.dirname(currentFileName), file);
|
|
11
|
-
return ts.updateCall(importNode, importNode.expression, undefined, [ts.createStringLiteral(absolute)]);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const lessToStringTransformer = function (context) {
|
|
18
|
-
return (sourceFile) => {
|
|
19
|
-
function visitor(node) {
|
|
20
|
-
// if (node && node.kind == ts.SyntaxKind.ImportDeclaration) {
|
|
21
|
-
// return visitImportNode(node as ts.ImportDeclaration);
|
|
22
|
-
// }
|
|
23
|
-
if (node && ts.isCallExpression(node)) {
|
|
24
|
-
const result = visitRequireNode(node);
|
|
25
|
-
if (result)
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
return ts.visitEachChild(node, visitor, context);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return ts.visitEachChild(sourceFile, visitor, context);
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
exports.default = function (program, pluginOptions) {
|
|
35
|
-
return lessToStringTransformer;
|
|
36
|
-
}
|
package/compile/compile.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
export function compile(target, options) {
|
|
4
|
-
const config = {
|
|
5
|
-
declaration: true,
|
|
6
|
-
declarationDir: "dist/typings",
|
|
7
|
-
experimentalDecorators: true,
|
|
8
|
-
emitDecoratorMetadata: true,
|
|
9
|
-
module: ts.ModuleKind.ES2020,
|
|
10
|
-
moduleResolution: ts.ModuleResolutionKind.NodeJs,
|
|
11
|
-
lib: ["lib.es2020.d.ts", "lib.dom.d.ts"],
|
|
12
|
-
noEmitHelpers: false,
|
|
13
|
-
noImplicitAny: true,
|
|
14
|
-
downlevelIteration: true,
|
|
15
|
-
noImplicitOverride: true,
|
|
16
|
-
noImplicitReturns: true,
|
|
17
|
-
outDir: "dist/esm",
|
|
18
|
-
target: ts.ScriptTarget.ES2018
|
|
19
|
-
}
|
|
20
|
-
// Note that there is another overload for `createWatchCompilerHost` that takes
|
|
21
|
-
// a set of root files.
|
|
22
|
-
const host = ts.createWatchCompilerHost(
|
|
23
|
-
[ target ],
|
|
24
|
-
config,
|
|
25
|
-
ts.sys
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
// You can technically override any given hook on the host, though you probably
|
|
29
|
-
// don't need to.
|
|
30
|
-
// Note that we're assuming `origCreateProgram` and `origPostProgramCreate`
|
|
31
|
-
// doesn't use `this` at all.
|
|
32
|
-
const origCreateProgram = host.createProgram;
|
|
33
|
-
host.createProgram = (rootNames, options, host, oldProgram) => {
|
|
34
|
-
console.log("** We're about to create the program! **");
|
|
35
|
-
return origCreateProgram(rootNames, options, host, oldProgram);
|
|
36
|
-
};
|
|
37
|
-
const origPostProgramCreate = host.afterProgramCreate;
|
|
38
|
-
|
|
39
|
-
host.afterProgramCreate = program => {
|
|
40
|
-
console.log("** We finished making the program! **");
|
|
41
|
-
origPostProgramCreate(program);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
// `createWatchProgram` creates an initial program, watches files, and updates
|
|
45
|
-
// the program over time.
|
|
46
|
-
ts.createWatchProgram(host);
|
|
47
|
-
}
|