Webpack 3.10.0和babel-polyfill在运行时发出

时间:2018-01-31 08:52:21

标签: reactjs babel webpack-3 babel-polyfill

我目前正在使用Webpack 3.10.0以及以下babel软件包。

"babel": "^6.23.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "~6.13.2",
"babel-preset-react": "~6.11.1",

我的问题是,在运行时我在浏览器控制台上收到以下错误,页面无法加载。

bundle.js:737 Uncaught Error: Cannot find module "babel-polyfill"
    at webpackMissingModule (bundle.js:737)
    at Object.<anonymous> (bundle.js:737)
    at __webpack_require__ (bundle.js:679)
    at bundle.js:725
    at bundle.js:728 

我的webpack.config.js如下所示。

var webpack = require('webpack');
var path = require('path');


module.exports = {
    devtool: 'eval-source-map',
    entry: {
        bundle: ['babel-polyfill', './Main.js'],
        bundleIntegrated: ['babel-polyfill', './MainInt.js']
    },
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, '../assets/myproject')
    },
    target: 'web',
    devServer: {
        inline: true,
        port: 3000,
        proxy: {
            '/myct/**': {
                target: 'http://localhost:9000',
                secure: false,
                changeOrigin: true
            }
        }
    },
    plugins: [
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
    ],
    resolve: {
        extensions: ['.js', '.jsx'],
        alias: {
            config: path.join(__dirname, 'config/config.dev')
        }
    },
    module: {
        rules: [
                {
                    test: /.(js|jsx)$/,
                    enforce: 'pre',
                    exclude: /node_modules/,
                    use: [
                            {
                                loader: "eslint-loader",
                            }
                        ],
                },
                {
                    test: /\.(js|jsx)$/,
                    exclude: /(node_modules|__tests__)/,
                    loader: "babel-loader",
                },
                {
                    test: /\.css$/,
                    use: [
                            "style-loader",
                            "css-loader",
                        ],
                },
                {
                    test: /\.less$/,
                    use: [
                            "style-loader",
                            "css-loader",
                            "less-loader",
                        ],
                },
                {
                    test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/,
                    use: [
                            {
                                loader: "file-loader?name=../assets/mastering/fonts/[name].[ext]",
                            },
                        ],
                },
        ],
    }
}

我已按照https://babeljs.io/docs/usage/polyfill/中的说明将babel-polyfill导入Main.js和MainInt.js文件中,但我还没有使用new webpack.optimize.ModuleConcatenationPlugin()

有关如何解决此问题的任何想法。在此先感谢:)

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。谢谢大家的帮助。

在文件名中给出完整路径可以解决问题。

 output: {
        path: path.resolve(__dirname, '../assets/myproject'),
        filename: '../assets/myproject/[name].js',
    },