@sveltejs/vite-plugin-svelte 1.4.0 → 2.0.0-beta.0

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.
@@ -1,96 +0,0 @@
1
- // src/preprocess.ts
2
- import path from "path";
3
- import * as vite from "vite";
4
- var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss", "sss"];
5
- var supportedScriptLangs = ["ts"];
6
- function vitePreprocess(opts) {
7
- const preprocessor = {};
8
- if (opts?.script !== false) {
9
- preprocessor.script = viteScript().script;
10
- }
11
- if (opts?.style !== false) {
12
- const styleOpts = typeof opts?.style == "object" ? opts?.style : void 0;
13
- preprocessor.style = viteStyle(styleOpts).style;
14
- }
15
- return preprocessor;
16
- }
17
- function viteScript() {
18
- return {
19
- async script({ attributes, content, filename = "" }) {
20
- const lang = attributes.lang;
21
- if (!supportedScriptLangs.includes(lang))
22
- return;
23
- const transformResult = await vite.transformWithEsbuild(content, filename, {
24
- loader: lang,
25
- target: "esnext",
26
- tsconfigRaw: {
27
- compilerOptions: {
28
- importsNotUsedAsValues: "preserve",
29
- preserveValueImports: true
30
- }
31
- }
32
- });
33
- return {
34
- code: transformResult.code,
35
- map: transformResult.map
36
- };
37
- }
38
- };
39
- }
40
- function viteStyle(config = {}) {
41
- let transform;
42
- const style = async ({ attributes, content, filename = "" }) => {
43
- const lang = attributes.lang;
44
- if (!supportedStyleLangs.includes(lang))
45
- return;
46
- if (!transform) {
47
- let resolvedConfig;
48
- if (style.__resolvedConfig) {
49
- resolvedConfig = style.__resolvedConfig;
50
- } else if (isResolvedConfig(config)) {
51
- resolvedConfig = config;
52
- } else {
53
- resolvedConfig = await vite.resolveConfig(
54
- config,
55
- process.env.NODE_ENV === "production" ? "build" : "serve"
56
- );
57
- }
58
- transform = getCssTransformFn(resolvedConfig);
59
- }
60
- const moduleId = `${filename}.${lang}`;
61
- const result = await transform(content, moduleId);
62
- if (result.map?.sources?.[0] === moduleId) {
63
- result.map.sources[0] = path.basename(filename);
64
- }
65
- return {
66
- code: result.code,
67
- map: result.map ?? void 0
68
- };
69
- };
70
- style.__resolvedConfig = null;
71
- return { style };
72
- }
73
- function getCssTransformFn(config) {
74
- if (vite.preprocessCSS) {
75
- return async (code, filename) => {
76
- return vite.preprocessCSS(code, filename, config);
77
- };
78
- } else {
79
- const pluginName = "vite:css";
80
- const plugin = config.plugins.find((p) => p.name === pluginName);
81
- if (!plugin) {
82
- throw new Error(`failed to find plugin ${pluginName}`);
83
- }
84
- if (!plugin.transform) {
85
- throw new Error(`plugin ${pluginName} has no transform`);
86
- }
87
- return plugin.transform.bind(null);
88
- }
89
- }
90
- function isResolvedConfig(config) {
91
- return !!config.inlineConfig;
92
- }
93
- export {
94
- vitePreprocess
95
- };
96
- //# sourceMappingURL=preprocess.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/preprocess.ts"],"sourcesContent":["import path from 'path';\nimport * as vite from 'vite';\nimport type { ESBuildOptions, ResolvedConfig } from 'vite';\n// eslint-disable-next-line node/no-missing-import\nimport type { Preprocessor, PreprocessorGroup } from 'svelte/types/compiler/preprocess';\n\nconst supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss', 'sss'];\nconst supportedScriptLangs = ['ts'];\n\nexport function vitePreprocess(opts?: {\n\tscript?: boolean;\n\tstyle?: boolean | vite.InlineConfig | vite.ResolvedConfig;\n}) {\n\tconst preprocessor: PreprocessorGroup = {};\n\tif (opts?.script !== false) {\n\t\tpreprocessor.script = viteScript().script;\n\t}\n\tif (opts?.style !== false) {\n\t\tconst styleOpts = typeof opts?.style == 'object' ? opts?.style : undefined;\n\t\tpreprocessor.style = viteStyle(styleOpts).style;\n\t}\n\treturn preprocessor;\n}\n\nfunction viteScript(): { script: Preprocessor } {\n\treturn {\n\t\tasync script({ attributes, content, filename = '' }) {\n\t\t\tconst lang = attributes.lang as string;\n\t\t\tif (!supportedScriptLangs.includes(lang)) return;\n\t\t\tconst transformResult = await vite.transformWithEsbuild(content, filename, {\n\t\t\t\tloader: lang as ESBuildOptions['loader'],\n\t\t\t\ttarget: 'esnext',\n\t\t\t\ttsconfigRaw: {\n\t\t\t\t\tcompilerOptions: {\n\t\t\t\t\t\t// svelte typescript needs this flag to work with type imports\n\t\t\t\t\t\timportsNotUsedAsValues: 'preserve',\n\t\t\t\t\t\tpreserveValueImports: true\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t});\n\t\t\treturn {\n\t\t\t\tcode: transformResult.code,\n\t\t\t\tmap: transformResult.map\n\t\t\t};\n\t\t}\n\t};\n}\n\nfunction viteStyle(config: vite.InlineConfig | vite.ResolvedConfig = {}): {\n\tstyle: Preprocessor;\n} {\n\tlet transform: CssTransform;\n\tconst style: Preprocessor = async ({ attributes, content, filename = '' }) => {\n\t\tconst lang = attributes.lang as string;\n\t\tif (!supportedStyleLangs.includes(lang)) return;\n\t\tif (!transform) {\n\t\t\tlet resolvedConfig: vite.ResolvedConfig;\n\t\t\t// @ts-expect-error special prop added if running in v-p-s\n\t\t\tif (style.__resolvedConfig) {\n\t\t\t\t// @ts-expect-error\n\t\t\t\tresolvedConfig = style.__resolvedConfig;\n\t\t\t} else if (isResolvedConfig(config)) {\n\t\t\t\tresolvedConfig = config;\n\t\t\t} else {\n\t\t\t\tresolvedConfig = await vite.resolveConfig(\n\t\t\t\t\tconfig,\n\t\t\t\t\tprocess.env.NODE_ENV === 'production' ? 'build' : 'serve'\n\t\t\t\t);\n\t\t\t}\n\t\t\ttransform = getCssTransformFn(resolvedConfig);\n\t\t}\n\t\tconst moduleId = `${filename}.${lang}`;\n\t\tconst result = await transform(content, moduleId);\n\t\t// patch sourcemap source to point back to original filename\n\t\tif (result.map?.sources?.[0] === moduleId) {\n\t\t\tresult.map.sources[0] = path.basename(filename);\n\t\t}\n\t\treturn {\n\t\t\tcode: result.code,\n\t\t\tmap: result.map ?? undefined\n\t\t};\n\t};\n\t// @ts-expect-error tag so can be found by v-p-s\n\tstyle.__resolvedConfig = null;\n\treturn { style };\n}\n\n// eslint-disable-next-line no-unused-vars\ntype CssTransform = (code: string, filename: string) => Promise<{ code: string; map?: any }>;\n\nfunction getCssTransformFn(config: ResolvedConfig): CssTransform {\n\t// API is only available in Vite 3.2 and above\n\t// TODO: Remove Vite plugin hack when bump peer dep to Vite 3.2\n\tif (vite.preprocessCSS) {\n\t\treturn async (code, filename) => {\n\t\t\treturn vite.preprocessCSS(code, filename, config);\n\t\t};\n\t} else {\n\t\tconst pluginName = 'vite:css';\n\t\tconst plugin = config.plugins.find((p) => p.name === pluginName);\n\t\tif (!plugin) {\n\t\t\tthrow new Error(`failed to find plugin ${pluginName}`);\n\t\t}\n\t\tif (!plugin.transform) {\n\t\t\tthrow new Error(`plugin ${pluginName} has no transform`);\n\t\t}\n\t\t// @ts-expect-error\n\t\treturn plugin.transform.bind(null);\n\t}\n}\n\nfunction isResolvedConfig(config: any): config is vite.ResolvedConfig {\n\treturn !!config.inlineConfig;\n}\n"],"mappings":";AAAA,OAAO,UAAU;AACjB,YAAY,UAAU;AAKtB,IAAM,sBAAsB,CAAC,OAAO,QAAQ,QAAQ,QAAQ,QAAQ,UAAU,WAAW,KAAK;AAC9F,IAAM,uBAAuB,CAAC,IAAI;AAE3B,SAAS,eAAe,MAG5B;AACF,QAAM,eAAkC,CAAC;AACzC,MAAI,MAAM,WAAW,OAAO;AAC3B,iBAAa,SAAS,WAAW,EAAE;AAAA,EACpC;AACA,MAAI,MAAM,UAAU,OAAO;AAC1B,UAAM,YAAY,OAAO,MAAM,SAAS,WAAW,MAAM,QAAQ;AACjE,iBAAa,QAAQ,UAAU,SAAS,EAAE;AAAA,EAC3C;AACA,SAAO;AACR;AAEA,SAAS,aAAuC;AAC/C,SAAO;AAAA,IACN,MAAM,OAAO,EAAE,YAAY,SAAS,WAAW,GAAG,GAAG;AACpD,YAAM,OAAO,WAAW;AACxB,UAAI,CAAC,qBAAqB,SAAS,IAAI;AAAG;AAC1C,YAAM,kBAA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