学习webpack4 - ES6语法转化
学习webpack4 - 基础配置
学习webpack4 - HTML处理
学习webpack4 - 样式处理
学习webpack4 - ES6语法转化
学习webpack4 - 第三方库的使用
学习webpack4 - 抽离公共代码
...持续中
=======================================================
ES6语法转化
注意:开始之前以下内容之前,需要配置一些webpack的基础配置,传送门:学习webpack4.x - 基础配置
当前目录结构为:
- index.js 文件内容:
require('./index.css'); require('./index.scss');
- webpack.config.js文件内容:
let path = require('path'); let HtmlWebpackPlugin = require('html-webpack-plugin'); let MiniCssExtractPlugin = require('mini-css-extract-plugin'); //抽离CSS let OptimizeCssPlugin = require('optimize-css-assets-webpack-plugin'); //优化项,比如压缩css等 let UglifyJsPlugin = require('uglifyjs-webpack-plugin'); //压缩js module.exports = { // mode: 'development', //优化项配置 optimization: { minimizer: [ new OptimizeCssPlugin(), new UglifyJsPlugin({ cache: true, //缓存 parallel: true, //并发打包 sourceMap: true //源码映射便于调试 }) ] }, //开一个本地服务 devServer: { port: 3000, //端口号 progress: true, //进度条 contentBase: './dist', //指定目录运行服务 open: true //自动打开浏览器 }, entry: './src/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist') }, // 模块配置 module: { rules: [{ test: /\.(css|scss)$/, use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader'] }] }, //插件配置 plugins: [ new HtmlWebpackPlugin({ template: './src/index.html', //原始文件 filename: 'index.html', //打包后的文件名称 hash: true, //hash }), new MiniCssExtractPlugin({ filename: 'main.css' //抽离出的css文件名称 }) ] }
- package.json文件内容:
{ "name": "webpack", "version": "1.0.0", "main": "index.js", "license": "MIT", "scripts": { "dev": "webpack --mode development && webpack-dev-server", "build": "webpack --mode production" }, "devDependencies": { "autoprefixer": "^9.4.7", "css-loader": "^2.1.0", "html-webpack-plugin": "^3.2.0", "less": "^3.9.0", "less-loader": "^4.1.0", "mini-css-extract-plugin": "^0.5.0", "node-sass": "^4.11.0", "optimize-css-assets-webpack-plugin": "^5.0.1", "postcss-loader": "^3.0.0", "sass-loader": "^7.1.0", "style-loader": "^0.23.1", "uglifyjs-webpack-plugin": "^2.1.1", "webpack": "^4.29.4", "webpack-cli": "^3.2.3", "webpack-dev-server": "^3.1.14", "webpack-html-plugin": "^0.1.1" } }
将ES6转化为ES5
step1: 打开src/index.js,输入:
const fn = () => { console.log('丸子'); } fn ();
step2: 配置webpack.config.js文件:
将ES6转成ES5,需要babel-loader,配置规则为:
module.exports = { //... module: { //... { test: /\.js$/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'] // 根据目标浏览器自动转换为相应es5代码 } } } } };
step3: 安装插件:
yarn add babel-loader @babel/core @babel/preset-env -D
尝试运行: npm run dev, 成功!如下图:
相关推荐
不知道该写啥QAQ 2020-11-12
webfullStack 2020-11-09
Yvettre 2020-09-15
想做大牛的蜗牛 2020-10-30
gloria0 2020-10-26
gaojie0 2020-09-11
SelinaChan 2020-08-14
不知道该写啥QAQ 2020-08-09
gloria0 2020-08-09
不知道该写啥QAQ 2020-08-02
hline 2020-07-29
SelinaChan 2020-07-28
wangdianyong 2020-07-23
webpackvuees 2020-07-23
yqoxygen 2020-07-20
不知道该写啥QAQ 2020-07-18
waterv 2020-07-18
81463166 2020-07-17