gpt4 book ai didi

javascript - 在 nuxt 中使用 babel-polyfill(而不是 polyfill.io)

转载 作者:行者123 更新时间:2023-11-29 20:46:34 30 4
gpt4 key购买 nike

我正在使用 nuxt vuetify 模板构建一个静态站点,虽然它很棒并且提供了很多已经为我完成的配置和设置,但我发现很难让我的站点在 Internet Explorer (11) 上顺利运行。

由于ES6代码需要转译,所以我尝试在我的应用中配置babel,这是我第一次配置babel(所以,请忽略我对领域知识的缺乏)。网上有很多issues/questions,我几乎都试过了,但都是徒劳。

TL;DR,我正在使用 Nuxt 2.x(和内部的 Webpack 4),我不知道在哪里以及如何包含 @babel/polyfill 因为,

  1. 我们在 Nuxt 应用程序中没有定义或明确的入口点,我可以在其中导入该文件 import '@babel.polyfill',如许多地方所述,包括babel官方文档

  2. webpack 4 也没有 entry.vendor 字段,我可以在其中注入(inject) polyfill。

这是我的nuxt.config.js:

babel: {
presets: [
['@babel/preset-env', {
'useBuiltIns': 'usage',
'targets': '> 0.25%, not dead'
}]
],
plugins: ['@babel/plugin-syntax-dynamic-import', [
'@babel/plugin-transform-runtime',
{
'corejs': false,
'helpers': true,
'regenerator': true,
'useESModules': false
}
]],
'configFile': false,
'babelrc': false
}

这里是我使用的包的版本:

"dependencies": {
"@babel/polyfill": "^7.2.5"
}

"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.2.3",
"@babel/runtime-corejs2": "^7.3.0",
"babel-core": "^6.26.3",
"babel-eslint": "^10.0.1",
"babel-loader": "^8.0.5",
"babel-plugin-dynamic-import-node": "^2.2.0"
}

正在关注 this thread ,我还想将 babel polyfill 作为 vendor 推送到 webpack 条目对象,但它给了我错误:Cannot set property 'vendor' of undefined 因为,显然,webpack 4 configs 没有输入字段,就像 webpack 3 曾经有的一样。

这是我的webapack 配置:

{ 
name: 'client',
mode: 'production',
devtool: false,
optimization:
{ runtimeChunk: 'single',
minimize: true,
minimizer: undefined,
splitChunks:
{ chunks: 'all',
automaticNameDelimiter: '.',
name: undefined,
cacheGroups: [Object] } },
output:
{ path: '/home/waleed/project/.nuxt/dist/client',
filename: '[chunkhash].js',
chunkFilename: '[chunkhash].js',
publicPath: '/_nuxt/' },
performance: { maxEntrypointSize: 1024000, hints: 'warning' },
resolve:
{ extensions: [ '.wasm', '.mjs', '.js', '.json', '.vue', '.jsx' ],
alias:
{ '~': '/home/waleed/project',
'~~': '/home/waleed/project',
'@': '/home/waleed/project',
'@@': '/home/waleed/project',
assets: '/home/waleed/project/assets',
static: '/home/waleed/project/static' },
modules:
[ 'node_modules',
'/home/waleed/projectnode_modules',
'/home/waleed/project/node_modules/@nuxt/config/node_modules' ] },
resolveLoader:
{ modules:
[ 'node_modules',
'/home/waleed/project/node_modules',
'/home/waleed/project/node_modules/@nuxt/config/node_modules' ] },
module:
{ rules:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ] },
plugins:
[ VueLoaderPlugin {},
WarnFixPlugin {},
WebpackBarPlugin {
profile: false,
handler: [Function],
modulesCount: 500,
showEntries: false,
showModules: true,
showActiveModules: true,
options: [Object],
reporters: [Array] },
MiniCssExtractPlugin { options: [Object] },
HtmlWebpackPlugin { options: [Object] },
HtmlWebpackPlugin { options: [Object] },
VueSSRClientPlugin { options: [Object] },
DefinePlugin { definitions: [Object] } ]
}

P.S:我已经看到并尝试过this question ,它肯定也有一些作用,但我的用例要求不要使用 polyfill.io。

最佳答案

对于所有那些像我一样敲头的人,(作为临时解决方案)我决定使用 babel-polyfill CDN并将其 url 包含在 nuxt.config.js 文件的脚本部分中,如下所示

script: [
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.2.5/polyfill.min.js' }
]

但是,我仍在寻找更简洁、更符合 ES6 风格的 importrequire 语法来实现所需的结果。因此,如果您有这样的解决方案,请随时添加更多答案。

关于javascript - 在 nuxt 中使用 babel-polyfill(而不是 polyfill.io),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54307255/

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