2

I have a embeddable widget written using react and bundle to a single js file using webpack 4.29.3 and babel 7. Everything works fine in webpack-dev-server and production by just inserting <script type="text/javascript" src="myWidget.js"></script> tag in html. My webpack config is as follows: (I have included babel-polyfill)

const HtmlWebPackPlugin = require("html-webpack-plugin");

module.exports = env => {
let htmlTemplate = "./public/index.html";
return {
    entry: [ "@babel/polyfill","./src/index.js"],
    output: {
        path: __dirname + '/dist',
        filename: 'chat.js',
        library: 'Chat',
        libraryTarget: 'umd',
        umdNamedDefine: true
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.css$/,
                use: ["style-loader", "css-loader"]
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: htmlTemplate,
            filename: "./index.html",
            inject: 'head'
        })
    ]
}};

my babel config is as follows:

  {
  "presets": [
    "@babel/env",
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}

But when I try to embed the widget to a site written in angular 4 it says TypeError: t.finally is not a function though I have include the polyfill in the react webpack build. I found the reason to be that zone.js in angular overrides the babel polyfill. When I updated the host site (angular site) zone.js version from 0.8.14 to 0.8.26 embeddable widget works fine. My question is in actual scenario I have no control on the host site where widget is embed. So in such scenario how can I overcome that?

Hansika Weerasena
  • 3,046
  • 1
  • 13
  • 22
  • 1
    Try loading polyfill dynamically when your widget is created. https://cdnjs.com/libraries/babel-polyfill – UjinT34 Mar 15 '19 at 18:08

0 Answers0