lvyjs 0.2.0 → 0.2.1

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.
@@ -6,13 +6,13 @@ import { readdirSync } from 'fs';
6
6
  * @param dir 目录路径
7
7
  * @returns 文件路径数组
8
8
  */
9
- const getFiles = (dir) => {
9
+ const getScriptFiles = (dir) => {
10
10
  const results = [];
11
11
  const list = readdirSync(dir, { withFileTypes: true });
12
12
  list.forEach(item => {
13
13
  const fullPath = join(dir, item.name);
14
14
  if (item.isDirectory()) {
15
- results.push(...getFiles(fullPath));
15
+ results.push(...getScriptFiles(fullPath));
16
16
  }
17
17
  else if (item.isFile() &&
18
18
  /\.(ts|js|jsx|tsx)$/.test(item.name) &&
@@ -23,4 +23,4 @@ const getFiles = (dir) => {
23
23
  return results;
24
24
  };
25
25
 
26
- export { getFiles };
26
+ export { getScriptFiles };
@@ -4,7 +4,7 @@ import typescript from '@rollup/plugin-typescript';
4
4
  import commonjs from '@rollup/plugin-commonjs';
5
5
  import json from '@rollup/plugin-json';
6
6
  import styles from 'rollup-plugin-styles';
7
- import { getFiles } from './get-files.js';
7
+ import { getScriptFiles } from './get-files.js';
8
8
  import alias from '@rollup/plugin-alias';
9
9
  import { rollupStylesCSSImport } from '../plugins/loader-css.js';
10
10
  import { rollupAssets } from '../plugins/loader-files.js';
@@ -28,14 +28,14 @@ const onwarn = (warning, warn) => {
28
28
  * @param inputs
29
29
  * @param output
30
30
  */
31
- const buildJS = async (inputs, output) => {
31
+ const buildJS = async (inputs) => {
32
32
  if (!global.lvyConfig)
33
33
  global.lvyConfig = {};
34
34
  if (!global.lvyConfig.build)
35
35
  global.lvyConfig.build = {};
36
36
  // 插件
37
37
  const plugins = [];
38
- if (global.lvyConfig.alias) {
38
+ if (global.lvyConfig?.alias) {
39
39
  plugins.push(alias(global.lvyConfig.alias));
40
40
  }
41
41
  if (global.lvyConfig.assets) {
@@ -117,10 +117,11 @@ const buildJS = async (inputs, output) => {
117
117
  });
118
118
  // 写入输出文件
119
119
  await bundle.write({
120
- dir: output,
120
+ dir: 'lib',
121
121
  format: 'es',
122
122
  sourcemap: false,
123
123
  preserveModules: true,
124
+ assetFileNames: 'assets/[name]-[hash][extname]',
124
125
  ...rollupOptions
125
126
  });
126
127
  };
@@ -128,9 +129,15 @@ const buildJS = async (inputs, output) => {
128
129
  *
129
130
  * @param script
130
131
  */
131
- async function buildAndRun(input, output) {
132
- const inputFiles = getFiles(join(process.cwd(), input));
133
- await buildJS(inputFiles, output);
132
+ async function buildAndRun() {
133
+ // rollup 配置
134
+ let inputDir = 'src';
135
+ if (global.lvyConfig?.rollupOptions?.input) {
136
+ inputDir = global.lvyConfig.rollupOptions.input;
137
+ delete global.lvyConfig.rollupOptions['input'];
138
+ }
139
+ const inputFiles = getScriptFiles(join(process.cwd(), inputDir));
140
+ await buildJS(inputFiles);
134
141
  }
135
142
 
136
143
  export { buildAndRun, buildJS };
package/lib/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { defineConfig, initConfig } from './loader/store.js';
1
+ export { defineConfig, initConfig, usePlugin } from './loader/store.js';
package/lib/index.js CHANGED
@@ -1,40 +1,32 @@
1
1
  import { buildAndRun } from './build/rullup.js';
2
2
  import { initConfig } from './loader/store.js';
3
- export { defineConfig } from './loader/store.js';
3
+ export { defineConfig, usePlugin } from './loader/store.js';
4
4
 
5
5
  /**
6
6
  * @param input
7
7
  */
8
8
  const onDev = async () => {
9
- // 修改config
10
- for (const plugin of global.lvyConfig.plugins) {
11
- if (plugin?.config) {
12
- const cfg = await plugin.config(global.lvyConfig);
13
- for (const key in cfg) {
14
- // 不能覆盖plugins
15
- if (cfg[key] != 'plugins') {
16
- // 覆盖
17
- global.lvyConfig[key] = cfg[key];
18
- }
9
+ const apps = [];
10
+ if (Array.isArray(global.lvyConfig?.plugins)) {
11
+ // 修改config
12
+ for (const plugin of global.lvyConfig.plugins) {
13
+ if (!plugin) {
14
+ continue;
19
15
  }
16
+ apps.push(plugin(global.lvyConfig));
20
17
  }
21
18
  }
22
19
  // 执行loader
23
20
  await import('./main.js');
24
- // 执行 useApp
25
- for (const plugin of global.lvyConfig.plugins) {
26
- if (plugin?.useApp)
27
- await plugin.useApp();
21
+ //
22
+ for (const app of apps) {
23
+ if (!app) {
24
+ continue;
25
+ }
26
+ if (typeof app == 'function')
27
+ app(global.lvyConfig);
28
28
  }
29
29
  };
30
- /**
31
- *
32
- * @param input
33
- * @param ouput
34
- */
35
- const onBuild = () => {
36
- buildAndRun('src', 'lib');
37
- };
38
30
  const main = async () => {
39
31
  if (process.argv.includes('--lvy-dev')) {
40
32
  await initConfig();
@@ -42,7 +34,7 @@ const main = async () => {
42
34
  }
43
35
  else if (process.argv.includes('--lvy-build')) {
44
36
  await initConfig();
45
- onBuild();
37
+ buildAndRun();
46
38
  }
47
39
  };
48
40
  main();
@@ -0,0 +1,4 @@
1
+ const assetsReg = /\.(png|jpg|jpeg|gif|svg|webp|ico)$/;
2
+ const cssReg = /\.(css|scss)$/;
3
+
4
+ export { assetsReg, cssReg };
@@ -7,11 +7,11 @@ const plugins = [];
7
7
  *
8
8
  */
9
9
  const initPlugins = () => {
10
- if (typeof global.lvyConfig?.assets != 'boolean') {
10
+ if (global.lvyConfig?.assets) {
11
11
  plugins.push(esBuildAsstes(global.lvyConfig.assets));
12
12
  }
13
13
  if (typeof global.lvyConfig?.esbuild?.styles != 'boolean') {
14
- plugins.push(esBuildCSS(global.lvyConfig.esbuild.styles));
14
+ plugins.push(esBuildCSS(global.lvyConfig.esbuild?.styles));
15
15
  }
16
16
  };
17
17
  /**
@@ -52,6 +52,9 @@ const ESBuild = async (input) => {
52
52
  external: ['*'],
53
53
  ...options
54
54
  });
55
+ if (!result.outputFiles) {
56
+ return '';
57
+ }
55
58
  // 返回结果
56
59
  return result.outputFiles.map(file => new TextDecoder().decode(file.contents)).join('\n');
57
60
  };
@@ -1,9 +1,8 @@
1
1
  import { resolve, relative, dirname, join } from 'path';
2
2
  import { spawn } from 'child_process';
3
3
  import crypto from 'node:crypto';
4
+ import { assetsReg, cssReg } from './config.js';
4
5
 
5
- const assetsReg = /\.(png|jpg|jpeg|gif|svg|webp|ico)$/;
6
- const cssReg = /\.(css|scss)$/;
7
6
  /**
8
7
  *
9
8
  * @param input
@@ -63,19 +62,21 @@ const esBuildAlias = (alias) => {
63
62
  }
64
63
  };
65
64
  const handleAsstesFile = (url) => {
66
- for (const { find, replacement } of entries) {
67
- if (typeof find === 'string') {
68
- // 使用 startsWith 处理字符串类型
69
- if (url.startsWith(find)) {
70
- const fileUrl = join(replacement, url.replace(find, ''));
71
- return `export default "${convertPath(fileUrl)}";`;
65
+ if (entries) {
66
+ for (const { find, replacement } of entries) {
67
+ if (typeof find === 'string') {
68
+ // 使用 startsWith 处理字符串类型
69
+ if (url.startsWith(find)) {
70
+ const fileUrl = join(replacement, url.replace(find, ''));
71
+ return `export default "${convertPath(fileUrl)}";`;
72
+ }
72
73
  }
73
- }
74
- else if (find instanceof RegExp) {
75
- // 使用 test 方法处理正则表达式类型
76
- if (find.test(url)) {
77
- const fileUrl = join(replacement, url.replace(find, ''));
78
- return `export default "${convertPath(fileUrl)}";`;
74
+ else if (find instanceof RegExp) {
75
+ // 使用 test 方法处理正则表达式类型
76
+ if (find.test(url)) {
77
+ const fileUrl = join(replacement, url.replace(find, ''));
78
+ return `export default "${convertPath(fileUrl)}";`;
79
+ }
79
80
  }
80
81
  }
81
82
  }
@@ -101,21 +102,23 @@ const handleCSS = (fileUrl) => {
101
102
  * @returns {string|null} 加载结果
102
103
  */
103
104
  const handleCSSPath = (url) => {
104
- for (const { find, replacement } of entries) {
105
- if (typeof find === 'string') {
106
- // 使用 startsWith 处理字符串类型
107
- if (url.startsWith(find)) {
108
- const fileUrl = join(replacement, url.replace(find, ''));
109
- const outputDir = handleCSS(fileUrl);
110
- return `export default "${convertPath(outputDir)}";`;
105
+ if (entries) {
106
+ for (const { find, replacement } of entries) {
107
+ if (typeof find === 'string') {
108
+ // 使用 startsWith 处理字符串类型
109
+ if (url.startsWith(find)) {
110
+ const fileUrl = join(replacement, url.replace(find, ''));
111
+ const outputDir = handleCSS(fileUrl);
112
+ return `export default "${convertPath(outputDir)}";`;
113
+ }
111
114
  }
112
- }
113
- else if (find instanceof RegExp) {
114
- // 使用 test 方法处理正则表达式类型
115
- if (find.test(url)) {
116
- const fileUrl = join(replacement, url.replace(find, ''));
117
- const outputDir = handleCSS(fileUrl);
118
- return `export default "${convertPath(outputDir)}";`;
115
+ else if (find instanceof RegExp) {
116
+ // 使用 test 方法处理正则表达式类型
117
+ if (find.test(url)) {
118
+ const fileUrl = join(replacement, url.replace(find, ''));
119
+ const outputDir = handleCSS(fileUrl);
120
+ return `export default "${convertPath(outputDir)}";`;
121
+ }
119
122
  }
120
123
  }
121
124
  }
@@ -125,7 +128,7 @@ const handleCSSPath = (url) => {
125
128
  *
126
129
  * @param param0
127
130
  */
128
- const esBuildAsstes = (optoins = {}) => {
131
+ const esBuildAsstes = (optoins) => {
129
132
  // 默认配置
130
133
  const filter = optoins?.filter ?? assetsReg;
131
134
  const namespace = 'assets';
@@ -169,7 +172,7 @@ const esBuildAsstes = (optoins = {}) => {
169
172
  * @param param0
170
173
  * @returns
171
174
  */
172
- const esBuildCSS = (optoins = {}) => {
175
+ const esBuildCSS = (optoins) => {
173
176
  const filter = optoins?.filter || cssReg;
174
177
  const namespace = optoins?.namespace || 'css';
175
178
  // 返回插件
@@ -1,4 +1,4 @@
1
- import { RollupAliasOptions } from '@rollup/plugin-alias';
1
+ import { Alias } from '@rollup/plugin-alias';
2
2
  import { RollupStylesCSSImportOptions } from '../plugins/loader-css.js';
3
3
  import { RollupCommonJSOptions } from '@rollup/plugin-commonjs';
4
4
  import { RollupJsonOptions } from '@rollup/plugin-json';
@@ -12,32 +12,23 @@ type Options = {
12
12
  /**
13
13
  * 配置调整机及其回调插件
14
14
  */
15
- plugins?: {
16
- /**
17
- * 应用名
18
- */
19
- name: string;
20
- /**
21
- * ⚠️直接optoins进行调整
22
- * @param options
23
- * @returns
24
- */
25
- config?: (options: Options) => Options;
26
- /**
27
- * 执行
28
- */
29
- useApp?: () => void;
30
- }[];
15
+ plugins?: ((options: Options) => ((options: Options) => void) | void | undefined | null)[];
31
16
  /**
32
17
  * 别名
33
18
  */
34
19
  alias?: {
35
- entries?: RollupAliasOptions['entries'];
20
+ /**
21
+ * 别名规则
22
+ */
23
+ entries?: Alias[];
36
24
  };
37
25
  /**
38
26
  * 静态资源识别
39
27
  */
40
28
  assets?: {
29
+ /**
30
+ * 过滤得到指定格式的文件识别之为静态资源
31
+ */
41
32
  filter?: RegExp;
42
33
  };
43
34
  /**
@@ -57,7 +48,12 @@ type Options = {
57
48
  /**
58
49
  * ⚠️ RollupBuild['write'] 配置
59
50
  */
60
- rollupOptions?: RollupBuildOptions;
51
+ rollupOptions?: RollupBuildOptions & {
52
+ /**
53
+ * 默认 src
54
+ */
55
+ input?: string;
56
+ };
61
57
  /**
62
58
  *使用插件
63
59
  */
@@ -85,6 +81,12 @@ type Options = {
85
81
  typescript?: RollupTypescriptOptions | false;
86
82
  } | false;
87
83
  };
84
+ /**
85
+ *
86
+ * @param options
87
+ * @returns
88
+ */
89
+ declare const usePlugin: (load: (options: Options) => ((options: Options) => void) | void | undefined | null) => (options: Options) => ((options: Options) => void) | void | undefined | null;
88
90
  /**
89
91
  *
90
92
  */
@@ -99,6 +101,6 @@ declare const initConfig: () => Promise<void>;
99
101
  * @param param0
100
102
  * @returns
101
103
  */
102
- declare const defineConfig: (optoins?: Options) => Options;
104
+ declare const defineConfig: (optoins?: Options) => Options | undefined;
103
105
 
104
- export { type Options, defineConfig, initConfig };
106
+ export { type Options, defineConfig, initConfig, usePlugin };
@@ -1,6 +1,12 @@
1
1
  import { existsSync } from 'fs';
2
2
  import { join } from 'path';
3
3
 
4
+ /**
5
+ *
6
+ * @param options
7
+ * @returns
8
+ */
9
+ const usePlugin = (load) => load;
4
10
  /**
5
11
  *
6
12
  */
@@ -34,4 +40,4 @@ const initConfig = async () => {
34
40
  */
35
41
  const defineConfig = (optoins) => optoins;
36
42
 
37
- export { defineConfig, initConfig };
43
+ export { defineConfig, initConfig, usePlugin };
@@ -0,0 +1,4 @@
1
+ const assetsReg = /\.(png|jpg|jpeg|gif|svg|webp|ico)$/;
2
+ const cssReg = /\.(css|scss)$/;
3
+
4
+ export { assetsReg, cssReg };
@@ -1,16 +1,18 @@
1
1
  import { createFilter } from '@rollup/pluginutils';
2
2
  import { resolve, dirname, basename } from 'node:path';
3
+ import { cssReg } from './config.js';
3
4
 
4
5
  /**
5
6
  *
6
7
  * @returns
7
8
  */
8
- const rollupStylesCSSImport = ({ include = /\.(css|scss)$/i } = {}) => {
9
+ const rollupStylesCSSImport = (options) => {
10
+ const { include = cssReg } = options ?? {};
9
11
  const filter = createFilter(include, null);
10
12
  return {
11
13
  name: 'c-css',
12
14
  resolveId(source, importer) {
13
- if (filter(source)) {
15
+ if (filter(source) && importer) {
14
16
  return resolve(dirname(importer), source);
15
17
  }
16
18
  },
@@ -7,6 +7,6 @@ type RollupAssetsOptions = {
7
7
  * @param {Object} options
8
8
  * @returns {Object}
9
9
  */
10
- declare const rollupAssets: ({ filter }?: RollupAssetsOptions) => InputPluginOption;
10
+ declare const rollupAssets: (options?: RollupAssetsOptions) => InputPluginOption;
11
11
 
12
12
  export { type RollupAssetsOptions, rollupAssets };
@@ -1,15 +1,17 @@
1
1
  import { resolve, dirname, basename } from 'path';
2
2
  import { readFileSync } from 'fs';
3
+ import { assetsReg } from './config.js';
3
4
 
4
5
  /**
5
6
  * @param {Object} options
6
7
  * @returns {Object}
7
8
  */
8
- const rollupAssets = ({ filter = /\.(png|jpg|jpeg|gif|svg|webp|ico)$/ } = {}) => {
9
+ const rollupAssets = (options) => {
10
+ const { filter = assetsReg } = options ?? {};
9
11
  return {
10
12
  name: 'rollup-node-files',
11
13
  resolveId(source, importer) {
12
- if (filter.test(source)) {
14
+ if (filter.test(source) && importer) {
13
15
  return resolve(dirname(importer), source);
14
16
  }
15
17
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lvyjs",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "tsx compile script",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
package/tsconfig.json CHANGED
@@ -1,25 +1,31 @@
1
1
  {
2
2
  "compilerOptions": {
3
- // model
3
+ /* model */
4
4
  "target": "ESNext",
5
5
  "module": "ESNext",
6
- "allowJs": false,
7
6
  "esModuleInterop": true,
7
+ /* json */
8
8
  "resolveJsonModule": true,
9
- // jsx
9
+ /* Bundler mode */
10
10
  "jsx": "react",
11
11
  "moduleResolution": "bundler",
12
12
  "allowImportingTsExtensions": true,
13
+ "moduleDetection": "force",
13
14
  "noEmit": true,
14
- // version
15
+ "skipLibCheck": true,
16
+ /* version */
15
17
  "ignoreDeprecations": "5.0",
16
- // strict
18
+ /* strict */
19
+ "allowJs": false,
17
20
  "noImplicitAny": false,
18
21
  "useDefineForClassFields": true,
19
22
  "isolatedModules": true,
23
+ "forceConsistentCasingInFileNames": true,
24
+ /* Linting */
25
+ "strict": true,
20
26
  "noUnusedLocals": true,
21
27
  "noUnusedParameters": true,
22
28
  "noFallthroughCasesInSwitch": true,
23
- "forceConsistentCasingInFileNames": true
29
+ "noUncheckedSideEffectImports": true
24
30
  }
25
31
  }