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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ 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 the module name to match named tags from any module, including
re-export modules:

```js
{
modules: {
'*': ['html', {name: 'css', encapsulation: 'style'}]
}
}
```

This keeps the stricter binding-based behavior for explicitly configured modules,
but allows projects with wrapper modules to minify every `html` or `css` tag by
name.

### `failOnError`

Determines whether an error should be thrown when minification failed. defaults to true.
Expand Down
26 changes: 5 additions & 21 deletions fixtures/transform-template-literals-after.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
function _templateObject2() {
const data = _taggedTemplateLiteral(["<span class=\"", "\" disabled=\"", "\" >test</span>"]);
_templateObject2 = function() {
return data;
};
return data;
}

function _templateObject(){
const data = _taggedTemplateLiteral(["<span class=", " disabled=", ">test</span>"]);
_templateObject = function(){
return data;
};
return data;
}
var _templateObject, _templateObject2;

function _taggedTemplateLiteral(strings, raw) {
if (!raw) {
raw = strings.slice(0);
}
return Object.freeze(Object.defineProperties(strings, {raw: {value: Object.freeze(raw)}}));
function _taggedTemplateLiteral(e, t) {
return t || (t = e.slice(0)), Object.freeze(Object.defineProperties(e, {raw: {value: Object.freeze(t)}}));
}

import {html, render} from 'lit-html';

const myclass = 'test';
const disabled = false;
html(_templateObject(),myclass,disabled);render(_templateObject2(),myclass,disabled);
html(_templateObject || (_templateObject = _taggedTemplateLiteral(["<span class=", " disabled=", ">test</span>"])), myclass, disabled);
render(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["<span class=\"", "\" disabled=\"", "\" >test</span>"])), myclass, disabled);
11 changes: 11 additions & 0 deletions fixtures/wildcard-local-tag-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const html = String.raw;
const css = String.raw;
const render = String.raw;

html`<span class="test" >ok</span>`;
css`
.item {
color: red;
}
`;
render`<span class="test" ></span>`;
7 changes: 7 additions & 0 deletions fixtures/wildcard-local-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const html = String.raw;
const css = String.raw;
const render = String.raw;

html`<span class=test>ok</span>`;
css`.item{color:red}`;
render`<span class="test" ></span>`;
12 changes: 12 additions & 0 deletions fixtures/wildcard-named-import-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {html as pageHtml, render} from './template-tags';
import {css} from './theme';

const color = 'red';

pageHtml`<div class="card" disabled="disabled" >${color}</div>`;
css`
.card {
color: ${color};
}
`;
render`<div class="card" disabled="disabled" ></div>`;
8 changes: 8 additions & 0 deletions fixtures/wildcard-named-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {html as pageHtml, render} from './template-tags';
import {css} from './theme';

const color = 'red';

pageHtml`<div class=card disabled=disabled>${color}</div>`;
css`.card{color:${color}}`;
render`<div class="card" disabled="disabled" ></div>`;
26 changes: 17 additions & 9 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const isBuiltinModule = require('is-builtin-module');
const createMinifyCSS = require('./minify-css');

const wildcardModule = '*';

function ownerName(importSource) {
const parts = importSource.split('/', importSource[0] === '@' ? 2 : 1);

Expand Down Expand Up @@ -73,19 +75,20 @@ function normalizeModuleConfig(name, items) {
}

function findModuleConfig(modulesConfig, importSource) {
if (importSource[0] === '.' || importSource[0] === '/') {
return null;
}

if (modulesConfig[importSource]) {
if (importSource[0] !== '.' && importSource[0] !== '/' && modulesConfig[importSource]) {
return modulesConfig[importSource];
}

try {
return modulesConfig[bareName(importSource)] || null;
} catch (_) {
return null;
if (importSource[0] !== '.' && importSource[0] !== '/') {
try {
const moduleConfig = modulesConfig[bareName(importSource)];
if (moduleConfig) {
return moduleConfig;
}
} catch (_) {}
}

return modulesConfig[wildcardModule] || null;
}

function getModuleConfig(modulesConfig, importSource) {
Expand All @@ -105,6 +108,10 @@ function normalizeModulesConfig(modules) {
return modulesConfig;
}

function getWildcardModuleConfig(modulesConfig) {
return modulesConfig[wildcardModule] || normalizeModuleConfig(wildcardModule, []);
}

function normalizeMinifierConfig(config) {
if (!config || !config.htmlMinifier) {
return undefined;
Expand All @@ -126,5 +133,6 @@ function normalizeMinifierConfig(config) {
module.exports = {
normalizeModulesConfig,
getModuleConfig,
getWildcardModuleConfig,
normalizeMinifierConfig
};
14 changes: 13 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const htmlMinifier = require('html-minifier-terser');

const cookRawQuasi = require('./cook-raw-quasi');
const {normalizeModulesConfig, normalizeMinifierConfig, getModuleConfig} = require('./config.js');
const {normalizeModulesConfig, normalizeMinifierConfig, getModuleConfig, getWildcardModuleConfig} = require('./config');

function setupDefaultBindingOption(bindings, binding, moduleConfig) {
if (!moduleConfig.defaultExport) {
Expand Down Expand Up @@ -105,13 +105,24 @@ function handleStar(path, state, objectName, optionsFilter) {
}

function handleSimple(path, state, binding, itemCheck) {
const tagName = binding;
if (typeof binding === 'string') {
binding = path.scope.getBinding(binding);
}

const bindings = state.bindings.filter(item => item.binding === binding && item.star === false && itemCheck(item));
if (bindings.length === 1) {
minify(path, state, bindings[0].options);
return;
}

if (typeof tagName !== 'string') {
return;
}

const wildcardOptions = state.wildcardModuleConfig.namedExports.filter(options => options.name === tagName && itemCheck({options}));
if (wildcardOptions.length === 1) {
minify(path, state, wildcardOptions[0]);
}
}

Expand Down Expand Up @@ -149,6 +160,7 @@ module.exports = babel => {
Program: {
enter() {
this.moduleConfigs = normalizeModulesConfig(this.opts.modules);
this.wildcardModuleConfig = getWildcardModuleConfig(this.moduleConfigs);
this.minifierConfig = normalizeMinifierConfig(this.opts);
this.failOnError = this.opts.failOnError !== false;
this.logOnError = this.opts.logOnError !== false;
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ const cssLitConfig = {
logOnError: false,
htmlMinifier
};
const wildcardConfig = {
modules: {
'*': [{name: 'html'}, {name: 'css', encapsulation: 'style'}]
},
strictCSS: true,
failOnError: false,
logOnError: false,
htmlMinifier
};

const defaultChooConfig = {
modules: {
Expand Down Expand Up @@ -318,6 +327,8 @@ test('default import', fileTest, 'choo', null, defaultChooConfig);
test('named import', fileTest, 'lit-html', null, complexLitConfig);
test('ignore copy', fileTest, null, true);
test('import star', fileTest);
test('wildcard named import', fileTest, null, null, wildcardConfig);
test('wildcard local tag', fileTest, null, null, wildcardConfig);
test('templated special attributes', fileTest);
test('import of main module file', fileTest);
test('non-main module file is ignored', fileTest, null, true);
Expand Down