看知乎有人讨论不需要打包?难道你的软件免费别人吗?
cd ~/Workspace
mkdir helloworld
cd helloworld
安装webpack和CLI
yarn add webpack webpack-cli -D
See https://webpack.docschina.org/guides/getting-started/#basic-setup
安装webpack-node-externals
yarn add webpack-node-externals -D
See https://www.npmjs.com/package/webpack-node-externals
打包服务器端代码
See https://webpack.docschina.org/concepts/
创建webpack.config.js
文件。
const nodeExternals = require("webpack-node-externals");
const path = require("path");
module.exports = {
entry: "./src/main.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist"),
},
target: "node",
externals: [nodeExternals()],
};
修改package.json
:
{
"main": "dist/bundle.js",
//...
"scripts": {
"dev-build": "webpack --mode development --progress --watch",
"build": "webpack --mode production --progress --watch",
"dev": "set NODE_ENV=development && nodemon run",
"run": "node run"
},
//...
}
dev-build
根据配置文件,用webpack
将文件打包成bundle.js
放到dist
目录。build
根据配置文件,用webpack
将文件打包成bundle.js
放到dist
目录。dev
通过nodemon
运行。