lvyjs 0.2.14 → 0.2.15

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.
@@ -7,11 +7,11 @@
7
7
  * @param inputs
8
8
  * @param output
9
9
  */
10
- declare const buildJS: (inputs: string[]) => Promise<void>
10
+ declare const buildJS: (inputs: string[]) => Promise<void>;
11
11
  /**
12
12
  *
13
13
  * @param script
14
14
  */
15
- declare function buildAndRun(): Promise<void>
15
+ declare function buildAndRun(): Promise<void>;
16
16
 
17
- export { buildAndRun, buildJS }
17
+ export { buildAndRun, buildJS };
@@ -1,14 +1,16 @@
1
- import { rollup } from 'rollup'
2
- import { join } from 'path'
3
- import typescript from '@rollup/plugin-typescript'
4
- import commonjs from '@rollup/plugin-commonjs'
5
- import json from '@rollup/plugin-json'
6
- import styles from 'rollup-plugin-styles'
7
- import { getScriptFiles } from './utils/files.js'
8
- import alias from '@rollup/plugin-alias'
9
- import { rollupStylesCSSImport } from './plugins/loader-css.js'
10
- import { rollupAssets } from './plugins/loader-files.js'
11
- import { createAlias } from '../config.js'
1
+ import { rollup } from 'rollup';
2
+ import { join } from 'path';
3
+ import typescript from '@rollup/plugin-typescript';
4
+ import commonjs from '@rollup/plugin-commonjs';
5
+ import json from '@rollup/plugin-json';
6
+ import styles from 'rollup-plugin-styles';
7
+ import { getScriptFiles } from './utils/files.js';
8
+ import alias from '@rollup/plugin-alias';
9
+ import { rollupStylesCSSImport } from './plugins/loader-css.js';
10
+ import { rollupAssets } from './plugins/loader-files.js';
11
+ import { createAlias } from '../config.js';
12
+ import { statSync, readFileSync } from 'fs';
13
+ import zlib from 'zlib';
12
14
 
13
15
  /**
14
16
  * 用于忽略警告
@@ -16,9 +18,10 @@ import { createAlias } from '../config.js'
16
18
  * @param warn
17
19
  */
18
20
  const onwarn = (warning, warn) => {
19
- if (warning.code === 'UNRESOLVED_IMPORT') return
20
- warn(warning)
21
- }
21
+ if (warning.code === 'UNRESOLVED_IMPORT')
22
+ return;
23
+ warn(warning);
24
+ };
22
25
  /**
23
26
  * 打包 JS
24
27
  * *** 注意 **
@@ -28,95 +31,114 @@ const onwarn = (warning, warn) => {
28
31
  * @param inputs
29
32
  * @param output
30
33
  */
