I have a very specific requirement where I need to split the JS script tag into one file and the CSS link tag into another file using HtmlWebpackPlugin.
At the moment, both script and link tags are going into both files. Is there a way to do them separately?
Here is my current Webpack file:
import webpack from 'webpack'
import path from 'path'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
import autoprefixer from 'autoprefixer'
const extractCSS = new ExtractTextPlugin({
filename: 'css/app.bundle.css',
allChunks: true
})
const createCSSfile = new HtmlWebpackPlugin({
chunks: ['app'],
minify: {
collapseWhitespace: true
},
hash: true,
template: 'src/ejs/css.ejs',
filename: 'templates/css.php'
})
const createJSfile = new HtmlWebpackPlugin({
chunks: ['app'],
minify: {
collapseWhitespace: true
},
hash: true,
template: 'src/ejs/js.ejs',
filename: 'templates/js.php'
})
const config = {
entry: {
'app': [
path.resolve(__dirname, 'src/js/app.js'),
path.resolve(__dirname, 'src/scss/app.scss')
]
},
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist',
filename: 'js/app.bundle.js',
sourceMapFilename: 'js/app.bundle.map'
},
devtool: 'source-map',
watch: true,
watchOptions: {
ignored: /node_modules/,
aggregateTimeout: 300,
poll: 1000
},
module: {
rules: [
{
test: /\.(png|gif|jpg|jpeg)$/,
use: [
{
loader: 'file-loader',
options: {
name: '/images/[name].[ext]'
}
}
]
},
{
test: /\.(eot|ttf|woff|woff2|otf)$/,
use: [
{
loader: 'file-loader',
options: {
name: '/fonts/[name].[ext]'
}
}
]
},
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: [require('@babel/plugin-proposal-object-rest-spread')]
}
}
},
{
test: /\.scss$/,
use: extractCSS.extract([
{
loader: 'css-loader'
},
{
loader: 'postcss-loader',
options: {
plugins () {
return [
autoprefixer({
browsers: [
'last 2 versions',
'Safari >= 8',
'Explorer >= 9',
'Android >= 4'
]
})
]
}
}
},
{
loader: 'sass-loader'
}
])
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'js/app.common',
filename: 'js/app.common.js',
minChunks: 2
}),
createCSSfile,
createJSfile,
extractCSS
]
}
export default config
Each .ejs file is empty and generates the following inside the .php files:
<head><link href="/dist/css/app.bundle.css?bdba9ec6846a7d92d61f" rel="stylesheet"></head><script type="text/javascript" src="/dist/js/app.bundle.js?bdba9ec6846a7d92d61f"></script>
Is there a way to separate them?
Also, I noticed it is inserting a head tag for the CSS link; is there a way to stop this happening?