Skip to content

Commit

Permalink
fix: better source map handling for webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 27, 2021
1 parent 568076b commit 992f322
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 943 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@
"webpack-virtual-modules": "^0.4.3"
},
"devDependencies": {
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@nuxtjs/eslint-config-typescript": "^6.0.1",
"@types/express": "^4.17.13",
"@types/fs-extra": "^9.0.12",
"@types/jest": "^27.0.1",
"@types/node": "^16.7.1",
"babel-loader": "^8.2.2",
"chalk": "^4.1.2",
"enhanced-resolve": "^5.8.2",
"eslint": "^7.32.0",
Expand Down
8 changes: 5 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin as RollupPlugin, PluginContextMeta as RollupContextMeta } from 'rollup'
import type { Plugin as RollupPlugin, PluginContextMeta as RollupContextMeta, SourceMap } from 'rollup'
import type { Compiler as WebpackCompiler, WebpackPluginInstance } from 'webpack'
import type { Plugin as VitePlugin } from 'vite'
import type VirtualModulesPlugin from 'webpack-virtual-modules'
Expand All @@ -11,12 +11,14 @@ export {

export type Thenable<T> = T | Promise<T>

export type TransformResult = string | { code: string; map?: SourceMap | null; } | null | undefined

export interface UnpluginOptions {
name: string;
enforce?: 'post' | 'pre' | undefined;
transformInclude?: (id: string) => boolean;
transform?: (this: UnpluginContext, code: string, id: string) => Thenable<string | { code: string; map: any; } | null | undefined>;
load?: (this: UnpluginContext, id: string) => Thenable<string | null | undefined>
transform?: (this: UnpluginContext, code: string, id: string) => Thenable<TransformResult>;
load?: (this: UnpluginContext, id: string) => Thenable<TransformResult>
resolveId?: (id: string, importer?: string) => Thenable<string | null | undefined>

// framework specify extends
Expand Down
10 changes: 6 additions & 4 deletions src/webpack/loaders/load.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { LoaderContext } from 'webpack'
import { UnpluginContext } from '../../types'

export default async function load (this: LoaderContext<any>, source: string) {
export default async function load (this: LoaderContext<any>, source: string, map: any) {
const callback = this.async()
const { unpluginName } = this.query
const plugin = this._compiler?.$unpluginContext[unpluginName]

if (!plugin?.load) {
return callback(null, source)
return callback(null, source, map)
}

const context: UnpluginContext = {
Expand All @@ -23,8 +23,10 @@ export default async function load (this: LoaderContext<any>, source: string) {
const res = await plugin.load.call(context, id)

if (res == null) {
callback(null, source)
callback(null, source, map)
} else if (typeof res !== 'string') {
callback(null, res.code, res.map ?? map)
} else {
callback(null, res)
callback(null, res, map)
}
}
2 changes: 1 addition & 1 deletion src/webpack/loaders/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default async function transform (this: LoaderContext<any>, source: strin
if (res == null) {
callback(null, source, map)
} else if (typeof res !== 'string') {
callback(null, res.code, res.map)
callback(null, res.code, res.map ?? map)
} else {
callback(null, res, map)
}
Expand Down
Loading

0 comments on commit 992f322

Please sign in to comment.