31
- const buildJS = async inputs => {
32
- if (!global.lvyConfig) global.lvyConfig = {}
33
- if (!global.lvyConfig.build) global.lvyConfig.build = {}
34
- // 插件
35
- const plugins = []
36
- if (typeof global.lvyConfig?.alias !== 'boolean') {
37
- plugins.push(alias(global.lvyConfig?.alias ?? {}))
38
- }
39
- if (typeof global.lvyConfig?.assets !== 'boolean') {
40
- plugins.push(rollupAssets(global.lvyConfig?.assets ?? {}))
41
- }
42
- if (typeof global.lvyConfig?.styles !== 'boolean') {
43
- if (!global.lvyConfig.alias) global.lvyConfig.alias = {}
44
- plugins.push(
45
- styles({
46
- alias: createAlias(global.lvyConfig?.alias),
47
- mode: ['inject', () => '']
48
- })
49
- )
50
- plugins.push(rollupStylesCSSImport(global.lvyConfig.styles))
51
- }
52
- plugins.push(json())
53
- if (typeof global.lvyConfig.build != 'boolean') {
54
- //
55
- for (const key in global.lvyConfig.build) {
56
- if (typeof global.lvyConfig.build[key] == 'boolean') {
57
- continue
58
- }
59
- if (key == 'commonjs' && !global.lvyConfig.build['@rollup/plugin-commonjs']) {
60
- plugins.push(commonjs(global.lvyConfig.build[key]))
61
- } else if (key == 'typescript' && !global.lvyConfig.build['@rollup/plugin-typescript']) {
62
- plugins.push(typescript(global.lvyConfig.build[key]))
63
- } else if (key == 'OutputOptions') {
64
- continue
65
- } else if (key == 'RollupOptions') {
66
- continue
67
- }
34
+ const buildJS = async (inputs) => {
35
+ if (!global.lvyConfig)
36
+ global.lvyConfig = {};
37
+ if (!global.lvyConfig.build)
38
+ global.lvyConfig.build = {};
39
+ // 插件
40
+ const plugins = [];
41
+ if (typeof global.lvyConfig?.alias !== 'boolean') {
42
+ plugins.push(alias(global.lvyConfig?.alias ?? {}));
68
43
  }
69
- // 如果不存在这些配置
70
- const keys = ['commonjs', 'typescript']
71
- for (const key of keys) {
72
- // 如果是布尔值
73
- if (typeof global.lvyConfig.build[key] == 'boolean') {
74
- continue
75
- }
76
- // 存在这些配置
77
- if (global.lvyConfig.build[key]) {
78
- continue
79
- }
80
- //
81
- if (key === 'commonjs') {
82
- plugins.push(commonjs())
83
- } else if (key === 'typescript') {
84
- plugins.push(typescript())
85
- }
44
+ if (typeof global.lvyConfig?.assets !== 'boolean') {
45
+ plugins.push(rollupAssets(global.lvyConfig?.assets ?? {}));
86
46
  }
87
- }
88
- const RollupOptions = global.lvyConfig?.build?.RollupOptions ?? {}
89
- const plg = await RollupOptions?.plugins
90
- const pl = plg && typeof plg != 'boolean' ? plg : []
91
- // build
92
- const bundle = await rollup({
93
- input: inputs,
94
- onwarn: onwarn,
95
- ...RollupOptions,
96
- plugins: Array.isArray(pl) ? [...plugins, ...pl] : pl
97
- })
98
- const OutputOptions = global.lvyConfig?.build?.OutputOptions ?? []
99
- // 写入输出文件
100
- await bundle.write({
101
- dir: 'lib',
102
- format: 'es',
103
- sourcemap: false,
104
- preserveModules: true,
105
- assetFileNames: 'assets/[name]-[hash][extname]',
106
- ...OutputOptions
107
- })
108
- }
47
+ if (typeof global.lvyConfig?.styles !== 'boolean') {
48
+ if (!global.lvyConfig.alias)
49
+ global.lvyConfig.alias = {};
50
+ plugins.push(styles({
51
+ alias: createAlias(global.lvyConfig?.alias),
52
+ mode: ['inject', () => '']
53
+ }));
54
+ plugins.push(rollupStylesCSSImport(global.lvyConfig.styles));
55
+ }
56
+ plugins.push(json());
57
+ if (typeof global.lvyConfig.build != 'boolean') {
58
+ //
59
+ for (const key in global.lvyConfig.build) {
60
+ if (typeof global.lvyConfig.build[key] == 'boolean') {
61
+ continue;
62
+ }
63
+ if (key == 'commonjs' && !global.lvyConfig.build['@rollup/plugin-commonjs']) {
64
+ plugins.push(commonjs(global.lvyConfig.build[key]));
65
+ }
66
+ else if (key == 'typescript' && !global.lvyConfig.build['@rollup/plugin-typescript']) {
67
+ plugins.push(typescript(global.lvyConfig.build[key]));
68
+ }
69
+ else if (key == 'OutputOptions') {
70
+ continue;
71
+ }
72
+ else if (key == 'RollupOptions') {
73
+ continue;
74
+ }
75
+ }
76
+ // 如果不存在这些配置
77
+ const keys = ['commonjs', 'typescript'];
78
+ for (const key of keys) {
79
+ // 如果是布尔值
80
+ if (typeof global.lvyConfig.build[key] == 'boolean') {
81
+ continue;
82
+ }
83
+ // 存在这些配置
84
+ if (global.lvyConfig.build[key]) {
85
+ continue;
86
+ }
87
+ //
88
+ if (key === 'commonjs') {
89
+ plugins.push(commonjs());
90
+ }
91
+ else if (key === 'typescript') {
92
+ plugins.push(typescript());
93
+ }
94
+ }
95
+ }
96
+ const RollupOptions = global.lvyConfig?.build?.RollupOptions ?? {};
97
+ const plg = await RollupOptions?.plugins;
98
+ const pl = plg && typeof plg != 'boolean' ? plg : [];
99
+ // build
100
+ const bundle = await rollup({
101
+ input: inputs,
102
+ onwarn: onwarn,
103
+ ...RollupOptions,
104
+ plugins: Array.isArray(pl) ? [...plugins, ...pl] : pl
105
+ });
106
+ const OutputOptions = global.lvyConfig?.build?.OutputOptions ?? [];
107
+ // 获取 dir 的值
108
+ const outputDir = OutputOptions['dir'] || 'lib';
109
+ // 写入输出文件
110
+ const { output } = await bundle.write({
111
+ dir: outputDir,
112
+ format: 'es',
113
+ sourcemap: false,
114
+ preserveModules: true,
115
+ assetFileNames: 'assets/[name]-[hash][extname]',
116
+ ...OutputOptions
117
+ });
118
+ // 打印产出地址和文件大小
119
+ console.log(`✓ ${output.length} modules transformed.`);
120
+ for (const file of output) {
121
+ const filePath = join(outputDir, file.fileName);
122
+ const fileSize = statSync(filePath).size;
123
+ const fileContent = readFileSync(filePath);
124
+ const gzipFileSize = zlib.gzipSync(fileContent).length;
125
+ console.log(`${filePath.padEnd(40)} ${(fileSize / 1024).toFixed(2)} kB │ gzip: ${(gzipFileSize / 1024).toFixed(2)} kB`);
126
+ }
127
+ //
128
+ };
109
129
  /**
110
130
  *
111
131
  * @param script
112
132
  */
113
133
  async function buildAndRun() {
114
- if (!global.lvyConfig) global.lvyConfig = {}
115
- if (!global.lvyConfig.build) global.lvyConfig.build = {}
116
- // rollup 配置
117
- let inputDir = global.lvyConfig?.build?.OutputOptions?.input ?? 'src'
118
- const inputFiles = getScriptFiles(join(process.cwd(), inputDir))
119
- await buildJS(inputFiles)
134
+ if (!global.lvyConfig)
135
+ global.lvyConfig = {};
136
+ if (!global.lvyConfig.build)
137
+ global.lvyConfig.build = {};
138
+ // rollup 配置
139
+ let inputDir = global.lvyConfig?.build?.OutputOptions?.input ?? 'src';
140
+ const inputFiles = getScriptFiles(join(process.cwd(), inputDir));
141
+ await buildJS(inputFiles);
120
142
  }
121
143
 
122
- export { buildAndRun, buildJS }
144
+ export { buildAndRun, buildJS };
@@ -1,54 +1,56 @@
1
- import { createFilter } from '@rollup/pluginutils'
2
- import { resolve, dirname, basename } from 'node:path'
3
- import { stylesRegExp } from '../../config.js'
1
+ import { createFilter } from '@rollup/pluginutils';
2
+ import { basename, resolve, dirname } from 'node:path';
3
+ import { stylesRegExp } from '../../config.js';
4
4
 
5
5
  /**
6
6
  *
7
7
  * @returns
8
8
  */
9
- const rollupStylesCSSImport = options => {
10
- const include = options?.filter ?? stylesRegExp
11
- const filter = createFilter(include, null)
12
- return {
13
- name: 'c-css',
14
- resolveId(source, importer) {
15
- if (filter(source) && importer) {
16
- return resolve(dirname(importer), source)
17
- }
18
- },
19
- load(id) {
20
- if (filter(id)) this.addWatchFile(resolve(id))
21
- return null
22
- },
23
- async transform(code, id) {
24
- if (!filter(id)) return null
25
- // 删除 export default css
26
- const codex = code.replace(/(export|default css)/g, '')
27
- // 使用 eval 执行代码并获取默认导出的值
28
- const evalCode = `
9
+ const rollupStylesCSSImport = (options) => {
10
+ const include = options?.filter ?? stylesRegExp;
11
+ const filter = createFilter(include, null);
12
+ return {
13
+ name: 'c-css',
14
+ resolveId(source, importer) {
15
+ if (filter(source) && importer) {
16
+ return resolve(dirname(importer), source);
17
+ }
18
+ },
19
+ load(id) {
20
+ if (filter(id))
21
+ this.addWatchFile(resolve(id));
22
+ return null;
23
+ },
24
+ async transform(code, id) {
25
+ if (!filter(id))
26
+ return null;
27
+ // 删除 export default css
28
+ const codex = code.replace(/(export|default css)/g, '');
29
+ // 使用 eval 执行代码并获取默认导出的值
30
+ const evalCode = `
29
31
  (() => {
30
32
  ${codex}
31
33
  return css;
32
34
  })()
33
- `
34
- // 得到css变量的值
35
- const csscode = eval(evalCode)
36
- // 确保最后生产的css文件
37
- const refeId = this.emitFile({
38
- // 属于静态资源
39
- type: 'asset',
40
- name: basename(`${id}.css`),
41
- // 内容
42
- source: csscode
43
- })
44
- const contents = [
45
- `const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
46
- `const fileUrl = import.meta.ROLLUP_FILE_URL_${refeId}.replace(reg, '');`,
47
- 'export default fileUrl;'
48
- ].join('\n')
49
- return contents
50
- }
51
- }
52
- }
35
+ `;
36
+ // 得到css变量的值
37
+ const csscode = eval(evalCode);
38
+ // 确保最后生产的css文件
39
+ const refeId = this.emitFile({
40
+ // 属于静态资源
41
+ type: 'asset',
42
+ name: basename(`${id}.css`),
43
+ // 内容
44
+ source: csscode
45
+ });
46
+ const contents = [
47
+ `const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
48
+ `const fileUrl = import.meta.ROLLUP_FILE_URL_${refeId}.replace(reg, '');`,
49
+ 'export default fileUrl;'
50
+ ].join('\n');
51
+ return contents;
52
+ }
53
+ };
54
+ };
53
55
 
