/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/15/*jslint proto:true*/1617var inherits = require('util').inherits;18var Resource = require('./Resource');1920/**21* Resource for *.js files22* A heavier version of JS that does extract more information (gziped size).23* @extends {JSLite}24* @class25* @param {String} path path of the resource26*/27function JS(path) {28Resource.call(this, path);2930this.id = null;3132this.options = {};33this._requiredCSSMap = {};34this._requiredModuleMap = {};35this._requiredLegacyComponentsMap = {};36this._requiredTextToResolvedID = {};37}38inherits(JS, Resource);39JS.__proto__ = Resource;404142// move default options to the prototype, to reduce serialized size43JS.prototype.jsxDOMImplementor = null;44JS.prototype.networkSize = 0;45JS.prototype.isJSXEnabled = false;46JS.prototype.isModule = false;47JS.prototype.isJavelin = false;48JS.prototype.isRunWhenReady = false;49JS.prototype.isPolyfill = false;50JS.prototype.isLegacy = false;51JS.prototype.isPermanent = false;52JS.prototype.isNopackage = false;5354// do not modify this arrays in loader, only override55JS.prototype.definedJavelinSymbols = [];56JS.prototype.requiredJavelinSymbols = [];57JS.prototype.requiredDynamicModules = [];58JS.prototype.requiredLazyModules = [];59JS.prototype.requiredCSS = [];6061/**62* Initially, these are the strings inside of calls to `require()`. They may not63* be the moduleIDs that you intend to load - they could be relative require64* paths etc. After `postProcess`, each item in the array is replaced with the65* actual resource IDs that the `require()` call resolved to - if it differs66* from the original text argument. The `_requiredTextToResolvedID` records67* which required "text" was resolved to which ID in the final `requiredModules`68* array, so that you can packaging tools are free to use that "history" of the69* the resolution to statically replace the argument to require().70*/71JS.prototype.requiredModules = [];72JS.prototype.requiredLegacyComponents = [];73JS.prototype.suggests = [];74JS.prototype.polyfillUAs = [];7576JS.prototype.type = 'JS';7778JS.prototype.addRequiredModule = function(x) {79this._requiredModuleMap[x] = true;80};8182JS.prototype.addRequiredLegacyComponent = function(x) {83this._requiredLegacyComponentsMap[x] = true;84};8586JS.prototype.addRequiredCSS = function(x) {87this._requiredCSSMap[x] = true;88};8990JS.prototype.finalize = function() {91var keys = Object.keys(this._requiredModuleMap);92if (keys.length) {93this.requiredModules = keys;94}95keys = Object.keys(this._requiredLegacyComponentsMap);96if (keys.length) {97this.requiredLegacyComponents = keys;98}99keys = Object.keys(this._requiredCSSMap);100if (keys.length) {101this.requiredCSS = keys;102}103};104105106/**107* `_requiredModuleMap` records the original form that the module was required108* in, before `postProcess` has normalized it to the canonical ID form.109* `_requiredTextToResolvedID` associates the two. So if your JS has110* `require('./path/comp.js')`, then the JS resource instance will have:111*112* _requiredModuleMap: {'./path/comp.js': true}113* requiredModules: ['package-name/path/comp.js']114* _requiredTextToResolvedID: {'./path/comp.js': 'package-name/path/comp.js'}115*116* @param {string} origName String passsed to require() in js file.117* @param {string} modID Canonical module ID origName resolves to from the118* perspective of this particular resource.119*/120JS.prototype.recordRequiredModuleOrigin = function(origName, modID) {121this._requiredTextToResolvedID[origName] = modID;122};123124/**125* @param {str} origName Originally required name as in `require('./x/y.js')`126* @return {string} canonical module ID - which might have been redirected using127* `recordRequiredModuleOrigin` or not.128*/129JS.prototype.getModuleIDByOrigin = function(origName) {130return this._requiredTextToResolvedID[origName] || origName;131};132133module.exports = JS;134135136