Skip to content

feat(plugins/import): add <import> filter support (options.import) #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2018
Merged
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
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,40 @@ If your application includes many HTML Components or certain HTML Components are
</div>
```

#### `{Boolean}`

**webpack.config.js**
```js
{
loader: 'html-loader',
options: {
import: false
}
}
```

#### `{RegExp}`

**webpack.config.js**
```js
{
loader: 'html-loader',
options: {
import: // TODO add URL filter method (#158)
import: /filter/
}
}
```

#### `{Function}`

**webpack.config.js**
```js
{
loader: 'html-loader',
options: {
import (url) {
return /filter/.test(url)
}
}
}
```
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ export default function loader(html, map, meta) {

const plugins = [];

if (options.url) plugins.push(urls());
if (options.import) plugins.push(imports(options));
// HTML URL Plugin
if (options.url) {
plugins.push(urls());
}

// HTML IMPORT Plugin
if (options.import) {
plugins.push(imports(options));
}

// TODO(michael-ciniawsky)
// <imports src=""./file.html"> aren't minified (options.template) (#160)
if (options.minimize) plugins.push(minifier());
Expand Down
7 changes: 6 additions & 1 deletion src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
"type": "boolean"
},
"import": {
"type": "boolean"
"anyOf": [
{ "type": "string" },
{ "type": "boolean" },
{ "instanceof": "RegExp" },
{ "instanceof": "Function" }
]
},
"template": {
"type": [ "boolean", "string" ]
Expand Down
23 changes: 19 additions & 4 deletions src/plugins/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@
// External URL (Protocol URL)
const URL = /^\w+:\/\//;
const TAGS = [ { tag: 'import' }, { tag: 'include' } ];
// TODO(michael-ciniawsky)
// add filter method for urls (e.g `options.import`) (#158)
const filter = (url) => {
return URL.test(url) || url.startsWith('//');

const filter = (url, options) => {
if (URL.test(url)) {
return true;
}

if (url.startsWith('//')) {
return true;
}

if (options.import instanceof RegExp) {
return options.import.test(url);
}

if (typeof options.import === 'function') {
return options.import(url);
}

return false;
};

export default function (options = {}) {
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/options/import/filter/fixture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Ignore -->
<import src="//file.html"></import>
<import src="//cdn.com/file.html"></import>
<import src="http://cdn.com/file.html"></import>
<import src="https://cdn.com/file.html"></import>
<!-- Transform -->
<import src="./1.html"></import>
<import src="/2.html"></import>
<!-- Filter (Ignore) -->
<import src="./filter/1.html"></import>
<import src="/filter/2.html"></import>
<!-- Transform -->
<include src="./1.html"></include>
<include src="/2.html"></include>
<!-- Filter (Ignore) -->
<include src="./filter/1.html"></include>
<include src="/filter/2.html"></include>

3 changes: 3 additions & 0 deletions test/fixtures/options/import/filter/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import html from './fixture.html';

export default html;
99 changes: 98 additions & 1 deletion test/options/__snapshots__/import.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options import {Boolean} 1`] = `
exports[`Options import {Boolean} - false 1`] = `
"// HTML Imports


// HTML Exports


// HTML
export default \`<!DOCTYPE html>
<html lang=\\"en\\">
<head>
<meta charset=\\"utf-8\\">
<title>HTML Loader</title>
</head>
<body>
<!-- Ignore -->
<import src=\\"//file.html\\"></import>
<import src=\\"//cdn.com/file.html\\"></import>
<import src=\\"http://cdn.com/file.html\\"></import>
<import src=\\"https://cdn.com/file.html\\"></import>
<!-- Transform -->
<import src=\\"./1.html\\"></import>
<import src=\\"/2.html\\"></import>

<include src=\\"./1.html\\"></include>
<include src=\\"/2.html\\"></include>
</body>
</html>
\`"
`;

exports[`Options import {Boolean} - true - default 1`] = `
"// HTML Imports
import HTML__IMPORT__0 from './1.html';
import HTML__IMPORT__1 from '/2.html';
Expand Down Expand Up @@ -32,5 +63,71 @@ export default \`<!DOCTYPE html>
\${HTML__IMPORT__3}
</body>
</html>
\`"
`;

exports[`Options import {Function} 1`] = `
"// HTML Imports
import HTML__IMPORT__0 from './1.html';
import HTML__IMPORT__1 from '/2.html';
import HTML__IMPORT__2 from './1.html';
import HTML__IMPORT__3 from '/2.html';


// HTML Exports


// HTML
export default \`<!-- Ignore -->




<!-- Transform -->
\${HTML__IMPORT__0}
\${HTML__IMPORT__1}
<!-- Filter (Ignore) -->


<!-- Transform -->
\${HTML__IMPORT__2}
\${HTML__IMPORT__3}
<!-- Filter (Ignore) -->



\`"
`;

exports[`Options import {RegExp} 1`] = `
"// HTML Imports
import HTML__IMPORT__0 from './1.html';
import HTML__IMPORT__1 from '/2.html';
import HTML__IMPORT__2 from './1.html';
import HTML__IMPORT__3 from '/2.html';


// HTML Exports


// HTML
export default \`<!-- Ignore -->




<!-- Transform -->
\${HTML__IMPORT__0}
\${HTML__IMPORT__1}
<!-- Filter (Ignore) -->


<!-- Transform -->
\${HTML__IMPORT__2}
\${HTML__IMPORT__3}
<!-- Filter (Ignore) -->



\`"
`;
52 changes: 51 additions & 1 deletion test/options/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import webpack from '../helpers/compiler';

describe('Options', () => {
describe('import', () => {
test('{Boolean}', async () => {
test('{Boolean} - true - default', async () => {
const config = {
loader: {
test: /\.html$/,
Expand All @@ -16,5 +16,55 @@ describe('Options', () => {

expect(source).toMatchSnapshot();
});

test('{Boolean} - false', async () => {
const config = {
loader: {
test: /\.html$/,
options: {
import: false
},
},
};

const stats = await webpack('options/import/fixture.js', config);
const { source } = stats.toJson().modules[1];

expect(source).toMatchSnapshot();
});

test('{RegExp}', async () => {
const config = {
loader: {
test: /\.html$/,
options: {
import: /filter/
},
},
};

const stats = await webpack('options/import/filter/fixture.js', config);
const { source } = stats.toJson().modules[1];

expect(source).toMatchSnapshot();
});

test('{Function}', async () => {
const config = {
loader: {
test: /\.html$/,
options: {
import (url) {
return /filter/.test(url)
}
},
},
};

const stats = await webpack('options/import/filter/fixture.js', config);
const { source } = stats.toJson().modules[1];

expect(source).toMatchSnapshot();
});
});
});