简体   繁体   English

捆绑时,力矩未定义

[英]Moment is undefined when bundled

I'm trying to bundle moment.js but it keeps returning undefined after it's bundled: 我正在尝试捆绑moment.js,但是在捆绑后它始终返回未定义的状态:

My webpack 我的webpack

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

module.exports = {
    mode: "development",
    entry: {
        frb: "./src/app/loader.js"
    },
    output: {
        path: path.resolve(__dirname, "./src/lib"),
        filename: "bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /(node_modules)/,
                loader: "babel-loader",
                query: {
                    presets: [
                        [
                            "@babel/preset-env",
                            {
                                targets: {
                                    chrome: "58",
                                    ie: "9"
                                }
                            }
                        ]
                    ]
                }
            }
        ]
    },
    plugins: [
        // Ignore all locale files of moment.js
        new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
        new webpack.ProvidePlugin({
            moment: "moment"
        })
    ]
};

My loader.js just cotains: 我的loader.js仅包含以下内容:

require("moment")

Remove the entire plugins section from your webpack config and see if it works. 从您的webpack配置中删除整个插件部分,然后查看它是否有效。 There are few issues/docs I read and that last part is just trying to import minimal code from moment and should be used with specific moment version. 我读的问题/文档很少,而最后一部分只是试图从现在开始导入最少的代码,应该与特定的版本一起使用。

Take a look at following links. 看一下以下链接。 https://webpack.js.org/plugins/ignore-plugin/ https://github.com/moment/moment/issues/2373 https://webpack.js.org/plugins/ignore-plugin/ https://github.com/moment/moment/issues/2373

Another thing I would suggest is try requiring some other module in your loader.js apart from moment and see if that is part of your bundle. 我建议的另一件事是尝试在loader.js中要求其他模块,而不要检查它是否是捆绑包的一部分。 Just to verify this problem is with just moment or all modules. 只是为了验证这个问题仅在瞬间还是在所有模块上。 If it occurs for all modules then there is also something else which is incorrect in config. 如果所有模块都出现这种情况,则说明配置中还有其他错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM