0

I am using WebPack 4 to build my TypeScript applications.

One of my modules uses the moment-timezone library, for example...

import * as moment from 'moment-timezone';
$('#label-locale').text(moment.locale());

and this works as expected.

To make better use of caching, I would like to reference moment and moment-timezone from a Content Delivery Network (CDN) and have added the following to my HTML page:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.min.js"></script>

I also modified the webpack.config.js as follows:

module.exports = {
    externals: {
        'jquery': 'jQuery',
        'moment': 'moment-timezone'
    }
};

Now, when I run my code, I get the following error:

ReferenceError: timezone is not defined

I am new to WebPack and have tried things like adding 'timezone': 'moment-timezone' to the externals without success.

Any help/direction would be very much appreciated.

4

1 回答 1

0

这个是我想多了。 moment-timezone实际上“只是”将moment对象扩展为 include moment.tz,所以我所要做的就是修改webpack.config.js如下:

module.exports = {
    externals: {
        'jquery': 'jQuery',
        'moment-timezone': 'moment'
    }
};

因此,CDN 引用已经更新了对象,而不是WebPack有效地从对象构建整个定义树。至少,我认为这就是正在发生的事情。node_modules/moment-timezonemoment

于 2018-12-03T15:43:28.580 回答