Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/rspack/loaders/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,30 @@ export default async function transform(

const id = this.resource
const context = createContext(this)
const res = await plugin.transform.call(
Object.assign(
{},
this._compilation && createBuildContext(this._compiler, this._compilation, this),
context,
),
source,
id,
)

if (res == null)
callback(null, source, map)
else if (typeof res !== 'string')
callback(null, res.code, map == null ? map : (res.map || map))
else callback(null, res, map)
try {
const res = await plugin.transform.call(
Object.assign(
{},
this._compilation && createBuildContext(this._compiler, this._compilation, this),
context,
),
source,
id,
)

if (res == null)
callback(null, source, map)
else if (typeof res !== 'string')
callback(null, res.code, map == null ? map : (res.map || map))
else callback(null, res, map)
}
catch (error) {
if (error instanceof Error) {
callback(error)
}
else {
callback(new Error(String(error)))
}
}
}
47 changes: 29 additions & 18 deletions src/webpack/loaders/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,34 @@ export default async function transform(this: LoaderContext<any>, source: string
return callback(null, source, map)

const context = createContext(this)
const res = await plugin.transform.call(
Object.assign({}, createBuildContext({
addWatchFile: (file) => {
this.addDependency(file)
},
getWatchFiles: () => {
return this.getDependencies()
},
}, this._compiler!, this._compilation, this), context),
source,
this.resource,
)

if (res == null)
callback(null, source, map)
else if (typeof res !== 'string')
callback(null, res.code, map == null ? map : (res.map || map))
else
callback(null, res, map)
try {
const res = await plugin.transform.call(
Object.assign({}, createBuildContext({
addWatchFile: (file) => {
this.addDependency(file)
},
getWatchFiles: () => {
return this.getDependencies()
},
}, this._compiler!, this._compilation, this), context),
source,
this.resource,
)

if (res == null)
callback(null, source, map)
else if (typeof res !== 'string')
callback(null, res.code, map == null ? map : (res.map || map))
else
callback(null, res, map)
}
catch (error) {
if (error instanceof Error) {
callback(error)
}
else {
callback(new Error(String(error)))
}
}
}