@cmmn/tools 2.0.1 → 2.0.3

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/bundle/bundle.js CHANGED
@@ -9,6 +9,7 @@ export async function bundle(...options) {
9
9
  minify: options.includes('--minify'),
10
10
  prod: options.includes('--prod'),
11
11
  stats: options.includes('--stats'),
12
+ noSideEffects: options.includes('--no-side-effects')
12
13
  });
13
14
  const configs = configOptions.flatMap(x => new ConfigCreator(x).getConfig());
14
15
  const contexts = [];
@@ -28,6 +28,7 @@ export class ConfigCreator {
28
28
  * target: string
29
29
  * prod: boolean,
30
30
  * inject: string
31
+ * noSideEffects: boolean
31
32
  * }}
32
33
  */
33
34
  options;
@@ -104,10 +105,10 @@ export class ConfigCreator {
104
105
  return this.options.module.split(',');
105
106
  }
106
107
  getOutExtension(format, platform){
107
- const ext = (this.options.minify ? '.min' : '') + {
108
+ const ext = (this.options.minify ? '.min' : '') + ({
108
109
  es: '.js',
109
110
  cjs: '.cjs'
110
- }[format];
111
+ }[format] ?? '.js');
111
112
  if (this.platforms.length == 1)
112
113
  return ext;
113
114
  return "."+platform+ext;
@@ -124,13 +125,13 @@ export class ConfigCreator {
124
125
  { out: this.options.name, in: this.options.input }
125
126
  ],
126
127
  bundle: true,
127
- minify: this.options.minify || this.options.prod,
128
+ minify: this.options.minify,
128
129
  sourcemap: this.options.prod ? false : 'external',
129
130
  target: ['chrome88', 'safari14', 'firefox88'],
130
131
  outdir: 'dist/bundle',
131
132
  metafile: true,
132
133
  absWorkingDir: this.root,
133
- treeShaking: this.options.minify,
134
+ treeShaking: this.options.prod,
134
135
  format: ({
135
136
  es: 'esm'
136
137
  })[format] ?? format,
@@ -156,6 +157,21 @@ export class ConfigCreator {
156
157
  plugins: [
157
158
  lessLoader(),
158
159
  ...this.getHtmlPlugin(),
160
+ ...(this.options.noSideEffects ? [{
161
+ name: 'no-side-effects',
162
+ setup(build){
163
+ build.onResolve({ filter: /.*/ }, async args => {
164
+ if (args.pluginData) return // Ignore this if we called ourselves
165
+
166
+ const { path, ...rest } = args
167
+ rest.pluginData = true // Avoid infinite recursion
168
+ const result = await build.resolve(path, rest)
169
+
170
+ result.sideEffects = false
171
+ return result
172
+ })
173
+ }
174
+ }] : [])
159
175
  ],
160
176
  })));
161
177
  }
@@ -80,12 +80,12 @@ class Visitor {
80
80
  const outFile = path.resolve(absSource, importPath).replaceAll(path.sep, '/') + '.js';
81
81
  fs.mkdirSync(path.dirname(outFile), {recursive: true});
82
82
  fs.writeFileSync(outFile, 'export default `'+content.replaceAll('`','\\`')+'`', 'utf-8');
83
- return importPath+".js";
83
+ return importPath.replaceAll(path.sep, '/') + ".js";
84
84
  }
85
85
  if (this.config.copy.test(importPath)) {
86
- return path.relative(absSource,abs);
86
+ return path.relative(absSource,abs).replaceAll(path.sep, '/');
87
87
  }
88
- return importPath;
88
+ return importPath.replaceAll(path.sep, '/');
89
89
  }
90
90
 
91
91
  visitSourceFile = sourceFile => ts.visitEachChild(sourceFile, node => this.visit(node,sourceFile), this.context);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmmn/tools",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Compilation, bundling, code generator, testing.",
5
5
  "main": "dist/rollup.config.js",
6
6
  "type": "module",