54
- export { rollupStylesCSSImport }
56
+ export { rollupStylesCSSImport };
@@ -1,36 +1,36 @@
1
- import { resolve, dirname, basename } from 'path'
2
- import { readFileSync } from 'fs'
3
- import { assetsRegExp } from '../../config.js'
1
+ import { basename, resolve, dirname } from 'path';
2
+ import { readFileSync } from 'fs';
3
+ import { assetsRegExp } from '../../config.js';
4
4
 
5
5
  /**
6
6
  * @param {Object} options
7
7
  * @returns {Object}
8
8
  */
9
- const rollupAssets = options => {
10
- const { filter = assetsRegExp } = options ?? {}
11
- return {
12
- name: 'rollup-node-files',
13
- resolveId(source, importer) {
14
- if (filter.test(source) && importer) {
15
- return resolve(dirname(importer), source)
16
- }
17
- },
18
- load(id) {
19
- if (filter.test(id)) {
20
- const referenceId = this.emitFile({
21
- type: 'asset',
22
- name: basename(id),
23
- source: readFileSync(id)
24
- })
25
- const contents = [
26
- `const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
27
- `const fileUrl = import.meta.ROLLUP_FILE_URL_${referenceId}.replace(reg, '');`,
28
- 'export default fileUrl;'
29
- ].join('\n')
30
- return contents
31
- }
32
- }
33
- }
34
- }
9
+ const rollupAssets = (options) => {
10
+ const { filter = assetsRegExp } = options ?? {};
11
+ return {
12
+ name: 'rollup-node-files',
13
+ resolveId(source, importer) {
14
+ if (filter.test(source) && importer) {
15
+ return resolve(dirname(importer), source);
16
+ }
17
+ },
18
+ load(id) {
19
+ if (filter.test(id)) {
20
+ const referenceId = this.emitFile({
21
+ type: 'asset',
22
+ name: basename(id),
23
+ source: readFileSync(id)
24
+ });
25
+ const contents = [
26
+ `const reg = ['win32'].includes(process.platform) ? /^file:\\/\\/\\// : /^file:\\/\\// ;`,
27
+ `const fileUrl = import.meta.ROLLUP_FILE_URL_${referenceId}.replace(reg, '');`,
28
+ 'export default fileUrl;'
29
+ ].join('\n');
30
+ return contents;
31
+ }
32
+ }
33
+ };
34
+ };
35
35
 
