constructor({ depGraphHelpers, localPath, file, getTransformCacheKey, globalTransformCache, moduleCache, options, reporter, transformCode, }: ConstructorArgs) { if (!isAbsolutePath(file)) { throw new Error('Expected file to be absolute path but got ' + file); } this.localPath = localPath; this.path = file; this.type = 'Module'; this._moduleCache = moduleCache; this._transformCode = transformCode; this._getTransformCacheKey = getTransformCacheKey; this._depGraphHelpers = depGraphHelpers; this._options = options || {}; this._reporter = reporter; this._globalCache = globalTransformCache; this._readPromises = new Map(); this._readResultsByOptionsKey = new Map(); }
constructor({ file, fastfs, moduleCache, cache, extractor = extractRequires, transformCode, depGraphHelpers, options, }) { if (!isAbsolutePath(file)) { throw new Error('Expected file to be absolute path but got ' + file); } this.path = file; this.type = 'Module'; this._fastfs = fastfs; this._moduleCache = moduleCache; this._cache = cache; this._extractor = extractor; this._transformCode = transformCode; this._depGraphHelpers = depGraphHelpers; this._options = options; }
return this._read().then(json => { const {browser} = json; if (!browser || typeof browser !== 'object') { return name; } if (name[0] !== '/') { return browser[name] || name; } if (!isAbsolutePath(name)) { throw new Error(`Expected ${name} to be absolute path`); } const relPath = './' + path.relative(this.root, name); const redirect = browser[relPath] || browser[relPath + '.js'] || browser[relPath + '.json']; if (redirect) { return path.join( this.root, redirect ); } return name; });
_resolveNodeDependency(fromModule, toModuleName) { if (toModuleName[0] === '.' || toModuleName[1] === '/') { const potentialModulePath = isAbsolutePath(toModuleName) ? toModuleName : path.join(path.dirname(fromModule.path), toModuleName); return this._redirectRequire(fromModule, potentialModulePath).then( realModuleName => this._loadAsFile(realModuleName) .catch(() => this._loadAsDir(realModuleName)) ); } else { return this._redirectRequire(fromModule, toModuleName).then( realModuleName => { const searchQueue = []; for (let currDir = path.dirname(fromModule.path); currDir !== '/'; currDir = path.dirname(currDir)) { searchQueue.push( path.join(currDir, 'node_modules', realModuleName) ); } let p = Promise.reject(new Error('Node module not found')); searchQueue.forEach(potentialModulePath => { p = p.catch( () => this._loadAsFile(potentialModulePath) ).catch( () => this._loadAsDir(potentialModulePath) ); }); return p; }); } }
return this.read().then(json => { var replacements = getReplacements(json); if (!replacements || typeof replacements !== 'object') { return name; } if (name[0] !== '/') { return replacements[name] || name; } if (!isAbsolutePath(name)) { throw new Error(`Expected ${name} to be absolute path`); } const relPath = './' + path.relative(this.root, name); const redirect = replacements[relPath] || replacements[relPath + '.js'] || replacements[relPath + '.json']; if (redirect) { return path.join( this.root, redirect ); } return name; });
constructor({ file, fastfs, moduleCache, cache, transformCode, transformCacheKey, depGraphHelpers, options, }: ConstructorArgs) { if (!isAbsolutePath(file)) { throw new Error('Expected file to be absolute path but got ' + file); } this.path = file; this.type = 'Module'; this._fastfs = fastfs; this._moduleCache = moduleCache; this._cache = cache; this._transformCode = transformCode; this._transformCacheKey = transformCacheKey; invariant( transformCode == null || transformCacheKey != null, 'missing transform cache key', ); this._depGraphHelpers = depGraphHelpers; this._options = options || {}; this._readPromises = new Map(); }
value: function get(filepath, field, loaderCb) { if (!isAbsolutePath(filepath)) { throw new Error('Use absolute paths'); } return this.has(filepath, field) ? this._data[filepath].data[field] : this.set(filepath, field, loaderCb(filepath)); }
resolveDependency(fromModule: TModule, toModuleName: string): TModule { const resHash = resolutionHash(fromModule.path, toModuleName); const immediateResolution = this._immediateResolutionCache[resHash]; if (immediateResolution) { return immediateResolution; } const cacheResult = result => { this._immediateResolutionCache[resHash] = result; return result; }; if ( !this._options.helpers.isNodeModulesDir(fromModule.path) && !(isRelativeImport(toModuleName) || isAbsolutePath(toModuleName)) ) { const result = tryResolveSync( () => this._resolveHasteDependency(fromModule, toModuleName), () => this._resolveNodeDependency(fromModule, toModuleName), ); return cacheResult(result); } return cacheResult(this._resolveNodeDependency(fromModule, toModuleName)); }
return this._redirectRequire(fromModule, toModuleName).then(function (realModuleName) { if (isRelativeImport(realModuleName) || isAbsolutePath(realModuleName)) { // derive absolute path /.../node_modules/fromModuleDir/realModuleName var fromModuleParentIdx = fromModule.path.lastIndexOf('node_modules/') + 13; var fromModuleDir = fromModule.path.slice(0, fromModule.path.indexOf('/', fromModuleParentIdx)); var absPath = path.join(fromModuleDir, realModuleName); return _this5._resolveFileOrDir(fromModule, absPath); } var searchQueue = []; for (var currDir = path.dirname(fromModule.path); currDir !== realPath.parse(fromModule.path).root; currDir = path.dirname(currDir)) { searchQueue.push(path.join(currDir, 'node_modules', realModuleName)); } var p = Promise.reject(new UnableToResolveError(fromModule, toModuleName, 'Node module not found')); searchQueue.forEach(function (potentialModulePath) { p = _this5._tryResolve(function () { return _this5._tryResolve(function () { return p; }, function () { return _this5._loadAsFile(potentialModulePath, fromModule, toModuleName); }); }, function () { return _this5._loadAsDir(potentialModulePath, fromModule, toModuleName); }); }); return p; });
function getPackagerServer(args, config) { let transformerPath = args.transformer; if (!isAbsolutePath(transformerPath)) { transformerPath = path.resolve(process.cwd(), transformerPath); } return ReactPackager.createServer({ nonPersistent: args.nonPersistent, projectRoots: args.projectRoots, blacklistRE: config.getBlacklistRE(), cacheVersion: '3', getTransformOptionsModulePath: config.getTransformOptionsModulePath, transformModulePath: transformerPath, extraNodeModules: config.extraNodeModules, assetRoots: args.assetRoots, assetExts: [ 'bmp', 'gif', 'jpg', 'jpeg', 'png', 'psd', 'svg', 'webp', // Image formats 'm4v', 'mov', 'mp4', 'mpeg', 'mpg', 'webm', // Video formats 'aac', 'aiff', 'caf', 'm4a', 'mp3', 'wav', // Audio formats 'html', 'pdf', // Document formats ], resetCache: args.resetCache || args['reset-cache'], verbose: args.verbose, }); }