当您使用 Next.js 实现自定义服务器时,isn't bundled in the production build 。我的问题是(在尝试之前),可以 custom webpack config用来捆绑它吗?
我想要这样做的原因是我不想在生产版本中发布代码。
编辑:我正在使用 Next.js 7.0.2 和 Webpack 4.20.2。所以this是自定义该版本的 webpack 配置的正确链接。
最佳答案
好吧,经过实验,您可以向现有的 Next.js webpack 配置添加另一个条目,但由于自定义服务器需要不同的设置,因此实际上最好单独进行。我现在有一个 build-server
脚本,它将自定义服务器捆绑到名为 .server
的文件夹中。我还在该配置和 Next.js webpack 配置中添加了混淆。
这是我的 next-config.js
:
const withCSS = require('@zeit/next-css')
const WebpackObfuscator = require('webpack-obfuscator')
module.exports = withCSS({
poweredByHeader: false,
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
if (process.env.NODE_ENV === 'production') {
// Extend default config with obfuscation
config.plugins.push(
new WebpackObfuscator ({
rotateStringArray: true
})
)
}
// Important: return the modified config
return config
}
})
这是我的webpack.config.server.js
:
const path = require('path')
const nodeExternals = require('webpack-node-externals')
const WebpackObfuscator = require('webpack-obfuscator')
// Config to build Express server and associated server-side code bundle
module.exports = {
mode: 'production',
target: 'node',
entry: './server/server.js',
externals: [nodeExternals()], // Ignore node_modules
output: {
path: path.resolve(__dirname, '.server'),
filename: 'server.bundle.js',
},
plugins: [
// Obfuscate the code
new WebpackObfuscator ({
rotateStringArray: true
})
]
}
build-server
脚本是:
"build-server": "webpack --config ./webpack.config.server.js"
关于webpack - 我可以使用 webpack 捆绑 Next.js 自定义服务器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68731541/