@porotkin/vite-plugin-react-kotlinjs 0.0.3 → 0.0.4
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/README.md +1 -1
- package/package.json +1 -2
- package/src/refresh-wrapper.js +1 -0
- package/src/vite-babel.js +10 -97
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ This plugin is inspired by the original
|
|
|
10
10
|
Add the following dependency to your `build.gradle.kts`:
|
|
11
11
|
```kotlin
|
|
12
12
|
dependencies {
|
|
13
|
-
jsMainImplementation(devNpm("@porotkin/vite-plugin-react-kotlinjs", "^0.0.
|
|
13
|
+
jsMainImplementation(devNpm("@porotkin/vite-plugin-react-kotlinjs", "^0.0.4"))
|
|
14
14
|
}
|
|
15
15
|
```
|
|
16
16
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@porotkin/vite-plugin-react-kotlinjs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Support React fast refresh in Kotlin/JS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Mikhail Porotkin",
|
|
@@ -21,7 +21,6 @@
|
|
|
21
21
|
"postinstall": "node copyRefreshRuntime.js"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@babel/core": "^7.28.4",
|
|
25
24
|
"@rolldown/pluginutils": "1.0.0-beta.41",
|
|
26
25
|
"react-refresh": "^0.17.0"
|
|
27
26
|
},
|
package/src/refresh-wrapper.js
CHANGED
package/src/vite-babel.js
CHANGED
|
@@ -1,111 +1,24 @@
|
|
|
1
1
|
import {addRefreshWrapper} from "./refresh-wrapper.js";
|
|
2
2
|
|
|
3
|
-
let babel;
|
|
4
|
-
|
|
5
|
-
async function loadBabel() {
|
|
6
|
-
if (!babel) babel = await import("@babel/core");
|
|
7
|
-
return babel;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
let isProduction = true;
|
|
11
|
-
let projectRoot = process.cwd();
|
|
12
|
-
let runPluginOverrides;
|
|
13
|
-
|
|
14
|
-
function canSkipBabel(plugins, babelOptions) {
|
|
15
|
-
return !(plugins.length || babelOptions.presets.length || babelOptions.configFile || babelOptions.babelrc);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const loadedPlugin = /* @__PURE__ */ new Map();
|
|
19
|
-
|
|
20
|
-
function loadPlugin(path) {
|
|
21
|
-
const cached = loadedPlugin.get(path);
|
|
22
|
-
if (cached) return cached;
|
|
23
|
-
const promise = import(path).then((module) => {
|
|
24
|
-
const value = module.default || module;
|
|
25
|
-
loadedPlugin.set(path, value);
|
|
26
|
-
return value;
|
|
27
|
-
});
|
|
28
|
-
loadedPlugin.set(path, promise);
|
|
29
|
-
return promise;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function createBabelOptions(rawOptions) {
|
|
33
|
-
const babelOptions = {
|
|
34
|
-
babelrc: false,
|
|
35
|
-
configFile: false,
|
|
36
|
-
...rawOptions
|
|
37
|
-
};
|
|
38
|
-
babelOptions.plugins ||= [];
|
|
39
|
-
babelOptions.presets ||= [];
|
|
40
|
-
babelOptions.overrides ||= [];
|
|
41
|
-
babelOptions.parserOpts ||= {};
|
|
42
|
-
babelOptions.parserOpts.plugins ||= [];
|
|
43
|
-
return babelOptions;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
3
|
export const viteBabelPlugin = (mainOptions) => ({
|
|
47
4
|
name: "vite:react-plugin-kotlinjs",
|
|
48
5
|
enforce: "pre",
|
|
49
6
|
configResolved(config) {
|
|
50
|
-
|
|
51
|
-
isProduction = config.isProduction;
|
|
52
|
-
mainOptions.skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
|
|
53
|
-
const hooks = config.plugins.map((plugin) => plugin.api?.reactBabel).filter(function (value) {
|
|
54
|
-
return value !== void 0;
|
|
55
|
-
});
|
|
56
|
-
if (hooks.length > 0) runPluginOverrides = (babelOptions, context) => {
|
|
57
|
-
hooks.forEach((hook) => hook(babelOptions, context, config));
|
|
58
|
-
};
|
|
7
|
+
mainOptions.skipFastRefresh = config.isProduction || config.command === "build" || config.server.hmr === false;
|
|
59
8
|
},
|
|
60
9
|
transform: {
|
|
61
|
-
async handler(code, id
|
|
10
|
+
async handler(code, id) {
|
|
62
11
|
const [filepath] = id.split("?");
|
|
63
12
|
if (!mainOptions?.filter(filepath) || !mainOptions?.getComponentName(code)) return;
|
|
64
|
-
const ssr = options?.ssr === true;
|
|
65
|
-
const babelOptions = (() => {
|
|
66
|
-
const newBabelOptions = createBabelOptions({});
|
|
67
|
-
runPluginOverrides?.(newBabelOptions, {
|
|
68
|
-
id,
|
|
69
|
-
ssr
|
|
70
|
-
});
|
|
71
|
-
return newBabelOptions;
|
|
72
|
-
})();
|
|
73
|
-
const plugins = [...babelOptions.plugins];
|
|
74
13
|
const useFastRefresh = !mainOptions.skipFastRefresh;
|
|
75
|
-
if (useFastRefresh)
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
sourceFileName: filepath,
|
|
84
|
-
retainLines: !isProduction,
|
|
85
|
-
parserOpts: {
|
|
86
|
-
...babelOptions.parserOpts,
|
|
87
|
-
sourceType: "module",
|
|
88
|
-
allowAwaitOutsideFunction: true,
|
|
89
|
-
plugins: parserPlugins
|
|
90
|
-
},
|
|
91
|
-
generatorOpts: {
|
|
92
|
-
...babelOptions.generatorOpts,
|
|
93
|
-
importAttributesKeyword: "with",
|
|
94
|
-
decoratorsBeforeExport: true
|
|
95
|
-
},
|
|
96
|
-
plugins,
|
|
97
|
-
sourceMaps: true
|
|
98
|
-
});
|
|
99
|
-
if (result) {
|
|
100
|
-
if (!useFastRefresh) return {
|
|
101
|
-
code: result.code,
|
|
102
|
-
map: result.map
|
|
103
|
-
};
|
|
104
|
-
return {
|
|
105
|
-
code: addRefreshWrapper(result.code, "vite:react-plugin-kotlinjs", id, mainOptions?.getComponentName) ?? result.code,
|
|
106
|
-
map: result.map
|
|
107
|
-
};
|
|
108
|
-
}
|
|
14
|
+
if (!useFastRefresh) return {
|
|
15
|
+
code: code,
|
|
16
|
+
map: null,
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
code: addRefreshWrapper(code, "vite:react-plugin-kotlinjs", id, mainOptions?.getComponentName),
|
|
20
|
+
map: null,
|
|
21
|
+
};
|
|
109
22
|
}
|
|
110
23
|
}
|
|
111
24
|
})
|