From 1abea57d7adc8913876f00e107e5b36d956873b6 Mon Sep 17 00:00:00 2001 From: anx00 Date: Tue, 12 May 2026 11:21:00 +0200 Subject: [PATCH] Add wildcard module matching --- README.md | 26 ++++++++++++++++++++++++++ fixtures/wildcard-modules-source.js | 23 +++++++++++++++++++++++ fixtures/wildcard-modules.js | 15 +++++++++++++++ lib/config.js | 8 +++++--- test/test.js | 8 ++++++++ 5 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 fixtures/wildcard-modules-source.js create mode 100644 fixtures/wildcard-modules.js diff --git a/README.md b/README.md index 31e6f97..8155fc9 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,32 @@ A list of module names or import paths where tags are imported from. The values the arrays refers to the export names, not the import names. `null` refers to the default export. +Use `"*"` as a module name to match configured named exports from any import or +require source: + +```json +{ + "plugins": [ + ["template-html-minifier", { + "modules": { + "*": [ + "html", + {"name": "css", "encapsulation": "style"} + ] + }, + "htmlMinifier": { + "collapseWhitespace": true, + "minifyCSS": true + } + }] + ] +} +``` + +This is useful when `html` or `css` tags are re-exported by application-specific +modules and matching a fixed package name would be too strict. Exact module +entries still take precedence over the wildcard entry. + ### `failOnError` Determines whether an error should be thrown when minification failed. defaults to true. diff --git a/fixtures/wildcard-modules-source.js b/fixtures/wildcard-modules-source.js new file mode 100644 index 0000000..ba79408 --- /dev/null +++ b/fixtures/wildcard-modules-source.js @@ -0,0 +1,23 @@ +import {html, css as style} from './template-tags'; +import {html as page} from 'custom-html'; +import untouchedHtml from 'custom-html'; + +html` +
+ Hello +
+`; + +style` + .example { + color: red; + } +`; + +page`World`; + +untouchedHtml` +
+ Hello +
+`; diff --git a/fixtures/wildcard-modules.js b/fixtures/wildcard-modules.js new file mode 100644 index 0000000..d1285ec --- /dev/null +++ b/fixtures/wildcard-modules.js @@ -0,0 +1,15 @@ +import {html, css as style} from './template-tags'; +import {html as page} from 'custom-html'; +import untouchedHtml from 'custom-html'; + +html`
Hello
`; + +style`.example{color:red}`; + +page`World`; + +untouchedHtml` +
+ Hello +
+`; diff --git a/lib/config.js b/lib/config.js index 6911f70..bc3f975 100644 --- a/lib/config.js +++ b/lib/config.js @@ -73,8 +73,10 @@ function normalizeModuleConfig(name, items) { } function findModuleConfig(modulesConfig, importSource) { + const wildcardConfig = modulesConfig['*'] || null; + if (importSource[0] === '.' || importSource[0] === '/') { - return null; + return wildcardConfig; } if (modulesConfig[importSource]) { @@ -82,9 +84,9 @@ function findModuleConfig(modulesConfig, importSource) { } try { - return modulesConfig[bareName(importSource)] || null; + return modulesConfig[bareName(importSource)] || wildcardConfig; } catch (_) { - return null; + return wildcardConfig; } } diff --git a/test/test.js b/test/test.js index 63f2d63..b7481e9 100644 --- a/test/test.js +++ b/test/test.js @@ -111,6 +111,13 @@ const namedWrongMemberConfig = { }, htmlMinifier }; +const wildcardConfig = { + modules: { + '*': ['html', {name: 'css', encapsulation: 'style'}] + }, + strictCSS: true, + htmlMinifier +}; const fixturePath = path.resolve(__dirname, '..', 'fixtures'); @@ -344,6 +351,7 @@ test('ignore calls that are obj.require', fileTest, null, true); test('tolerate built-in modules', fileTest, null, true); test('ignore unknown modules', fileTest, null, true); test('ignore relative import', fileTest, null, true); +test('wildcard modules', fileTest, null, null, wildcardConfig); test('require member class of default export', fileTest, 'require-hyperhtml-default', null, defaultHyperConfig); test('require member class of default export from non-matching module', fileTest, 'require-hyperhtml-default', true, defaultWrongMemberConfig);