Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80669 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
/*jslint proto:true*/
17
18
var inherits = require('util').inherits;
19
var Resource = require('./Resource');
20
21
/**
22
* Resource for *.js files
23
* A heavier version of JS that does extract more information (gziped size).
24
* @extends {JSLite}
25
* @class
26
* @param {String} path path of the resource
27
*/
28
function JS(path) {
29
Resource.call(this, path);
30
31
this.id = null;
32
33
this.options = {};
34
this._requiredCSSMap = {};
35
this._requiredModuleMap = {};
36
this._requiredLegacyComponentsMap = {};
37
this._requiredTextToResolvedID = {};
38
}
39
inherits(JS, Resource);
40
JS.__proto__ = Resource;
41
42
43
// move default options to the prototype, to reduce serialized size
44
JS.prototype.jsxDOMImplementor = null;
45
JS.prototype.networkSize = 0;
46
JS.prototype.isJSXEnabled = false;
47
JS.prototype.isModule = false;
48
JS.prototype.isJavelin = false;
49
JS.prototype.isRunWhenReady = false;
50
JS.prototype.isPolyfill = false;
51
JS.prototype.isLegacy = false;
52
JS.prototype.isPermanent = false;
53
JS.prototype.isNopackage = false;
54
55
// do not modify this arrays in loader, only override
56
JS.prototype.definedJavelinSymbols = [];
57
JS.prototype.requiredJavelinSymbols = [];
58
JS.prototype.requiredDynamicModules = [];
59
JS.prototype.requiredLazyModules = [];
60
JS.prototype.requiredCSS = [];
61
62
/**
63
* Initially, these are the strings inside of calls to `require()`. They may not
64
* be the moduleIDs that you intend to load - they could be relative require
65
* paths etc. After `postProcess`, each item in the array is replaced with the
66
* actual resource IDs that the `require()` call resolved to - if it differs
67
* from the original text argument. The `_requiredTextToResolvedID` records
68
* which required "text" was resolved to which ID in the final `requiredModules`
69
* array, so that you can packaging tools are free to use that "history" of the
70
* the resolution to statically replace the argument to require().
71
*/
72
JS.prototype.requiredModules = [];
73
JS.prototype.requiredLegacyComponents = [];
74
JS.prototype.suggests = [];
75
JS.prototype.polyfillUAs = [];
76
77
JS.prototype.type = 'JS';
78
79
JS.prototype.addRequiredModule = function(x) {
80
this._requiredModuleMap[x] = true;
81
};
82
83
JS.prototype.addRequiredLegacyComponent = function(x) {
84
this._requiredLegacyComponentsMap[x] = true;
85
};
86
87
JS.prototype.addRequiredCSS = function(x) {
88
this._requiredCSSMap[x] = true;
89
};
90
91
JS.prototype.finalize = function() {
92
var keys = Object.keys(this._requiredModuleMap);
93
if (keys.length) {
94
this.requiredModules = keys;
95
}
96
keys = Object.keys(this._requiredLegacyComponentsMap);
97
if (keys.length) {
98
this.requiredLegacyComponents = keys;
99
}
100
keys = Object.keys(this._requiredCSSMap);
101
if (keys.length) {
102
this.requiredCSS = keys;
103
}
104
};
105
106
107
/**
108
* `_requiredModuleMap` records the original form that the module was required
109
* in, before `postProcess` has normalized it to the canonical ID form.
110
* `_requiredTextToResolvedID` associates the two. So if your JS has
111
* `require('./path/comp.js')`, then the JS resource instance will have:
112
*
113
* _requiredModuleMap: {'./path/comp.js': true}
114
* requiredModules: ['package-name/path/comp.js']
115
* _requiredTextToResolvedID: {'./path/comp.js': 'package-name/path/comp.js'}
116
*
117
* @param {string} origName String passsed to require() in js file.
118
* @param {string} modID Canonical module ID origName resolves to from the
119
* perspective of this particular resource.
120
*/
121
JS.prototype.recordRequiredModuleOrigin = function(origName, modID) {
122
this._requiredTextToResolvedID[origName] = modID;
123
};
124
125
/**
126
* @param {str} origName Originally required name as in `require('./x/y.js')`
127
* @return {string} canonical module ID - which might have been redirected using
128
* `recordRequiredModuleOrigin` or not.
129
*/
130
JS.prototype.getModuleIDByOrigin = function(origName) {
131
return this._requiredTextToResolvedID[origName] || origName;
132
};
133
134
module.exports = JS;
135
136