@sveltejs/vite-plugin-svelte 1.0.0-next.30 → 1.0.0-next.31
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/README.md +1 -0
- package/dist/index.cjs +69 -10
- package/dist/index.cjs.map +3 -3
- package/dist/index.js +67 -8
- package/dist/index.js.map +3 -3
- package/package.json +7 -7
- package/src/index.ts +7 -1
- package/src/utils/error.ts +92 -0
- package/src/utils/esbuild.ts +7 -2
- package/src/utils/log.ts +1 -1
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { RollupError } from 'rollup';
|
|
2
|
+
import { Warning } from './options';
|
|
3
|
+
import { buildExtendedLogMessage } from './log';
|
|
4
|
+
import { PartialMessage } from 'esbuild';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* convert an error thrown by svelte.compile to a RollupError so that vite displays it in a user friendly way
|
|
8
|
+
* @param error a svelte compiler error, which is a mix of Warning and an error
|
|
9
|
+
* @returns {RollupError} the converted error
|
|
10
|
+
*/
|
|
11
|
+
export function toRollupError(error: Warning & Error): RollupError {
|
|
12
|
+
const { filename, frame, start, code, name } = error;
|
|
13
|
+
const rollupError: RollupError = {
|
|
14
|
+
name, // needed otherwise sveltekit coalesce_to_error turns it into a string
|
|
15
|
+
id: filename,
|
|
16
|
+
message: buildExtendedLogMessage(error), // include filename:line:column so that it's clickable
|
|
17
|
+
frame: formatFrameForVite(frame),
|
|
18
|
+
code,
|
|
19
|
+
stack: ''
|
|
20
|
+
};
|
|
21
|
+
if (start) {
|
|
22
|
+
rollupError.loc = {
|
|
23
|
+
line: start.line,
|
|
24
|
+
column: start.column,
|
|
25
|
+
file: filename
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return rollupError;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* convert an error thrown by svelte.compile to an esbuild PartialMessage
|
|
33
|
+
* @param error a svelte compiler error, which is a mix of Warning and an error
|
|
34
|
+
* @returns {PartialMessage} the converted error
|
|
35
|
+
*/
|
|
36
|
+
export function toESBuildError(error: Warning & Error): PartialMessage {
|
|
37
|
+
const { filename, frame, start } = error;
|
|
38
|
+
const partialMessage: PartialMessage = {
|
|
39
|
+
text: buildExtendedLogMessage(error)
|
|
40
|
+
};
|
|
41
|
+
if (start) {
|
|
42
|
+
partialMessage.location = {
|
|
43
|
+
line: start.line,
|
|
44
|
+
column: start.column,
|
|
45
|
+
file: filename,
|
|
46
|
+
lineText: lineFromFrame(start.line, frame) // needed to get a meaningful error message on cli
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return partialMessage;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* extract line with number from codeframe
|
|
54
|
+
*/
|
|
55
|
+
function lineFromFrame(lineNo: number, frame?: string): string {
|
|
56
|
+
if (!frame) {
|
|
57
|
+
return '';
|
|
58
|
+
}
|
|
59
|
+
const lines = frame.split('\n');
|
|
60
|
+
const errorLine = lines.find((line) => line.trimStart().startsWith(`${lineNo}: `));
|
|
61
|
+
return errorLine ? errorLine.substring(errorLine.indexOf(': ') + 3) : '';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* vite error overlay expects a specific format to show frames
|
|
66
|
+
* this reformats svelte frame (colon separated, less whitespace)
|
|
67
|
+
* to one that vite displays on overlay ( pipe separated, more whitespace)
|
|
68
|
+
* e.g.
|
|
69
|
+
* ```
|
|
70
|
+
* 1: foo
|
|
71
|
+
* 2: bar;
|
|
72
|
+
* ^
|
|
73
|
+
* 3: baz
|
|
74
|
+
* ```
|
|
75
|
+
* to
|
|
76
|
+
* ```
|
|
77
|
+
* 1 | foo
|
|
78
|
+
* 2 | bar;
|
|
79
|
+
* ^
|
|
80
|
+
* 3 | baz
|
|
81
|
+
* ```
|
|
82
|
+
* @see https://github.com/vitejs/vite/blob/96591bf9989529de839ba89958755eafe4c445ae/packages/vite/src/client/overlay.ts#L116
|
|
83
|
+
*/
|
|
84
|
+
function formatFrameForVite(frame?: string): string {
|
|
85
|
+
if (!frame) {
|
|
86
|
+
return '';
|
|
87
|
+
}
|
|
88
|
+
return frame
|
|
89
|
+
.split('\n')
|
|
90
|
+
.map((line) => (line.match(/^\s+\^/) ? ' ' + line : ' ' + line.replace(':', ' | ')))
|
|
91
|
+
.join('\n');
|
|
92
|
+
}
|
package/src/utils/esbuild.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { DepOptimizationOptions } from 'vite';
|
|
|
4
4
|
import { Compiled } from './compile';
|
|
5
5
|
import { log } from './log';
|
|
6
6
|
import { CompileOptions, ResolvedOptions } from './options';
|
|
7
|
+
import { toESBuildError } from './error';
|
|
7
8
|
|
|
8
9
|
type EsbuildOptions = NonNullable<DepOptimizationOptions['esbuildOptions']>;
|
|
9
10
|
type EsbuildPlugin = NonNullable<EsbuildOptions['plugins']>[number];
|
|
@@ -20,8 +21,12 @@ export function esbuildSveltePlugin(options: ResolvedOptions): EsbuildPlugin {
|
|
|
20
21
|
|
|
21
22
|
build.onLoad({ filter: svelteFilter }, async ({ path: filename }) => {
|
|
22
23
|
const code = await fs.readFile(filename, 'utf8');
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
try {
|
|
25
|
+
const contents = await compileSvelte(options, { filename, code });
|
|
26
|
+
return { contents };
|
|
27
|
+
} catch (e) {
|
|
28
|
+
return { errors: [toESBuildError(e)] };
|
|
29
|
+
}
|
|
25
30
|
});
|
|
26
31
|
}
|
|
27
32
|
};
|
package/src/utils/log.ts
CHANGED
|
@@ -155,7 +155,7 @@ function warnBuild(w: Warning) {
|
|
|
155
155
|
log.warn.enabled && log.warn(buildExtendedLogMessage(w), w.frame);
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
function buildExtendedLogMessage(w: Warning) {
|
|
158
|
+
export function buildExtendedLogMessage(w: Warning) {
|
|
159
159
|
const parts = [];
|
|
160
160
|
if (w.filename) {
|
|
161
161
|
parts.push(w.filename);
|