@porotkin/vite-plugin-react-kotlinjs 0.0.2 → 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 +3 -14
- 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
|
@@ -8,27 +8,15 @@ window.$RefreshSig$ = () => (type) => type;`
|
|
|
8
8
|
|
|
9
9
|
export const getPreambleCode = () => preambleCode
|
|
10
10
|
|
|
11
|
-
function capitalize(str) {
|
|
12
|
-
if (!str) return '';
|
|
13
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
11
|
export function addRefreshWrapper(code, pluginName, id, getComponentName) {
|
|
17
12
|
const componentName = getComponentName?.(code)
|
|
18
13
|
if (!componentName) return code;
|
|
19
14
|
|
|
20
|
-
const packageName = id.split('/kotlin/').pop()
|
|
21
|
-
.replace(`${componentName}.mjs`, '')
|
|
22
|
-
.split(/[\/\\-]/g)
|
|
23
|
-
.map(part => capitalize(part))
|
|
24
|
-
.join('')
|
|
25
|
-
|
|
26
15
|
const registerHmr = `
|
|
27
16
|
import * as RefreshRuntime from "${runtimePublicPath}";
|
|
28
17
|
const inWebWorker = typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope;
|
|
29
18
|
if (import.meta.hot && !inWebWorker) {
|
|
30
|
-
|
|
31
|
-
$RefreshReg$(component, "${componentName + packageName}")
|
|
19
|
+
$RefreshReg$(get_${componentName}?.(), "${componentName}")
|
|
32
20
|
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
|
33
21
|
RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify(id)}, currentExports);
|
|
34
22
|
import.meta.hot.accept((nextExports) => {
|
|
@@ -39,6 +27,7 @@ if (import.meta.hot && !inWebWorker) {
|
|
|
39
27
|
});
|
|
40
28
|
}
|
|
41
29
|
function normalizeExports(value) {
|
|
30
|
+
if (!value) return undefined;
|
|
42
31
|
const [[key, type]] = Object.entries(value)
|
|
43
32
|
const name = key.replace("get_", "");
|
|
44
33
|
return {
|
|
@@ -46,7 +35,7 @@ function normalizeExports(value) {
|
|
|
46
35
|
}
|
|
47
36
|
}
|
|
48
37
|
function $RefreshReg$(type, id) {
|
|
49
|
-
return RefreshRuntime.register(type,
|
|
38
|
+
return RefreshRuntime.register(type, ${JSON.stringify(id)} + " " + id);
|
|
50
39
|
}
|
|
51
40
|
function $RefreshSig$() {
|
|
52
41
|
return RefreshRuntime.createSignatureFunctionForTransform();
|
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
|
})
|