@bakery-framework/core 2.4.1 → 2.4.2
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/package.json +1 -1
- package/src/compiler/compiler.ts +31 -22
- package/src/logger/serve-log.ts +10 -2
package/package.json
CHANGED
package/src/compiler/compiler.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import { Strings } from '../cache/string'
|
|
2
2
|
import { Bakery } from '../core/bakery'
|
|
3
3
|
import { PluginHooks } from '../core/plugins'
|
|
4
|
-
import {
|
|
5
|
-
compLog,
|
|
6
|
-
errorMsg,
|
|
7
|
-
errorWithPosition,
|
|
8
|
-
handlerLog,
|
|
9
|
-
} from '../logger/serve-log'
|
|
4
|
+
import { compLog, errorWithPosition, handlerLog } from '../logger/serve-log'
|
|
10
5
|
import type { MapOf } from '../types'
|
|
11
6
|
import { is, Try, toHash } from '../utils/common'
|
|
12
7
|
import { FileSystem as fs } from '../utils/fs'
|
|
@@ -128,10 +123,14 @@ function preprocessImports(source: string, filePath: fs.AbsolutePath): string {
|
|
|
128
123
|
* Compile `source`, naming `path` so a failure can say where it happened.
|
|
129
124
|
*
|
|
130
125
|
* The two branches are not symmetric on purpose. Without a path there is no
|
|
131
|
-
* file to
|
|
132
|
-
* fragments of an SFC it has already parsed), so a failure
|
|
133
|
-
* source back.
|
|
134
|
-
*
|
|
126
|
+
* file to resolve imports against and no caller that can render a 500 (the
|
|
127
|
+
* Vue plugin compiles fragments of an SFC it has already parsed), so a failure
|
|
128
|
+
* logs and hands the source back. `origin.file` names the file such a
|
|
129
|
+
* fragment came from, for that log line only: without it the line read
|
|
130
|
+
* "Failed to compile inline source: Transform failed", and a component that
|
|
131
|
+
* would not parse in the browser had to be found by hand. With a path the
|
|
132
|
+
* failure is a served request, and the answer is `null`: `TSHandler` turns
|
|
133
|
+
* that into the 500 it has always had a branch for.
|
|
135
134
|
*
|
|
136
135
|
* It used to be neither. The transpiler was wrapped only on the pathless
|
|
137
136
|
* branch, so a syntax error in a `.ts` asset threw straight past `compile()`,
|
|
@@ -140,22 +139,31 @@ function preprocessImports(source: string, filePath: fs.AbsolutePath): string {
|
|
|
140
139
|
* `Unhandled Server Error: <message>` with no file and no line while the client
|
|
141
140
|
* got `An unexpected error occurred.` A total blackout for a one-character typo.
|
|
142
141
|
*/
|
|
143
|
-
export function compileText(
|
|
142
|
+
export function compileText(
|
|
143
|
+
source: string,
|
|
144
|
+
origin?: { file: string },
|
|
145
|
+
): Promise<string>
|
|
144
146
|
export function compileText(
|
|
145
147
|
source: string,
|
|
146
148
|
path: fs.AbsolutePath,
|
|
147
149
|
): Promise<string | null>
|
|
148
|
-
export async function compileText(
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
150
|
+
export async function compileText(
|
|
151
|
+
source: string,
|
|
152
|
+
where?: fs.AbsolutePath | { file: string },
|
|
153
|
+
) {
|
|
154
|
+
if (typeof where !== 'string') {
|
|
155
|
+
const [failed, content] = await Try.catch(
|
|
156
|
+
(await getTranspiler()).transform(source),
|
|
157
|
+
)
|
|
158
|
+
if (!failed) return content!
|
|
159
|
+
|
|
160
|
+
const error = errorWithPosition(failed)
|
|
161
|
+
if (where) compLog.COMPILE_FAIL({ file: where.file, error })
|
|
162
|
+
else compLog.COMPILE_SOURCE_FAIL({ error })
|
|
163
|
+
return source
|
|
157
164
|
}
|
|
158
165
|
|
|
166
|
+
const path = where
|
|
159
167
|
source = preprocessImports(source, path)
|
|
160
168
|
|
|
161
169
|
const [failed, transformed] = await Try.catch(
|
|
@@ -163,8 +171,9 @@ export async function compileText(source: string, path?: fs.AbsolutePath) {
|
|
|
163
171
|
)
|
|
164
172
|
|
|
165
173
|
if (failed) {
|
|
166
|
-
// `errorWithPosition`, not `errorMsg`:
|
|
167
|
-
// stack, so `errorMsg` degrades to
|
|
174
|
+
// `errorWithPosition`, not `errorMsg`: what `transform()` rejects with
|
|
175
|
+
// has no stack, so `errorMsg` degrades to a bare message and drops the
|
|
176
|
+
// line number, the only thing that makes this actionable. The file comes
|
|
168
177
|
// from `{file}`; the diagnostic's own `position.file` is `input.ts`,
|
|
169
178
|
// because `transform()` was handed a string.
|
|
170
179
|
compLog.COMPILE_FAIL({ file: path, error: errorWithPosition(failed) })
|
package/src/logger/serve-log.ts
CHANGED
|
@@ -257,12 +257,20 @@ export function errorDetail(err: any): string {
|
|
|
257
257
|
* `position.file` is `input.ts` whenever `transform()` was handed a source
|
|
258
258
|
* string rather than a path (which is every compile this codebase does), so
|
|
259
259
|
* printing it beside the real path would contradict it.
|
|
260
|
+
*
|
|
261
|
+
* `transform()` rejects with an `AggregateError` whose own message is only
|
|
262
|
+
* "Transform failed", with no position, and whose `errors` are the
|
|
263
|
+
* diagnostics, so the first of those is the one read. The rest usually follow
|
|
264
|
+
* from it: a `;` inside an object literal is reported as `Expected "}" but
|
|
265
|
+
* found ";"`, then as `Unexpected }` at the brace after it.
|
|
260
266
|
*/
|
|
261
267
|
export function errorWithPosition(err: any): string {
|
|
262
|
-
const
|
|
268
|
+
const first: Diagnostic =
|
|
269
|
+
Array.isArray(err?.errors) && err.errors.length ? err.errors[0] : err
|
|
270
|
+
const position = first?.position
|
|
263
271
|
if (!position || position.line == null) return errorMsg(err)
|
|
264
272
|
|
|
265
|
-
const message =
|
|
273
|
+
const message = first.message || errorMsg(first)
|
|
266
274
|
return `${message} (line ${position.line}, column ${position.column ?? 0})`
|
|
267
275
|
}
|
|
268
276
|
|