Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions fixtures/wildcard-modules-source.js
Original file line number Diff line number Diff line change
@@ -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`
<div class="example" >
Hello
</div>
`;

style`
.example {
color: red;
}
`;

page`<span data-id="test" >World</span>`;

untouchedHtml`
<div class="example" >
Hello
</div>
`;
15 changes: 15 additions & 0 deletions fixtures/wildcard-modules.js
Original file line number Diff line number Diff line change
@@ -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`<div class=example>Hello</div>`;

style`.example{color:red}`;

page`<span data-id=test>World</span>`;

untouchedHtml`
<div class="example" >
Hello
</div>
`;
8 changes: 5 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,20 @@ function normalizeModuleConfig(name, items) {
}

function findModuleConfig(modulesConfig, importSource) {
const wildcardConfig = modulesConfig['*'] || null;

if (importSource[0] === '.' || importSource[0] === '/') {
return null;
return wildcardConfig;
}

if (modulesConfig[importSource]) {
return modulesConfig[importSource];
}

try {
return modulesConfig[bareName(importSource)] || null;
return modulesConfig[bareName(importSource)] || wildcardConfig;
} catch (_) {
return null;
return wildcardConfig;
}
}

Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ const namedWrongMemberConfig = {
},
htmlMinifier
};
const wildcardConfig = {
modules: {
'*': ['html', {name: 'css', encapsulation: 'style'}]
},
strictCSS: true,
htmlMinifier
};

const fixturePath = path.resolve(__dirname, '..', 'fixtures');

Expand Down Expand Up @@ -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);
Expand Down