webpack安装及配置
新建webpack
npm init
npm i webpack --save-dev
npm i jquery --save
// package.json
{
"name": "webpack-test",
"version": "1.0.0",
"description": "webpack-test",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.16.0"
},
"dependencies": {
"jquery": "^3.3.1"
}
}
// 新建webpack.config.js、src目录、src/app.js
var path = require('path')
var webpack = require('webpack')
module.exports = {
context: path.resolve(__dirname, './src'),
entry: './app.js',
output: {
path: path.resolve(__dirname, './dist'),
filename: 'bundle.js'
}
}
// app.js
console.log(100)
package.json的"scripts":
中添加"start": "webpack",
{
"name": "webpack-test",
"version": "1.0.0",
"description": "webpack-test",
"main": "index.js",
"scripts": {
"start": "webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.16.0"
},
"dependencies": {
"jquery": "^3.3.1"
}
}
// .gitignore
.DS_Store
node_modules/
dist/
data/
.gitignore
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
npm start
index.html中引入bundle.js
// index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>test</title>
</head>
<body>
<div>test</div>
<div id="root"></div>
<script src="dist/bundle.js"></script>
</body>
</html>