zoukankan      html  css  js  c++  java
  • 关于babel和webpack结合使用的实践

    1.webapack和babel的高版本文件配置

    1.项目根目录下package.json文件内容如下:

    {
      "name": "dive-into-webpack",
      "version": "1.0.0",
      "scripts": {
        "build": "webpack --w"
      },
      "dependencies": {
        "@babel/polyfill": "7.8.7",
        "@babel/runtime": "7.8.7",
        "core-js": "3.6.4"
      },
      "devDependencies": {
        "@babel/plugin-proposal-class-properties": "7.8.3",
        "@babel/plugin-syntax-dynamic-import": "7.2.0",
        "@babel/cli": "7.8.4",
        "@babel/core": "7.8.7",
        "@babel/preset-env": "7.8.7",
        "@babel/runtime": "7.8.7",
        "@babel/types": "7.8.7",
        "babel-loader": "8.0.6",
        "webpack": "4.39.0",
        "webpack-bundle-analyzer": "3.6.0",
        "webpack-cli": "3.3.6"
      }
    }
    
    

    2.项目根目录下.babelrc文件内容如下:

    {
      "plugins": [
      ],
      "presets": [
        [
          // "env"
          "@babel/preset-env",
          {
            "targets": {
              "browsers": [
                "chrome >= 70",
                "edge >= 15",
                "firefox >= 60",
                "ie >= 11"
              ]
            },
            "useBuiltIns": "usage",
            "corejs": 3,
            "comments": true,
     		"sourceMap": "inline"
          }
        ],    
      ],
    }
    

    3.项目根目录下webpack.config.js文件 内容如下:

    const path = require('path');
    
    module.exports = {
      // JS 执行入口文件
      entry: './main.js',
      output: {
        // 把所有依赖的模块合并输出到一个 bundle.js 文件
        filename: 'bundle.js',
        // 输出文件都放到 dist 目录下
        path: path.resolve(__dirname, './dist'),
      },
      module: {
        rules: [
          {
            test: /.js$/,
            // use: ['babel-loader'],
            use: {
              loader: 'babel-loader',
              query: {
                cacheDirectory: true,
                presets: [
                  [
                    '@babel/preset-env'
                  ]
                ],
              },
            },
          },
        ]
      },
      devtool: 'source-map' // 输出 source-map 方便直接调试 ES6 源码
    };
    
    

    4.项目根目录下index.html文件 内容如下

    <html>
    <head>
      <meta charset="UTF-8">
    </head>
    <body>
    <div id="app"></div>
    <!--导入 webpack 输出的 JS 文件-->
    <script src="./dist/bundle.js"></script>
    </body>
    </html>
    
    

    5.项目根目录下main.js文件内容如下:

    //引入babel polyfill
    import "@babel/polyfill"
    // 通过 ES6 模块化规范导入 show 函数
    import { show } from './show';
    // 执行 show 函数
    show('Webpack');
    
    const funA=x=>x+1;
    
    const Gen = (time) => {
        return new Promise((resolve, reject) => {
            setTimeout(function () {
                if (time < 500) {
                    reject(time);
                } else {
                    resolve(time);
                }
            }, time);
        });
    };
    
    Gen(Math.random() * 1000)
        .then((val) => console.log("resolve", val))
        .catch((err) => console.log("err"))
        .finally(() => console.log("finish"));
    console.log([1,2,3].includes(1));
    
    
    

    6.项目根目录下show.js文件内容如下:

    // 操作 DOM 元素,把 content 显示到网页上
    export function show(content) {
      window.document.getElementById('app').innerText = `Hello,${content}`;
    }
    
    

    2.webapack和babel的低版本配置

    低版本的配置,可以更明显的看到babel转换后的代码,以及webpack压缩之后的文件。方便新手理解

    1.package.json文件内容如下:

    {
      "name": "dive-into-webpack",
      "version": "1.0.0",
      "scripts": {
        "build": "webpack --w"
      },
      "dependencies": {
        "@babel/polyfill": "7.8.7",
        "@babel/runtime": "7.8.7",
      },
      "devDependencies": {
        "babel-core": "^6.26.0",
        "babel-loader": "^7.1.2",
        "babel-preset-env": "^1.6.0",
        "webpack": "^3.4.0"
      }
    }
    
    

    2.项目根目录下.babelrc文件内容如下:

    {
      "presets": [
        "env"
      ]
    }
    
    

    3.项目根目录下webpack.config.js文件 内容如下:

    const path = require('path');
    
    module.exports = {
      // JS 执行入口文件
      entry: './main.js',
      output: {
        // 把所有依赖的模块合并输出到一个 bundle.js 文件
        filename: 'bundle.js',
        // 输出文件都放到 dist 目录下
        path: path.resolve(__dirname, './dist'),
      },
      module: {
        rules: [
          {
            test: /.js$/,
            use: ['babel-loader'],
          },
        ]
      },
      devtool: 'source-map' // 输出 source-map 方便直接调试 ES6 源码
    };
    
    
  • 相关阅读:
    深入解析QML引擎,第1部分:QML文件加载
    解释器原理
    NLP入门(十)使用LSTM进行文本情感分析
    Python之将Python字符串生成PDF
    SPARQL入门(二)使用Java操作ARQ
    SPARQL入门(一)SPARQL简介与简单使用
    NLP入门(九)词义消岐(WSD)的简介与实现
    利用百度文字识别API识别图像中的文字
    NLP入门(八)使用CRF++实现命名实体识别(NER)
    Cayley图数据库的可视化(Visualize)
  • 原文地址:https://www.cnblogs.com/zdjBlog/p/14388167.html
Copyright © 2011-2022 走看看