@cmmn/tools 1.6.7 → 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.
package/bin.js CHANGED
@@ -1,13 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import {bundle} from "./bundle/bundle.js";
4
+ import {serve} from "./serve/serve.js";
4
5
  import {compile} from "./compile/compile.js";
5
6
  import {gen} from "./gen/gen.js";
6
7
 
7
8
  const [action, ...args] = process.argv.slice(2);
8
9
 
9
10
  const actions = {
10
- bundle, compile, gen
11
+ bundle, compile, gen, serve
11
12
  }
12
13
 
13
14
  if (action in actions) {
package/bundle/bundle.js CHANGED
@@ -1,76 +1,17 @@
1
- import {ConfigCreator} from "./rollup.config.js";
2
1
  import {rollup, watch} from "rollup";
3
- import fs from "fs";
4
- import path from "path";
5
- import fg from "fast-glob";
6
-
7
- function getProjectConfig(rootDir, cmmn, options) {
8
- const configCreator = new ConfigCreator({
9
- ...options,
10
- ...cmmn,
11
- });
12
- configCreator.setRootDir(rootDir);
13
- return configCreator.getConfig();
14
- }
15
-
16
- function getPackageConfigs(rootDir, options, name = null) {
17
- const pckPath = path.join(rootDir, 'package.json');
18
- if (!fs.existsSync(pckPath))
19
- return [];
20
- const results = [];
21
- const pkg = JSON.parse(fs.readFileSync(pckPath));
22
- if (name) {
23
- results.push(...getProjectConfig(rootDir, pkg.cmmn[name], {
24
- ...options,
25
- name
26
- }));
27
- } else {
28
- for (let name in pkg.cmmn) {
29
- results.push(...getProjectConfig(rootDir, pkg.cmmn[name], {
30
- ...options,
31
- name
32
- }));
33
- }
34
- }
35
- return results;
36
- }
37
-
38
- function getLernaSubPackages(lernaFile, options) {
39
- const config = JSON.parse(fs.readFileSync(lernaFile, 'utf8'));
40
- const packages = config.packages;
41
- const dirs = packages.flatMap(pkg => fg.sync([pkg], {
42
- absolute: true,
43
- globstar: true,
44
- onlyDirectories: true,
45
- cwd: path.dirname(lernaFile)
46
- }));
47
- return dirs.flatMap(dir => getPackageConfigs(dir, options));
48
- }
2
+ import {getConfigOptions} from "./getConfigs.js";
3
+ import {ConfigCreator} from "./rollup.config.js";
49
4
 
50
- function getConfigs(options) {
51
- if (!options.input || options.project) {
52
- const rootDir = process.cwd();
53
- const lernaPath = path.join(rootDir, 'lerna.json');
54
- if (fs.existsSync(lernaPath)) {
55
- return getLernaSubPackages(lernaPath, options);
56
- }
57
- return getPackageConfigs(process.cwd(), options);
58
- }
59
- if (!options.input.includes('.') || !fs.existsSync(options.input)) {
60
- return getPackageConfigs(process.cwd(), options, options.input);
61
- }
62
- const creator = new ConfigCreator(options);
63
- return creator.getConfig();
64
- }
65
5
 
66
6
  export async function bundle(...options) {
67
- const configs = getConfigs({
7
+ const configOptions = getConfigOptions({
68
8
  input: options.filter(x => !x.startsWith('-'))[0],
69
9
  project: options.includes('-b'),
70
10
  minify: options.includes('--prod'),
71
11
  devServer: options.includes('--run'),
72
12
  stats: options.includes('--stats'),
73
13
  });
14
+ const configs = configOptions.flatMap(x => new ConfigCreator(x).getConfig());
74
15
  if (!options.includes('--watch')) {
75
16
  for (let config of configs) {
76
17
  for (let key in config.input){
@@ -0,0 +1,60 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import fg from "fast-glob";
4
+
5
+ function getProjectConfig(rootDir, cmmn, options) {
6
+ return {
7
+ ...options,
8
+ ...cmmn,
9
+ rootDir: rootDir
10
+ };
11
+ }
12
+
13
+ function getPackageConfigs(rootDir, options, name = null) {
14
+ const pckPath = path.join(rootDir, 'package.json');
15
+ if (!fs.existsSync(pckPath))
16
+ return [];
17
+ const results = [];
18
+ const pkg = JSON.parse(fs.readFileSync(pckPath));
19
+ if (name) {
20
+ results.push(getProjectConfig(rootDir, pkg.cmmn[name], {
21
+ ...options,
22
+ name
23
+ }));
24
+ } else {
25
+ for (let name in pkg.cmmn) {
26
+ results.push(getProjectConfig(rootDir, pkg.cmmn[name], {
27
+ ...options,
28
+ name
29
+ }));
30
+ }
31
+ }
32
+ return results;
33
+ }
34
+
35
+ function getLernaSubPackages(lernaFile, options) {
36
+ const config = JSON.parse(fs.readFileSync(lernaFile, 'utf8'));
37
+ const packages = config.packages;
38
+ const dirs = packages.flatMap(pkg => fg.sync([pkg], {
39
+ absolute: true,
40
+ globstar: true,
41
+ onlyDirectories: true,
42
+ cwd: path.dirname(lernaFile)
43
+ }));
44
+ return dirs.flatMap(dir => getPackageConfigs(dir, options));
45
+ }
46
+
47
+ export function getConfigOptions(options) {
48
+ if (!options.input || options.project) {
49
+ const rootDir = process.cwd();
50
+ const lernaPath = path.join(rootDir, 'lerna.json');
51
+ if (fs.existsSync(lernaPath)) {
52
+ return getLernaSubPackages(lernaPath, options);
53
+ }
54
+ return getPackageConfigs(process.cwd(), options);
55
+ }
56
+ if (!options.input.includes('.') || !fs.existsSync(options.input)) {
57
+ return getPackageConfigs(process.cwd(), options, options.input);
58
+ }
59
+ return [options];
60
+ }
@@ -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,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";
17
+
18
+ // import postcssPresetEnv from 'postcss-preset-env'
18
19
  /**
19
20
  * @typedef {import(rollup).RollupOptions} RollupOptions
20
21
  * @typedef {import(rollup).OutputOptions} OutputOptions
@@ -48,12 +49,14 @@ export class ConfigCreator {
48
49
 
49
50
  constructor(options) {
50
51
  this.options = {
52
+ module: 'es',
53
+ external: [],
54
+ name: 'index',
55
+ outDir: 'dist/bundle',
51
56
  ...options
52
57
  };
53
- }
54
-
55
- setRootDir(rootDir) {
56
- this.root = rootDir;
58
+ if (options.rootDir)
59
+ this.root = options.rootDir;
57
60
  }
58
61
 
59
62
  get outDir() {
@@ -84,6 +87,7 @@ export class ConfigCreator {
84
87
  sourcemap: true,
85
88
  format: module,
86
89
  globals: Array.isArray(this.options.external) ? Object.fromEntries(this.options.external.map(x => [x, x])) : this.options.external,
90
+ assetFileNames: "assets/[name][extname]",
87
91
  name: this.options.global ?? 'global',
88
92
  }));
89
93
  }
@@ -92,7 +96,17 @@ export class ConfigCreator {
92
96
  return html({
93
97
  publicPath: '/',
94
98
  dir: this.outDir,
95
- 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
+ }
96
110
  });
97
111
  }
98
112
 
@@ -107,7 +121,7 @@ export class ConfigCreator {
107
121
 
108
122
  get livereload() {
109
123
  return livereload({
110
- watch: [this.outDir, path.join(this.root, 'assets')],
124
+ watch: [this.outDir, path.join(this.root, 'assets'), this.options.html],
111
125
  verbose: false, // Disable console output
112
126
  // other livereload options
113
127
  port: 12345,
@@ -129,9 +143,10 @@ export class ConfigCreator {
129
143
  get plugins() {
130
144
  const result = [
131
145
  replace({
132
- 'process.env.NODE_ENV': JSON.stringify('development'),
146
+ 'process.env.NODE_ENV': JSON.stringify(this.options.minify ? 'production' : 'development'),
133
147
  preventAssignment: true
134
148
  }),
149
+ ...Styles(this.options),
135
150
  nodeResolve({
136
151
  browser: this.options.browser,
137
152
  dedupe: this.options.dedupe || []
@@ -155,15 +170,10 @@ export class ConfigCreator {
155
170
  namedExports: false,
156
171
  autoModules: true,
157
172
  }) : */
158
- styles({
159
- mode: "emit"
160
- }),
161
- image({
162
- output: `/assets`, // default the root
163
- extensions: /\.(png|jpg|jpeg|gif)$/, // support png|jpg|jpeg|gif|svg, and it's alse the default value
164
- limit: 8192, // default 8192(8k)
165
- exclude: 'node_modules/**'
166
- }),
173
+ // styles({
174
+ // autoModules: true,
175
+ // }),
176
+
167
177
  string({
168
178
  include: /\.(html|svg|less)$/,
169
179
  exclude: /\.module\.css/
@@ -214,12 +224,6 @@ export class ConfigCreator {
214
224
  * @returns {RollupOptions[]}
215
225
  */
216
226
  getConfig() {
217
- Object.assign(this.options, {
218
- module: this.options.module || 'es',
219
- external: this.options.external || [],
220
- name: this.options.name || 'index',
221
- outDir: this.options.outDir || 'dist'
222
- });
223
227
  if (this.options.external && typeof this.options.external === "string")
224
228
  this.options.external = [this.options.external]
225
229
  console.log(this.options.name, this.options);
@@ -248,6 +252,9 @@ export class ConfigCreator {
248
252
  },
249
253
  plugins: this.plugins,
250
254
  treeshake: this.options.minify ? "smallest" : "recommended",
255
+ watch: {
256
+ exclude: this.getExternals().concat(path.join(this.root, this.outDir)),
257
+ }
251
258
  }]
252
259
  }
253
260
  }
@@ -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
+ }
@@ -10,8 +10,13 @@
10
10
  "checkJs": false,
11
11
  "outDir": "./dist/esm",
12
12
  "skipLibCheck": true,
13
+ "incremental": true,
14
+ "disableSolutionSearching": true,
15
+ "assumeChangesOnlyAffectDirectDependencies": true,
13
16
  "skipDefaultLibCheck": true,
14
17
  "allowJs": true,
18
+ "disableReferencedProjectLoad": true,
19
+ "disableSourceOfProjectReferenceRedirect": true,
15
20
  "allowSyntheticDefaultImports": true,
16
21
  "emitDecoratorMetadata": true ,
17
22
  "noEmitHelpers": true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmmn/tools",
3
- "version": "1.6.7",
3
+ "version": "1.6.11",
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,12 +57,14 @@
54
57
  "fast-glob": "^3.2.11",
55
58
  "jest": "27.x.x",
56
59
  "less": "^4",
60
+ "live-server": "1",
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",
@@ -70,9 +75,10 @@
70
75
  "ts-jest": "27.x.x",
71
76
  "ttypescript": "1.5.13",
72
77
  "typescript": "4.6.x",
73
- "typescript-transform-paths": "^3.3.1"
78
+ "typescript-transform-paths": "^3.3.1",
79
+ "unique-slug": "*"
74
80
  },
75
81
  "author": "",
76
82
  "license": "ISC",
77
- "gitHead": "01c259a853bd898adde91115187af71c666772c5"
83
+ "gitHead": "2755b333808bf97f1d9454601c2a2c9a88e4a021"
78
84
  }
@@ -8,7 +8,7 @@ function visitExportNode(exportNode, sourceFile) {
8
8
  return ;
9
9
  }
10
10
  const file = exportNode.moduleSpecifier?.text ?? exportNode.test;
11
- if (!file)
11
+ if (!file || !file.startsWith('.'))
12
12
  return;
13
13
  const sourceFileDir = path.dirname(sourceFile.path);
14
14
  const abs = path.resolve(sourceFileDir, file);
@@ -28,7 +28,7 @@ function visitExportNode(exportNode, sourceFile) {
28
28
 
29
29
  function visitImportNode(importNode, sourceFile, options) {
30
30
  const file = importNode.moduleSpecifier?.text;
31
- if (!file)
31
+ if (!file || !file.startsWith('.'))
32
32
  return;
33
33
  const sourceFileDir = path.dirname(sourceFile.path);
34
34
  const abs = path.resolve(sourceFileDir, file);
package/readme.md CHANGED
@@ -28,4 +28,6 @@
28
28
  expect(1).toBe(2);
29
29
  }
30
30
  }
31
- ```
31
+ ```
32
+ * `cmmn serve [-b]`
33
+ serves bundled website (-b serves all websites in monorepo)
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
+ }