36
- export { rollupAssets }
36
+ export { rollupAssets };
@@ -1,27 +1,26 @@
1
- import { join } from 'path'
2
- import { readdirSync } from 'fs'
1
+ import { join } from 'path';
2
+ import { readdirSync } from 'fs';
3
3
 
4
4
  /**
5
5
  * 获取指定目录下的所有 ts、js、jsx、tsx 文件
6
6
  * @param dir 目录路径
7
7
  * @returns 文件路径数组
8
8
  */
9
- const getScriptFiles = dir => {
10
- const results = []
11
- const list = readdirSync(dir, { withFileTypes: true })
12
- list.forEach(item => {
13
- const fullPath = join(dir, item.name)
14
- if (item.isDirectory()) {
15
- results.push(...getScriptFiles(fullPath))
16
- } else if (
17
- item.isFile() &&
18
- /\.(ts|js|jsx|tsx)$/.test(item.name) &&
19
- !item.name.endsWith('.d.ts')
20
- ) {
21
- results.push(fullPath)
22
- }
23
- })
24
- return results
25
- }
9
+ const getScriptFiles = (dir) => {
10
+ const results = [];
11
+ const list = readdirSync(dir, { withFileTypes: true });
12
+ list.forEach(item => {
13
+ const fullPath = join(dir, item.name);
14
+ if (item.isDirectory()) {
15
+ results.push(...getScriptFiles(fullPath));
16
+ }
17
+ else if (item.isFile() &&
18
+ /\.(ts|js|jsx|tsx)$/.test(item.name) &&
19
+ !item.name.endsWith('.d.ts')) {
20
+ results.push(fullPath);
21
+ }
22
+ });
23
+ return results;
24
+ };
26
25
 
