vite-plugin-jdists 1.0.0

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/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ indent_style = space
6
+ indent_size = 4
7
+ end_of_line = lf
8
+ insert_final_newline = true
9
+ trim_trailing_whitespace = true
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # vite-plugin-jdists
2
+
3
+ ## Introduction
4
+ [![NPM version](https://img.shields.io/npm/v/vite-plugin-jdists.svg?style=flat-square)](https://npmjs.com/package/vite-plugin-jdists)
5
+
6
+ A vite plugin for [jdists](https://github.com/zswang/jdists).
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+
12
+ npm i -D vite-plugin-jdists
13
+
14
+ ## Usage
15
+ ```js vite.config.js
16
+ import { defineConfig, type PluginOption } from 'vite';
17
+
18
+ import viteJdistsPlugin from 'vite-plugin-jdists'
19
+
20
+ export default defineConfig({
21
+ ...
22
+ plugins: [
23
+ viteJdistsPlugin({
24
+ include: ['**/*.tsx', '**/*.ts'], // 修正路径模式
25
+ exclude: ['node_modules/**', 'dist/**'],
26
+ remove: ['prod'],
27
+ trigger: ['release']
28
+ }) as PluginOption
29
+ ]
30
+ ...
31
+ })
32
+
33
+ ```
@@ -0,0 +1 @@
1
+ var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`jdists`);c=s(c);let l=require(`@rollup/pluginutils`);function u(e={}){let{include:t=/\.(js|ts|jsx|tsx|vue|svelte|astro|md)$/,exclude:n=[],remove:r=[`debug`,`test`],trigger:i=[`release`],config:a,...o}=e,s=(0,l.createFilter)(t,n),u=e=>Array.isArray(e)?e.join(`,`):e;return{name:`vite-plugin-jdists`,enforce:`pre`,transform(e,t){if(!s(t))return null;try{let n={fromString:!0,path:t,...o};return r&&(n.remove=u(r)),i&&(n.trigger=u(i)),a&&(n.config=a),{code:c.default.build(e,n),map:null}}catch{return{code:e,map:null}}},handleHotUpdate(e){let{file:t,server:n,modules:r}=e;if(s(t))return console.log(`[vite-plugin-jdists] 热更新文件: ${t}`),r}}}module.exports=u;
@@ -0,0 +1,33 @@
1
+ import jdists from "jdists";
2
+ import { createFilter } from "@rollup/pluginutils";
3
+ function viteJdistsPlugin(n = {}) {
4
+ let { include: r = /\.(js|ts|jsx|tsx|vue|svelte|astro|md)$/, exclude: i = [], remove: a = ["debug", "test"], trigger: o = ["release"], config: s, ...c } = n, l = createFilter(r, i), u = (e) => Array.isArray(e) ? e.join(",") : e;
5
+ return {
6
+ name: "vite-plugin-jdists",
7
+ enforce: "pre",
8
+ transform(t, n) {
9
+ if (!l(n)) return null;
10
+ try {
11
+ let r = {
12
+ fromString: !0,
13
+ path: n,
14
+ ...c
15
+ };
16
+ return a && (r.remove = u(a)), o && (r.trigger = u(o)), s && (r.config = s), {
17
+ code: jdists.build(t, r),
18
+ map: null
19
+ };
20
+ } catch {
21
+ return {
22
+ code: t,
23
+ map: null
24
+ };
25
+ }
26
+ },
27
+ handleHotUpdate(e) {
28
+ let { file: t, server: n, modules: r } = e;
29
+ if (l(t)) return console.log(`[vite-plugin-jdists] 热更新文件: ${t}`), r;
30
+ }
31
+ };
32
+ }
33
+ export { viteJdistsPlugin as default };
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "vite-plugin-jdists",
3
+ "version": "1.0.0",
4
+ "description": "vite plugin jdists",
5
+ "author": "neverland",
6
+ "repository": "",
7
+ "main": "dist/index.cjs.js",
8
+ "scripts": {
9
+ "build": "vite build",
10
+ "dev": "vite build --watch"
11
+ },
12
+ "dependencies": {
13
+ "@rollup/pluginutils": "^5.3.0",
14
+ "jdists": "^2.2.4",
15
+ "vite": "8.0.0-beta.8"
16
+ },
17
+ "license": "MIT",
18
+ "bugs": {
19
+ "url": "https://github.com/sbramimond/vite-plugin-jdists/issues"
20
+ },
21
+ "homepage": "https://github.com/sbramimond/vite-plugin-jdists#readme"
22
+ }
23
+
24
+
25
+
package/src/index.js ADDED
@@ -0,0 +1,108 @@
1
+ import jdists from "jdists";
2
+ import { createFilter } from "@rollup/pluginutils";
3
+
4
+ /**
5
+ * Vite 插件:使用 jdists 处理代码
6
+ * @param {Object} options - 插件配置
7
+ * @returns {Object} Vite 插件对象
8
+ */
9
+ export default function viteJdistsPlugin(options = {}) {
10
+ const {
11
+ include = /\.(js|ts|jsx|tsx|vue|svelte|astro|md)$/,
12
+ exclude = [],
13
+ remove = ["debug", "test"],
14
+ trigger = ["release"],
15
+ config,
16
+ ...otherOptions
17
+ } = options;
18
+
19
+ // 创建文件过滤器
20
+ const filter = createFilter(include, exclude);
21
+
22
+ // 处理 remove 和 trigger 参数,确保格式正确
23
+ const normalizeArrayParam = (param) => {
24
+ if (Array.isArray(param)) {
25
+ return param.join(",");
26
+ }
27
+ return param;
28
+ };
29
+
30
+ return {
31
+ name: "vite-plugin-jdists",
32
+
33
+ // 强制插件在 transform 阶段运行
34
+ enforce: "pre",
35
+
36
+ /**
37
+ * 转换代码
38
+ * @param {string} code - 源代码
39
+ * @param {string} id - 文件路径
40
+ * @returns {string} 处理后的代码
41
+ */
42
+ transform(code, id) {
43
+ // 检查文件是否需要处理
44
+ if (!filter(id)) {
45
+ return null;
46
+ }
47
+
48
+ try {
49
+ // 构建 jdists 配置
50
+ const jdistsConfig = {
51
+ fromString: true,
52
+ path: id,
53
+ ...otherOptions,
54
+ };
55
+
56
+ // 添加可选的配置项
57
+ if (remove) {
58
+ jdistsConfig.remove = normalizeArrayParam(remove);
59
+ }
60
+
61
+ if (trigger) {
62
+ jdistsConfig.trigger = normalizeArrayParam(trigger);
63
+ }
64
+
65
+ if (config) {
66
+ jdistsConfig.config = config;
67
+ }
68
+
69
+ // console.log(`[vite-plugin-jdists] 处理文件: ${id}`, jdistsConfig);
70
+
71
+ // 处理代码
72
+ const result = jdists.build(code, jdistsConfig);
73
+
74
+ // 返回转换结果
75
+ return {
76
+ code: result,
77
+ map: null,
78
+ };
79
+ } catch (error) {
80
+ // 错误处理
81
+ // console.error(`[vite-plugin-jdists] 处理文件失败: ${id}`);
82
+ // console.error('错误详情:', error.message);
83
+ // console.error('当前配置:', options);
84
+
85
+ // 返回原始代码,不中断构建
86
+ return {
87
+ code,
88
+ map: null,
89
+ };
90
+ }
91
+ },
92
+
93
+ /**
94
+ * 处理热更新
95
+ * @param {Object} ctx - 热更新上下文
96
+ */
97
+ handleHotUpdate(ctx) {
98
+ const { file, server, modules } = ctx;
99
+
100
+ // 检查文件是否需要处理
101
+ if (filter(file)) {
102
+ console.log(`[vite-plugin-jdists] 热更新文件: ${file}`);
103
+ // 返回所有模块以强制重新加载
104
+ return modules;
105
+ }
106
+ },
107
+ };
108
+ }
package/vite.config.js ADDED
@@ -0,0 +1,30 @@
1
+ import { defineConfig } from 'vite';
2
+ import path from 'path';
3
+
4
+ export default defineConfig({
5
+ build: {
6
+ lib: {
7
+ entry: path.resolve(__dirname, 'src/index.js'),
8
+ name: 'vitePluginJdists',
9
+ fileName: (format) => `index.${format}.js`,
10
+ formats: ['cjs', 'es']
11
+ },
12
+ rollupOptions: {
13
+ external: [
14
+ 'vite',
15
+ 'fs',
16
+ 'path',
17
+ 'jdists',
18
+ '@rollup/pluginutils'
19
+ ],
20
+ output: {
21
+ globals: {
22
+ vite: 'vite',
23
+ jdists: 'jdists',
24
+ '@rollup/pluginutils': 'pluginutils'
25
+ }
26
+ }
27
+ }
28
+ },
29
+ plugins: []
30
+ });