@sveltejs/vite-plugin-svelte 1.3.1 → 1.4.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.
- package/dist/index.cjs +456 -346
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -13
- package/dist/index.js +448 -339
- package/dist/index.js.map +1 -1
- package/dist/preprocess.cjs +127 -0
- package/dist/preprocess.cjs.map +1 -0
- package/dist/preprocess.d.ts +9 -0
- package/dist/preprocess.js +96 -0
- package/dist/preprocess.js.map +1 -0
- package/package.json +5 -15
- package/src/handle-hot-update.ts +24 -24
- package/src/index.ts +23 -24
- package/src/preprocess.ts +114 -0
- package/src/utils/compile.ts +71 -23
- package/src/utils/id.ts +55 -3
- package/src/utils/load-raw.ts +132 -0
- package/src/utils/options.ts +16 -9
- package/src/utils/preprocess.ts +9 -150
- package/src/utils/__tests__/sourcemap.spec.ts +0 -25
- package/src/utils/sourcemap.ts +0 -58
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
|
|
26
|
+
// src/preprocess.ts
|
|
27
|
+
var preprocess_exports = {};
|
|
28
|
+
__export(preprocess_exports, {
|
|
29
|
+
vitePreprocess: () => vitePreprocess
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(preprocess_exports);
|
|
32
|
+
var import_path = __toESM(require("path"), 1);
|
|
33
|
+
var vite = __toESM(require("vite"), 1);
|
|
34
|
+
var supportedStyleLangs = ["css", "less", "sass", "scss", "styl", "stylus", "postcss", "sss"];
|
|
35
|
+
var supportedScriptLangs = ["ts"];
|
|
36
|
+
function vitePreprocess(opts) {
|
|
37
|
+
const preprocessor = {};
|
|
38
|
+
if (opts?.script !== false) {
|
|
39
|
+
preprocessor.script = viteScript().script;
|
|
40
|
+
}
|
|
41
|
+
if (opts?.style !== false) {
|
|
42
|
+
const styleOpts = typeof opts?.style == "object" ? opts?.style : void 0;
|
|
43
|
+
preprocessor.style = viteStyle(styleOpts).style;
|
|
44
|
+
}
|
|
45
|
+
return preprocessor;
|
|
46
|
+
}
|
|
47
|
+
function viteScript() {
|
|
48
|
+
return {
|
|
49
|
+
async script({ attributes, content, filename = "" }) {
|
|
50
|
+
const lang = attributes.lang;
|
|
51
|
+
if (!supportedScriptLangs.includes(lang))
|
|
52
|
+
return;
|
|
53
|
+
const transformResult = await vite.transformWithEsbuild(content, filename, {
|
|
54
|
+
loader: lang,
|
|
55
|
+
target: "esnext",
|
|
56
|
+
tsconfigRaw: {
|
|
57
|
+
compilerOptions: {
|
|
58
|
+
importsNotUsedAsValues: "preserve",
|
|
59
|
+
preserveValueImports: true
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
return {
|
|
64
|
+
code: transformResult.code,
|
|
65
|
+
map: transformResult.map
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function viteStyle(config = {}) {
|
|
71
|
+
let transform;
|
|
72
|
+
const style = async ({ attributes, content, filename = "" }) => {
|
|
73
|
+
const lang = attributes.lang;
|
|
74
|
+
if (!supportedStyleLangs.includes(lang))
|
|
75
|
+
return;
|
|
76
|
+
if (!transform) {
|
|
77
|
+
let resolvedConfig;
|
|
78
|
+
if (style.__resolvedConfig) {
|
|
79
|
+
resolvedConfig = style.__resolvedConfig;
|
|
80
|
+
} else if (isResolvedConfig(config)) {
|
|
81
|
+
resolvedConfig = config;
|
|
82
|
+
} else {
|
|
83
|
+
resolvedConfig = await vite.resolveConfig(
|
|
84
|
+
config,
|
|
85
|
+
process.env.NODE_ENV === "production" ? "build" : "serve"
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
transform = getCssTransformFn(resolvedConfig);
|
|
89
|
+
}
|
|
90
|
+
const moduleId = `${filename}.${lang}`;
|
|
91
|
+
const result = await transform(content, moduleId);
|
|
92
|
+
if (result.map?.sources?.[0] === moduleId) {
|
|
93
|
+
result.map.sources[0] = import_path.default.basename(filename);
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
code: result.code,
|
|
97
|
+
map: result.map ?? void 0
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
style.__resolvedConfig = null;
|
|
101
|
+
return { style };
|
|
102
|
+
}
|
|
103
|
+
function getCssTransformFn(config) {
|
|
104
|
+
if (vite.preprocessCSS) {
|
|
105
|
+
return async (code, filename) => {
|
|
106
|
+
return vite.preprocessCSS(code, filename, config);
|
|
107
|
+
};
|
|
108
|
+
} else {
|
|
109
|
+
const pluginName = "vite:css";
|
|
110
|
+
const plugin = config.plugins.find((p) => p.name === pluginName);
|
|
111
|
+
if (!plugin) {
|
|
112
|
+
throw new Error(`failed to find plugin ${pluginName}`);
|
|
113
|
+
}
|
|
114
|
+
if (!plugin.transform) {
|
|
115
|
+
throw new Error(`plugin ${pluginName} has no transform`);
|
|
116
|
+
}
|
|
117
|
+
return plugin.transform.bind(null);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function isResolvedConfig(config) {
|
|
121
|
+
return !!config.inlineConfig;
|
|
122
|
+
}
|
|
123
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
124
|
+
0 && (module.exports = {
|
|
125
|
+
vitePreprocess
|
|
126
|
+
});
|
|
127
|
+
//# sourceMappingURL=preprocess.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAiB;AACjB,WAAsB;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,kBAAkB,MAAW,0BAAqB,SAAS,UAAU;AAAA,QAC1E,QAAQ;AAAA,QACR,QAAQ;AAAA,QACR,aAAa;AAAA,UACZ,iBAAiB;AAAA,YAEhB,wBAAwB;AAAA,YACxB,sBAAsB;AAAA,UACvB;AAAA,QACD;AAAA,MACD,CAAC;AACD,aAAO;AAAA,QACN,MAAM,gBAAgB;AAAA,QACtB,KAAK,gBAAgB;AAAA,MACtB;AAAA,IACD;AAAA,EACD;AACD;AAEA,SAAS,UAAU,SAAkD,CAAC,GAEpE;AACD,MAAI;AACJ,QAAM,QAAsB,OAAO,EAAE,YAAY,SAAS,WAAW,GAAG,MAAM;AAC7E,UAAM,OAAO,WAAW;AACxB,QAAI,CAAC,oBAAoB,SAAS,IAAI;AAAG;AACzC,QAAI,CAAC,WAAW;AACf,UAAI;AAEJ,UAAI,MAAM,kBAAkB;AAE3B,yBAAiB,MAAM;AAAA,MACxB,WAAW,iBAAiB,MAAM,GAAG;AACpC,yBAAiB;AAAA,MAClB,OAAO;AACN,yBAAiB,MAAW;AAAA,UAC3B;AAAA,UACA,QAAQ,IAAI,aAAa,eAAe,UAAU;AAAA,QACnD;AAAA,MACD;AACA,kBAAY,kBAAkB,cAAc;AAAA,IAC7C;AACA,UAAM,WAAW,GAAG,YAAY;AAChC,UAAM,SAAS,MAAM,UAAU,SAAS,QAAQ;AAEhD,QAAI,OAAO,KAAK,UAAU,OAAO,UAAU;AAC1C,aAAO,IAAI,QAAQ,KAAK,YAAAA,QAAK,SAAS,QAAQ;AAAA,IAC/C;AACA,WAAO;AAAA,MACN,MAAM,OAAO;AAAA,MACb,KAAK,OAAO,OAAO;AAAA,IACpB;AAAA,EACD;AAEA,QAAM,mBAAmB;AACzB,SAAO,EAAE,MAAM;AAChB;AAKA,SAAS,kBAAkB,QAAsC;AAGhE,MAAS,oBAAe;AACvB,WAAO,OAAO,MAAM,aAAa;AAChC,aAAY,mBAAc,MAAM,UAAU,MAAM;AAAA,IACjD;AAAA,EACD,OAAO;AACN,UAAM,aAAa;AACnB,UAAM,SAAS,OAAO,QAAQ,KAAK,CAAC,MAAM,EAAE,SAAS,UAAU;AAC/D,QAAI,CAAC,QAAQ;AACZ,YAAM,IAAI,MAAM,yBAAyB,YAAY;AAAA,IACtD;AACA,QAAI,CAAC,OAAO,WAAW;AACtB,YAAM,IAAI,MAAM,UAAU,6BAA6B;AAAA,IACxD;AAEA,WAAO,OAAO,UAAU,KAAK,IAAI;AAAA,EAClC;AACD;AAEA,SAAS,iBAAiB,QAA4C;AACrE,SAAO,CAAC,CAAC,OAAO;AACjB;","names":["path"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as vite from 'vite';
|
|
2
|
+
import { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
|
|
3
|
+
|
|
4
|
+
declare function vitePreprocess(opts?: {
|
|
5
|
+
script?: boolean;
|
|
6
|
+
style?: boolean | vite.InlineConfig | vite.ResolvedConfig;
|
|
7
|
+
}): PreprocessorGroup;
|
|
8
|
+
|
|
9
|
+
export { vitePreprocess };
|
|
@@ -0,0 +1,96 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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,kBAAkB,MAAW,0BAAqB,SAAS,UAAU;AAAA,QAC1E,QAAQ;AAAA,QACR,QAAQ;AAAA,QACR,aAAa;AAAA,UACZ,iBAAiB;AAAA,YAEhB,wBAAwB;AAAA,YACxB,sBAAsB;AAAA,UACvB;AAAA,QACD;AAAA,MACD,CAAC;AACD,aAAO;AAAA,QACN,MAAM,gBAAgB;AAAA,QACtB,KAAK,gBAAgB;AAAA,MACtB;AAAA,IACD;AAAA,EACD;AACD;AAEA,SAAS,UAAU,SAAkD,CAAC,GAEpE;AACD,MAAI;AACJ,QAAM,QAAsB,OAAO,EAAE,YAAY,SAAS,WAAW,GAAG,MAAM;AAC7E,UAAM,OAAO,WAAW;AACxB,QAAI,CAAC,oBAAoB,SAAS,IAAI;AAAG;AACzC,QAAI,CAAC,WAAW;AACf,UAAI;AAEJ,UAAI,MAAM,kBAAkB;AAE3B,yBAAiB,MAAM;AAAA,MACxB,WAAW,iBAAiB,MAAM,GAAG;AACpC,yBAAiB;AAAA,MAClB,OAAO;AACN,yBAAiB,MAAW;AAAA,UAC3B;AAAA,UACA,QAAQ,IAAI,aAAa,eAAe,UAAU;AAAA,QACnD;AAAA,MACD;AACA,kBAAY,kBAAkB,cAAc;AAAA,IAC7C;AACA,UAAM,WAAW,GAAG,YAAY;AAChC,UAAM,SAAS,MAAM,UAAU,SAAS,QAAQ;AAEhD,QAAI,OAAO,KAAK,UAAU,OAAO,UAAU;AAC1C,aAAO,IAAI,QAAQ,KAAK,KAAK,SAAS,QAAQ;AAAA,IAC/C;AACA,WAAO;AAAA,MACN,MAAM,OAAO;AAAA,MACb,KAAK,OAAO,OAAO;AAAA,IACpB;AAAA,EACD;AAEA,QAAM,mBAAmB;AACzB,SAAO,EAAE,MAAM;AAChB;AAKA,SAAS,kBAAkB,QAAsC;AAGhE,MAAS,oBAAe;AACvB,WAAO,OAAO,MAAM,aAAa;AAChC,aAAY,mBAAc,MAAM,UAAU,MAAM;AAAA,IACjD;AAAA,EACD,OAAO;AACN,UAAM,aAAa;AACnB,UAAM,SAAS,OAAO,QAAQ,KAAK,CAAC,MAAM,EAAE,SAAS,UAAU;AAC/D,QAAI,CAAC,QAAQ;AACZ,YAAM,IAAI,MAAM,yBAAyB,YAAY;AAAA,IACtD;AACA,QAAI,CAAC,OAAO,WAAW;AACtB,YAAM,IAAI,MAAM,UAAU,6BAA6B;AAAA,IACxD;AAEA,WAAO,OAAO,UAAU,KAAK,IAAI;AAAA,EAClC;AACD;AAEA,SAAS,iBAAiB,QAA4C;AACrE,SAAO,CAAC,CAAC,OAAO;AACjB;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltejs/vite-plugin-svelte",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "dominikg",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
8
8
|
"src",
|
|
9
|
-
"
|
|
10
|
-
"LICENSE",
|
|
11
|
-
"package.json"
|
|
9
|
+
"*.d.ts"
|
|
12
10
|
],
|
|
13
11
|
"type": "module",
|
|
14
12
|
"main": "dist/index.cjs",
|
|
@@ -50,28 +48,20 @@
|
|
|
50
48
|
"vitefu": "^0.2.2"
|
|
51
49
|
},
|
|
52
50
|
"peerDependencies": {
|
|
53
|
-
"diff-match-patch": "^1.0.5",
|
|
54
51
|
"svelte": "^3.44.0",
|
|
55
52
|
"vite": "^3.0.0"
|
|
56
53
|
},
|
|
57
|
-
"peerDependenciesMeta": {
|
|
58
|
-
"diff-match-patch": {
|
|
59
|
-
"optional": true
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
54
|
"devDependencies": {
|
|
63
55
|
"@types/debug": "^4.1.7",
|
|
64
|
-
"
|
|
65
|
-
"diff-match-patch": "^1.0.5",
|
|
66
|
-
"esbuild": "^0.15.14",
|
|
56
|
+
"esbuild": "^0.15.16",
|
|
67
57
|
"rollup": "^2.79.1",
|
|
68
58
|
"svelte": "^3.53.1",
|
|
69
59
|
"tsup": "^6.5.0",
|
|
70
|
-
"vite": "^3.2.
|
|
60
|
+
"vite": "^3.2.4"
|
|
71
61
|
},
|
|
72
62
|
"scripts": {
|
|
73
63
|
"dev": "pnpm build:ci --sourcemap --watch src",
|
|
74
|
-
"build:ci": "rimraf dist && tsup-node src/index.ts --format esm,cjs --no-splitting --shims",
|
|
64
|
+
"build:ci": "rimraf dist && tsup-node src/index.ts src/preprocess.ts --format esm,cjs --no-splitting --shims",
|
|
75
65
|
"build": "pnpm build:ci --dts --sourcemap"
|
|
76
66
|
}
|
|
77
67
|
}
|
package/src/handle-hot-update.ts
CHANGED
|
@@ -20,7 +20,7 @@ export async function handleHotUpdate(
|
|
|
20
20
|
log.debug(`handleHotUpdate called before initial transform for ${svelteRequest.id}`);
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
|
-
const { read, server } = ctx;
|
|
23
|
+
const { read, server, modules } = ctx;
|
|
24
24
|
|
|
25
25
|
const cachedJS = cache.getJS(svelteRequest);
|
|
26
26
|
const cachedCss = cache.getCSS(svelteRequest);
|
|
@@ -35,41 +35,41 @@ export async function handleHotUpdate(
|
|
|
35
35
|
throw e;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
const affectedModules =
|
|
38
|
+
const affectedModules = [...modules];
|
|
39
39
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
40
|
+
const cssIdx = modules.findIndex((m) => m.id === svelteRequest.cssId);
|
|
41
|
+
if (cssIdx > -1) {
|
|
42
|
+
const cssUpdated = cssChanged(cachedCss, compileData.compiled.css);
|
|
43
|
+
if (!cssUpdated) {
|
|
44
|
+
log.debug(`skipping unchanged css for ${svelteRequest.cssId}`);
|
|
45
|
+
affectedModules.splice(cssIdx, 1);
|
|
46
|
+
}
|
|
46
47
|
}
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
const jsIdx = modules.findIndex((m) => m.id === svelteRequest.id);
|
|
49
|
+
if (jsIdx > -1) {
|
|
50
|
+
const jsUpdated = jsChanged(cachedJS, compileData.compiled.js, svelteRequest.filename);
|
|
51
|
+
if (!jsUpdated) {
|
|
52
|
+
log.debug(`skipping unchanged js for ${svelteRequest.id}`);
|
|
53
|
+
affectedModules.splice(jsIdx, 1);
|
|
54
|
+
// transform won't be called, log warnings here
|
|
55
|
+
logCompilerWarnings(svelteRequest, compileData.compiled.warnings, options);
|
|
56
|
+
}
|
|
52
57
|
}
|
|
53
58
|
|
|
54
|
-
if (!jsUpdated) {
|
|
55
|
-
// transform won't be called, log warnings here
|
|
56
|
-
logCompilerWarnings(svelteRequest, compileData.compiled.warnings, options);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const result = [...affectedModules].filter(Boolean) as ModuleNode[];
|
|
60
|
-
|
|
61
59
|
// TODO is this enough? see also: https://github.com/vitejs/vite/issues/2274
|
|
62
|
-
const ssrModulesToInvalidate =
|
|
60
|
+
const ssrModulesToInvalidate = affectedModules.filter((m) => !!m.ssrTransformResult);
|
|
63
61
|
if (ssrModulesToInvalidate.length > 0) {
|
|
64
62
|
log.debug(`invalidating modules ${ssrModulesToInvalidate.map((m) => m.id).join(', ')}`);
|
|
65
63
|
ssrModulesToInvalidate.forEach((moduleNode) => server.moduleGraph.invalidateModule(moduleNode));
|
|
66
64
|
}
|
|
67
|
-
if (
|
|
65
|
+
if (affectedModules.length > 0) {
|
|
68
66
|
log.debug(
|
|
69
|
-
`handleHotUpdate for ${svelteRequest.id} result: ${
|
|
67
|
+
`handleHotUpdate for ${svelteRequest.id} result: ${affectedModules
|
|
68
|
+
.map((m) => m.id)
|
|
69
|
+
.join(', ')}`
|
|
70
70
|
);
|
|
71
71
|
}
|
|
72
|
-
return
|
|
72
|
+
return affectedModules;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
function cssChanged(prev?: Code, next?: Code): boolean {
|
package/src/index.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { HmrContext, ModuleNode, Plugin, ResolvedConfig, UserConfig } from 'vite
|
|
|
4
4
|
import { isDepExcluded } from 'vitefu';
|
|
5
5
|
import { handleHotUpdate } from './handle-hot-update';
|
|
6
6
|
import { log, logCompilerWarnings } from './utils/log';
|
|
7
|
-
import {
|
|
8
|
-
import { buildIdParser, IdParser
|
|
7
|
+
import { type CompileSvelte, createCompileSvelte } from './utils/compile';
|
|
8
|
+
import { buildIdParser, IdParser } from './utils/id';
|
|
9
9
|
import {
|
|
10
10
|
buildExtraViteConfig,
|
|
11
11
|
validateInlineOptions,
|
|
@@ -23,6 +23,7 @@ import { toRollupError } from './utils/error';
|
|
|
23
23
|
import { saveSvelteMetadata } from './utils/optimizer';
|
|
24
24
|
import { svelteInspector } from './ui/inspector/plugin';
|
|
25
25
|
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';
|
|
26
|
+
import { loadRaw } from './utils/load-raw';
|
|
26
27
|
|
|
27
28
|
interface PluginAPI {
|
|
28
29
|
/**
|
|
@@ -45,11 +46,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
45
46
|
let options: ResolvedOptions;
|
|
46
47
|
let viteConfig: ResolvedConfig;
|
|
47
48
|
/* eslint-disable no-unused-vars */
|
|
48
|
-
let compileSvelte:
|
|
49
|
-
svelteRequest: SvelteRequest,
|
|
50
|
-
code: string,
|
|
51
|
-
options: Partial<ResolvedOptions>
|
|
52
|
-
) => Promise<CompileData>;
|
|
49
|
+
let compileSvelte: CompileSvelte;
|
|
53
50
|
/* eslint-enable no-unused-vars */
|
|
54
51
|
|
|
55
52
|
let resolvedSvelteSSR: Promise<PartialResolvedId | null>;
|
|
@@ -103,23 +100,26 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
103
100
|
setupWatchers(options, cache, requestParser);
|
|
104
101
|
},
|
|
105
102
|
|
|
106
|
-
load(id, opts) {
|
|
103
|
+
async load(id, opts) {
|
|
107
104
|
const ssr = !!opts?.ssr;
|
|
108
105
|
const svelteRequest = requestParser(id, !!ssr);
|
|
109
106
|
if (svelteRequest) {
|
|
110
|
-
const { filename, query } = svelteRequest;
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if (
|
|
115
|
-
|
|
116
|
-
|
|
107
|
+
const { filename, query, raw } = svelteRequest;
|
|
108
|
+
if (raw) {
|
|
109
|
+
return loadRaw(svelteRequest, compileSvelte, options);
|
|
110
|
+
} else {
|
|
111
|
+
if (query.svelte && query.type === 'style') {
|
|
112
|
+
const css = cache.getCSS(svelteRequest);
|
|
113
|
+
if (css) {
|
|
114
|
+
log.debug(`load returns css for ${filename}`);
|
|
115
|
+
return css;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// prevent vite asset plugin from loading files as url that should be compiled in transform
|
|
119
|
+
if (viteConfig.assetsInclude(filename)) {
|
|
120
|
+
log.debug(`load returns raw content for ${filename}`);
|
|
121
|
+
return fs.readFileSync(filename, 'utf-8');
|
|
117
122
|
}
|
|
118
|
-
}
|
|
119
|
-
// prevent vite asset plugin from loading files as url that should be compiled in transform
|
|
120
|
-
if (viteConfig.assetsInclude(filename)) {
|
|
121
|
-
log.debug(`load returns raw content for ${filename}`);
|
|
122
|
-
return fs.readFileSync(filename, 'utf-8');
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
},
|
|
@@ -128,14 +128,12 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
128
128
|
const ssr = !!opts?.ssr;
|
|
129
129
|
const svelteRequest = requestParser(importee, ssr);
|
|
130
130
|
if (svelteRequest?.query.svelte) {
|
|
131
|
-
if (svelteRequest.query.type === 'style') {
|
|
131
|
+
if (svelteRequest.query.type === 'style' && !svelteRequest.raw) {
|
|
132
132
|
// return cssId with root prefix so postcss pipeline of vite finds the directory correctly
|
|
133
133
|
// see https://github.com/sveltejs/vite-plugin-svelte/issues/14
|
|
134
134
|
log.debug(`resolveId resolved virtual css module ${svelteRequest.cssId}`);
|
|
135
135
|
return svelteRequest.cssId;
|
|
136
136
|
}
|
|
137
|
-
log.debug(`resolveId resolved ${importee}`);
|
|
138
|
-
return importee; // query with svelte tag, an id we generated, no need for further analysis
|
|
139
137
|
}
|
|
140
138
|
|
|
141
139
|
if (ssr && importee === 'svelte') {
|
|
@@ -188,7 +186,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
188
186
|
async transform(code, id, opts) {
|
|
189
187
|
const ssr = !!opts?.ssr;
|
|
190
188
|
const svelteRequest = requestParser(id, ssr);
|
|
191
|
-
if (!svelteRequest || svelteRequest.query.
|
|
189
|
+
if (!svelteRequest || svelteRequest.query.type === 'style' || svelteRequest.raw) {
|
|
192
190
|
return;
|
|
193
191
|
}
|
|
194
192
|
let compileData;
|
|
@@ -238,6 +236,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
|
|
|
238
236
|
return plugins.filter(Boolean);
|
|
239
237
|
}
|
|
240
238
|
|
|
239
|
+
export { vitePreprocess } from './preprocess';
|
|
241
240
|
export { loadSvelteConfig } from './utils/load-svelte-config';
|
|
242
241
|
|
|
243
242
|
export {
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import * as vite from 'vite';
|
|
3
|
+
import type { ESBuildOptions, ResolvedConfig } from 'vite';
|
|
4
|
+
// eslint-disable-next-line node/no-missing-import
|
|
5
|
+
import type { Preprocessor, PreprocessorGroup } from 'svelte/types/compiler/preprocess';
|
|
6
|
+
|
|
7
|
+
const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss', 'sss'];
|
|
8
|
+
const supportedScriptLangs = ['ts'];
|
|
9
|
+
|
|
10
|
+
export function vitePreprocess(opts?: {
|
|
11
|
+
script?: boolean;
|
|
12
|
+
style?: boolean | vite.InlineConfig | vite.ResolvedConfig;
|
|
13
|
+
}) {
|
|
14
|
+
const preprocessor: PreprocessorGroup = {};
|
|
15
|
+
if (opts?.script !== false) {
|
|
16
|
+
preprocessor.script = viteScript().script;
|
|
17
|
+
}
|
|
18
|
+
if (opts?.style !== false) {
|
|
19
|
+
const styleOpts = typeof opts?.style == 'object' ? opts?.style : undefined;
|
|
20
|
+
preprocessor.style = viteStyle(styleOpts).style;
|
|
21
|
+
}
|
|
22
|
+
return preprocessor;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function viteScript(): { script: Preprocessor } {
|
|
26
|
+
return {
|
|
27
|
+
async script({ attributes, content, filename = '' }) {
|
|
28
|
+
const lang = attributes.lang as string;
|
|
29
|
+
if (!supportedScriptLangs.includes(lang)) return;
|
|
30
|
+
const transformResult = await vite.transformWithEsbuild(content, filename, {
|
|
31
|
+
loader: lang as ESBuildOptions['loader'],
|
|
32
|
+
target: 'esnext',
|
|
33
|
+
tsconfigRaw: {
|
|
34
|
+
compilerOptions: {
|
|
35
|
+
// svelte typescript needs this flag to work with type imports
|
|
36
|
+
importsNotUsedAsValues: 'preserve',
|
|
37
|
+
preserveValueImports: true
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return {
|
|
42
|
+
code: transformResult.code,
|
|
43
|
+
map: transformResult.map
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function viteStyle(config: vite.InlineConfig | vite.ResolvedConfig = {}): {
|
|
50
|
+
style: Preprocessor;
|
|
51
|
+
} {
|
|
52
|
+
let transform: CssTransform;
|
|
53
|
+
const style: Preprocessor = async ({ attributes, content, filename = '' }) => {
|
|
54
|
+
const lang = attributes.lang as string;
|
|
55
|
+
if (!supportedStyleLangs.includes(lang)) return;
|
|
56
|
+
if (!transform) {
|
|
57
|
+
let resolvedConfig: vite.ResolvedConfig;
|
|
58
|
+
// @ts-expect-error special prop added if running in v-p-s
|
|
59
|
+
if (style.__resolvedConfig) {
|
|
60
|
+
// @ts-expect-error
|
|
61
|
+
resolvedConfig = style.__resolvedConfig;
|
|
62
|
+
} else if (isResolvedConfig(config)) {
|
|
63
|
+
resolvedConfig = config;
|
|
64
|
+
} else {
|
|
65
|
+
resolvedConfig = await vite.resolveConfig(
|
|
66
|
+
config,
|
|
67
|
+
process.env.NODE_ENV === 'production' ? 'build' : 'serve'
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
transform = getCssTransformFn(resolvedConfig);
|
|
71
|
+
}
|
|
72
|
+
const moduleId = `${filename}.${lang}`;
|
|
73
|
+
const result = await transform(content, moduleId);
|
|
74
|
+
// patch sourcemap source to point back to original filename
|
|
75
|
+
if (result.map?.sources?.[0] === moduleId) {
|
|
76
|
+
result.map.sources[0] = path.basename(filename);
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
code: result.code,
|
|
80
|
+
map: result.map ?? undefined
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
// @ts-expect-error tag so can be found by v-p-s
|
|
84
|
+
style.__resolvedConfig = null;
|
|
85
|
+
return { style };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// eslint-disable-next-line no-unused-vars
|
|
89
|
+
type CssTransform = (code: string, filename: string) => Promise<{ code: string; map?: any }>;
|
|
90
|
+
|
|
91
|
+
function getCssTransformFn(config: ResolvedConfig): CssTransform {
|
|
92
|
+
// API is only available in Vite 3.2 and above
|
|
93
|
+
// TODO: Remove Vite plugin hack when bump peer dep to Vite 3.2
|
|
94
|
+
if (vite.preprocessCSS) {
|
|
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
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function isResolvedConfig(config: any): config is vite.ResolvedConfig {
|
|
113
|
+
return !!config.inlineConfig;
|
|
114
|
+
}
|