如何将moment.js加载到webpack中

时间:2017-01-24 16:57:40

标签: javascript webpack momentjs

我想将moment.js作为插件加载到webpack中。有谁知道怎么做?我在尝试在骨干视图中调用define('moment')时没有定义错误“时刻”

这是我的webpack配置:

app.use(webpackMiddleware(webpack({
    context: __dirname + '/src',
    entr: './admin-entry.js',
    entry: {
        'admin-entry': './admin-entry.js',
        'admin-login': './admin-login.js'
    },
    output: {
        path: '/webpack',
        publicPath: '/webpack',
        filename: '[name].js'
    },

    module: {
        noParse: [
            new RegExp(jqueryPath),
            new RegExp(lodashPath),
            new RegExp(qPath),
            new RegExp(cookiePath),
            new RegExp(momentPath),
            new RegExp(dateRangePickerPath)
        ]

    },
    resolve: {
        alias: {
            'jquery': jqueryPath,
            'lodash': lodashPath,
            'underscore': lodashPath,
            'backbone': backbonePath,
            'cookie': cookiePath,
            'daterangepicker': dateRangePickerPath,
            'moment': momentPath,
            'q': qPath
        },
        modulesDirectories: [
            'node_modules'
        ]
    },
    devtool: 'eval-source-map',
    plugins: [
        new webpack.SourceMapDevToolPlugin('webpack/[file].map', null, '[absolute-resource-path]', '[absolute-resource-path]')
    ]
}), {
    noInfo: false,
    quiet: false,
    lazy: false,
    // switch into lazy mode
    // that means no watching, but recompilation on every request
    watchOptions: {
        aggregateTimeout: 300,
        poll: true
    },
    publicPath: '/webpack',
    // public path to bind the middleware to
    // use the same as in webpack

    stats: {
        colors: true
    }
}));

1 个答案:

答案 0 :(得分:0)

首先,请确保使用npm命令安装时刻

  

npm安装时刻

现在,您应该在node_modules文件夹中有时间。然后,您应该将时刻作为全局变量添加到项目中,以便更改Webpack来为您解决该问题。为此,请如下更改webpack.config.js文件:

    plugins: [
      .... 
      new webpack.ProvidePlugin({
          moment : 'moment'
      })
      ....
    ]