@cmmn/tools 1.6.1 → 1.6.5

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,255 +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
-
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
+ 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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmmn/tools",
3
- "version": "1.6.1",
3
+ "version": "1.6.5",
4
4
  "description": "Compilation, bundling, code generator, testing.",
5
5
  "main": "dist/rollup.config.js",
6
6
  "type": "module",
@@ -68,10 +68,10 @@
68
68
  "sinon": "10.x.x",
69
69
  "ts-jest": "27.x.x",
70
70
  "ttypescript": "1.5.13",
71
- "typescript": "4.x.x",
71
+ "typescript": "4.6.x",
72
72
  "typescript-transform-paths": "^3.3.1"
73
73
  },
74
74
  "author": "",
75
75
  "license": "ISC",
76
- "gitHead": "85610cde052017550ebf22518bcf134e7bbea65c"
76
+ "gitHead": "2db22d85dccbaad27b5e8ecf64e8f25685328892"
77
77
  }