From 98c20aabcd53ef9ee1cb7009469954927282925f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E6=9D=83?= Date: Tue, 14 Nov 2023 17:37:30 +0800 Subject: [PATCH] fix: rspack plugin transform includes everything --- src/rspack/index.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/rspack/index.ts b/src/rspack/index.ts index 0d211f1d..38a801b4 100644 --- a/src/rspack/index.ts +++ b/src/rspack/index.ts @@ -1,6 +1,6 @@ import { resolve } from 'path' import type { RspackPluginInstance, RuleSetUseItem } from '@rspack/core' -import { toArray } from '../utils' +import { normalizeAbsolutePath, toArray } from '../utils' import type { UnpluginContextMeta, UnpluginFactory, @@ -47,15 +47,28 @@ export function getRspackPlugin>( // transform hook if (plugin.transform) { - const use: RuleSetUseItem = { - loader: TRANSFORM_LOADER, - options: { plugin }, - } + const useLoader: RuleSetUseItem[] = [ + { + loader: TRANSFORM_LOADER, + options: { plugin }, + }, + ] + const useNone: RuleSetUseItem[] = [] compiler.options.module.rules.unshift({ enforce: plugin.enforce, - include: /.*/, - use, + use: (data) => { + if (data.resource == null) + return useNone + + const id = normalizeAbsolutePath( + data.resource + (data.resourceQuery || ''), + ) + if (!plugin.transformInclude || plugin.transformInclude(id)) + return useLoader + + return useNone + }, }) }