Skip to content

Commit 3ba4a6a

Browse files
committed
feat: add 'noAssetMatch' option, 'inline-bundle' -> 'inline-asset'
1 parent f4fb3fc commit 3ba4a6a

File tree

4 files changed

+54
-61
lines changed

4 files changed

+54
-61
lines changed

README.md

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,63 +26,37 @@ $ npm i inline-source-webpack-plugin -D
2626
<div class="container">
2727
<h1>hello world!</h1>
2828
</div>
29-
<!-- 'inline-bundle' attribute tell us to embed file that generated by webpack -->
30-
<script inline inline-bundle="bundle\.\w+\.js$" inline-bundle-delete></script>
29+
<!-- 'inline-asset' attribute tell us to embed file that generated by webpack -->
30+
<script inline inline-asset="runtime\.\w+\.js$" inline-asset-delete></script>
31+
<script inline inline-asset="bundle\.\w+\.js$" inline-asset-delete></script>
3132
</body>
3233
</html>
3334
```
3435

35-
```js
36-
/* ./demo/src/inline.js */
37-
function Person() {
38-
}
36+
You can find this demo in the demo directory and view the output:
3937

40-
Person.prototype.sayHello = function () {
41-
var word = 'hello';
42-
console.log(word);
43-
};
44-
```
38+
```bash
39+
# install dependency
40+
npm i
4541

46-
```js
47-
/* ./demo/src/bundle.js */
48-
console.log('This file is build by webpack.But InlineSourceWebpackPlugin will embed it into html file.');
49-
```
42+
# enter the demo directory
43+
cd demo
5044

51-
```css
52-
/* ./demo/src/inline.css */
53-
.container {
54-
border: 1px solid #000;
55-
}
45+
# build
46+
npm run build
5647
```
5748

58-
Output:
59-
60-
```html
61-
<!-- ./demo/dist/index.html -->
62-
<!DOCTYPE html>
63-
<html lang="en">
64-
<head>
65-
<meta charset="UTF-8">
66-
<title>test</title>
67-
<style>.container{border:1px solid #ff2c58}</style>
68-
<script>function Person(){}Person.prototype.sayHello=function(){console.log("[inline]:","hello world!")},(new Person).sayHello();</script>
69-
</head>
70-
<body>
71-
<div class="container">
72-
<h1>hello world!</h1>
73-
</div>
74-
<!-- 'inline-bundle' attribute tell us to embed file that generated by webpack -->
75-
<script>(window.webpackJsonp=window.webpackJsonp||[]).push([["bundle"],[,function(i,e){console.log("This file is build by webpack.But InlineSourceWebpackPlugin will embed it into html file.")}],[[1,"runtime"]]]);</script>
76-
<script type="text/javascript" src="/inline-source-webpack-plugin/demo/dist/runtime.ee77f51ffadaa60b61bb.js"></script><script type="text/javascript" src="/inline-source-webpack-plugin/demo/dist/index.52decaca808fa7663ddf.js"></script></body>
77-
</html>
78-
```
49+
## Usage
7950

