@sveltejs/vite-plugin-svelte 1.1.0 → 1.1.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/dist/index.cjs +77 -102
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +72 -99
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
- package/src/utils/preprocess.ts +33 -26
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltejs/vite-plugin-svelte",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "dominikg",
|
|
6
6
|
"files": [
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"types": "dist/index.d.ts",
|
|
17
17
|
"exports": {
|
|
18
18
|
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
19
20
|
"import": "./dist/index.js",
|
|
20
21
|
"require": "./dist/index.cjs"
|
|
21
22
|
},
|
|
@@ -61,11 +62,11 @@
|
|
|
61
62
|
"@types/debug": "^4.1.7",
|
|
62
63
|
"@types/diff-match-patch": "^1.0.32",
|
|
63
64
|
"diff-match-patch": "^1.0.5",
|
|
64
|
-
"esbuild": "^0.15.
|
|
65
|
+
"esbuild": "^0.15.13",
|
|
65
66
|
"rollup": "^2.79.1",
|
|
66
67
|
"svelte": "^3.52.0",
|
|
67
|
-
"tsup": "^6.
|
|
68
|
-
"vite": "^3.
|
|
68
|
+
"tsup": "^6.4.0",
|
|
69
|
+
"vite": "^3.2.3"
|
|
69
70
|
},
|
|
70
71
|
"scripts": {
|
|
71
72
|
"dev": "pnpm build:ci --sourcemap --watch src",
|
package/src/utils/preprocess.ts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
ESBuildOptions,
|
|
4
|
-
ResolvedConfig,
|
|
5
|
-
TransformResult,
|
|
6
|
-
Plugin
|
|
7
|
-
} from 'vite';
|
|
1
|
+
import * as vite from 'vite';
|
|
2
|
+
import type { ESBuildOptions, ResolvedConfig, Plugin } from 'vite';
|
|
8
3
|
import MagicString from 'magic-string';
|
|
9
4
|
import { preprocess } from 'svelte/compiler';
|
|
10
5
|
import { Preprocessor, PreprocessorGroup, Processed, ResolvedOptions } from './options';
|
|
11
|
-
import { TransformPluginContext } from 'rollup';
|
|
12
6
|
import { log } from './log';
|
|
13
7
|
import { buildSourceMap } from './sourcemap';
|
|
14
8
|
import path from 'path';
|
|
@@ -21,7 +15,7 @@ function createViteScriptPreprocessor(): Preprocessor {
|
|
|
21
15
|
return async ({ attributes, content, filename = '' }) => {
|
|
22
16
|
const lang = attributes.lang as string;
|
|
23
17
|
if (!supportedScriptLangs.includes(lang)) return;
|
|
24
|
-
const transformResult = await transformWithEsbuild(content, filename, {
|
|
18
|
+
const transformResult = await vite.transformWithEsbuild(content, filename, {
|
|
25
19
|
loader: lang as ESBuildOptions['loader'],
|
|
26
20
|
target: 'esnext',
|
|
27
21
|
tsconfigRaw: {
|
|
@@ -40,34 +34,47 @@ function createViteScriptPreprocessor(): Preprocessor {
|
|
|
40
34
|
}
|
|
41
35
|
|
|
42
36
|
function createViteStylePreprocessor(config: ResolvedConfig): Preprocessor {
|
|
43
|
-
const
|
|
44
|
-
const plugin = config.plugins.find((p) => p.name === pluginName);
|
|
45
|
-
if (!plugin) {
|
|
46
|
-
throw new Error(`failed to find plugin ${pluginName}`);
|
|
47
|
-
}
|
|
48
|
-
if (!plugin.transform) {
|
|
49
|
-
throw new Error(`plugin ${pluginName} has no transform`);
|
|
50
|
-
}
|
|
51
|
-
const pluginTransform = plugin.transform!.bind(null as unknown as TransformPluginContext);
|
|
37
|
+
const transform = getCssTransformFn(config);
|
|
52
38
|
return async ({ attributes, content, filename = '' }) => {
|
|
53
39
|
const lang = attributes.lang as string;
|
|
54
40
|
if (!supportedStyleLangs.includes(lang)) return;
|
|
55
41
|
const moduleId = `${filename}.${lang}`;
|
|
56
|
-
const
|
|
57
|
-
content,
|
|
58
|
-
moduleId
|
|
59
|
-
)) as TransformResult;
|
|
42
|
+
const result = await transform(content, moduleId);
|
|
60
43
|
// patch sourcemap source to point back to original filename
|
|
61
|
-
if (
|
|
62
|
-
|
|
44
|
+
if (result.map?.sources?.[0] === moduleId) {
|
|
45
|
+
result.map.sources[0] = path.basename(filename);
|
|
63
46
|
}
|
|
64
47
|
return {
|
|
65
|
-
code:
|
|
66
|
-
map:
|
|
48
|
+
code: result.code,
|
|
49
|
+
map: result.map ?? undefined
|
|
67
50
|
};
|
|
68
51
|
};
|
|
69
52
|
}
|
|
70
53
|
|
|
54
|
+
// eslint-disable-next-line no-unused-vars
|
|
55
|
+
type CssTransform = (code: string, filename: string) => Promise<{ code: string; map?: any }>;
|
|
56
|
+
|
|
57
|
+
function getCssTransformFn(config: ResolvedConfig): CssTransform {
|
|
58
|
+
// API is only available in Vite 3.2 and above
|
|
59
|
+
// TODO: Remove Vite plugin hack when bump peer dep to Vite 3.2
|
|
60
|
+
if (vite.preprocessCSS) {
|
|
61
|
+
return async (code, filename) => {
|
|
62
|
+
return vite.preprocessCSS(code, filename, config);
|
|
63
|
+
};
|
|
64
|
+
} else {
|
|
65
|
+
const pluginName = 'vite:css';
|
|
66
|
+
const plugin = config.plugins.find((p) => p.name === pluginName);
|
|
67
|
+
if (!plugin) {
|
|
68
|
+
throw new Error(`failed to find plugin ${pluginName}`);
|
|
69
|
+
}
|
|
70
|
+
if (!plugin.transform) {
|
|
71
|
+
throw new Error(`plugin ${pluginName} has no transform`);
|
|
72
|
+
}
|
|
73
|
+
// @ts-expect-error
|
|
74
|
+
return plugin.transform.bind(null);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
71
78
|
function createVitePreprocessorGroup(config: ResolvedConfig): PreprocessorGroup {
|
|
72
79
|
return {
|
|
73
80
|
markup({ content, filename }) {
|