File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ class HtmlWebpackHandleCssInjectPlugin {
2
+ constructor ( options = { } ) {
3
+ this . options = options ;
4
+ }
5
+
6
+ apply ( compiler ) {
7
+ const handleHtmlWebpackPluginAfterHtmlProcessing = ( data ) => {
8
+ const { filter } = this . options ;
9
+ if ( ! filter ) {
10
+ return ;
11
+ }
12
+ data . html = data . html . replace ( / < l i n k .+ ?> (? = (?: < .+ ?> ) * < \/ h e a d > ) / g, ( link ) => {
13
+ const filePath = link . match ( / ( h r e f = " ) ( .* ) " / ) [ 2 ] ;
14
+ return filter ( filePath , link ) ? link : '' ;
15
+ } ) ;
16
+ return new Promise ( ( resolve ) => {
17
+ resolve ( data ) ;
18
+ } ) ;
19
+ } ;
20
+
21
+ if ( compiler . hooks ) {
22
+ // webpack 4 support
23
+ compiler . hooks . compilation . tap ( 'htmlWebpackPluginAfterHtmlProcessing' , ( compilation ) => {
24
+ compilation . hooks . htmlWebpackPluginAfterHtmlProcessing . tapPromise ( 'htmlWebpackPluginAfterHtmlProcessing' , handleHtmlWebpackPluginAfterHtmlProcessing ) ;
25
+ } ) ;
26
+ } else {
27
+ // Hook into the html-webpack-plugin processing
28
+ compiler . plugin ( 'compilation' , ( compilation ) => {
29
+ compilation . plugin ( 'html-webpack-plugin-after-html-processing' , handleHtmlWebpackPluginAfterHtmlProcessing ) ;
30
+ } ) ;
31
+ }
32
+ }
33
+ }
34
+
35
+ module . exports = HtmlWebpackHandleCssInjectPlugin ;
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ const fs = require('fs');
3
3
const webpack = require ( 'webpack' ) ;
4
4
const ExtractTextPlugin = require ( 'extract-text-webpack-plugin' ) ;
5
5
const HtmlwebpackPlugin = require ( 'html-webpack-plugin' ) ;
6
+ const HtmlWebpackHandleCssInjectPlugin = require ( './HtmlWebpackHandleCssInjectPlugin' ) ;
6
7
7
8
const { STYLE_DEBUG } = process . env ;
8
9
// 主题路径
@@ -88,6 +89,11 @@ module.exports = {
88
89
title : 'webpack 多主题打包演示' ,
89
90
template : 'src/index.html' ,
90
91
inject : true
92
+ } ) ,
93
+ new HtmlWebpackHandleCssInjectPlugin ( {
94
+ filter : ( filePath ) => {
95
+ return filePath . includes ( 'style' ) ;
96
+ }
91
97
} )
92
98
] ,
93
99
devtool : STYLE_DEBUG === 'SOURCE' && 'source-map'
You can’t perform that action at this time.
0 commit comments