@cmmn/tools 1.6.8 → 1.6.12

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
@@ -1,7 +1,8 @@
1
1
  import {rollup, watch} from "rollup";
2
2
  import {getConfigOptions} from "./getConfigs.js";
3
3
  import {ConfigCreator} from "./rollup.config.js";
4
-
4
+ import fs from "fs";
5
+ import path from "path";
5
6
 
6
7
  export async function bundle(...options) {
7
8
  const configOptions = getConfigOptions({
@@ -19,7 +20,9 @@ export async function bundle(...options) {
19
20
  }
20
21
  const build = await rollup(config);
21
22
  for (let out of config.output){
22
- console.log(`SUCCESS: ${out.dir} ${out.entryFileNames.replace('[name]', Object.keys(config.input)[0])}`);
23
+ const file = path.join(out.dir, out.entryFileNames.replace('[name]', Object.keys(config.input)[0]));
24
+ const stat = fs.statSync(file);
25
+ console.log(`SUCCESS: ${file} (${(stat.size/1024/1024).toFixed(3)}Mb)`);
23
26
  await build.write(out);
24
27
  }
25
28
  }
@@ -1,9 +1,7 @@
1
1
  import commonjs from '@rollup/plugin-commonjs';
2
2
  import nodeResolve from '@rollup/plugin-node-resolve';
3
- import image from "rollup-plugin-img";
4
3
  import {terser} from "rollup-plugin-terser"
5
4
  import {visualizer} from 'rollup-plugin-visualizer';
6
- import styles from "rollup-plugin-styles";
7
5
  import {string} from "rollup-plugin-string";
8
6
  import serve from 'rollup-plugin-serve';
9
7
  import builtins from 'rollup-plugin-node-builtins';
@@ -15,6 +13,7 @@ import json from '@rollup/plugin-json';
15
13
  import alias from '@rollup/plugin-alias';
16
14
  import replace from '@rollup/plugin-replace';
17
15
  import sourcemaps from 'rollup-plugin-sourcemaps';
16
+ import {Styles} from "./styles.js";
18
17
 
19
18
  /**
20
19
  * @typedef {import(rollup).RollupOptions} RollupOptions
@@ -84,9 +83,10 @@ export class ConfigCreator {
84
83
  entryFileNames: this.getOutputFileName(module, this.options.minify),
85
84
  // file: output,
86
85
  dir: this.outDir,
87
- sourcemap: true,
86
+ sourcemap: this.options.minify ? false : 'inline',
88
87
  format: module,
89
- globals: Array.isArray(this.options.external) ? Object.fromEntries(this.options.external.map(x => [x, x])) : this.options.external,
88
+ globals: module === 'umd' ? (Array.isArray(this.options.external) ? Object.fromEntries(this.options.external.map(x => [x, x])) : this.options.external) : [],
89
+ assetFileNames: "assets/[name][extname]",
90
90
  name: this.options.global ?? 'global',
91
91
  }));
92
92
  }
@@ -95,7 +95,17 @@ export class ConfigCreator {
95
95
  return html({
96
96
  publicPath: '/',
97
97
  dir: this.outDir,
98
- template: () => fs.readFileSync(path.join(this.root, this.options.html), 'utf8')
98
+ inject: false,
99
+ template: ({bundle}) => {
100
+ const inject = Object.keys(bundle.bundle).map(key => {
101
+ if (key.endsWith('css'))
102
+ return `<link rel="stylesheet" href="/${key}" >`;
103
+ if (key.endsWith('js'))
104
+ return `<script type="module" defer src="/${key}"></script>`;
105
+ })
106
+ const html = fs.readFileSync(path.join(this.root, this.options.html), 'utf8')
107
+ return html.replace('</head>', inject.join('\n') + '</head>');
108
+ }
99
109
  });
100
110
  }
101
111
 
@@ -132,16 +142,19 @@ export class ConfigCreator {
132
142
  get plugins() {
133
143
  const result = [
134
144
  replace({
135
- 'process.env.NODE_ENV': JSON.stringify('development'),
145
+ 'process.env.NODE_ENV': JSON.stringify(this.options.minify ? 'production' : 'development'),
136
146
  preventAssignment: true
137
147
  }),
148
+ ...Styles(this.options),
149
+ commonjs({
150
+ requireReturnsDefault: "namespace",
151
+ transformMixedEsModules: true,
152
+ defaultIsModuleExports: true
153
+ }),
138
154
  nodeResolve({
139
155
  browser: this.options.browser,
140
156
  dedupe: this.options.dedupe || []
141
157
  }),
142
- commonjs({
143
- requireReturnsDefault: "namespace"
144
- }),
145
158
  sourcemaps(),
146
159
  builtins(),
147
160
  /*this.options.styles === 'modules' ? postCSS({
@@ -158,15 +171,10 @@ export class ConfigCreator {
158
171
  namedExports: false,
159
172
  autoModules: true,
160
173
  }) : */
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
- }),
174
+ // styles({
175
+ // autoModules: true,
176
+ // }),
177
+
170
178
  string({
171
179
  include: /\.(html|svg|less)$/,
172
180
  exclude: /\.module\.css/
@@ -244,7 +252,7 @@ export class ConfigCreator {
244
252
 
245
253
  },
246
254
  plugins: this.plugins,
247
- treeshake: this.options.minify ? "smallest" : "recommended",
255
+ treeshake: this.options.minify ? "smallest" : "safest",
248
256
  watch: {
249
257
  exclude: this.getExternals().concat(path.join(this.root, this.outDir)),
250
258
  }
@@ -0,0 +1,65 @@
1
+ import loader from 'postcss-modules/build/css-loader-core/loader.js';
2
+ import image from "rollup-plugin-img";
3
+ import postCssImport from 'postcss-import';
4
+ import cssModules from '@modular-css/rollup';
5
+ import slug from "unique-slug";
6
+ import styles from "rollup-plugin-styles";
7
+
8
+ class Loader extends loader.default {
9
+ failStart = '/' + process.cwd();
10
+
11
+ fetch(_newPath, relativeTo, _trace) {
12
+ if (relativeTo.startsWith(this.failStart))
13
+ relativeTo = relativeTo.substring(1);
14
+ // let newPath = _newPath.replace(/^["']|["']$/g, "");
15
+ // let relativeDir = path.dirname(relativeTo),
16
+ // rootRelativePath = path.resolve(relativeDir, newPath),
17
+ // fileRelativePath = path.resolve(path.join(this.root, relativeDir), newPath);
18
+ // relativeTo = path.relative(process.cwd(), relativeTo);
19
+ // console.log({root: this.root, newPath, relativeTo, relativeDir, rootRelativePath, fileRelativePath});
20
+ return super.fetch(_newPath, relativeTo, _trace);
21
+ }
22
+ }
23
+
24
+ export function Styles(options) {
25
+ return [
26
+ image({
27
+ output: `/assets`, // default the root
28
+ extensions: /\.(png|jpg|jpeg|gif)$/, // support png|jpg|jpeg|gif|svg, and it's alse the default value
29
+ // exclude: 'node_modules/**'
30
+ }),
31
+ options.styles === "modules" ? cssModules({
32
+ common: 'common.css',
33
+ before: [postCssImport],
34
+ namer: function (file, selector) {
35
+ return selector + "_" +
36
+ file.replace(/([\/\\]index)?(\.module)?\.css$/, "").split(/[\\\/]/).pop() + "_" +
37
+ slug(file)
38
+ }
39
+ }): styles({
40
+ mode: "emit"
41
+ }),
42
+ // postcss({
43
+ // extract: 'output.css',
44
+ // ident: 'postcss',
45
+ // modules: {
46
+ // root: '',
47
+ // generateScopedName: `[name]_[local]_[hash:base64:5]`,
48
+ // Loader: Loader
49
+ // },
50
+ // use: ['sass'],
51
+ // plugins: [
52
+ // // postCssDuplicates(),
53
+ // postCssImport()
54
+ // // postcssModules({
55
+ // // generateScopedName: '[local]',
56
+ // // root: '',
57
+ // // Loader: Loader
58
+ // // }),
59
+ // // postcssPresetEnv({
60
+ // // stage: 0,
61
+ // // }),
62
+ // ],
63
+ // }),
64
+ ];
65
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmmn/tools",
3
- "version": "1.6.8",
3
+ "version": "1.6.12",
4
4
  "description": "Compilation, bundling, code generator, testing.",
5
5
  "main": "dist/rollup.config.js",
6
6
  "type": "module",
@@ -34,14 +34,17 @@
34
34
  "gen/*",
35
35
  "compile/*",
36
36
  "bundle/*",
37
+ "serve/*",
37
38
  "plugins/*",
38
39
  "test/*"
39
40
  ],
40
41
  "dependencies": {
41
42
  "@jest/globals": "27.x.x",
43
+ "@modular-css/rollup": "28",
42
44
  "@open-wc/rollup-plugin-html": "^1.2.5",
43
45
  "@rollup/plugin-alias": "3",
44
46
  "@rollup/plugin-commonjs": "^21",
47
+ "@rollup/plugin-image": "2",
45
48
  "@rollup/plugin-json": "4",
46
49
  "@rollup/plugin-node-resolve": "^13",
47
50
  "@rollup/plugin-replace": "4.x.x",
@@ -54,26 +57,28 @@
54
57
  "fast-glob": "^3.2.11",
55
58
  "jest": "27.x.x",
56
59
  "less": "^4",
60
+ "live-server": "1.2.2",
61
+ "postcss-import": "14",
62
+ "postcss-modules": "*",
57
63
  "rollup": "^2",
58
- "rollup-plugin-img": "1.x.x",
64
+ "rollup-plugin-img": "1",
59
65
  "rollup-plugin-livereload": "^2.0.5",
60
66
  "rollup-plugin-node-builtins": "^2.1.2",
61
67
  "rollup-plugin-node-globals": "^1.4.0",
62
- "rollup-plugin-postcss": "4.x.x",
63
68
  "rollup-plugin-serve": "^1.1.0",
64
69
  "rollup-plugin-sourcemaps": "^0.6.3",
65
70
  "rollup-plugin-string": "^3.0.0",
66
71
  "rollup-plugin-styles": "^4",
67
72
  "rollup-plugin-terser": "^7",
68
73
  "rollup-plugin-visualizer": "^5.5.4",
69
- "servor": "4.x.x",
70
74
  "sinon": "10.x.x",
71
75
  "ts-jest": "27.x.x",
72
76
  "ttypescript": "1.5.13",
73
77
  "typescript": "4.6.x",
74
- "typescript-transform-paths": "^3.3.1"
78
+ "typescript-transform-paths": "^3.3.1",
79
+ "unique-slug": "*"
75
80
  },
76
81
  "author": "",
77
82
  "license": "ISC",
78
- "gitHead": "f4e9729796c27c9c9f42b30082b44e8afa6d62c4"
83
+ "gitHead": "96da82c2b5c2a0c8bead5eeb8005d00261812153"
79
84
  }
package/serve/serve.js ADDED
@@ -0,0 +1,21 @@
1
+ import {getConfigOptions} from "../bundle/getConfigs.js";
2
+ import {join, relative, resolve} from "path";
3
+ import liveServer from "live-server";
4
+
5
+ export function serve(...options) {
6
+ const configs = getConfigOptions({
7
+ project: options.includes('-b'),
8
+ });
9
+ configs.filter(x => x.port).forEach(async x => {
10
+ const root = relative(process.cwd(), join(x.rootDir, x.outDir ?? 'dist/bundle'));
11
+ const server = await liveServer.start({
12
+ root: root,
13
+ file: 'index.html',
14
+ port: x.port,
15
+ open: false,
16
+ mount: x.mount && Object.entries(x.mount).map(([from, to]) => [from, resolve(x.rootDir, to)])
17
+ });
18
+
19
+ })
20
+
21
+ }