Skip to content

Commit

Permalink
fix: use options for webpack loader, close #3
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 12, 2021
1 parent 8f4fc53 commit 8273ae6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 80 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./dist/webpack/loader": {
"require": "./dist/webpack/loader.cjs"
}
},
"main": "dist/index.cjs",
Expand Down
5 changes: 5 additions & 0 deletions siroc.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineSirocConfig } from 'siroc'

export default defineSirocConfig({

})
2 changes: 1 addition & 1 deletion src/rollup.ts → src/rollup/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Plugin as RollupPlugin } from 'rollup'
import { UnpluginInstance, UnpluginFactory } from './types'
import { UnpluginInstance, UnpluginFactory } from '../types'

export function getRollupPlugin <UserOptions = {}> (
factory: UnpluginFactory<UserOptions>
Expand Down
16 changes: 0 additions & 16 deletions src/utils.ts

This file was deleted.

63 changes: 0 additions & 63 deletions src/webpack.ts

This file was deleted.

44 changes: 44 additions & 0 deletions src/webpack/index.ts
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)
}
18 changes: 18 additions & 0 deletions src/webpack/loader.ts
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)
}
}

0 comments on commit 8273ae6

Please sign in to comment.