-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use options for webpack loader, close #3
- Loading branch information
Showing
7 changed files
with
71 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { defineSirocConfig } from 'siroc' | ||
|
||
export default defineSirocConfig({ | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { resolve } from 'path' | ||
import { Compiler } from 'webpack' | ||
import { UnpluginInstance, UnpluginFactory } from '../types' | ||
|
||
export function getWebpackPlugin<UserOptions = {}> ( | ||
factory: UnpluginFactory<UserOptions> | ||
): UnpluginInstance<UserOptions>['webpack'] { | ||
class UnpluginWebpackPlugin { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor (public userOptions?: UserOptions) {} | ||
apply (compiler: Compiler) { | ||
const rawPlugin = factory(this.userOptions) | ||
|
||
// @ts-expect-error | ||
if (!compiler.$unpluginContext) { | ||
// @ts-expect-error | ||
compiler.$unpluginContext = {} | ||
} | ||
// @ts-expect-error | ||
compiler.$unpluginContext[rawPlugin.name] = rawPlugin | ||
|
||
if (rawPlugin.transform) { | ||
compiler.options.module.rules.push({ | ||
include (id: string) { | ||
if (rawPlugin.transformInclude) { | ||
return rawPlugin.transformInclude(id) | ||
} else { | ||
return true | ||
} | ||
}, | ||
enforce: rawPlugin.enforce, | ||
use: [{ | ||
loader: resolve(__dirname, '..', 'dist/webpack/loader.cjs'), | ||
options: { | ||
unpluginName: rawPlugin.name | ||
} | ||
}] | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return UserOptions => new UnpluginWebpackPlugin(UserOptions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { LoaderContext } from 'webpack' | ||
|
||
export default async function (this: LoaderContext<any>, source: string, map: any) { | ||
const callback = this.async() | ||
const { unpluginName } = this.query | ||
// @ts-expect-error | ||
const plugin = this._compiler.$unpluginContext[unpluginName] | ||
|
||
const res = await plugin.transform(source, this.resource) | ||
|
||
if (res == null) { | ||
callback(null, source, map) | ||
} else if (typeof res !== 'string') { | ||
callback(null, res.code, res.map) | ||
} else { | ||
callback(null, res, map) | ||
} | ||
} |