27
- export { getScriptFiles }
26
+ export { getScriptFiles };
package/lib/store.d.ts CHANGED
@@ -1,106 +1,88 @@
1
- import { RollupCommonJSOptions } from '@rollup/plugin-commonjs'
2
- import { RollupTypescriptOptions } from '@rollup/plugin-typescript'
3
- import { RollupOptions, OutputOptions } from 'rollup'
4
- import { Alias } from './typing.js'
1
+ import { RollupCommonJSOptions } from '@rollup/plugin-commonjs';
2
+ import { RollupTypescriptOptions } from '@rollup/plugin-typescript';
3
+ import { RollupOptions, OutputOptions } from 'rollup';
4
+ import { Alias } from './typing.js';
5
5
 
6
- type PluginsValue = (options: Options) => void
7
- type PluginsCallBack =
8
- | PluginsValue
9
- | {
10
- load?: PluginsValue
11
- build?: PluginsValue
12
- }
13
- type PluginsOptions = (options: Options) => PluginsCallBack | void
6
+ type PluginsValue = (options: Options) => void;
7
+ type PluginsCallBack = PluginsValue | {
8
+ load?: PluginsValue;
9
+ build?: PluginsValue;
10
+ };
11
+ type PluginsOptions = (options: Options) => PluginsCallBack | void;
14
12
  type Options = {
15
- /**
16
- * 配置调整机及其回调插件
17
- */
18
- plugins?: PluginsOptions[]
19
- /**
20
- * 别名
21
- */
22
- alias?:
23
- | {
13
+ /**
14
+ * 配置调整机及其回调插件
15
+ */
16
+ plugins?: PluginsOptions[];
17
+ /**
18
+ * 别名
19
+ */
20
+ alias?: {
24
21
  /**
25
22
  * 别名规则
26
23
  */
27
- entries?: Alias[]
28
- }
29
- | false
30
- /**
31
- * 静态资源识别
32
- */
33
- assets?:
34
- | {
24
+ entries?: Alias[];
25
+ } | false;
26
+ /**
27
+ * 静态资源识别
28
+ */
29
+ assets?: {
35
30
  /**
36
31
  * 过滤得到指定格式的文件识别之为静态资源
37
32
  */
38
- filter?: RegExp
39
- }
40
- | false
41
- styles?:
42
- | {
33
+ filter?: RegExp;
34
+ } | false;
35
+ styles?: {
43
36
  /**
44
37
  * 过滤得到指定格式的文件识别之为静态资源
45
38
  */
46
- filter?: RegExp
47
- }
48
- | false
49
- /**
50
- * 打包时配置
51
- */
52
- build?:
53
- | {
39
+ filter?: RegExp;
40
+ } | false;
41
+ /**
42
+ * 打包时配置
43
+ */
44
+ build?: {
54
45
  /**
55
46
  * cjs文件处理
56
47
  */
57
- commonjs?: RollupCommonJSOptions | false
48
+ commonjs?: RollupCommonJSOptions | false;
58
49
  /**
59
50
  * ts配置
60
51
  */
61
- typescript?: RollupTypescriptOptions | false
52
+ typescript?: RollupTypescriptOptions | false;
62
53
  /**
63
54
  *
64
55
  */
65
- RollupOptions?: RollupOptions
56
+ RollupOptions?: RollupOptions;
66
57
  /**
67
58
  *
68
59
  */
69
60
  OutputOptions?: OutputOptions & {
70
- /**
71
- * 默认 src
72
- */
73
- input?: string
74
- }
75
- }
76
- | false
77
- }
61
+ /**
62
+ * 默认 src
63
+ */
64
+ input?: string;
65
+ };
66
+ } | false;
67
+ };
78
68
  /**
79
69
  *
80
70
  */
81
71
  declare global {
82
- var lvyConfig: Options
72
+ var lvyConfig: Options;
83
73
  }
84
74
  /**
85
75
  *
86
76
  */
87
- declare const initConfig: () => Promise<void>
77
+ declare const initConfig: () => Promise<void>;
88
78
  /**
89
79
  * @returns
90
80
  */
91
- declare const getOptions: () => Options
81
+ declare const getOptions: () => Options;
92
82
  /**
93
83
  * @param param0
94
84
  * @returns
95
85
  */
96
- declare const defineConfig: (optoins?: Options) => Options | undefined
86
+ declare const defineConfig: (optoins?: Options) => Options | undefined;
97
87
 
98
- export {
99
- type Options,
100
- type PluginsCallBack,
101
- type PluginsOptions,
102
- type PluginsValue,
103
- defineConfig,
104
- getOptions,
105
- initConfig
106
- }
88
+ export { type Options, type PluginsCallBack, type PluginsOptions, type PluginsValue, defineConfig, getOptions, initConfig };