-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
68 additions
and
62 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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { getRollupPlugin } from './rollup' | ||
import { UnpluginOptions, UnpluginInstance } from './types' | ||
import { UnpluginInstance, UnpluginFactory } from './types' | ||
import { getWebpackPlugin } from './webpack' | ||
|
||
export function defineUnplugin<UserOptions = {}> ( | ||
options: UnpluginOptions<UserOptions> | ||
factory: UnpluginFactory<UserOptions> | ||
): UnpluginInstance<UserOptions> { | ||
return { | ||
get rollup () { | ||
return getRollupPlugin(options) | ||
return getRollupPlugin(factory) | ||
}, | ||
get webpack () { | ||
return getWebpackPlugin(options) | ||
return getWebpackPlugin(factory) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,20 +1,25 @@ | ||
import { UnpluginInstance, UnpluginOptions } from './types' | ||
import { Plugin as RollupPlugin } from 'rollup' | ||
import { UnpluginInstance, UnpluginFactory } from './types' | ||
|
||
export function getRollupPlugin <UserOptions = {}> ( | ||
options: UnpluginOptions<UserOptions> | ||
factory: UnpluginFactory<UserOptions> | ||
): UnpluginInstance<UserOptions>['rollup'] { | ||
return (userOptions?: UserOptions) => { | ||
const hooks = options.setup(userOptions) | ||
const rawPlugin = factory(userOptions) | ||
|
||
return { | ||
name: options.name, | ||
enforce: options.enforce, | ||
transform (code, id) { | ||
if (hooks.transformInclude && !hooks.transformInclude(id)) { | ||
const plugin: RollupPlugin = { | ||
...rawPlugin | ||
} | ||
|
||
if (rawPlugin.transform && rawPlugin.transformInclude) { | ||
plugin.transform = (code, id) => { | ||
if (rawPlugin.transformInclude && !rawPlugin.transformInclude(id)) { | ||
return null | ||
} | ||
return hooks.transform?.(code, id) || null | ||
return rawPlugin.transform!(code, id) | ||
} | ||
} | ||
|
||
return plugin | ||
} | ||
} |
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