@rsvelte/vite-plugin-svelte 0.5.0 → 0.5.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.
- package/package.json +5 -2
- package/src/utils/compile.js +8 -7
- package/src/utils/map-edits.js +76 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsvelte/vite-plugin-svelte",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "dominikg",
|
|
6
6
|
"files": [
|
|
@@ -34,12 +34,15 @@
|
|
|
34
34
|
},
|
|
35
35
|
"homepage": "https://github.com/baseballyama/rsvelte/tree/main/apps/npm/vite-plugin-svelte#readme",
|
|
36
36
|
"dependencies": {
|
|
37
|
+
"@jridgewell/remapping": "^2.3.5",
|
|
38
|
+
"@jridgewell/trace-mapping": "^0.3.31",
|
|
37
39
|
"@sveltejs/vite-plugin-svelte-inspector": "^5.0.0",
|
|
40
|
+
"acorn": "^8.16.0",
|
|
38
41
|
"deepmerge": "^4.3.1",
|
|
39
42
|
"magic-string": "^1.0.0",
|
|
40
43
|
"obug": "^2.1.0",
|
|
41
44
|
"vitefu": "^1.1.1",
|
|
42
|
-
"@rsvelte/vite-plugin-svelte-native": "^0.3.
|
|
45
|
+
"@rsvelte/vite-plugin-svelte-native": "^0.3.7"
|
|
43
46
|
},
|
|
44
47
|
"peerDependencies": {
|
|
45
48
|
"svelte": "^5.0.0",
|
package/src/utils/compile.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as svelte from '@rsvelte/vite-plugin-svelte-native';
|
|
2
2
|
import { safeBase64Hash } from './hash.js';
|
|
3
3
|
import { log } from './log.js';
|
|
4
|
+
import { addPartialAcceptExports, applyMapEdits } from './map-edits.js';
|
|
4
5
|
|
|
5
6
|
import { mapToRelative } from './sourcemaps.js';
|
|
6
7
|
import { enhanceCompileError } from './error.js';
|
|
@@ -109,12 +110,11 @@ export function createCompileSvelte() {
|
|
|
109
110
|
// TODO remove later
|
|
110
111
|
if (
|
|
111
112
|
options.server?.config.experimental.hmrPartialAccept &&
|
|
112
|
-
compiled.js.code.includes('import.meta.hot
|
|
113
|
+
compiled.js.code.includes('import.meta.hot')
|
|
113
114
|
) {
|
|
114
|
-
compiled
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
);
|
|
115
|
+
applyMapEdits(compiled, filename, (editor, generated) => {
|
|
116
|
+
addPartialAcceptExports(editor, generated);
|
|
117
|
+
});
|
|
118
118
|
}
|
|
119
119
|
} catch (e) {
|
|
120
120
|
enhanceCompileError(e, code, options.preprocess);
|
|
@@ -137,8 +137,9 @@ export function createCompileSvelte() {
|
|
|
137
137
|
const hasCss = compiled.css?.code?.trim()?.length ?? 0 > 0;
|
|
138
138
|
// compiler might not emit css with mode none or it may be empty
|
|
139
139
|
if (emitCss && hasCss) {
|
|
140
|
-
|
|
141
|
-
|
|
140
|
+
applyMapEdits(compiled, filename, (editor) => {
|
|
141
|
+
editor.append(`\nimport ${JSON.stringify(cssId)};\n`);
|
|
142
|
+
});
|
|
142
143
|
}
|
|
143
144
|
}
|
|
144
145
|
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import remapping from '@jridgewell/remapping';
|
|
2
|
+
import { parse } from 'acorn';
|
|
3
|
+
import MagicString from 'magic-string';
|
|
4
|
+
|
|
5
|
+
/** @param {any} callee */
|
|
6
|
+
function isHotAcceptCallee(callee) {
|
|
7
|
+
return (
|
|
8
|
+
callee.type === 'MemberExpression' &&
|
|
9
|
+
!callee.computed &&
|
|
10
|
+
callee.property.type === 'Identifier' &&
|
|
11
|
+
callee.property.name === 'accept' &&
|
|
12
|
+
callee.object.type === 'MemberExpression' &&
|
|
13
|
+
!callee.object.computed &&
|
|
14
|
+
callee.object.property.type === 'Identifier' &&
|
|
15
|
+
callee.object.property.name === 'hot' &&
|
|
16
|
+
callee.object.object.type === 'MetaProperty' &&
|
|
17
|
+
callee.object.object.meta.name === 'import' &&
|
|
18
|
+
callee.object.object.property.name === 'meta'
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {any} node
|
|
24
|
+
* @param {any[]} calls
|
|
25
|
+
*/
|
|
26
|
+
function collectHotAcceptCalls(node, calls) {
|
|
27
|
+
if (!node || typeof node !== 'object') return;
|
|
28
|
+
if (node.type === 'CallExpression' && isHotAcceptCallee(node.callee)) {
|
|
29
|
+
calls.push(node);
|
|
30
|
+
}
|
|
31
|
+
for (const value of Object.values(node)) {
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
for (const child of value) collectHotAcceptCalls(child, calls);
|
|
34
|
+
} else if (value && typeof value === 'object' && typeof value.type === 'string') {
|
|
35
|
+
collectHotAcceptCalls(value, calls);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {{ js: { code: string, map: any } }} compiled
|
|
42
|
+
* @param {string} filename
|
|
43
|
+
* @param {(editor: MagicString, code: string) => void} edit
|
|
44
|
+
*/
|
|
45
|
+
export function applyMapEdits(compiled, filename, edit) {
|
|
46
|
+
const editor = new MagicString(compiled.js.code);
|
|
47
|
+
edit(editor, compiled.js.code);
|
|
48
|
+
const previousMap = compiled.js.map;
|
|
49
|
+
compiled.js.code = editor.toString();
|
|
50
|
+
if (!previousMap) return;
|
|
51
|
+
const editMap = editor.generateMap({ hires: true, source: filename });
|
|
52
|
+
let loadedPreviousMap = false;
|
|
53
|
+
compiled.js.map = remapping(/** @type {any} */ (editMap), (source) => {
|
|
54
|
+
if (source !== filename || loadedPreviousMap) return null;
|
|
55
|
+
loadedPreviousMap = true;
|
|
56
|
+
return /** @type {any} */ (previousMap);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {MagicString} editor
|
|
62
|
+
* @param {string} code
|
|
63
|
+
*/
|
|
64
|
+
export function addPartialAcceptExports(editor, code) {
|
|
65
|
+
const calls = [];
|
|
66
|
+
collectHotAcceptCalls(parse(code, { ecmaVersion: 'latest', sourceType: 'module' }), calls);
|
|
67
|
+
for (const call of calls) {
|
|
68
|
+
const firstArgument = call.arguments[0];
|
|
69
|
+
if (!firstArgument || firstArgument.type === 'SpreadElement') continue;
|
|
70
|
+
editor.overwrite(
|
|
71
|
+
call.callee.start,
|
|
72
|
+
firstArgument.start,
|
|
73
|
+
`${code.slice(call.callee.start, call.callee.end)}Exports([\"default\"],`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|