From ba24c37fd3424bdeeccac67a52a264adff437315 Mon Sep 17 00:00:00 2001 From: Marc Henry Schultz <85400359+mhsdesign@users.noreply.github.com> Date: Wed, 23 Nov 2022 19:45:18 +0100 Subject: [PATCH] BUGFIX: Dont resolve css files which are not matched in cssModulesMatch --- src/index.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1ed53b2..07e0f8c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,6 +26,8 @@ const LOAD_STYLE_NAMESPACE = 'stylePlugin' const SKIP_RESOLVE = 'esbuild-style-plugin-skipResolve' const styleFilter = /.\.(css|sass|scss|less|styl)$/ +const createCssModulesMatchFromOptions = (options: PluginOptions) => options.cssModulesMatch || /\.module\./ + const handleCSSModules = (mapping: { data: any }, cssModulesOptions: CssModulesOptions) => { const _getJSON = cssModulesOptions.getJSON @@ -48,11 +50,14 @@ const onTempStyleResolve = async (build: PluginBuild, args: OnResolveArgs): Prom } } -const onStyleResolve = async (build: PluginBuild, args: OnResolveArgs): Promise => { +const onStyleResolve = (options: PluginOptions) => async (build: PluginBuild, args: OnResolveArgs): Promise => { const { namespace } = args if (args.pluginData === SKIP_RESOLVE || namespace === LOAD_STYLE_NAMESPACE || namespace === LOAD_TEMP_NAMESPACE) return + // Check if we should even handle this path + if (!createCssModulesMatchFromOptions(options).test(path)) return + const result = await build.resolve(args.path, { resolveDir: args.resolveDir, pluginData: SKIP_RESOLVE }) if (result.errors.length > 0) { return { errors: result.errors } @@ -83,7 +88,7 @@ const onTempLoad = async (args: OnLoadArgs): Promise => { const onStyleLoad = (options: PluginOptions) => async (args: OnLoadArgs): Promise => { // { extract: false } is for SSR since we only need the css mapping and not the actual css file const extract = options.extract === undefined ? true : options.extract - const cssModulesMatch = options.cssModulesMatch || /\.module\./ + const cssModulesMatch = createCssModulesMatchFromOptions(options) const isCSSModule = args.path.match(cssModulesMatch) const cssModulesOptions = options.cssModulesOptions || {} const renderOptions = options.renderOptions @@ -137,7 +142,7 @@ const stylePlugin = (options: PluginOptions = {}) => ({ } // Resolve all css or other style here - build.onResolve({ filter: styleFilter }, onStyleResolve.bind(null, build)) + build.onResolve({ filter: styleFilter }, onStyleResolve(options).bind(null, build)) build.onResolve({ filter: /^ni:/, namespace: LOAD_STYLE_NAMESPACE }, onTempStyleResolve.bind(null, build)) // New temp files from rendered css must be evaluated