Skip to content

Commit 171cc5b

Browse files
committed
feat: support delete bundle file after inline task(close #1)
1 parent 2a78672 commit 171cc5b

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ module.exports = {
100100
```
101101

102102
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`).
103+
Add `inline-bundle-delete` attribute for deleting the bundle after inline task.
103104
```html
104-
<script inline inline-bundle="Your bundle path/Your bundle name"></script>
105+
<script inline inline-bundle-delete inline-bundle="Your bundle path/Your bundle name"></script>
105106
```
106107

107108
## License

index.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {inlineSource} = require('inline-source');
22

33
class InlineSourceWebpackPlugin {
44
constructor(options = {}) {
5+
this.deleteBundles = [];
56
this.options = Object.assign({
67
compress: false
78
}, options);
@@ -22,6 +23,10 @@ class InlineSourceWebpackPlugin {
2223
for (let name in compilation.assets) {
2324
if (name.indexOf(bundle) > -1) {
2425
source.content = compilation.assets[name].source();
26+
if (source.props['bundle-delete']) {
27+
// mark the bundle that need to delete
28+
this.deleteBundles.push(name);
29+
}
2530
break;
2631
}
2732
}
@@ -55,27 +60,48 @@ class InlineSourceWebpackPlugin {
5560
});
5661
}
5762

63+
/**
64+
* delete target bundle
65+
* @param compilation
66+
* @private
67+
*/
68+
_deleteBundle(compilation) {
69+
if (this.deleteBundles.length) {
70+
this.deleteBundles.forEach(bundle => delete compilation.assets[bundle]);
71+
}
72+
this.deleteBundles = [];
73+
}
74+
5875
apply(compiler) {
5976
if ('hooks' in compiler) {
77+
const name = this.constructor.name;
6078
// webpack 4 or higher
61-
compiler.hooks.compilation.tap(this.constructor.name, compilation => {
79+
compiler.hooks.compilation.tap(name, compilation => {
6280
// if htmlWebpackPlugin is not exist, just do nothing
6381
if (compilation.hooks.htmlWebpackPluginAfterHtmlProcessing) {
6482
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(
65-
this.constructor.name,
83+
name,
6684
(data, cb) => {
6785
this._process(compilation, data, cb);
6886
}
6987
);
7088
}
7189
});
90+
compiler.hooks.emit.tapAsync(name, (compilation, callback) => {
91+
this._deleteBundle(compilation);
92+
callback && callback();
93+
});
7294
} else {
7395
// webpack 2 or 3
7496
compiler.plugin('compilation', compilation => {
7597
compilation.plugin('html-webpack-plugin-after-html-processing', (data, cb) => {
7698
this._process(compilation, data, cb);
7799
});
78100
});
101+
compiler.plugin('emit', (compilation, callback) => {
102+
this._deleteBundle(compilation);
103+
callback && callback();
104+
});
79105
}
80106
}
81107
}

0 commit comments

Comments
 (0)