@cmmn/tools 1.5.0 → 1.6.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.
@@ -1,256 +1,255 @@
1
- import commonjs from '@rollup/plugin-commonjs';
2
- import nodeResolve from '@rollup/plugin-node-resolve';
3
- import image from "rollup-plugin-img";
4
- import {terser} from "rollup-plugin-terser"
5
- import {visualizer} from 'rollup-plugin-visualizer';
6
- import styles from "rollup-plugin-styles";
7
- import {string} from "rollup-plugin-string";
8
- import serve from 'rollup-plugin-serve';
9
- import builtins from 'rollup-plugin-node-builtins';
10
- import livereload from 'rollup-plugin-livereload';
11
- import fs from "fs";
12
- import path from "path";
13
- import html from '@open-wc/rollup-plugin-html';
14
- import json from '@rollup/plugin-json';
15
- import alias from '@rollup/plugin-alias';
16
- import replace from '@rollup/plugin-replace';
17
- import postCSS from "rollup-plugin-postcss"
18
-
19
- import flexbugs from 'postcss-flexbugs-fixes';
20
- /**
21
- * @typedef {import(rollup).RollupOptions} RollupOptions
22
- * @typedef {import(rollup).OutputOptions} OutputOptions
23
- */
24
-
25
- export class ConfigCreator {
26
-
27
- /**
28
- * @type {{
29
- * minify: boolean,
30
- * input: string,
31
- * devServer: boolean,
32
- * module: string,
33
- * external: string[],
34
- * stats: boolean,
35
- * name: string,
36
- * styles: 'modules' | null,
37
- * outDir: string,
38
- * html: string,
39
- * browser: boolean,
40
- * dedupe: string[]
41
- * }}
42
- */
43
- options;
44
-
45
- /**
46
- * @type {string}
47
- */
48
- root = process.cwd();
49
-
50
-
51
- constructor(options) {
52
- this.options = {
53
- ...options
54
- };
55
- }
56
-
57
- setRootDir(rootDir) {
58
- this.root = rootDir;
59
- }
60
-
61
- get outDir() {
62
- return path.join(this.root, this.options.outDir);
63
- }
64
-
65
- getOutputFileName(module, minify) {
66
- switch (module) {
67
- case "cjs":
68
- return `[name]${minify ? '.min' : ''}.cjs`;
69
- case "es":
70
- return `[name]${minify ? '.min' : ''}.js`;
71
- default:
72
- return `[name]-${module}${minify ? '.min' : ''}.js`;
73
- }
74
- }
75
-
76
- /**
77
- *
78
- * @returns {OutputOptions}
79
- */
80
- get output() {
81
- // const output = `${this.options.name ?? 'index'}-${this.options.module}${this.options.minify ? '.min' : ''}.js`;
82
- return this.options.module.split(',').map(module => ({
83
- entryFileNames: this.getOutputFileName(module, this.options.minify),
84
- // file: output,
85
- dir: this.outDir,
86
- sourcemap: true,
87
- format: module,
88
- globals: Array.isArray(this.options.external) ? Object.fromEntries(this.options.external.map(x => [x, x])) : this.options.external,
89
- name: this.options.global ?? 'global',
90
- }));
91
- }
92
-
93
- get html() {
94
- return html({
95
- publicPath: '/',
96
- dir: this.outDir,
97
- template: () => fs.readFileSync(path.join(this.root, this.options.html), 'utf8')
98
- });
99
- }
100
-
101
- get devServer() {
102
- return serve({
103
- open: false,
104
- contentBase: [this.outDir, path.join(this.root, 'assets')],
105
- port: this.options.port ?? 3000,
106
- historyApiFallback: true
107
- });
108
- }
109
-
110
- get livereload() {
111
- return livereload({
112
- watch: [this.outDir, path.join(this.root, 'assets')],
113
- verbose: false, // Disable console output
114
- // other livereload options
115
- port: 12345,
116
- delay: 300,
117
- })
118
- }
119
-
120
- get visualizer() {
121
- return visualizer({
122
- open: true,
123
- sourcemap: true,
124
- template: 'treemap',
125
- brotliSize: true,
126
-
127
- filename: path.join(this.outDir, '/stats.html')
128
- })
129
- }
130
-
131
- get plugins() {
132
- const result = [
133
- replace({
134
- 'process.env.NODE_ENV': JSON.stringify('development'),
135
- preventAssignment: true
136
- }),
137
- nodeResolve({
138
- browser: this.options.browser,
139
- dedupe: this.options.dedupe || []
140
- }),
141
- commonjs({
142
- requireReturnsDefault: "namespace",
143
- dynamicRequireTargets: [
144
- 'node_modules/ulid/*.js'
145
- ]
146
- }),
147
- builtins(),
148
- this.options.styles === 'modules' ? postCSS({
149
- mode: [
150
- "inject",
151
- {container: "head", prepend: true, attributes: {id: "global"}},
152
- ],
153
- plugins: [
154
- flexbugs,
155
- ],
156
- modules: {
157
- root: ''
158
- },
159
- namedExports: false,
160
- autoModules: true,
161
- }) : styles({
162
- mode: "emit"
163
- }),
164
- image({
165
- output: `/assets`, // default the root
166
- extensions: /\.(png|jpg|jpeg|gif)$/, // support png|jpg|jpeg|gif|svg, and it's alse the default value
167
- limit: 8192, // default 8192(8k)
168
- exclude: 'node_modules/**'
169
- }),
170
- string({
171
- include: /\.(html|svg|less)$/,
172
- exclude: /\.module\.css/
173
- }),
174
- json(),
175
-
176
- ];
177
- if (this.options.alias) {
178
- result.unshift(alias({
179
- entries: this.options.alias
180
- }));
181
- console.log(this.options.alias)
182
- }
183
- if (this.options.html || this.options.input.endsWith('.html')) {
184
- result.push(this.html);
185
- }
186
- if (this.options.minify) {
187
- result.push(terser({
188
- module: true,
189
- ecma: 2020,
190
- compress: true,
191
- keep_classnames: false,
192
- keep_fnames: false,
193
- mangle: true,
194
- output: {
195
- comments: false
196
- }
197
- }));
198
- }
199
- if (this.options.devServer) {
200
- result.push(this.devServer, this.livereload);
201
- }
202
- if (this.options.stats) {
203
- result.push(this.visualizer);
204
- }
205
- return result;
206
- }
207
-
208
- getExternals() {
209
- if (!this.options.external)
210
- return [];
211
- if (Array.isArray(this.options.external))
212
- return this.options.external.map(s => new RegExp(s));
213
- return Object.keys(this.options.external).map(s => new RegExp(s));
214
- }
215
-
216
- /**
217
- * @returns {RollupOptions[]}
218
- */
219
- getConfig() {
220
- Object.assign(this.options, {
221
- module: this.options.module || 'es',
222
- external: this.options.external || [],
223
- name: this.options.name || 'index',
224
- outDir: this.options.outDir || 'dist'
225
- });
226
- if (this.options.external && typeof this.options.external === "string")
227
- this.options.external = [this.options.external]
228
- console.log(this.options.name, this.options);
229
- return [{
230
- input: {
231
- [this.options.name]: path.join(this.root, this.options.input)
232
- },
233
- output: this.output,
234
- external: this.getExternals(),
235
- onwarn(warning) {
236
- switch (warning.code) {
237
- case 'CIRCULAR_DEPENDENCY':
238
- return;
239
- case 'THIS_IS_UNDEFINED':
240
- console.log(`${warning.message} at`);
241
- console.log(`\t${warning.id}`);
242
- break;
243
- case 'PLUGIN_WARNING':
244
- console.log(`${warning.message} at`);
245
- console.log(`\t${warning.id}`);
246
- break;
247
- default:
248
- console.warn(`\t${warning.code}(!) ${warning.message}`)
249
- }
250
-
251
- },
252
- plugins: this.plugins,
253
- treeshake: this.options.minify ? "smallest" : "recommended",
254
- }]
255
- }
256
- }
1
+ import commonjs from '@rollup/plugin-commonjs';
2
+ import nodeResolve from '@rollup/plugin-node-resolve';
3
+ import image from "rollup-plugin-img";
4
+ import {terser} from "rollup-plugin-terser"
5
+ import {visualizer} from 'rollup-plugin-visualizer';
6
+ import styles from "rollup-plugin-styles";
7
+ import {string} from "rollup-plugin-string";
8
+ import serve from 'rollup-plugin-serve';
9
+ import builtins from 'rollup-plugin-node-builtins';
10
+ import livereload from 'rollup-plugin-livereload';
11
+ import fs from "fs";
12
+ import path from "path";
13
+ import html from '@open-wc/rollup-plugin-html';
14
+ import json from '@rollup/plugin-json';
15
+ import alias from '@rollup/plugin-alias';
16
+ import replace from '@rollup/plugin-replace';
17
+
18
+ /**
19
+ * @typedef {import(rollup).RollupOptions} RollupOptions
20
+ * @typedef {import(rollup).OutputOptions} OutputOptions
21
+ */
22
+
23
+ export class ConfigCreator {
24
+
25
+ /**
26
+ * @type {{
27
+ * minify: boolean,
28
+ * input: string,
29
+ * devServer: boolean,
30
+ * module: string,
31
+ * external: string[],
32
+ * stats: boolean,
33
+ * name: string,
34
+ * styles: 'modules' | null,
35
+ * outDir: string,
36
+ * html: string,
37
+ * browser: boolean,
38
+ * dedupe: string[]
39
+ * }}
40
+ */
41
+ options;
42
+
43
+ /**
44
+ * @type {string}
45
+ */
46
+ root = process.cwd();
47
+
48
+
49
+ constructor(options) {
50
+ this.options = {
51
+ ...options
52
+ };
53
+ }
54
+
55
+ setRootDir(rootDir) {
56
+ this.root = rootDir;
57
+ }
58
+
59
+ get outDir() {
60
+ return path.join(this.root, this.options.outDir);
61
+ }
62
+
63
+ getOutputFileName(module, minify) {
64
+ switch (module) {
65
+ case "cjs":
66
+ return `[name]${minify ? '.min' : ''}.cjs`;
67
+ case "es":
68
+ return `[name]${minify ? '.min' : ''}.js`;
69
+ default:
70
+ return `[name]-${module}${minify ? '.min' : ''}.js`;
71
+ }
72
+ }
73
+
74
+ /**
75
+ *
76
+ * @returns {OutputOptions}
77
+ */
78
+ get output() {
79
+ // const output = `${this.options.name ?? 'index'}-${this.options.module}${this.options.minify ? '.min' : ''}.js`;
80
+ return this.options.module.split(',').map(module => ({
81
+ entryFileNames: this.getOutputFileName(module, this.options.minify),
82
+ // file: output,
83
+ dir: this.outDir,
84
+ sourcemap: true,
85
+ format: module,
86
+ globals: Array.isArray(this.options.external) ? Object.fromEntries(this.options.external.map(x => [x, x])) : this.options.external,
87
+ name: this.options.global ?? 'global',
88
+ }));
89
+ }
90
+
91
+ get html() {
92
+ return html({
93
+ publicPath: '/',
94
+ dir: this.outDir,
95
+ template: () => fs.readFileSync(path.join(this.root, this.options.html), 'utf8')
96
+ });
97
+ }
98
+
99
+ get devServer() {
100
+ return serve({
101
+ open: false,
102
+ contentBase: [this.outDir, path.join(this.root, 'assets')],
103
+ port: this.options.port ?? 3000,
104
+ historyApiFallback: true
105
+ });
106
+ }
107
+
108
+ get livereload() {
109
+ return livereload({
110
+ watch: [this.outDir, path.join(this.root, 'assets')],
111
+ verbose: false, // Disable console output
112
+ // other livereload options
113
+ port: 12345,
114
+ delay: 300,
115
+ })
116
+ }
117
+
118
+ get visualizer() {
119
+ return visualizer({
120
+ open: true,
121
+ sourcemap: true,
122
+ template: 'treemap',
123
+ brotliSize: true,
124
+
125
+ filename: path.join(this.outDir, '/stats.html')
126
+ })
127
+ }
128
+
129
+ get plugins() {
130
+ const result = [
131
+ replace({
132
+ 'process.env.NODE_ENV': JSON.stringify('development'),
133
+ preventAssignment: true
134
+ }),
135
+ nodeResolve({
136
+ browser: this.options.browser,
137
+ dedupe: this.options.dedupe || []
138
+ }),
139
+ commonjs({
140
+ requireReturnsDefault: "namespace",
141
+ dynamicRequireTargets: [
142
+ 'node_modules/ulid/*.js'
143
+ ]
144
+ }),
145
+ builtins(),
146
+ /*this.options.styles === 'modules' ? postCSS({
147
+ mode: [
148
+ "inject",
149
+ {container: "head", prepend: true, attributes: {id: "global"}},
150
+ ],
151
+ plugins: [
152
+ flexbugs,
153
+ ],
154
+ modules: {
155
+ root: ''
156
+ },
157
+ namedExports: false,
158
+ autoModules: true,
159
+ }) : */
160
+ styles({
161
+ mode: "emit"
162
+ }),
163
+ image({
164
+ output: `/assets`, // default the root
165
+ extensions: /\.(png|jpg|jpeg|gif)$/, // support png|jpg|jpeg|gif|svg, and it's alse the default value
166
+ limit: 8192, // default 8192(8k)
167
+ exclude: 'node_modules/**'
168
+ }),
169
+ string({
170
+ include: /\.(html|svg|less)$/,
171
+ exclude: /\.module\.css/
172
+ }),
173
+ json(),
174
+
175
+ ];
176
+ if (this.options.alias) {
177
+ result.unshift(alias({
178
+ entries: this.options.alias
179
+ }));
180
+ console.log(this.options.alias)
181
+ }
182
+ if (this.options.html || this.options.input.endsWith('.html')) {
183
+ result.push(this.html);
184
+ }
185
+ if (this.options.minify) {
186
+ result.push(terser({
187
+ module: true,
188
+ ecma: 2020,
189
+ compress: true,
190
+ keep_classnames: false,
191
+ keep_fnames: false,
192
+ mangle: true,
193
+ output: {
194
+ comments: false
195
+ }
196
+ }));
197
+ }
198
+ if (this.options.devServer) {
199
+ result.push(this.devServer, this.livereload);
200
+ }
201
+ if (this.options.stats) {
202
+ result.push(this.visualizer);
203
+ }
204
+ return result;
205
+ }
206
+
207
+ getExternals() {
208
+ if (!this.options.external)
209
+ return [];
210
+ if (Array.isArray(this.options.external))
211
+ return this.options.external.map(s => new RegExp(s));
212
+ return Object.keys(this.options.external).map(s => new RegExp(s));
213
+ }
214
+
215
+ /**
216
+ * @returns {RollupOptions[]}
217
+ */
218
+ getConfig() {
219
+ Object.assign(this.options, {
220
+ module: this.options.module || 'es',
221
+ external: this.options.external || [],
222
+ name: this.options.name || 'index',
223
+ outDir: this.options.outDir || 'dist'
224
+ });
225
+ if (this.options.external && typeof this.options.external === "string")
226
+ this.options.external = [this.options.external]
227
+ console.log(this.options.name, this.options);
228
+ return [{
229
+ input: {
230
+ [this.options.name]: path.join(this.root, this.options.input)
231
+ },
232
+ output: this.output,
233
+ external: this.getExternals(),
234
+ onwarn(warning) {
235
+ switch (warning.code) {
236
+ case 'CIRCULAR_DEPENDENCY':
237
+ return;
238
+ case 'THIS_IS_UNDEFINED':
239
+ console.log(`${warning.message} at`);
240
+ console.log(`\t${warning.id}`);
241
+ break;
242
+ case 'PLUGIN_WARNING':
243
+ console.log(`${warning.message} at`);
244
+ console.log(`\t${warning.id}`);
245
+ break;
246
+ default:
247
+ console.warn(`\t${warning.code}(!) ${warning.message}`)
248
+ }
249
+
250
+ },
251
+ plugins: this.plugins,
252
+ treeshake: this.options.minify ? "smallest" : "recommended",
253
+ }]
254
+ }
255
+ }
@@ -1,33 +1,33 @@
1
- import ts from "ttypescript";
2
- import {resolve, relative} from 'path';
3
-
4
- const rootDir = process.cwd();
5
-
6
- export function compile(...flags) {
7
-
8
- const host = ts.createSolutionBuilderWithWatchHost(ts.sys, createProgram);
9
- host.useCaseSensitiveFileNames();
10
-
11
- const builderFactory = flags.includes('--watch') ?
12
- ts.createSolutionBuilderWithWatch :
13
- ts.createSolutionBuilder;
14
-
15
- const builder = builderFactory(host, [rootDir], {
16
- incremental: true,
17
- dry: false
18
- }, {
19
- excludeDirectories: ["node_modules", "dist"],
20
- });
21
- builder.clean(rootDir);
22
- builder.build(rootDir);
23
- }
24
-
25
- function createProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences) {
26
- options.outDir = resolve(options.configFilePath, '../dist/esm');
27
- options.declarationDir = resolve(options.configFilePath, '../dist/typings');
28
- options.baseUrl = resolve(options.configFilePath, '../');
29
- console.log('\t', relative(process.cwd(), options.baseUrl));
30
- return ts.createEmitAndSemanticDiagnosticsBuilderProgram(
31
- rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences
32
- )
33
- }
1
+ import ts from "ttypescript";
2
+ import {resolve, relative} from 'path';
3
+
4
+ const rootDir = process.cwd();
5
+
6
+ export function compile(...flags) {
7
+
8
+ const host = ts.createSolutionBuilderWithWatchHost(ts.sys, createProgram);
9
+ host.useCaseSensitiveFileNames();
10
+
11
+ const builderFactory = flags.includes('--watch') ?
12
+ ts.createSolutionBuilderWithWatch :
13
+ ts.createSolutionBuilder;
14
+
15
+ const builder = builderFactory(host, [rootDir], {
16
+ incremental: true,
17
+ dry: false
18
+ }, {
19
+ excludeDirectories: ["node_modules", "dist"],
20
+ });
21
+ builder.clean(rootDir);
22
+ builder.build(rootDir);
23
+ }
24
+
25
+ function createProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences) {
26
+ options.outDir = resolve(options.configFilePath, '../dist/esm');
27
+ options.declarationDir = resolve(options.configFilePath, '../dist/typings');
28
+ options.baseUrl = resolve(options.configFilePath, '../');
29
+ console.log('\t', relative(process.cwd(), options.baseUrl));
30
+ return ts.createEmitAndSemanticDiagnosticsBuilderProgram(
31
+ rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences
32
+ )
33
+ }