https://webpack.js.org/guides/getting-started/
npm init -y
npm install webpack webpack-cli —save-dev
npm install —save lodash
webpack-demo
|- package.json
|- webpack.config.js
|- /dist
|- main.js
|- index.html
|- /src
|- index.js
|- /node_modules
<!-- /dist/index.html -->
<!doctype html>
<html>
<head>
<title>Getting Started</title>
</head>
<body>
<script src="main.js"></script>
</body>
</html>
// /webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist')
}
};
// /index.js
import _ from 'lodash';
function component() {
let element = document.createElement('div');
element.innerHTML = _.join(['Hello', 'webpack'], ' ');
return element;
}
document.body.appendChild(component());
控制台,执行webpack
会自动生成dist文件夹,index.html页面要手动拷贝进去
会从src中找到index.js然后“通过加工”在dist文件夹中自动生成main.js,需要你配置加载器,就会对js进行加工了