首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在webpack/babel建筑中使用CustomElement v1填充?

如何在webpack/babel建筑中使用CustomElement v1填充?
EN

Stack Overflow用户
提问于 2017-04-05 17:12:50
回答 1查看 1.5K关注 0票数 4

虽然webpack,但我很难让此WebComponents填充+本机-shim在所有设备上正常工作。

关于我的设置的一些背景:* Webpack2 + babel-6 * app是用ES6编写的,转到ES5 *导入一个用ES6编写的node_module包,该包定义/注册应用程序中使用的CustomElement

相应的webpack dev配置如下所示:

代码语言:javascript
复制
const config = webpackMerge(baseConfig, {
  entry: [
    'webpack/hot/only-dev-server',
    '@webcomponents/custom-elements/src/native-shim',
    '@webcomponents/custom-elements',
    '<module that uses CustomElements>/dist/src/main',
    './src/client',
  ],
  output: {
    path: path.resolve(__dirname, './../dist/assets/'),
    filename: 'app.js',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        options: {
          cacheDirectory: true,
        },
        include: [
          path.join(NODE_MODULES_DIR, '<module that uses CustomElements>'),
          path.join(__dirname, '../src'),
        ],
      },
    ],
  },
...

键取:*我需要在CustomElement之前加载<module that uses CustomElements> *我需要在应用程序soure * <module that uses CustomElements>是ES6之前加载<module that uses CustomElements>,所以我们正在传送它(因此在babel-loader中包含)。

上述功能在现代ES6浏览器( IE桌面Chrome )中如预期的那样工作,但是

它在旧的浏览器中不起作用。在较旧的浏览器(例如iOS 8)中,我得到以下错误:

代码语言:javascript
复制
SyntaxError: Unexpected token ')'

指向本机shim中的开放匿名函数:

代码语言:javascript
复制
(() => {
  'use strict';

  // Do nothing if `customElements` does not exist.
  if (!window.customElements) return;

  const NativeHTMLElement = window.HTMLElement;
  const nativeDefine = window.customElements.define;
  const nativeGet = window.customElements.get;

因此,在我看来,native-shim似乎需要转移到ES5上:

代码语言:javascript
复制
        include: [
+         path.join(NODE_MODULES_DIR, '@webcomponents/custom-elements/src/native-shim'),
          path.join(NODE_MODULES_DIR, '<module that uses CustomElements>'),
          path.join(__dirname, '../src'),
        ],

...but这样做现在破坏了Chrome和iOS 8,并出现了以下错误:

代码语言:javascript
复制
app.js:1 Uncaught TypeError: Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
    at new StandInElement (native-shim.js:122)
    at HTMLDocument.createElement (<anonymous>:1:1545)
    at ReactDOMComponent.mountComponent (ReactDOMComponent.js:504)
    at Object.mountComponent (ReactReconciler.js:46)
    at ReactCompositeComponentWrapper.performInitialMount (ReactCompositeComponent.js:371)
    at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:258)
    at Object.mountComponent (ReactReconciler.js:46)
    at Object.updateChildren (ReactChildReconciler.js:121)
    at ReactDOMComponent._reconcilerUpdateChildren (ReactMultiChild.js:208)
    at ReactDOMComponent._updateChildren (ReactMultiChild.js:312)

。。这就引出了本机shim中的constructor()行:

代码语言:javascript
复制
  window.customElements.define = (tagname, elementClass) => {
    const elementProto = elementClass.prototype;
    const StandInElement = class extends NativeHTMLElement {
      constructor() {

呼。因此,我非常不清楚我们是如何将其包含在基于webpack的构建中的,在该构建中,使用CustomElements的依赖项是ES6 (并且需要转换)。

  • 将本机-shim转换为es5不起作用。
  • 在包入口点顶部使用本机shim is不适用于iOS 8,但对Chrome有效。
  • 不包括原生shim破坏了Chrome和iOS。

在这一点上,我对web组件感到非常沮丧。我只想使用这个依赖关系,它恰好是用web组件构建的。我怎样才能让它在webpack的建筑中正常工作,并在所有的设备上工作呢?我是不是漏掉了什么明显的东西?

为了后代而我的.babelrc配置(最相关的.babelrc配置):

代码语言:javascript
复制
{
  "presets": [
    ["es2015", { "modules": false }],
    "react"
  ],
  "plugins": [
    "transform-custom-element-classes",
    "transform-object-rest-spread",
    "transform-object-assign",
    "transform-exponentiation-operator"
  ],
  "env": {
    "test": {
      "plugins": [
        [ "babel-plugin-webpack-alias", { "config": "./cfg/test.js" } ]
      ]
    },
    "dev": {
      "plugins": [
        "react-hot-loader/babel",
        [ "babel-plugin-webpack-alias", { "config": "./cfg/dev.js" } ]
      ]
    },
    "dist": {
      "plugins": [
        [ "babel-plugin-webpack-alias", { "config": "./cfg/dist.js" } ],
        "transform-react-constant-elements",
        "transform-react-remove-prop-types",
        "minify-dead-code-elimination",
        "minify-constant-folding"
      ]
    },
    "production": {
      "plugins": [
        [ "babel-plugin-webpack-alias", { "config": "./cfg/server.js" } ],
        "transform-react-constant-elements",
        "transform-react-remove-prop-types",
        "minify-dead-code-elimination",
        "minify-constant-folding"
      ]
    }
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-23 16:13:48

我能够在下面的.babelrc插件管道中实现类似的功能。看起来唯一的区别是https://babeljs.io/docs/plugins/transform-es2015-classes/https://babeljs.io/docs/plugins/transform-es2015-classes/,但我真的不记得他们具体解决了哪些问题:

代码语言:javascript
复制
{
  "plugins": [
    "transform-runtime",
    ["babel-plugin-transform-builtin-extend", {
      "globals": ["Error", "Array"]
    }],
    "syntax-async-functions",
    "transform-async-to-generator",
    "transform-custom-element-classes",
    "transform-es2015-classes"
  ]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43237591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档