@sveltejs/vite-plugin-svelte 2.0.0-beta.1 → 2.0.0-beta.3
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.d.ts +2 -11
- package/dist/index.js +127 -136
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +21 -1
- package/src/preprocess.ts +11 -26
- package/src/utils/options.ts +0 -9
- package/src/utils/preprocess.ts +0 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltejs/vite-plugin-svelte",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "dominikg",
|
|
6
6
|
"files": [
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/debug": "^4.1.7",
|
|
53
|
-
"esbuild": "^0.16.
|
|
53
|
+
"esbuild": "^0.16.3",
|
|
54
54
|
"rollup": "^2.79.1",
|
|
55
55
|
"svelte": "^3.54.0",
|
|
56
56
|
"tsup": "^6.5.0",
|
package/src/index.ts
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
|
|
19
19
|
import { ensureWatchedFile, setupWatchers } from './utils/watch';
|
|
20
20
|
import { resolveViaPackageJsonSvelte } from './utils/resolve';
|
|
21
|
+
import { PartialResolvedId } from 'rollup';
|
|
21
22
|
import { toRollupError } from './utils/error';
|
|
22
23
|
import { saveSvelteMetadata } from './utils/optimizer';
|
|
23
24
|
import { svelteInspector } from './ui/inspector/plugin';
|
|
@@ -46,7 +47,9 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
46
47
|
let viteConfig: ResolvedConfig;
|
|
47
48
|
/* eslint-disable no-unused-vars */
|
|
48
49
|
let compileSvelte: CompileSvelte;
|
|
50
|
+
/* eslint-enable no-unused-vars */
|
|
49
51
|
|
|
52
|
+
let resolvedSvelteSSR: Promise<PartialResolvedId | null>;
|
|
50
53
|
const api: PluginAPI = {};
|
|
51
54
|
const plugins: Plugin[] = [
|
|
52
55
|
{
|
|
@@ -86,7 +89,6 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
86
89
|
if (isSvelteMetadataChanged) {
|
|
87
90
|
// Force Vite to optimize again. Although we mutate the config here, it works because
|
|
88
91
|
// Vite's optimizer runs after `buildStart()`.
|
|
89
|
-
// TODO: verify this works in vite3
|
|
90
92
|
viteConfig.optimizeDeps.force = true;
|
|
91
93
|
}
|
|
92
94
|
},
|
|
@@ -133,6 +135,24 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
133
135
|
}
|
|
134
136
|
}
|
|
135
137
|
|
|
138
|
+
if (ssr && importee === 'svelte') {
|
|
139
|
+
if (!resolvedSvelteSSR) {
|
|
140
|
+
resolvedSvelteSSR = this.resolve('svelte/ssr', undefined, { skipSelf: true }).then(
|
|
141
|
+
(svelteSSR) => {
|
|
142
|
+
log.debug('resolved svelte to svelte/ssr');
|
|
143
|
+
return svelteSSR;
|
|
144
|
+
},
|
|
145
|
+
(err) => {
|
|
146
|
+
log.debug(
|
|
147
|
+
'failed to resolve svelte to svelte/ssr. Update svelte to a version that exports it',
|
|
148
|
+
err
|
|
149
|
+
);
|
|
150
|
+
return null; // returning null here leads to svelte getting resolved regularly
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return resolvedSvelteSSR;
|
|
155
|
+
}
|
|
136
156
|
//@ts-expect-error scan
|
|
137
157
|
const scan = !!opts?.scan; // scanner phase of optimizeDeps
|
|
138
158
|
const isPrebundled =
|
package/src/preprocess.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import
|
|
3
|
-
import type { ESBuildOptions, ResolvedConfig } from 'vite';
|
|
2
|
+
import { preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
|
|
3
|
+
import type { ESBuildOptions, InlineConfig, ResolvedConfig } from 'vite';
|
|
4
4
|
// eslint-disable-next-line node/no-missing-import
|
|
5
5
|
import type { Preprocessor, PreprocessorGroup } from 'svelte/types/compiler/preprocess';
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ const supportedScriptLangs = ['ts'];
|
|
|
9
9
|
|
|
10
10
|
export function vitePreprocess(opts?: {
|
|
11
11
|
script?: boolean;
|
|
12
|
-
style?: boolean |
|
|
12
|
+
style?: boolean | InlineConfig | ResolvedConfig;
|
|
13
13
|
}) {
|
|
14
14
|
const preprocessor: PreprocessorGroup = {};
|
|
15
15
|
if (opts?.script !== false) {
|
|
@@ -27,7 +27,7 @@ function viteScript(): { script: Preprocessor } {
|
|
|
27
27
|
async script({ attributes, content, filename = '' }) {
|
|
28
28
|
const lang = attributes.lang as string;
|
|
29
29
|
if (!supportedScriptLangs.includes(lang)) return;
|
|
30
|
-
const transformResult = await
|
|
30
|
+
const transformResult = await transformWithEsbuild(content, filename, {
|
|
31
31
|
loader: lang as ESBuildOptions['loader'],
|
|
32
32
|
target: 'esnext',
|
|
33
33
|
tsconfigRaw: {
|
|
@@ -46,7 +46,7 @@ function viteScript(): { script: Preprocessor } {
|
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
function viteStyle(config:
|
|
49
|
+
function viteStyle(config: InlineConfig | ResolvedConfig = {}): {
|
|
50
50
|
style: Preprocessor;
|
|
51
51
|
} {
|
|
52
52
|
let transform: CssTransform;
|
|
@@ -54,7 +54,7 @@ function viteStyle(config: vite.InlineConfig | vite.ResolvedConfig = {}): {
|
|
|
54
54
|
const lang = attributes.lang as string;
|
|
55
55
|
if (!supportedStyleLangs.includes(lang)) return;
|
|
56
56
|
if (!transform) {
|
|
57
|
-
let resolvedConfig:
|
|
57
|
+
let resolvedConfig: ResolvedConfig;
|
|
58
58
|
// @ts-expect-error special prop added if running in v-p-s
|
|
59
59
|
if (style.__resolvedConfig) {
|
|
60
60
|
// @ts-expect-error
|
|
@@ -62,7 +62,7 @@ function viteStyle(config: vite.InlineConfig | vite.ResolvedConfig = {}): {
|
|
|
62
62
|
} else if (isResolvedConfig(config)) {
|
|
63
63
|
resolvedConfig = config;
|
|
64
64
|
} else {
|
|
65
|
-
resolvedConfig = await
|
|
65
|
+
resolvedConfig = await resolveConfig(
|
|
66
66
|
config,
|
|
67
67
|
process.env.NODE_ENV === 'production' ? 'build' : 'serve'
|
|
68
68
|
);
|
|
@@ -89,26 +89,11 @@ function viteStyle(config: vite.InlineConfig | vite.ResolvedConfig = {}): {
|
|
|
89
89
|
type CssTransform = (code: string, filename: string) => Promise<{ code: string; map?: any }>;
|
|
90
90
|
|
|
91
91
|
function getCssTransformFn(config: ResolvedConfig): CssTransform {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return async (code, filename) => {
|
|
96
|
-
return vite.preprocessCSS(code, filename, config);
|
|
97
|
-
};
|
|
98
|
-
} else {
|
|
99
|
-
const pluginName = 'vite:css';
|
|
100
|
-
const plugin = config.plugins.find((p) => p.name === pluginName);
|
|
101
|
-
if (!plugin) {
|
|
102
|
-
throw new Error(`failed to find plugin ${pluginName}`);
|
|
103
|
-
}
|
|
104
|
-
if (!plugin.transform) {
|
|
105
|
-
throw new Error(`plugin ${pluginName} has no transform`);
|
|
106
|
-
}
|
|
107
|
-
// @ts-expect-error
|
|
108
|
-
return plugin.transform.bind(null);
|
|
109
|
-
}
|
|
92
|
+
return async (code, filename) => {
|
|
93
|
+
return preprocessCSS(code, filename, config);
|
|
94
|
+
};
|
|
110
95
|
}
|
|
111
96
|
|
|
112
|
-
function isResolvedConfig(config: any): config is
|
|
97
|
+
function isResolvedConfig(config: any): config is ResolvedConfig {
|
|
113
98
|
return !!config.inlineConfig;
|
|
114
99
|
}
|
package/src/utils/options.ts
CHANGED
|
@@ -680,15 +680,6 @@ export interface SvelteOptions {
|
|
|
680
680
|
* These options are considered experimental and breaking changes to them can occur in any release
|
|
681
681
|
*/
|
|
682
682
|
export interface ExperimentalOptions {
|
|
683
|
-
/**
|
|
684
|
-
* Use extra preprocessors that delegate style and TypeScript preprocessing to native Vite plugins
|
|
685
|
-
*
|
|
686
|
-
* Do not use together with `svelte-preprocess`!
|
|
687
|
-
*
|
|
688
|
-
* @default false
|
|
689
|
-
*/
|
|
690
|
-
useVitePreprocess?: boolean;
|
|
691
|
-
|
|
692
683
|
/**
|
|
693
684
|
* A function to update `compilerOptions` before compilation
|
|
694
685
|
*
|
package/src/utils/preprocess.ts
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
1
|
import type { ResolvedConfig, Plugin } from 'vite';
|
|
2
2
|
import MagicString from 'magic-string';
|
|
3
|
-
import { preprocess } from 'svelte/compiler';
|
|
4
3
|
import { PreprocessorGroup, ResolvedOptions } from './options';
|
|
5
4
|
import { log } from './log';
|
|
6
5
|
import path from 'path';
|
|
7
|
-
import { vitePreprocess } from '../preprocess';
|
|
8
|
-
|
|
9
|
-
function createVitePreprocessorGroup(config: ResolvedConfig): PreprocessorGroup {
|
|
10
|
-
return {
|
|
11
|
-
markup({ content, filename }) {
|
|
12
|
-
return preprocess(content, vitePreprocess({ style: config }), { filename });
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
6
|
|
|
17
7
|
/**
|
|
18
8
|
* this appends a *{} rule to component styles to force the svelte compiler to add style classes to all nodes
|
|
@@ -40,13 +30,6 @@ function buildExtraPreprocessors(options: ResolvedOptions, config: ResolvedConfi
|
|
|
40
30
|
const prependPreprocessors: PreprocessorGroup[] = [];
|
|
41
31
|
const appendPreprocessors: PreprocessorGroup[] = [];
|
|
42
32
|
|
|
43
|
-
if (options.experimental?.useVitePreprocess) {
|
|
44
|
-
log.warn(
|
|
45
|
-
'`experimental.useVitePreprocess` is deprecated. Use the `vitePreprocess()` preprocessor instead. See https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/preprocess.md for more information.'
|
|
46
|
-
);
|
|
47
|
-
prependPreprocessors.push(createVitePreprocessorGroup(config));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
33
|
// @ts-ignore
|
|
51
34
|
const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p?.sveltePreprocess);
|
|
52
35
|
if (pluginsWithPreprocessorsDeprecated.length > 0) {
|