Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8e08ebe

Browse files
authored
fix: add error handling to transform for rspack and webpack (#482)
* fix: add error handling to transform for Rspack and webpack * fix: improve error handling in transform
1 parent 9fbd3e4 commit 8e08ebe

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

src/rspack/loaders/transform.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,30 @@ export default async function transform(
1414

1515
const id = this.resource
1616
const context = createContext(this)
17-
const res = await plugin.transform.call(
18-
Object.assign(
19-
{},
20-
this._compilation && createBuildContext(this._compiler, this._compilation, this),
21-
context,
22-
),
23-
source,
24-
id,
25-
)
2617

27-
if (res == null)
28-
callback(null, source, map)
29-
else if (typeof res !== 'string')
30-
callback(null, res.code, map == null ? map : (res.map || map))
31-
else callback(null, res, map)
18+
try {
19+
const res = await plugin.transform.call(
20+
Object.assign(
21+
{},
22+
this._compilation && createBuildContext(this._compiler, this._compilation, this),
23+
context,
24+
),
25+
source,
26+
id,
27+
)
28+
29+
if (res == null)
30+
callback(null, source, map)
31+
else if (typeof res !== 'string')
32+
callback(null, res.code, map == null ? map : (res.map || map))
33+
else callback(null, res, map)
34+
}
35+
catch (error) {
36+
if (error instanceof Error) {
37+
callback(error)
38+
}
39+
else {
40+
callback(new Error(String(error)))
41+
}
42+
}
3243
}

src/webpack/loaders/transform.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,34 @@ export default async function transform(this: LoaderContext<any>, source: string
1010
return callback(null, source, map)
1111

1212
const context = createContext(this)
13-
const res = await plugin.transform.call(
14-
Object.assign({}, createBuildContext({
15-
addWatchFile: (file) => {
16-
this.addDependency(file)
17-
},
18-
getWatchFiles: () => {
19-
return this.getDependencies()
20-
},
21-
}, this._compiler!, this._compilation, this), context),
22-
source,
23-
this.resource,
24-
)
2513

26-
if (res == null)
27-
callback(null, source, map)
28-
else if (typeof res !== 'string')
29-
callback(null, res.code, map == null ? map : (res.map || map))
30-
else
31-
callback(null, res, map)
14+
try {
15+
const res = await plugin.transform.call(
16+
Object.assign({}, createBuildContext({
17+
addWatchFile: (file) => {
18+
this.addDependency(file)
19+
},
20+
getWatchFiles: () => {
21+
return this.getDependencies()
22+
},
23+
}, this._compiler!, this._compilation, this), context),
24+
source,
25+
this.resource,
26+
)
27+
28+
if (res == null)
29+
callback(null, source, map)
30+
else if (typeof res !== 'string')
31+
callback(null, res.code, map == null ? map : (res.map || map))
32+
else
33+
callback(null, res, map)
34+
}
35+
catch (error) {
36+
if (error instanceof Error) {
37+
callback(error)
38+
}
39+
else {
40+
callback(new Error(String(error)))
41+
}
42+
}
3243
}

0 commit comments

Comments
 (0)