node.js 如何将使用 Webpack 的节点部署到 heroku

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36223070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:04:43  来源:igfitidea点击:

How to deploy node that uses Webpack to heroku

node.jsherokuwebpack

提问by Inanc Gumus

I'm using webpack.

我正在使用 webpack。

Also I don't commit npm_modules folder and public folder, where all generated files are. And I can't figure out how to build my app (I have webpack build command) after deploy and setup my server (it's already looking for public folder).

此外,我不提交所有生成的文件所在的 npm_modules 文件夹和公共文件夹。在部署和设置我的服务器(它已经在寻找公共文件夹)之后,我无法弄清楚如何构建我的应用程序(我有 webpack build 命令)。

It seems me a bad idea to commit before upload. Maybe there are some gentle decisions... Any thoughts?

在上传之前提交似乎是个坏主意。也许有一些温和的决定......有什么想法吗?

Forked from: How to deploy node that uses Gulp to heroku

派生自:如何将使用 Gulp 的节点部署到 heroku

回答by Gaurav Mukherjee

"heroku-postbuild": "webpack -p --config ./webpack.prod.config.js --progress"

this is better because if you use postinstall, everytime you do npm ithe build script get fired

这更好,因为如果您使用 postinstall,每次执行npm i构建脚本时都会被解雇

回答by Zeeshan Hassan Memon

Do it in a postinstallscript as suggested by @orlando:

postinstall按照@orlando 的建议在脚本中执行此操作:

"postinstall": "webpack -p --config ./webpack.prod.config.js --progress"

In this approach make sure to heroku config:set NODE_ENV=productionheroku config:set NPM_CONFIG_PRODUCTION=true

在这种方法中,请确保 heroku config:set NODE_ENV=productionheroku config:set NPM_CONFIG_PRODUCTION=true

OR

或者

You can find this custom buildpackheroku-buildpack-webpackusable.

你可以找到这个自定义 buildpack heroku-buildpack-webpack可用。

These links might help you build understanding:

这些链接可能会帮助您建立理解:

回答by ford

In 2019, the simplest way to do this is to use the heroku/nodejsbuildpack (automatically selected if you have a package.jsonat the root of the repository) and add a buildscript to package json:

在2019年,最简单的方法是使用heroku/nodejsbuildpack(如果你package.json在存储库的根目录下有a ,则自动选择)并添加build脚本打包json:

"build": "webpack --mode production --config ./webpack.prod.config.js"

Heroku will automatically run this script on deploy. Bonus points because this is the intuitive script to run to test the build locally or in CI.

Heroku 将在部署时自动运行此脚本。加分项是因为这是运行以在本地或 CI 中测试构建的直观脚本。

This is described in Heroku's Best Practices for Node.js Developmentdocument:

Heroku 的Node.js 开发最佳实践文档中对此进行了描述:

npm's lifecycle scripts make great hooks for automation. Heroku provides custom hooks that allow you to run custom commands before or after we install your dependencies. If you need to run something before building your app, you can use the heroku-prebuildscript. Need to build assets with grunt, gulp, browserify, or webpack? Do it in a buildscript.

npm 的生命周期脚本为自动化提供了很好的挂钩。Heroku 提供自定义钩子,允许您在我们安装依赖项之前或之后运行自定义命令。如果您需要在构建应用程序之前运行某些内容,则可以使用该heroku-prebuild脚本。需要使用 grunt、gulp、browserify 或 webpack 构建资产吗?在build脚本中执行。