簡體   English   中英

如何將moment.js加載到webpack中

[英]how to load moment.js into webpack

我想將moment.js作為插件加載到webpack中。 有誰知道怎么做? 我在我的骨干視圖中嘗試調用define('moment')時沒有定義錯誤“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
    }
}));

首先,確保使用npm命令安裝時刻

npm安裝時刻

現在你應該在node_modules文件夾中有時間。 然后你應該將項目作為全局變量添加到項目中,這樣你就應該改變Webpack來為你解決它。為此,你需要更改你的webpack.config.js文件,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM