@cmmn/tools 1.6.17 → 1.6.21

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,287 +1,287 @@
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 {string} from "rollup-plugin-string";
6
- import serve from 'rollup-plugin-serve';
7
- import builtins from 'rollup-plugin-node-builtins';
8
- import livereload from 'rollup-plugin-livereload';
9
- import fs from "fs";
10
- import path from "path";
11
- import html from '@open-wc/rollup-plugin-html';
12
- import json from '@rollup/plugin-json';
13
- import alias from '@rollup/plugin-alias';
14
- import replace from '@rollup/plugin-replace';
15
- import sourcemaps from 'rollup-plugin-sourcemaps';
16
- import {Styles} from "./styles.js";
17
- /**
18
- * @typedef {import(rollup).RollupOptions} RollupOptions
19
- * @typedef {import(rollup).OutputOptions} OutputOptions
20
- */
21
-
22
- export class ConfigCreator {
23
-
24
- /**
25
- * @type {{
26
- * minify: boolean,
27
- * input: string,
28
- * devServer: boolean,
29
- * module: string,
30
- * external: string[],
31
- * stats: boolean,
32
- * name: string,
33
- * styles: 'modules' | null,
34
- * outDir: string,
35
- * html: string,
36
- * browser: boolean,
37
- * dedupe: string[]
38
- * }}
39
- */
40
- options;
41
-
42
- /**
43
- * @type {string}
44
- */
45
- root = process.cwd();
46
-
47
-
48
- constructor(options) {
49
- this.options = {
50
- module: 'es',
51
- external: [],
52
- name: 'index',
53
- outDir: 'dist/bundle',
54
- ...options
55
- };
56
- if (options.rootDir)
57
- this.root = options.rootDir;
58
- }
59
-
60
- get outDir() {
61
- return path.join(this.root, this.options.outDir);
62
- }
63
-
64
- getOutputFileName(module, minify) {
65
- switch (module) {
66
- case "cjs":
67
- return `[name]${minify ? '.min' : ''}.cjs`;
68
- case "es":
69
- return `[name]${minify ? '.min' : ''}.js`;
70
- default:
71
- return `[name]-${module}${minify ? '.min' : ''}.js`;
72
- }
73
- }
74
-
75
- /**
76
- *
77
- * @returns {OutputOptions}
78
- */
79
- get output() {
80
- // const output = `${this.options.name ?? 'index'}-${this.options.module}${this.options.minify ? '.min' : ''}.js`;
81
- return this.options.module.split(',').map(module => ({
82
- entryFileNames: this.getOutputFileName(module, this.options.minify),
83
- // file: output,
84
- dir: this.outDir,
85
- sourcemap: this.options.minify ? false : 'inline',
86
- format: module,
87
- globals: module === 'umd' ? (Array.isArray(this.options.external) ? Object.fromEntries(this.options.external.map(x => [x, x])) : this.options.external) : [],
88
- assetFileNames: "assets/[name][extname]",
89
- chunkFileNames: "chunks/[name].js",
90
- name: this.options.global ?? 'global',
91
- sourcemapPathTransform: sourceMap => {
92
- const p = path.relative(this.root, path.resolve(this.outDir, sourceMap));
93
- return path.join('/', this.options.package, p);
94
- }
95
- }));
96
- }
97
-
98
- get html() {
99
- return html({
100
- publicPath: '/',
101
- dir: this.outDir,
102
- inject: false,
103
- template: (x) => {
104
- let inject = Object.keys(x.bundle.bundle).map(key => {
105
- if (key.endsWith('css'))
106
- return `<link rel="stylesheet" href="/${key}" >`;
107
- if (key.endsWith('js'))
108
- return `<script type="module" defer src="/${key}"></script>`;
109
- }).join('\n');
110
- if (!this.options.minify) {
111
- const importMaps = Object.fromEntries(this.options.external
112
- .map(key => key.replace('.*', '/'))
113
- .map(key => [key, `/external/${this.options.alias[key] ?? key}`]));
114
- inject = `<script type="importmap" >${JSON.stringify({
115
- imports: importMaps
116
- })}</script>` + inject;
117
- }
118
- const html = fs.readFileSync(path.join(this.root, this.options.html), 'utf8')
119
- return html.replace('</head>', inject + '</head>');
120
- }
121
- });
122
- }
123
-
124
- get devServer() {
125
- return serve({
126
- open: false,
127
- contentBase: [this.outDir, path.join(this.root, 'assets')],
128
- port: this.options.port,
129
- historyApiFallback: true
130
- });
131
- }
132
-
133
- get livereload() {
134
- return livereload({
135
- watch: [this.outDir, path.join(this.root, 'assets'), this.options.html],
136
- verbose: false, // Disable console output
137
- // other livereload options
138
- port: 12345,
139
- delay: 300,
140
- })
141
- }
142
-
143
- get visualizer() {
144
- return visualizer({
145
- open: true,
146
- sourcemap: true,
147
- template: 'treemap',
148
- brotliSize: true,
149
-
150
- filename: path.join(this.outDir, '/stats.html')
151
- })
152
- }
153
-
154
- get plugins() {
155
- const result = [
156
- replace({
157
- 'process.env.NODE_ENV': JSON.stringify(this.options.minify ? 'production' : 'development'),
158
- preventAssignment: true
159
- }),
160
- ...Styles(this.options),
161
- commonjs({
162
- requireReturnsDefault: "namespace",
163
- transformMixedEsModules: true,
164
- defaultIsModuleExports: true
165
- }),
166
- nodeResolve({
167
- browser: this.options.browser,
168
- dedupe: this.options.dedupe || []
169
- }),
170
- sourcemaps(),
171
- builtins(),
172
- /*this.options.styles === 'modules' ? postCSS({
173
- mode: [
174
- "inject",
175
- {container: "head", prepend: true, attributes: {id: "global"}},
176
- ],
177
- plugins: [
178
- flexbugs,
179
- ],
180
- modules: {
181
- root: ''
182
- },
183
- namedExports: false,
184
- autoModules: true,
185
- }) : */
186
- // styles({
187
- // autoModules: true,
188
- // }),
189
-
190
- string({
191
- include: /\.(html|svg|less)$/,
192
- exclude: /\.module\.css/
193
- }),
194
- json(),
195
-
196
- ];
197
- if (this.options.alias) {
198
- result.unshift(alias({
199
- entries: Object.entries(this.options.alias).map(([key, value]) => ({
200
- find: key,
201
- replacement: value
202
- }))
203
- }));
204
- }
205
- if (this.options.html || this.options.input.endsWith('.html')) {
206
- result.push(this.html);
207
- result.push(watcher([path.join(this.root, this.options.html)]))
208
- }
209
- if (this.options.stats) {
210
- result.push(this.visualizer);
211
- }
212
- if (this.options.minify) {
213
- result.push(terser({
214
- module: true,
215
- ecma: 2020,
216
- compress: true,
217
- keep_classnames: false,
218
- keep_fnames: false,
219
- mangle: true,
220
- output: {
221
- comments: false
222
- }
223
- }));
224
- }
225
- if (this.options.devServer && this.options.port) {
226
- result.push(this.devServer, this.livereload);
227
- }
228
- return result;
229
- }
230
-
231
- getExternals() {
232
- if (!this.options.external)
233
- return [];
234
- if (Array.isArray(this.options.external))
235
- return this.options.external.map(s => new RegExp(s));
236
- return Object.keys(this.options.external).map(s => new RegExp(s));
237
- }
238
-
239
- /**
240
- * @returns {RollupOptions[]}
241
- */
242
- getConfig() {
243
- if (this.options.external && typeof this.options.external === "string")
244
- this.options.external = [this.options.external]
245
- console.log(this.options.name, this.options);
246
- return [{
247
- input: {
248
- [this.options.name]: path.join(this.root, this.options.input)
249
- },
250
- output: this.output,
251
- external: (this.options.minify && this.options.browser) ? [] : this.getExternals(),
252
- manualChunks: this.options.chunks,
253
- onwarn(warning) {
254
- switch (warning.code) {
255
- case 'CIRCULAR_DEPENDENCY':
256
- return;
257
- case 'THIS_IS_UNDEFINED':
258
- console.log(`${warning.message} at`);
259
- console.log(`\t${warning.id}`);
260
- break;
261
- case 'PLUGIN_WARNING':
262
- console.log(`${warning.message} at`);
263
- console.log(`\t${warning.id}`);
264
- break;
265
- default:
266
- console.warn(`\t${warning.code}(!) ${warning.message}`)
267
- }
268
-
269
- },
270
- plugins: this.plugins,
271
- treeshake: this.options.minify ? "smallest" : "safest",
272
- watch: {
273
- buildDelay: 300,
274
- clearScreen: false,
275
- exclude: this.getExternals().concat(path.join(this.root, this.outDir)),
276
- }
277
- }]
278
- }
279
- }
280
-
281
- const watcher = (files) => ({
282
- buildStart() {
283
- for (const file of files) {
284
- this.addWatchFile(file);
285
- }
286
- },
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 {string} from "rollup-plugin-string";
6
+ import serve from 'rollup-plugin-serve';
7
+ import builtins from 'rollup-plugin-node-builtins';
8
+ import livereload from 'rollup-plugin-livereload';
9
+ import fs from "fs";
10
+ import path from "path";
11
+ import html from '@open-wc/rollup-plugin-html';
12
+ import json from '@rollup/plugin-json';
13
+ import alias from '@rollup/plugin-alias';
14
+ import replace from '@rollup/plugin-replace';
15
+ import sourcemaps from 'rollup-plugin-sourcemaps';
16
+ import {Styles} from "./styles.js";
17
+ /**
18
+ * @typedef {import(rollup).RollupOptions} RollupOptions
19
+ * @typedef {import(rollup).OutputOptions} OutputOptions
20
+ */
21
+
22
+ export class ConfigCreator {
23
+
24
+ /**
25
+ * @type {{
26
+ * minify: boolean,
27
+ * input: string,
28
+ * devServer: boolean,
29
+ * module: string,
30
+ * external: string[],
31
+ * stats: boolean,
32
+ * name: string,
33
+ * styles: 'modules' | null,
34
+ * outDir: string,
35
+ * html: string,
36
+ * browser: boolean,
37
+ * dedupe: string[]
38
+ * }}
39
+ */
40
+ options;
41
+
42
+ /**
43
+ * @type {string}
44
+ */
45
+ root = process.cwd();
46
+
47
+
48
+ constructor(options) {
49
+ this.options = {
50
+ module: 'es',
51
+ external: [],
52
+ name: 'index',
53
+ outDir: 'dist/bundle',
54
+ ...options
55
+ };
56
+ if (options.rootDir)
57
+ this.root = options.rootDir;
58
+ }
59
+
60
+ get outDir() {
61
+ return path.join(this.root, this.options.outDir);
62
+ }
63
+
64
+ getOutputFileName(module, minify) {
65
+ switch (module) {
66
+ case "cjs":
67
+ return `[name]${minify ? '.min' : ''}.cjs`;
68
+ case "es":
69
+ return `[name]${minify ? '.min' : ''}.js`;
70
+ default:
71
+ return `[name]-${module}${minify ? '.min' : ''}.js`;
72
+ }
73
+ }
74
+
75
+ /**
76
+ *
77
+ * @returns {OutputOptions}
78
+ */
79
+ get output() {
80
+ // const output = `${this.options.name ?? 'index'}-${this.options.module}${this.options.minify ? '.min' : ''}.js`;
81
+ return this.options.module.split(',').map(module => ({
82
+ entryFileNames: this.getOutputFileName(module, this.options.minify),
83
+ // file: output,
84
+ dir: this.outDir,
85
+ sourcemap: this.options.minify ? false : 'inline',
86
+ format: module,
87
+ globals: module === 'umd' ? (Array.isArray(this.options.external) ? Object.fromEntries(this.options.external.map(x => [x, x])) : this.options.external) : [],
88
+ assetFileNames: "assets/[name][extname]",
89
+ chunkFileNames: "chunks/[name].js",
90
+ name: this.options.global ?? 'global',
91
+ sourcemapPathTransform: sourceMap => {
92
+ const p = path.relative(this.root, path.resolve(this.outDir, sourceMap));
93
+ return path.join('/', this.options.package, p);
94
+ }
95
+ }));
96
+ }
97
+
98
+ get html() {
99
+ return html({
100
+ publicPath: '/',
101
+ dir: this.outDir,
102
+ inject: false,
103
+ template: (x) => {
104
+ let inject = Object.keys(x.bundle.bundle).map(key => {
105
+ if (key.endsWith('css'))
106
+ return `<link rel="stylesheet" href="/${key}" >`;
107
+ if (key.endsWith('js'))
108
+ return `<script type="module" defer src="/${key}"></script>`;
109
+ }).join('\n');
110
+ if (!this.options.minify) {
111
+ const importMaps = Object.fromEntries(this.options.external
112
+ .map(key => key.replace('.*', '/'))
113
+ .map(key => [key, `/external/${this.options.alias?.[key] ?? key}`]));
114
+ inject = `<script type="importmap" >${JSON.stringify({
115
+ imports: importMaps
116
+ })}</script>` + inject;
117
+ }
118
+ const html = fs.readFileSync(path.join(this.root, this.options.html), 'utf8')
119
+ return html.replace('</head>', inject + '</head>');
120
+ }
121
+ });
122
+ }
123
+
124
+ get devServer() {
125
+ return serve({
126
+ open: false,
127
+ contentBase: [this.outDir, path.join(this.root, 'assets')],
128
+ port: this.options.port,
129
+ historyApiFallback: true
130
+ });
131
+ }
132
+
133
+ get livereload() {
134
+ return livereload({
135
+ watch: [this.outDir, path.join(this.root, 'assets'), this.options.html],
136
+ verbose: false, // Disable console output
137
+ // other livereload options
138
+ port: 12345,
139
+ delay: 300,
140
+ })
141
+ }
142
+
143
+ get visualizer() {
144
+ return visualizer({
145
+ open: true,
146
+ sourcemap: true,
147
+ template: 'treemap',
148
+ brotliSize: true,
149
+
150
+ filename: path.join(this.outDir, '/stats.html')
151
+ })
152
+ }
153
+
154
+ get plugins() {
155
+ const result = [
156
+ replace({
157
+ 'process.env.NODE_ENV': JSON.stringify(this.options.minify ? 'production' : 'development'),
158
+ preventAssignment: true
159
+ }),
160
+ ...Styles(this),
161
+ commonjs({
162
+ requireReturnsDefault: "namespace",
163
+ transformMixedEsModules: true,
164
+ defaultIsModuleExports: true
165
+ }),
166
+ nodeResolve({
167
+ browser: this.options.browser,
168
+ dedupe: this.options.dedupe || []
169
+ }),
170
+ sourcemaps(),
171
+ builtins(),
172
+ /*this.options.styles === 'modules' ? postCSS({
173
+ mode: [
174
+ "inject",
175
+ {container: "head", prepend: true, attributes: {id: "global"}},
176
+ ],
177
+ plugins: [
178
+ flexbugs,
179
+ ],
180
+ modules: {
181
+ root: ''
182
+ },
183
+ namedExports: false,
184
+ autoModules: true,
185
+ }) : */
186
+ // styles({
187
+ // autoModules: true,
188
+ // }),
189
+
190
+ string({
191
+ include: /\.(html|svg|less)$/,
192
+ exclude: /\.module\.css/
193
+ }),
194
+ json(),
195
+
196
+ ];
197
+ if (this.options.alias) {
198
+ result.unshift(alias({
199
+ entries: Object.entries(this.options.alias).map(([key, value]) => ({
200
+ find: key,
201
+ replacement: value
202
+ }))
203
+ }));
204
+ }
205
+ if (this.options.html || this.options.input.endsWith('.html')) {
206
+ result.push(this.html);
207
+ result.push(watcher([path.join(this.root, this.options.html)]))
208
+ }
209
+ if (this.options.stats) {
210
+ result.push(this.visualizer);
211
+ }
212
+ if (this.options.minify) {
213
+ result.push(terser({
214
+ module: true,
215
+ ecma: 2020,
216
+ compress: true,
217
+ keep_classnames: false,
218
+ keep_fnames: false,
219
+ mangle: true,
220
+ output: {
221
+ comments: false
222
+ }
223
+ }));
224
+ }
225
+ if (this.options.devServer && this.options.port) {
226
+ result.push(this.devServer, this.livereload);
227
+ }
228
+ return result;
229
+ }
230
+
231
+ getExternals() {
232
+ if (!this.options.external)
233
+ return [];
234
+ if (Array.isArray(this.options.external))
235
+ return this.options.external.map(s => new RegExp(s));
236
+ return Object.keys(this.options.external).map(s => new RegExp(s));
237
+ }
238
+
239
+ /**
240
+ * @returns {RollupOptions[]}
241
+ */
242
+ getConfig() {
243
+ if (this.options.external && typeof this.options.external === "string")
244
+ this.options.external = [this.options.external]
245
+ console.log(this.options.name, this.options);
246
+ return [{
247
+ input: {
248
+ [this.options.name]: path.join(this.root, this.options.input)
249
+ },
250
+ output: this.output,
251
+ external: (this.options.minify && this.options.browser) ? [] : this.getExternals(),
252
+ manualChunks: this.options.chunks,
253
+ onwarn(warning) {
254
+ switch (warning.code) {
255
+ case 'CIRCULAR_DEPENDENCY':
256
+ return;
257
+ case 'THIS_IS_UNDEFINED':
258
+ console.log(`${warning.message} at`);
259
+ console.log(`\t${warning.id}`);
260
+ break;
261
+ case 'PLUGIN_WARNING':
262
+ console.log(`${warning.message} at`);
263
+ console.log(`\t${warning.id}`);
264
+ break;
265
+ default:
266
+ console.warn(`\t${warning.code}(!) ${warning.message}`)
267
+ }
268
+
269
+ },
270
+ plugins: this.plugins,
271
+ treeshake: this.options.minify ? "smallest" : "safest",
272
+ watch: {
273
+ buildDelay: 300,
274
+ clearScreen: false,
275
+ exclude: this.getExternals().concat(path.join(this.root, this.outDir)),
276
+ }
277
+ }]
278
+ }
279
+ }
280
+
281
+ const watcher = (files) => ({
282
+ buildStart() {
283
+ for (const file of files) {
284
+ this.addWatchFile(file);
285
+ }
286
+ },
287
287
  });