@cmmn/tools 1.6.9 → 1.6.11

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,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,7 +13,9 @@ 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
 
18
+ // import postcssPresetEnv from 'postcss-preset-env'
19
19
  /**
20
20
  * @typedef {import(rollup).RollupOptions} RollupOptions
21
21
  * @typedef {import(rollup).OutputOptions} OutputOptions
@@ -87,6 +87,7 @@ export class ConfigCreator {
87
87
  sourcemap: true,
88
88
  format: module,
89
89
  globals: Array.isArray(this.options.external) ? Object.fromEntries(this.options.external.map(x => [x, x])) : this.options.external,
90
+ assetFileNames: "assets/[name][extname]",
90
91
  name: this.options.global ?? 'global',
91
92
  }));
92
93
  }
@@ -95,7 +96,17 @@ export class ConfigCreator {
95
96
  return html({
96
97
  publicPath: '/',
97
98
  dir: this.outDir,
98
- template: () => fs.readFileSync(path.join(this.root, this.options.html), 'utf8')
99
+ inject: false,
100
+ template: ({bundle}) => {
101
+ const inject = Object.keys(bundle.bundle).map(key => {
102
+ if (key.endsWith('css'))
103
+ return `<link rel="stylesheet" href="${key}" >`;
104
+ if (key.endsWith('js'))
105
+ return `<script defer src="${key}"></script>`;
106
+ })
107
+ const html = fs.readFileSync(path.join(this.root, this.options.html), 'utf8')
108
+ return html.replace('</head>', inject.join('\n') + '</head>');
109
+ }
99
110
  });
100
111
  }
101
112
 
@@ -132,9 +143,10 @@ export class ConfigCreator {
132
143
  get plugins() {
133
144
  const result = [
134
145
  replace({
135
- 'process.env.NODE_ENV': JSON.stringify('development'),
146
+ 'process.env.NODE_ENV': JSON.stringify(this.options.minify ? 'production' : 'development'),
136
147
  preventAssignment: true
137
148
  }),
149
+ ...Styles(this.options),
138
150
  nodeResolve({
139
151
  browser: this.options.browser,
140
152
  dedupe: this.options.dedupe || []
@@ -158,15 +170,10 @@ export class ConfigCreator {
158
170
  namedExports: false,
159
171
  autoModules: true,
160
172
  }) : */
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
- }),
173
+ // styles({
174
+ // autoModules: true,
175
+ // }),
176
+
170
177
  string({
171
178
  include: /\.(html|svg|less)$/,
172
179
  exclude: /\.module\.css/
@@ -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.9",
3
+ "version": "1.6.11",
4
4
  "description": "Compilation, bundling, code generator, testing.",
5
5
  "main": "dist/rollup.config.js",
6
6
  "type": "module",
@@ -40,9 +40,11 @@
40
40
  ],
41
41
  "dependencies": {
42
42
  "@jest/globals": "27.x.x",
43
+ "@modular-css/rollup": "28",
43
44
  "@open-wc/rollup-plugin-html": "^1.2.5",
44
45
  "@rollup/plugin-alias": "3",
45
46
  "@rollup/plugin-commonjs": "^21",
47
+ "@rollup/plugin-image": "2",
46
48
  "@rollup/plugin-json": "4",
47
49
  "@rollup/plugin-node-resolve": "^13",
48
50
  "@rollup/plugin-replace": "4.x.x",
@@ -55,26 +57,28 @@
55
57
  "fast-glob": "^3.2.11",
56
58
  "jest": "27.x.x",
57
59
  "less": "^4",
60
+ "live-server": "1",
61
+ "postcss-import": "14",
62
+ "postcss-modules": "*",
58
63
  "rollup": "^2",
59
- "rollup-plugin-img": "1.x.x",
64
+ "rollup-plugin-img": "1",
60
65
  "rollup-plugin-livereload": "^2.0.5",
61
66
  "rollup-plugin-node-builtins": "^2.1.2",
62
67
  "rollup-plugin-node-globals": "^1.4.0",
63
- "rollup-plugin-postcss": "4.x.x",
64
68
  "rollup-plugin-serve": "^1.1.0",
65
69
  "rollup-plugin-sourcemaps": "^0.6.3",
66
70
  "rollup-plugin-string": "^3.0.0",
67
71
  "rollup-plugin-styles": "^4",
68
72
  "rollup-plugin-terser": "^7",
69
73
  "rollup-plugin-visualizer": "^5.5.4",
70
- "servor": "4.x.x",
71
74
  "sinon": "10.x.x",
72
75
  "ts-jest": "27.x.x",
73
76
  "ttypescript": "1.5.13",
74
77
  "typescript": "4.6.x",
75
- "typescript-transform-paths": "^3.3.1"
78
+ "typescript-transform-paths": "^3.3.1",
79
+ "unique-slug": "*"
76
80
  },
77
81
  "author": "",
78
82
  "license": "ISC",
79
- "gitHead": "5b28fdf72e182a3c775b0012bb1c3248fc1955ec"
83
+ "gitHead": "2755b333808bf97f1d9454601c2a2c9a88e4a021"
80
84
  }
package/serve/serve.js CHANGED
@@ -1,20 +1,20 @@
1
1
  import {getConfigOptions} from "../bundle/getConfigs.js";
2
- import servor from "servor";
3
- import {relative, join} from "path";
2
+ import {join, relative, resolve} from "path";
3
+ import liveServer from "live-server";
4
4
 
5
5
  export function serve(...options) {
6
6
  const configs = getConfigOptions({
7
7
  project: options.includes('-b'),
8
8
  });
9
- configs.filter(x => x.port).forEach(x => {
10
- const root = relative(process.cwd(), join(x.rootDir, x.outDir));
11
- console.log(`serve ${root} at localhost:${x.port}`);
12
- servor({
13
- root,
14
- fallback: 'index.html',
15
- reload: true,
16
- port: x.port
17
- })
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
18
 
19
19
  })
20
20