Webpack需要全球参考的时刻

时间:2017-08-20 20:39:53

标签: webpack momentjs

所以我尝试了多种方法,但无论我做什么'时刻'都无法在全球范围内使用。很沮丧。谁能指出我在这方面的正确方向?

var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: {
        vendor: "./app/vendor.js"
    },
    resolve: {
        alias: {
            jquery: "jquery/src/jquery",
        }
    },
    module: {
        loaders: [
            {
                test: /\.css/,
                loader: ExtractTextPlugin.extract("css-loader")
            },
            {
                test: /\.(png|jpg|gif)$/,
                loader: "file-loader?name=app/img/[name].[ext]"
            }
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({
          $: "jquery",
          jQuery: "jquery",
          moment: "moment"
        }),
        new ExtractTextPlugin("app/styles.css")
    ],
    output: {
        filename: "./app/[name].bundle.js"
    }
}

如果您在下面注意到,我必须使用window.d3使d3全局可用,因为将其添加到webpack.providerplugin并不是专门用于d3。同样的事情,只有window.moment和var moment = require也不起作用。

window.d3 = require('d3');

require("jquery");
require('jquery-ui/ui/core');
require('jquery-ui/ui/widgets/datepicker');

require('lodash');
require('angular');

require('chosen-js');
require('chosen-js/chosen.css');

var moment = require('moment/min/moment-with-locales.js');
require('moment-timezone/builds/moment-timezone-with-data-2010-2020.js')

1 个答案:

答案 0 :(得分:1)

@Arjun - 是的,expose-loader对我有用。谢谢! https://github.com/webpack-contrib/expose-loader

require('expose-loader?moment!moment');