@cmmn/tools 1.2.3 → 1.4.1
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 +7 -1
- package/bundle/rollup.config.js +23 -8
- package/compile/tsconfig.json +1 -0
- package/compile/typings.d.ts +19 -0
- package/package.json +26 -4
- package/plugins/absolute-plugin.cjs +15 -9
- package/readme.md +26 -6
- package/test/index.d.ts +2 -0
- package/test/index.js +5 -0
- package/test/jest.config.js +46 -0
package/bin.js
CHANGED
|
@@ -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/rollup.config.js
CHANGED
|
@@ -4,8 +4,9 @@ import {terser} from "rollup-plugin-terser"
|
|
|
4
4
|
import {visualizer} from 'rollup-plugin-visualizer';
|
|
5
5
|
import styles from "rollup-plugin-styles";
|
|
6
6
|
import {string} from "rollup-plugin-string";
|
|
7
|
-
import serve from 'rollup-plugin-serve'
|
|
8
|
-
import
|
|
7
|
+
import serve from 'rollup-plugin-serve';
|
|
8
|
+
import builtins from 'rollup-plugin-node-builtins';
|
|
9
|
+
import livereload from 'rollup-plugin-livereload';
|
|
9
10
|
import fs from "fs";
|
|
10
11
|
import path from "path";
|
|
11
12
|
import html from '@open-wc/rollup-plugin-html';
|
|
@@ -56,20 +57,31 @@ export class ConfigCreator {
|
|
|
56
57
|
return path.join(this.root, this.options.outDir);
|
|
57
58
|
}
|
|
58
59
|
|
|
60
|
+
getOutputFileName(module, minify) {
|
|
61
|
+
switch (module) {
|
|
62
|
+
case "cjs":
|
|
63
|
+
return `[name]${minify ? '.min' : ''}.cjs`;
|
|
64
|
+
case "es":
|
|
65
|
+
return `[name]${minify ? '.min' : ''}.js`;
|
|
66
|
+
default:
|
|
67
|
+
return `[name]-${module}${minify ? '.min' : ''}.js`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
59
71
|
/**
|
|
60
72
|
*
|
|
61
73
|
* @returns {OutputOptions}
|
|
62
74
|
*/
|
|
63
75
|
get output() {
|
|
64
76
|
// const output = `${this.options.name ?? 'index'}-${this.options.module}${this.options.minify ? '.min' : ''}.js`;
|
|
65
|
-
return {
|
|
66
|
-
entryFileNames:
|
|
77
|
+
return this.options.module.split(',').map(module => ({
|
|
78
|
+
entryFileNames: this.getOutputFileName(module, this.options.minify),
|
|
67
79
|
// file: output,
|
|
68
80
|
dir: this.outDir,
|
|
69
81
|
sourcemap: true,
|
|
70
|
-
format:
|
|
82
|
+
format: module,
|
|
71
83
|
name: 'global',
|
|
72
|
-
};
|
|
84
|
+
}));
|
|
73
85
|
}
|
|
74
86
|
|
|
75
87
|
get html() {
|
|
@@ -84,7 +96,7 @@ export class ConfigCreator {
|
|
|
84
96
|
return serve({
|
|
85
97
|
open: false,
|
|
86
98
|
contentBase: [this.outDir, path.join(this.root, 'assets')],
|
|
87
|
-
port:
|
|
99
|
+
port: this.options.port ?? 3000,
|
|
88
100
|
historyApiFallback: true
|
|
89
101
|
});
|
|
90
102
|
}
|
|
@@ -112,14 +124,17 @@ export class ConfigCreator {
|
|
|
112
124
|
|
|
113
125
|
get plugins() {
|
|
114
126
|
const result = [
|
|
115
|
-
// builtins(),
|
|
116
127
|
nodeResolve({
|
|
117
128
|
browser: this.options.browser,
|
|
118
129
|
dedupe: this.options.dedupe || []
|
|
119
130
|
}),
|
|
120
131
|
commonjs({
|
|
121
132
|
requireReturnsDefault: "namespace",
|
|
133
|
+
dynamicRequireTargets: [
|
|
134
|
+
'node_modules/ulid/*.js'
|
|
135
|
+
]
|
|
122
136
|
}),
|
|
137
|
+
builtins(),
|
|
123
138
|
styles({
|
|
124
139
|
mode: "emit",
|
|
125
140
|
}),
|
package/compile/tsconfig.json
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare module "*.less"{
|
|
2
|
+
const style: string;
|
|
3
|
+
export default style;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
declare module "*.svg"{
|
|
7
|
+
const style: string;
|
|
8
|
+
export default style;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare module "*.css"{
|
|
12
|
+
const style: string;
|
|
13
|
+
export default style;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare module "*.html"{
|
|
17
|
+
const style: string;
|
|
18
|
+
export default style;
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cmmn/tools",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.4.1",
|
|
4
|
+
"description": "Compilation, bundling, code generator, testing.",
|
|
5
5
|
"main": "dist/rollup.config.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"publishConfig": {
|
|
@@ -11,22 +11,43 @@
|
|
|
11
11
|
"bin": {
|
|
12
12
|
"cmmn": "bin.js"
|
|
13
13
|
},
|
|
14
|
+
"exports": {
|
|
15
|
+
"./test": "./test/index.js",
|
|
16
|
+
"./test/config": "./test/jest.config.js"
|
|
17
|
+
},
|
|
18
|
+
"typesVersions": {
|
|
19
|
+
"*": {
|
|
20
|
+
"test": [
|
|
21
|
+
"./test/index.d.ts"
|
|
22
|
+
],
|
|
23
|
+
".": [
|
|
24
|
+
"./compile/typings.d.ts"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"typings": "compile/typings.d.ts",
|
|
14
29
|
"files": [
|
|
15
30
|
"bin.js",
|
|
16
31
|
"gen/*",
|
|
17
32
|
"compile/*",
|
|
18
33
|
"bundle/*",
|
|
19
|
-
"plugins/*"
|
|
34
|
+
"plugins/*",
|
|
35
|
+
"test/*"
|
|
20
36
|
],
|
|
21
37
|
"dependencies": {
|
|
38
|
+
"@jest/globals": "27.x.x",
|
|
22
39
|
"@open-wc/rollup-plugin-html": "^1.2.5",
|
|
23
40
|
"@rollup/plugin-alias": "3",
|
|
24
41
|
"@rollup/plugin-commonjs": "^21",
|
|
25
42
|
"@rollup/plugin-json": "4",
|
|
26
43
|
"@rollup/plugin-node-resolve": "^13",
|
|
27
44
|
"@rollup/plugin-typescript": "^8",
|
|
45
|
+
"@swc/jest": "0.2.17",
|
|
46
|
+
"@testdeck/jest": "0.2.0",
|
|
47
|
+
"@types/jest": "27.x.x",
|
|
28
48
|
"@web/rollup-plugin-html": "^1.10.1",
|
|
29
49
|
"fast-glob": "^3.2.11",
|
|
50
|
+
"jest": "27.x.x",
|
|
30
51
|
"less": "^4",
|
|
31
52
|
"rollup": "^2",
|
|
32
53
|
"rollup-plugin-livereload": "^2.0.5",
|
|
@@ -37,11 +58,12 @@
|
|
|
37
58
|
"rollup-plugin-styles": "^4",
|
|
38
59
|
"rollup-plugin-terser": "^7",
|
|
39
60
|
"rollup-plugin-visualizer": "^5.5.4",
|
|
61
|
+
"ts-jest": "27.x.x",
|
|
40
62
|
"ttypescript": "1.5.13",
|
|
41
63
|
"typescript": "4.4.3",
|
|
42
64
|
"typescript-transform-paths": "^3.3.1"
|
|
43
65
|
},
|
|
44
66
|
"author": "",
|
|
45
67
|
"license": "ISC",
|
|
46
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "d1405a0626569d43adc867fe660f4df8b5c3b492"
|
|
47
69
|
}
|
|
@@ -13,32 +13,35 @@ function visitExportNode(exportNode, sourceFile) {
|
|
|
13
13
|
const sourceFileDir = path.dirname(sourceFile.path);
|
|
14
14
|
const abs = path.resolve(sourceFileDir, file);
|
|
15
15
|
if (/\.(less|css|scss|sass|svg|png|html)$/.test(file)) {
|
|
16
|
-
|
|
16
|
+
const absSource = path.join(options.outDir, path.relative(options.baseUrl, sourceFileDir));
|
|
17
|
+
const relFile = path.relative(absSource, abs)
|
|
18
|
+
return ts.updateExportDeclaration(exportNode, exportNode.decorators, exportNode.modifiers, exportNode.exportClause, ts.createStringLiteral(relFile), exportNode.typeOnly);
|
|
17
19
|
}
|
|
18
20
|
if (fs.existsSync(abs + '.ts')) {
|
|
19
21
|
return ts.updateExportDeclaration(exportNode, exportNode.decorators, exportNode.modifiers, exportNode.exportClause, ts.createStringLiteral(file + '.js'), exportNode.typeOnly);
|
|
20
22
|
}
|
|
21
23
|
if (fs.existsSync(abs + '/')) {
|
|
22
|
-
const indexFile =
|
|
23
|
-
console.log(sourceFileDir, file, indexFile);
|
|
24
|
+
const indexFile = `${file}/index.js`;
|
|
24
25
|
return ts.updateExportDeclaration(exportNode, exportNode.decorators, exportNode.modifiers, exportNode.exportClause, ts.createStringLiteral(indexFile), exportNode.typeOnly);
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
function visitImportNode(importNode, sourceFile) {
|
|
29
|
+
function visitImportNode(importNode, sourceFile, options) {
|
|
29
30
|
const file = importNode.moduleSpecifier?.text;
|
|
30
31
|
if (!file)
|
|
31
32
|
return;
|
|
32
33
|
const sourceFileDir = path.dirname(sourceFile.path);
|
|
33
34
|
const abs = path.resolve(sourceFileDir, file);
|
|
34
35
|
if (/\.(less|css|scss|sass|svg|png|html)$/.test(file)) {
|
|
35
|
-
|
|
36
|
+
const absSource = path.join(options.outDir, path.relative(options.baseUrl, sourceFileDir));
|
|
37
|
+
const relFile = path.relative(absSource, abs)
|
|
38
|
+
return ts.updateImportDeclaration(importNode, importNode.decorators, importNode.modifiers, importNode.importClause, ts.createStringLiteral(relFile));
|
|
36
39
|
}
|
|
37
40
|
if (fs.existsSync(abs + '.ts')) {
|
|
38
41
|
return ts.updateImportDeclaration(importNode, importNode.decorators, importNode.modifiers, importNode.importClause, ts.createStringLiteral(file + '.js'));
|
|
39
42
|
}
|
|
40
43
|
if (fs.existsSync(abs + '/')) {
|
|
41
|
-
const indexFile =
|
|
44
|
+
const indexFile = `${file}/index.js`;
|
|
42
45
|
return ts.updateImportDeclaration(importNode, importNode.decorators, importNode.modifiers, importNode.importClause, ts.createStringLiteral(indexFile));
|
|
43
46
|
}
|
|
44
47
|
}
|
|
@@ -51,12 +54,15 @@ function visitRequireNode(importNode, sourceFile) {
|
|
|
51
54
|
const file = importNode.arguments[0].text;
|
|
52
55
|
if (/\.(less|css|scss|sass|svg|png|html)/.test(file)) {
|
|
53
56
|
const sourceFileDir = path.dirname(sourceFile.path);
|
|
54
|
-
const
|
|
55
|
-
|
|
57
|
+
const abs = path.join(sourceFileDir, file);
|
|
58
|
+
const absSource = path.join(options.outDir, path.relative(options.baseUrl, sourceFileDir));
|
|
59
|
+
const relFile = path.relative(absSource, abs)
|
|
60
|
+
return ts.updateCall(importNode, importNode.expression, undefined, [ts.createStringLiteral(relFile)]);
|
|
56
61
|
}
|
|
57
62
|
}
|
|
58
63
|
|
|
59
64
|
const lessToStringTransformer = function (context) {
|
|
65
|
+
const options = context.getCompilerOptions();
|
|
60
66
|
return (sourceFile) => {
|
|
61
67
|
function visitor(node) {
|
|
62
68
|
// if (node && node.kind == ts.SyntaxKind.ImportDeclaration) {
|
|
@@ -70,7 +76,7 @@ const lessToStringTransformer = function (context) {
|
|
|
70
76
|
return result;
|
|
71
77
|
}
|
|
72
78
|
if (ts.isImportDeclaration(node)) {
|
|
73
|
-
const result = visitImportNode(node, sourceFile);
|
|
79
|
+
const result = visitImportNode(node, sourceFile, options);
|
|
74
80
|
if (result)
|
|
75
81
|
return result;
|
|
76
82
|
}
|
package/readme.md
CHANGED
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
## Builder and bundler for your projects
|
|
2
2
|
|
|
3
3
|
### Use cases:
|
|
4
|
-
`cmmn compile [target] [-b] [--watch]`
|
|
5
|
-
> Runs typescript compiler
|
|
4
|
+
* `cmmn compile [target] [-b] [--watch]`
|
|
5
|
+
> Runs typescript compiler
|
|
6
6
|
|
|
7
|
-
`cmmn bundle [target] [-b] [--watch] [--run] [--prod]`
|
|
8
|
-
> Runs rollup bundler
|
|
7
|
+
* `cmmn bundle [target] [-b] [--watch] [--run] [--prod]`
|
|
8
|
+
> Runs rollup bundler
|
|
9
9
|
|
|
10
|
-
`cmmn gen name directory [-n]`
|
|
11
|
-
> Generates component with template at directory
|
|
10
|
+
* `cmmn gen name directory [-n]`
|
|
11
|
+
> Generates component with template at directory
|
|
12
|
+
|
|
13
|
+
* default `jest.config.js`:
|
|
14
|
+
```typescript
|
|
15
|
+
import config from "@cmmn/tools/test/config";
|
|
16
|
+
export default config;
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
* tests: `some.spec.ts`
|
|
20
|
+
```typescript
|
|
21
|
+
import {suite, test, expect} from "@cmmn/tools/test";
|
|
22
|
+
|
|
23
|
+
@suite
|
|
24
|
+
export class SomeSpec {
|
|
25
|
+
|
|
26
|
+
@test
|
|
27
|
+
equalsTest() {
|
|
28
|
+
expect(1).toBe(2);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
```
|
package/test/index.d.ts
ADDED
package/test/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import {pathsToModuleNameMapper} from "ts-jest";
|
|
3
|
+
|
|
4
|
+
const options = getTSConfig();
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
transform: {
|
|
8
|
+
'^.+\\.tsx?$': ['@swc/jest', {
|
|
9
|
+
jsc: {
|
|
10
|
+
parser: {
|
|
11
|
+
syntax: "typescript",
|
|
12
|
+
// tsx: true, // If you use react
|
|
13
|
+
dynamicImport: true,
|
|
14
|
+
decorators: true,
|
|
15
|
+
},
|
|
16
|
+
target: "es2021",
|
|
17
|
+
transform: {
|
|
18
|
+
decoratorMetadata: true,
|
|
19
|
+
},
|
|
20
|
+
paths: options.paths,
|
|
21
|
+
baseUrl: '.'
|
|
22
|
+
},
|
|
23
|
+
}],
|
|
24
|
+
},
|
|
25
|
+
roots: [process.cwd()],
|
|
26
|
+
moduleNameMapper: pathsToModuleNameMapper(options.paths ?? {}, {
|
|
27
|
+
prefix: '<rootDir>'
|
|
28
|
+
}),
|
|
29
|
+
extensionsToTreatAsEsm: ['.ts', '.tsx', '.jsx?'],
|
|
30
|
+
}
|
|
31
|
+
|
|
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
|
+
}
|