您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“如何解決webpack4配置ssr環(huán)境遇到“document is not defined”的問(wèn)題”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何解決webpack4配置ssr環(huán)境遇到“document is not defined”的問(wèn)題”這篇文章吧。
最近使用 webpack 4 配置 ssr 環(huán)境,發(fā)現(xiàn)的問(wèn)題:
ReferenceError: document is not defined
本次package.json使用版本信息:
{ "vue-loader": "^15.4.2", "mini-css-extract-plugin": "^0.4.3", "webpack": "^4.20.2", "webpack-cli": "^3.1.2" ... }
相關(guān)代碼
問(wèn)題原因:
在服務(wù)端渲染打包的配置中使用了mini-css-extract-plugin是的server bundle中會(huì)使用到document,node環(huán)境中不存在window對(duì)象,所以報(bào)錯(cuò)。
解決辦法:
在webpack.base.config.js中不配置樣式相關(guān)的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:
# 只展示先關(guān)配置 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:
# 只展示先關(guān)配置 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() ] })
以上是“如何解決webpack4配置ssr環(huán)境遇到“document is not defined”的問(wèn)題”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。