Webpack-Node-Js
一、准备
- 初始化项目命令
yarn init -y
- 安装webpack 配置所需依赖
yarn add webpack webpack-cli -D
- Babel 配置所需要的依赖
yarn add core-js babel-loader @babel/core @babel/preset-env @babel/plugin-transform-runtime @babel/runtime-corejs3 -D
二、Babel 配置
const presets = ['@babel/preset-env']
const plugins = [
['@babel/plugin-transform-runtime',{
corejs:3
}]
]
module.exports = {
presets,
plugins
}
三、Webpack 配置
const Path = require('path')
module.exports = {
mode:'development',
target:'node',
entry:Path.resolve(__dirname,'app.js'),
output:{
filename:'app.js',
path:Path.resolve(__dirname,'build')
},
module:{
rules:[
{
test:/\.js/,
exclude:/node_modules/,
use:['babel-loader']
}
]
}
}
四、package.json 命令与依赖
{
"name": "webpack-node",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "node build/app.js",
"build": "webpack build"
},
"devDependencies": {
"@babel/core": "^7.18.2",
"@babel/plugin-transform-runtime": "^7.18.2",
"@babel/preset-env": "^7.18.2",
"@babel/runtime-corejs3": "^7.18.3",
"babel-loader": "^8.2.5",
"webpack": "^5.72.1",
"webpack-cli": "^4.9.2"
}
}