如果没有“ new”,则无法调用Polymer 3 + webpack + babel类构造函数PolymerElement

时间:2019-01-18 09:13:00

标签: javascript webpack polymer babel mixins

我正在尝试扩展聚合物的混合类。它工作正常,但是当我尝试使用babel转换我的代码时,我得到没有'new'不能调用Class构造函数PolymerElement。我致电super会收到错误消息。我确定问题与扩展带有已编译类代码的本机类有关,但是我找不到能够解决问题的资源/示例。我的猜测是我需要对babel进行不同的配置,但是正如我所说,发现示例并没有帮助。 您可以在此处访问要在其中重现错误的存储库:https://github.com/skukan159/DebuggingPolymerWebpack

这是我的webpack配置:

const webpack = require('webpack');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const WorkboxPlugin = require('workbox-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');


const webcomponentsjs = './node_modules/@webcomponents/webcomponentsjs';

const polyfills = [
    {
        from: path.resolve(`${webcomponentsjs}/webcomponents-*.{js,map}`),
        to: path.join(__dirname, 'wwwroot'),
        flatten: true
    },
    {
        from: './node_modules/web-animations-js/*{.js,.js.map}',
        to: './node_modules/web-animations-js/[name].[ext]'
    }
];
const assets = [
    {
        from: 'manifest.json',
        to: path.resolve(__dirname, 'wwwroot'),
        context: './src/'
    },
    {
        from: 'sw-service-worker.js',
        to: path.resolve(__dirname, 'wwwroot'),
        context: './src/'
    },
    {
        from: 'include/images/*',
        to: path.resolve(__dirname, 'wwwroot'),
        context: './src/'
    },
    {
        from: '*.html',
        to: path.resolve(__dirname, 'wwwroot'),
        context: './src/'
    }
];

module.exports = function (env) {
    return {
        entry: './src/index.js',
        output: {
            publicPath: '/',
            path: path.resolve(__dirname, 'wwwroot'), changed
            filename: "[name].[contenthash].js",  
            chunkFilename: '[name].[contenthash].js',
        },
        node: {
            dns: 'mock',
            net: 'mock'
        },
        optimization: {
            splitChunks: {
                cacheGroups: {
                    vendor: {
                       test: /[\\/]node_modules[\\/]/,
                       name: 'vendors',
                       chunks: 'all'
                    }
                }
            },
            runtimeChunk: 'single',
        },

        module: {
            rules: [
                {
                    test: /\.js$/,
                   exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env', { exclude: ["transform-classes"] }],
                            plugins: [
                                "@babel/plugin-proposal-class-properties",
                                "@babel/plugin-proposal-object-rest-spread",
                                "@babel/plugin-syntax-dynamic-import"
                            ]
                        }
                    }
                },

                {
                    "test": /\.html$/,
                    "use": [{
                        loader: 'html-loader',
                    }]
                },
                {
                    test: /\.json$/,
                    use: 'json-loader'
                },
                {
                    test: /\.(png|svg|jpg|gif)$/,
                    use: [
                        {
                            loader: 'file-loader',
                            options: {
                                name: '/img/[name].[ext]'
                            }
                        }
                    ]
                }
            ]
        },

        resolve: {
            modules: [
                "node_modules",
                path.resolve(__dirname, "src")
            ],
            extensions: [".js", ".json"],

            alias: {
                'markjs': path.resolve(__dirname, "./node_modules/mark.js/dist/mark.min.js"),
            }
        },

        plugins: [
            new webpack.HashedModuleIdsPlugin(),
            new HtmlWebpackPlugin({
                template: 'src/index.html'}),
            new webpack.ProvidePlugin({
                IntlMessageFormat: ['intl-messageformat/index.js', 'default'],
                'Mark': 'markjs'
            }),
            new CleanWebpackPlugin(['wwwroot']),
            new WorkboxPlugin.GenerateSW({
                swDest: 'sw.js',
                clientsClaim: true,
                skipWaiting: true
            }),
            new CopyWebpackPlugin([...polyfills, ...assets,
                {
                    from: 'include/components/oidc-client/oidc-client.min.js',
                    to: path.resolve(__dirname, 'wwwroot'),
                    context: './src/'
                },
            ]),
        ],

        watch: true,
        watchOptions: {
            aggregateTimeout: 1000,
            poll: true,
            poll: 500,
        },
    }
};

这是我的.babelrc文件

    {
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "browsers": [ "last 2 versions", "ie >= 11" ]
        },
        "exclude": [ "transform-classes" ]
      }
    ]
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-syntax-dynamic-import"
  ]
}

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。我需要从webpack配置文件中注释掉babel loader的选项。只有这样,我的.babelrc配置文件才会被应用。