Webpack is a front-end tool to build JavaScript module scripts for browsers.
webpack编译JavaScript模块
Introduce
对比 Node.js 模块,webpack 模块能够以各种方式表达它们的依赖关系,几个例子如下:
- ES2015 import 语句
- CommonJS require() 语句
- AMD define 和 require 语句
- css/sass/less 文件中的 @import 语句。
- 样式(url(…))或 HTML 文件()中的图片链接(image url)
Some perception
webpack-dev-server
为你提供了一个简单的 web server,并且具有 live reloading(实时重新加载) 功能
How to install
npm install --save-dev webpack-dev-server
webpack.config.js often looks like…
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
module.exports = {
mode: 'development',
entry: {
app: './src/index.js',
print: './src/print.js'
},
devtool: 'inline-source-map',
devServer: {
contentBase: './dist'
},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'Development'
})
],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
Intially
keywords: entry file, web
- run the command below in project directoy
npm install webpack webpack-cli webpack-dev-server —save-dev
- configure
package.json
, add “dev”: “webpack-dev-server —open” within script field. - configure
webpack.config.js
```javascript module.exports = { entry: ‘./main.js’, output: { filename: ‘bundle.js’ } };
`main.js` is an **entry file**(which webpack follow to build `bundle.js`), write you own code
```javascript
document.write('<h1>Hello World</h1>');
index.html
<html>
<body>
<script type="text/javascript" src="bundle.js"></script>
</body>
</html>
- run command “npm run dev”
so the localhost page is supposed to be opened automatially
Pack
- run npx webpack
dist directoy generated and bundle.js blew.
- Now modify the index.html ```html
```
- open index.html in Chrome, the result is showing.
Question
what’s the relationship between vue and webpack