80-
note: You can find this demo in the demo directory.
51+
Available options include(refer to [this](https://github.com/popeindustries/inline-source#usage) for more options):
8152

82-
## Usage
83-
Available `options` include(refer to [this](https://github.com/popeindustries/inline-source#usage) for more options):
8453
- `compress`: enable/disable compression.(default `false`)
8554
- `rootpath`: path used for resolving inlineable paths.
55+
- `noAssetMatchReplace`: work with `noAssetMatch` option.(default `<!-- -->`)
56+
- `noAssetMatch`: define the behaviour while no asset match the value of `inline-asset` attribute.(default `1`)
57+
- `0`: do nothing and the tag is still reserved in the html.
58+
- `1`: throw warning tips and replace the tag with the content of `noAssetMatchReplace` option.
59+
- `2`: throw error tips and replace the tag with the content of `noAssetMatchReplace` option(This level will affect the compilation of webpack).
8660

8761
```javascript
8862
// webpack.config.js
@@ -103,14 +77,14 @@ module.exports = {
10377
};
10478
```
10579

106-
If you want to embed the files that generated by webpack or other plugin, you can use `inline-bundle` attribute to filter the files(Please don't try to use `src` or `href`).
107-
Add `inline-bundle-delete` attribute for deleting the bundle after inline task.
80+
If you want to embed the files that generated by webpack or other plugin, you can use `inline-asset` attribute to filter the files(Please don't try to use `src` or `href`).
81+
Add `inline-asset-delete` attribute for deleting the asset after inline task.
10882

10983
```html
110-
<script inline inline-bundle-delete inline-bundle="Your bundle path/Your bundle name"></script>
84+
<script inline inline-asset-delete inline-asset="Your asset path/Your asset name"></script>
11185
```
11286

113-
The value of `inline-bundle` attribute is a *regular expression*.
87+
The value of `inline-asset` attribute is a *regular expression*.
11488

11589
## License
11690

demo/src/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<div class="container">
1111
<h1>hello world!</h1>
1212
</div>
13-
<!-- 'inline-bundle' attribute tell us to embed file that generated by webpack -->
14-
<script inline inline-bundle="runtime\.\w+\.js$" inline-bundle-delete></script>
15-
<script inline inline-bundle="bundle\.\w+\.js$" inline-bundle-delete></script>
13+
<!-- 'inline-asset' attribute tell us to embed file that generated by webpack -->
14+
<script inline inline-asset="runtime\.\w+\.js$" inline-asset-delete></script>
15+
<script inline inline-asset="bundle\.\w+\.js$" inline-asset-delete></script>
1616
</body>
1717
</html>

demo/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ module.exports = {
2525
}),
2626
new InlineSourceWebpackPlugin({
2727
compress: true,
28-
rootpath: './src'
28+
rootpath: './src',
29+
noAssetMatch: 1
2930
})
3031
],
3132
mode: 'production'

index.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ class InlineSourceWebpackPlugin {
1919
* @private
2020
*/
2121
_process(compilation, data, cb) {
22-
const options = Object.assign({}, this.options);
22+
const options = Object.assign({
23+
noAssetMatch: 1,
24+
noAssetMatchReplace: `<!-- -->`
25+
}, this.options);
2326
options.handlers = (source, context) => {
24-
const bundle = source.props.bundle;
25-
if (bundle) {
26-
const regExp = new RegExp(bundle);
27+
const asset = source.props.asset;
28+
if (asset) {
29+
const regExp = new RegExp(asset);
2730
for (let name in compilation.assets) {
2831
if (regExp.test(name)) {
2932
source.content = compilation.assets[name].source();
3033
if (source.type === 'css') {
3134
// change tag type
3235
source.tag = 'style';
3336
}
34-
if (source.props['bundle-delete']) {
37+
if (source.props['asset-delete']) {
3538
// mark the asset that need to delete
3639
this.deleteAssets.push({
3740
name,
@@ -41,9 +44,24 @@ class InlineSourceWebpackPlugin {
4144
break;
4245
}
4346
}
47+
if (!source.content) {
48+
const noAssetMatchError = new Error(`[${this.constructor.name}]: no assets match '${asset}'.`);
49+
switch (options.noAssetMatch) {
50+
case 0:
51+
break;
52+
case 1:
53+
compilation.warnings.push(noAssetMatchError);
54+
source.replace = options.noAssetMatchReplace;
55+
break;
56+
case 2:
57+
compilation.errors.push(noAssetMatchError);
58+
source.replace = options.noAssetMatchReplace;
59+
break;
60+
}
61+
}
4462
}
4563
if (source.filepath) {
46-
// watch inline target
64+
// watch inline file
4765
if (compilation.fileDependencies.add) {
4866
compilation.fileDependencies.add(source.filepath);
4967
} else {
@@ -82,8 +100,8 @@ class InlineSourceWebpackPlugin {
82100
if (error) throw error;
83101
const attributes = dom[0].attribs;
84102
const url = attributes.href || attributes.src;
85-
needDelete = this.deleteAssets.some(bundle => {
86-
return bundle.regExp.test(url);
103+
needDelete = this.deleteAssets.some(asset => {
104+
return asset.regExp.test(url);
87105
});
88106
})
89107
);
@@ -101,7 +119,7 @@ class InlineSourceWebpackPlugin {
101119
*/
102120
_deleteAsset(compilation) {
103121
if (this.deleteAssets.length) {
104-
this.deleteAssets.forEach(bundle => delete compilation.assets[bundle.name]);
122+
this.deleteAssets.forEach(asset => delete compilation.assets[asset.name]);
105123
}
106124
this.deleteAssets = [];
107125
}

0 commit comments

Comments
 (0)