最近使用 webpack 4 配置 ssr 環境,發現的問題:javascript
ReferenceError: document is not defined
css
本次package.json使用版本信息:vue
{
"vue-loader": "^15.4.2",
"mini-css-extract-plugin": "^0.4.3",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2"
...
}
複製代碼
相關代碼java
在服務端渲染打包的配置中使用了mini-css-extract-plugin
是的server bundle中會使用到document
,node
環境中不存在window
對象,因此報錯。node
webpack.base.config.js
中不配置樣式相關的loader
:# 基本配置
const path = require('path')
const webpack = require('webpack')
const {VueLoaderPlugin} = require('vue-loader')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const resolve = dir => path.join(__dirname, '../', dir)
const isProd = process.env.NODE_ENV === 'production'
const base = {
mode: isProd ? 'production' : 'development',
devtool: isProd ? false : 'cheap-eval-source-map',
output: {
path: path.resolve(__dirname, '../dist'),
publicPath: '/dist/',
filename: '[name].[chunkhash].js'
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
'public': path.resolve(__dirname, '../public')
}
},
module: {
noParse: /es6-promise\.js$/, // avoid webpack shimming process
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
include: resolve("src")
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: file => (
/node_modules/.test(file) && !/\.vue\.js/.test(file)
)
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'url-loader',
options: {
limit: 10000,
name: '[name].[ext]?[hash]'
}
}
]
},
plugins: setPlugin()
}
function setPlugin() {
const base = [new VueLoaderPlugin()]
const dev = [new FriendlyErrorsPlugin()]
const prod = []
return base.concat(isProd ? prod : dev)
}
module.exports = base;
複製代碼
webpack.client.config.js
中使用mini-css-extract-plugin
:# 只展現先關配置
const webpack = require('webpack')
const merge = require('webpack-merge')
const base = require('./webpack.base.config')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const VueSSRClientPlugin = require('vue-server-renderer/client-plugin')
const isProd = process.env.NODE_ENV === 'production'
const config = merge(base, {
module: {
rules: [
{
test: /\.styl(us)?$/,
use: [
isProd ? MiniCssExtractPlugin.loader : 'vue-style-loader',
{
loader: 'css-loader'
},
'stylus-loader'
],
},
{
test: /\.(le|c)ss$/,
use: [
isProd ? MiniCssExtractPlugin.loader : 'vue-style-loader',
{
loader: 'css-loader'
},
'less-loader',
],
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: isProd ? '[name].[chunkhash].css' : '[name].css',
chunkFilename: isProd ? '[id].[chunkhash].css': '[id].css',
}),
new VueSSRClientPlugin()
]
})
module.exports = config
複製代碼
webpack.server.config.js
中不使用mini-css-extract-plugin
:# 只展現先關配置
const webpack = require('webpack')
const merge = require('webpack-merge')
const base = require('./webpack.base.config')
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin')
module.exports = merge(base, {
target: 'node',
module: {
rules: [
{
test: /\.styl(us)?$/,
use: [
'vue-style-loader',
{
loader: 'css-loader'
},
'stylus-loader'
],
},
{
test: /\.(le|c)ss$/,
use: [
'vue-style-loader',
{
loader: 'css-loader'
},
'less-loader',
],
}
]
},
plugins: [
new VueSSRServerPlugin()
]
})
複製代碼
tiodot/vnewswebpack
vue-loader@15.0.0-rc.1 in a server bundlegit
webpack-contrib/mini-css-extract-plugin es6
因爲本次webpack4版本比較新,周邊的插件沒能及時作出相應更新;github
相信通過社區的不斷努力,整個生態會更加健壯。web