SkyBlueFeet / skyui

MIT License
0 stars 0 forks source link

Vue Webpack4 SSR server.bundle.js报错document is not defined #6

Open SkyBlueFeet opened 4 years ago

SkyBlueFeet commented 4 years ago

Vue Webpack4 SSR server.bundle.js 报错 document is not defined

Detailed description of the problem

Problem Causes

是由于运行上述插件后异步导入组件时服务端代码会执行 document.createElement 方法插入 link 标签,但是此时 document 对象为 undefined,因此会出现该错误。作为验证,当你检查打包后的服务端代码时会发现有大量多余的 document 对象方法,服务端代码则无法运行。

Solution

Indirect Method:

Direct Method:

class ServerMiniCssExtractPlugin extends MiniCssExtractPlugin {
    getCssChunkObject(mainChunk) {
        return {};
    }
}

Reference

Unsolved

extract-css-chunks-webpack-plugin文档中提到该插件 SSR 友好,应当也会支持 SSR 的异步组件,但直接替换mini-css-extract-plugin并没有解决问题,不清楚是否需要进行特殊配置

Supplement

ItsRyanWu commented 4 years ago

for that unsolved problem you need to add

new webpack.optimize.LimitChunkCountPlugin({
    maxChunks: 1
})

in your webpack.server config file plugins then it just works

I don't know exactly what's the relation between them.