gpt4 book ai didi

javascript - 如何为 IE11 正确使用 babel/corejs3/webpack?

转载 作者:行者123 更新时间:2023-12-01 15:16:01 29 4
gpt4 key购买 nike

使用我当前的配置(见下文),我收到此错误:

   [object Error]{description: "Argument ob...", message: "Argument ob...", name: "TypeError", number: -2147418113, stack: "TypeError: ...", Symbol()_7.bs7gi3oa3wi: undefined}

我尝试根据 Symbol()_ ... : undefined} 进行挖掘但我找不到任何明确的迹象。

这是我的 .babel.config.js :
module.exports = function (api) {
api.cache(true);
const presets = [
[
'@babel/preset-env',
{
// modules: false,
corejs:"3.6.4",
useBuiltIns: 'usage',
targets: {
browsers: [
"edge >= 16",
"safari >= 9",
"firefox >= 57",
"ie >= 11",
"ios >= 9",
"chrome >= 49"
]
}
}
]
];
const plugins= [
["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }],
["@babel/plugin-proposal-class-properties", { "loose": true }]
];
return {
presets,
plugins
}
}

这是我的 webpackconfig.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
// exclude: /node_modules/,
exclude : [
/\bcore-js\b/,
/\bwebpack\/buildin\b/
],
use: {
loader: 'babel-loader',
options:{
sourceType: "unambiguous"
}
},
},
],
},
devtool:"cheap-source-map",
resolve: {
extensions: ['*', '.js'],
},
output: {
path: path.resolve(__dirname, './dist'),
filename: 'shim.js',
}
};

我也尝试了很多替代方案,这是我目前使用的, entry:"usage"不排除 node_modules .

这是来自我的 package.json :

 "devDependencies": {
"@babel/core": "^7.9.0",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-decorators": "^7.8.3",
"@babel/preset-env": "^7.9.5",
"babel-loader": "^8.1.0",
"eslint": "^6.8.0",
"eslint-config-google": "^0.14.0",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3",
"dotenv-webpack": "^1.7.0"
},
"dependencies": {
"core-js": "^3.6.4",
"ismobilejs": "^1.0.3",
"localforage": "1.7.3",
"postmate": "^1.5.2",
"uuid": "^7.0.3"
}

错误似乎来自第一次调用 Postmate图书馆即 new Postmate({...}) (我之前有一个 console.log)。在此电话之前,我有一个到 localforage并且 promise 成功完成。

最佳答案

使用 useBuiltIns: "usage"
您通常必须在代码条目文件中导入要使用的模块(例如 Postmate);没有填充物; @babel/preset-env 将相应地处理使用的每个 polyfill .此外,corejs 的版本在 @babel/preset-env必须是单个数字(即 32 )。babel.config.js的内容:

module.exports = function (api) {
api.cache(true);
const presets = [
[
'@babel/preset-env',
{
corejs : {
version : "3",
proposals : true
},
useBuiltIns: 'usage',
targets: {
browsers: [
"edge >= 16",
"safari >= 9",
"firefox >= 57",
"ie >= 11",
"ios >= 9",
"chrome >= 49"
]
}
}
]
];
const plugins= [
["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }],
["@babel/plugin-proposal-class-properties", { "loose": true }]
];
return {
presets,
plugins
}
}
webpackconfig.js的内容:
const path = require('path');

module.exports = {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: [
/\bcore-js\b/,
/\bwebpack\/buildin\b/
],
use: {
loader: 'babel-loader',
options: {
babelrc: false,
configFile: path.resolve(__dirname, 'babel.config.js'),
compact: false,
cacheDirectory: true,
sourceMaps: false,
},
},
},
],
},
devtool: "cheap-source-map",
output: {
path: path.resolve(__dirname, './dist'),
filename: 'shim.js',
}
}
入口JS文件内容 src/index.js :
import Postmate from 'postmate';

// Postmate and rest of the code
...
它将生成:
dist/shim.js      177K
dist/shim.js.map 140K
您可以使用 useBuiltIns: "usage" 测试在线分布式示例。在 IE 11 这里: https://zikro.gr/dbg/so/61044894/usage/ . (子 iFrame 有一个按钮,可以将父窗口背景颜色更改为随机颜色)
您可以找到包含整个项目的存储库和 用法示例分支在这个 Github 存储库/分支中: https://github.com/clytras/ie11-postmate/tree/usage
使用 useBuiltIns: "entry"
根据this issue disqussion "using Symbol causes exception in IE11" , 你必须:
  • 排除 @babel-runtimecore-js.js 的规则中文件。
  • corejs: "3"useBuiltIns: 'entry'@babel/preset-env预设在 babel.config.js文件。
  • 必须有core-js/stablepostmate在您的入口源 JS 文件中导入。
  • babel.config.js的内容:
    module.exports = function (api) {
    api.cache(true);
    const presets = [
    [
    '@babel/preset-env',
    {
    corejs:"3",
    useBuiltIns: 'entry',
    targets: {
    browsers: [
    "edge >= 16",
    "safari >= 9",
    "firefox >= 57",
    "ie >= 11",
    "ios >= 9",
    "chrome >= 49"
    ]
    }
    }
    ]
    ];
    const plugins= [
    ["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true }]
    ];
    return {
    presets,
    plugins
    }
    }
    webpackconfig.js的内容:
    const path = require('path');

    module.exports = {
    module: {
    rules: [
    {
    test: /\.(js|jsx)$/,
    exclude: /@babel(?:\/|\\{1,2})runtime|core-js/,
    use: {
    loader: 'babel-loader',
    options: {
    babelrc: false,
    configFile: path.resolve(__dirname, 'babel.config.js'),
    compact: false,
    cacheDirectory: true,
    sourceMaps: false,
    },
    },
    },
    ],
    },
    devtool:"cheap-source-map",
    output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'shim.js',
    }
    }
    入口JS文件内容 src/index.js :
    import 'core-js/stable';
    window.Postmate = require('postmate/build/postmate.min.js');

    // Postmate and rest of the code
    ...
    它将生成:
    dist/shim.js      641K
    dist/shim.js.map 459K
    您可以在 中测试IE 11 这里: https://zikro.gr/dbg/so/61044894/ .

    关于javascript - 如何为 IE11 正确使用 babel/corejs3/webpack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61044894/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com