从头整理webpack搭建流程html
entry
:入口配置output
:输出配置module
:文件解析模块配置plugin
:插件配置build/
:存放webpack构建配置文件src/
:项目开发目录webpack
public/
:公共静态文件script/
:脚本文件style/
:样式文件view/
:页面文件开发依赖web
babel-core
babel-loader
webpack
:这里使用webpack3
版本在build/
文件夹下面建立webpack.dev.js
,代码以下:npm
const path = require('path') module.exports = { entry: path.join(__dirname, '../src/script/index.js'), output: { path: path.join(__dirname, '../dist'), filename: 'js/[name].js' }, module: { loaders: [{ test: /\.js$/, use: 'babel-loader' }] } }
src/script/
文件夹下面建立index.js
,任意写几行代码以便测试src/view/
文件夹下面建立index.html
,引入上面的index.js
文件npm init -y
或者yarn init -y
建立package.json
文件,安装开发依赖在package.json
文件中添加scripts
属性json
"scripts": { "dev": "rm -rf dist & webpack --config build/webpack.dev.js" }
打开终端执行npm run dev
命令babel
引入html-webpack-plugin
插件测试
const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') module.export = { entry: path.join(__dirname, '../src/script/index.js'), output: { path: path.join(__dirname, '../dist'), filename: 'js/[name].js' }, module: { loaders: [{ test: /\.js$/, use: 'babel-loader' }] }, plugins: [ new HtmlWebpackPlugin({ // 打包生成html文件名,该文件被放置在输出目录中 filename: 'index.html', // 模板文件,以该文件生成打包后的html文件 template: path.join(__dirname, '../src/view/index.html') }) ] }