1 | const Dependency = require('webpack/lib/Dependency');
|
2 |
|
3 | /**
|
4 | * Mocks out some methods on the global $jscomp variable that Closure Library
|
5 | * expects in order to work with ES6 modules.
|
6 | *
|
7 | * Closure Library provides goog.module.declareNamespace to associate an ES6
|
8 | * module with a Closure namespace, enabling it to be goog.require'd.
|
9 | *
|
10 | * When goog.module.declareNamespace is called in a bundle,
|
11 | * $jscomp.getCurrentModulePath is expected to return a non-null value (though
|
12 | * this value is not stored, just asserted to be non-null). Then
|
13 | * $jscomp.require($jscomp.getCurrentModulePath()) is called in order to get the
|
14 | * ES6 module's exports and store it with the namespace passed to the call to
|
15 | * goog.module.declareNamespace. Closure stores these exports and associates
|
16 | * them with the namespace so they can later be goog.require'd.
|
17 | */
|
18 | class GoogLoaderEs6PrefixDependency extends Dependency {
|
19 | constructor(insertPosition) {
|
20 | super();
|
21 | this.insertPosition = insertPosition;
|
22 | }
|
23 |
|
24 | get type() {
|
25 | return 'goog loader es6 prefix';
|
26 | }
|
27 |
|
28 | updateHash(hash) {
|
29 | hash.update(this.insertPosition + '');
|
30 | }
|
31 | }
|
32 |
|
33 | class GoogLoaderEs6PrefixDependencyTemplate {
|
34 | apply(dep, source) {
|
35 | if (dep.insertPosition === null) {
|
36 | return;
|
37 | }
|
38 |
|
39 | source.insert(
|
40 | dep.insertPosition,
|
41 | `$jscomp.getCurrentModulePath = function() { return '<webpack module>'; };\n` +
|
42 | '$jscomp.require = function() { return __webpack_exports__ };\n'
|
43 | );
|
44 | }
|
45 | }
|
46 |
|
47 | module.exports = GoogLoaderEs6PrefixDependency;
|
48 | module.exports.Template = GoogLoaderEs6PrefixDependencyTemplate;
|