Skip to content

Commit

Permalink
feat: basic transform
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 12, 2021
1 parent ca5bad6 commit 74e17df
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
dist
coverage
.profile
temp
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { getRollupPlugin } from './rollup'
import { UnpluginOptions, UnpluginInstance } from './types'
import { getWebpackPlugin } from './webpack'

export function defineUnplugin<UserOptions = {}, ResolvedContext = UserOptions> (
options: UnpluginOptions<UserOptions, ResolvedContext>
export function defineUnplugin<UserOptions = {}> (
options: UnpluginOptions<UserOptions>
): UnpluginInstance<UserOptions> {
return {
get rollup () {
Expand Down
8 changes: 4 additions & 4 deletions src/rollup.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { UnpluginInstance, UnpluginOptions } from './types'

export function getRollupPlugin <UserOptions = {}, ResolvedContext = UserOptions> (
options: UnpluginOptions<UserOptions, ResolvedContext>
export function getRollupPlugin <UserOptions = {}> (
options: UnpluginOptions<UserOptions>
): UnpluginInstance<UserOptions>['rollup'] {
return (userOptions?: UserOptions) => {
const context = options.setup(userOptions)
const hooks = options.hooks(context)
const hooks = options.setup(userOptions)

return {
name: options.name,
enforce: options.enforce,
transform (code, id) {
if (!hooks.transformInclude?.(id)) {
return null
Expand Down
6 changes: 2 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ export interface UnpluginHooks {
transform?: (code: string, id: string) => string | { code: string; map: any; };
}

export interface UnpluginOptions<UserOptions, ResolvedContext = UserOptions> {
export interface UnpluginOptions<UserOptions> {
name: string;
enforce?: 'post' | 'pre' | undefined;
setup(options?: UserOptions): ResolvedContext;
hooks(options: ResolvedContext): UnpluginHooks;
rollup?: Partial<RollupPlugin>;
setup(options?: UserOptions): UnpluginHooks;
}

export interface UnpluginInstance<UserOptions> {
Expand Down
5 changes: 2 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { existsSync, mkdirSync } from 'fs'
import { dirname } from 'path'
import { join } from 'path/posix'
import { dirname, join, resolve } from 'path'

export function getPackageRoot () {
return dirname(require.resolve('unplugin/package.json'))
return resolve(dirname(__dirname))
}

export function getLoaderPath (name: string) {
Expand Down
39 changes: 18 additions & 21 deletions src/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,41 @@ import fs from 'fs'
import { UnpluginInstance, UnpluginOptions } from './types'
import { getLoaderPath } from './utils'

export function getWebpackPlugin<UserOptions = {}, ResolvedContext = UserOptions> (
options: UnpluginOptions<UserOptions, ResolvedContext>
export function getWebpackPlugin<UserOptions = {}> (
options: UnpluginOptions<UserOptions>
): UnpluginInstance<UserOptions>['webpack'] {
return class WebpackPlugin {
// eslint-disable-next-line no-useless-constructor
constructor (public userOptions?: UserOptions) {}

apply (compiler: any) {
const context = options.setup(this.userOptions)
const hooks = options.hooks(context)
const hooks = options.setup(this.userOptions)

if (!compiler.$unplugin) {
compiler.$unplugin = {}
}
compiler.$unplugin[options.name] = {
context,
hooks
}
compiler.$unplugin[options.name] = hooks

if (hooks.transform) {
const loaderPath = getLoaderPath(options.name)

fs.writeFileSync(loaderPath, `module.exports = async function(source) {
if (!this._compiler || !this._compiler.$unplugin) return source
const plugin = this._compiler.$unplugin['${options.name}']
fs.writeFileSync(loaderPath, `
module.exports = async function(source) {
if (!this._compiler || !this._compiler.$unplugin) return source
const plugin = this._compiler.$unplugin['${options.name}']
if (!plugin) return source
if (!plugin) return source
const res = await plugin.hooks.transform(source, this.resource)
const res = await plugin.transform(source, this.resource)
if (typeof res !== 'string') {
this.callback(null, res.code, res.map)
}
else {
this.callback(null, res)
}
}
if (typeof res !== 'string') {
this.callback(null, res.code, res.map)
}
else {
this.callback(null, res)
}
}
`, 'utf-8')

compiler.options.module.rules.push({
Expand Down

0 comments on commit 74e17df

Please sign in to comment.