代码之家  ›  专栏  ›  技术社区  ›  stevendesu

巴别尔没有转化常数

  •  0
  • stevendesu  · 技术社区  · 7 年前

    经过一个多小时的探索和调试,我相信我已经找到了 我的问题,但仍然需要一个解决方案。

    我的项目有如下目录结构:

    /
    |- dist/
    |- src/
    |- plugins/
    |- node_modules/
    |- webpack.config.js
    |- .babelrc
    |- ...
    

    这个 plugins 目录包含引用我编写的VideoJS插件的git子模块。这些插件都是各自独立的项目(有自己的 package.json ). 当导入和使用这些插件时,我在 src/index.js :

    import videojs from "video.js";
    import "./plugins/AutoplayFixPlugin";
    
    videojs("my-player", { plugins: { autoplayFixPlugin: {} } });
    

    Node module resolution algorithm ,这将:

    1. ./
    2. 认识到了吗 AutoplayFixPlugin
    3. 找不到一个 index.js , index.json ,或 index.node
    4. 成功地找到一个 文件
    5. 解析 它的文件 main 字段,并加载该文件

    这样,进口 ./plugins/AutoplayFixPlugin 正确装载 ./plugins/AutoplayFixPlugin/src/plugin.js

    问题是:当我以这种方式导入模块时, 我的装载机运行。它只是导入原始的源代码和巴贝尔从来没有在我的插件上执行。

    我试过替换 /插件/AutoplayFixPlugin ./plugins/AutoplayFixPlugin/src/plugin.js 但这也没用。我认为 包.json 在子目录中,不知何故会出现Webpack(或Babel)。

    更新2

    我已经确认 包.json 使Babel在插件文件上按预期运行。再加回去,巴贝尔就不工作了。我试着添加一个 .babelrc @babel/core 和 @babel/preset-env 本地的插件,但这也不起作用。

    由于某些原因,在任何具有 文件。

    原岗位

    webpack.config.js :

    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    const HtmlWebpackPlugin = require("html-webpack-plugin");
    const TerserWebpackPlugin = require("terser-webpack-plugin");
    
    module.exports = (env, argv) => ({
        devtool: argv.mode === "development" ? "inline-source-map" : "source-map",
        devServer: {
            contentBase: "./dist",
            host: "0.0.0.0"
        },
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: [
                        {
                            loader: "babel-loader",
                            options: {
                                cacheDirectory: true,
                                cacheCompression: false
                            }
                        }
                    ]
                },
                {
                    test: /\.css$/,
                    use: [
                        MiniCssExtractPlugin.loader,
                        "css-loader"
                    ]
                },
                {
                    test: /\.s(a|c)ss$/,
                    use: [
                        MiniCssExtractPlugin.loader,
                        "css-loader",
                        "sass-loader"
                    ]
                },
                {
                    test: /\.(jpg|png|gif|jpeg|svg)$/,
                    use: [
                        {
                            loader: "url-loader",
                            options: {
                                limit: 5000
                            }
                        },
                        "img-loader"
                    ]
                }
            ]
        },
        optimization: {
            minimizer: [
                new TerserWebpackPlugin({
                    sourceMap: true,
                    cache: true,
                    parallel: true,
                    terserOptions: {
                        compress: {
                            drop_console: true
                        }
                    }
                })
            ]
        },
        performance: {
            maxAssetSize: 9e9,
            maxEntrypointSize: 9e9
        },
        plugins: [
            new MiniCssExtractPlugin({
                filename: "[name].css",
                chunkFilename: "[id].css"
            }),
            new HtmlWebpackPlugin({
                template: "./index.html"
            })
        ]
    });
    

    :

    {
        "presets": [
            [
                "@babel/preset-env",
                {
                    "debug": true,
                    "include": [
                        "transform-block-scoping"
                    ]
                }
            ]
        ]
    }
    

    .browserslistrc :

    # Browsers that we support
    last 2 versions, not dead, > 1% in US
    # Microsoft no longer provides security updates for IE <= 10
    ie >= 11
    # Apple no longer provides updates for the iPad Mini
    ios >= 9
    

    npx browserslist 我得到以下输出:

    and_chr 71
    and_ff 64
    and_qq 1.2
    and_uc 11.8
    android 67
    android 4.4.3-4.4.4
    baidu 7.12
    chrome 72
    chrome 71
    edge 18
    edge 17
    firefox 65
    firefox 64
    ie 11
    ie_mob 11
    ios_saf 12.0-12.1
    ios_saf 11.3-11.4
    ios_saf 11.0-11.2
    ios_saf 10.3
    ios_saf 10.0-10.2
    ios_saf 9.3
    ios_saf 9.0-9.2
    ios_saf 8
    op_mini all
    op_mob 46
    opera 58
    opera 57
    safari 12
    safari 11.1
    samsung 8.2
    samsung 7.2-7.4
    

    我特别感兴趣的是 ios_saf 9.0-9.2 线(和 ios_saf 8 以及)。我桌上有一台运行iOS 9.3.5的iPad Mini,Safari抛出了一个错误:

    SyntaxError: Unexpected keyword 'const'. Const declarations are not supported in strict mode.
    

    const 关键字未转换为 var . 做些调查看起来像 there may be two steps to this transformation const -> let 通过 es6.constants ,那么 let -> var es6.blockScoping

    看着 node_modules/@babel/preset-env/data/plugin-features.js transform-block-scoping ,但我看不到这样的线 transform-constants 或者类似的。看着 Babel's official list of plugins constants 插件。只是 block-scoping let 到 变量

    我想也许 变换常数 被卷进 变换块范围 ( an idea that seems to be confirmed by their docs ),所以我想看看 变换块范围 通过启用 debug

    @babel/preset-env: `DEBUG` option
    
    Using targets:
    {
      "android": "4.4.3",
      "chrome": "71",
      "edge": "17",
      "firefox": "64",
      "ie": "11",
      "ios": "8",
      "opera": "57",
      "safari": "11.1"
    }
    
    Using modules transform: auto
    
    Using plugins:
      transform-template-literals { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-literals { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-function-name { "android":"4.4.3", "edge":"17", "ie":"11", "ios":"8" }
      transform-arrow-functions { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-block-scoped-functions { "android":"4.4.3", "ios":"8" }
      transform-classes { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-object-super { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-shorthand-properties { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-duplicate-keys { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-computed-properties { "android":"4.4.3", "ie":"11" }
      transform-for-of { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-sticky-regex { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-dotall-regex { "android":"4.4.3", "edge":"17", "firefox":"64", "ie":"11", "ios":"8" }
      transform-unicode-regex { "android":"4.4.3", "ie":"11", "ios":"8", "safari":"11.1" }
      transform-spread { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-parameters { "android":"4.4.3", "edge":"17", "ie":"11", "ios":"8" }
      transform-destructuring { "android":"4.4.3", "edge":"17", "ie":"11", "ios":"8" }
      ---> transform-block-scoping { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-typeof-symbol { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-new-target { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-regenerator { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-exponentiation-operator { "android":"4.4.3", "ie":"11", "ios":"8" }
      transform-async-to-generator { "android":"4.4.3", "ie":"11", "ios":"8" }
      proposal-async-generator-functions { "android":"4.4.3", "edge":"17", "ie":"11", "ios":"8", "safari":"11.1" }
      proposal-object-rest-spread { "android":"4.4.3", "edge":"17", "ie":"11", "ios":"8" }
      proposal-unicode-property-regex { "android":"4.4.3", "edge":"17", "firefox":"64", "ie":"11", "ios":"8" }
      proposal-json-strings { "android":"4.4.3", "edge":"17", "ie":"11", "ios":"8", "safari":"11.1" }
      proposal-optional-catch-binding { "android":"4.4.3", "edge":"17", "ie":"11", "ios":"8" }
      transform-named-capturing-groups-regex { "android":"4.4.3", "edge":"17", "firefox":"64", "ie":"11", "ios":"8" }
    
    Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
    

    至少从调试输出来看 块范围 正在运行。我还更新了我的 .浏览器列表 以下内容:

    last 999 versions
    

    即使在这之后 仍然 没有改变 常数 变量 .

    node_modules 以两种不同的方式创建文件夹。首先,查看源代码,我发现这个bug发生在 main.js (约72500行):

    /***/ "./plugins/AutoplayFixPlugin/src/AutoplayFixPlugin.js":
    /*!************************************************************!*\
      !*** ./plugins/AutoplayFixPlugin/src/AutoplayFixPlugin.js ***!
      \************************************************************/
    /*! exports provided: default */
    /***/ (function(module, __webpack_exports__, __webpack_require__) {
    
    "use strict";
    __webpack_require__.r(__webpack_exports__);
    /* harmony import */ var video_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! video.js */ "./node_modules/video.js/dist/video.es.js");
    /* harmony import */ var _utilities_Logger__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../../utilities/Logger */ "./utilities/Logger.js");
    /* harmony import */ var _utilities_Logger__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_utilities_Logger__WEBPACK_IMPORTED_MODULE_1__);
    /* harmony import */ var _package_json__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../package.json */ "./plugins/AutoplayFixPlugin/package.json");
    var _package_json__WEBPACK_IMPORTED_MODULE_2___namespace = /*#__PURE__*/__webpack_require__.t(/*! ../package.json */ "./plugins/AutoplayFixPlugin/package.json", 1);
    /* harmony import */ var _AutoplayFixPlugin_scss__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./AutoplayFixPlugin.scss */ "./plugins/AutoplayFixPlugin/src/AutoplayFixPlugin.scss");
    /* harmony import */ var _AutoplayFixPlugin_scss__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_AutoplayFixPlugin_scss__WEBPACK_IMPORTED_MODULE_3__);
    /* harmony import */ var _components_AutoplayMuteOverlay__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./components/AutoplayMuteOverlay */ "./plugins/AutoplayFixPlugin/src/components/AutoplayMuteOverlay.js");
    
    const Plugin = video_js__WEBPACK_IMPORTED_MODULE_0__["default"].getPlugin("plugin"); // Default options for the plugin.
    
    const defaults = {
      muted: false,
      inline: false
    };
    

    这是我自己的密码。不过,只是为了 100%确定 ,我删除了 exclude 我的电话线 webpack.config.js文件 . 这将我的编译时间从~2秒增加到~30秒,无论出于何种原因,都将错误的行号从~72500更改为~43600,否则没有任何效果。我的代码在iPad Mini上仍然失败,原因是:

    SyntaxError:意外的关键字“const”。严格模式下不支持常量声明。
    

    常数 到 变量 清晰地 不支持吗?即使目标扩大到 last 999 versions 它仍然没有执行这种转换。

    0 回复  |  直到 7 年前