diff --git a/.eslintignore b/.eslintignore index 1521c8b7..1db6dd2b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ dist +temp diff --git a/src/types.ts b/src/types.ts index 22b56211..aba7388a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,5 +13,5 @@ export interface UnpluginOptions { export interface UnpluginInstance { rollup: (options?: UserOptions) => RollupPlugin; - webpack: { new(): any; }; + webpack: (options?: UserOptions) => any; } diff --git a/src/utils.ts b/src/utils.ts index bb4dd026..969d89c9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -12,5 +12,5 @@ export function getLoaderPath (name: string) { mkdirSync(tempDir) } - return join(tempDir, 'loader-' + name + '.js') + return join(tempDir, 'webpack-loader-' + name + '.js') } diff --git a/src/webpack.ts b/src/webpack.ts index 8025159a..712cf5f4 100644 --- a/src/webpack.ts +++ b/src/webpack.ts @@ -5,10 +5,9 @@ import { getLoaderPath } from './utils' export function getWebpackPlugin ( options: UnpluginOptions ): UnpluginInstance['webpack'] { - return class WebpackPlugin { + class UnpluginWebpackPlugin { // eslint-disable-next-line no-useless-constructor constructor (public userOptions?: UserOptions) {} - apply (compiler: any) { const hooks = options.setup(this.userOptions) @@ -21,20 +20,23 @@ export function getWebpackPlugin ( const loaderPath = getLoaderPath(options.name) fs.writeFileSync(loaderPath, ` -module.exports = async function(source) { - if (!this._compiler || !this._compiler.$unplugin) return source +module.exports = async function(source, map) { + const callback = this.async() + if (!this._compiler || !this._compiler.$unplugin) + return callback(null, source, map) const plugin = this._compiler.$unplugin['${options.name}'] - if (!plugin) return source + if (!plugin) + return callback(null, source, map) const res = await plugin.transform(source, this.resource) if (typeof res !== 'string') { - this.callback(null, res.code, res.map) + callback(null, res.code, res.map) } else { - this.callback(null, res) + callback(null, res, map) } } `, 'utf-8') @@ -52,4 +54,6 @@ module.exports = async function(source) { } } } + + return UserOptions => new UnpluginWebpackPlugin(UserOptions) }