diff --git a/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js b/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js index 69ed11e6..3202274c 100644 --- a/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js +++ b/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js @@ -14,6 +14,7 @@ jest .dontMock('path') .dontMock('absolute-path') .dontMock('../docblock') + .dontMock('../../requirePattern') .setMock('../../../ModuleDescriptor', function(data) {return data;}); describe('DependencyGraph', function() { diff --git a/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js b/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js index 00c4a5c5..a23f6d50 100644 --- a/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js +++ b/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js @@ -12,6 +12,7 @@ var ModuleDescriptor = require('../../ModuleDescriptor'); var q = require('q'); var fs = require('fs'); var docblock = require('./docblock'); +var requirePattern = require('../requirePattern'); var path = require('path'); var isAbsolutePath = require('absolute-path'); var debug = require('debug')('DependecyGraph'); @@ -600,7 +601,6 @@ DependecyGraph.prototype._processAssetChange = function(eventType, file) { /** * Extract all required modules from a `code` string. */ -var requireRe = /\brequire\s*\(\s*[\'"]([^"\']+)["\']\s*\)/g; var blockCommentRe = /\/\*(.|\n)*?\*\//g; var lineCommentRe = /\/\/.+(\n|$)/g; function extractRequires(code) { @@ -609,7 +609,7 @@ function extractRequires(code) { code .replace(blockCommentRe, '') .replace(lineCommentRe, '') - .replace(requireRe, function(match, dep) { + .replace(requirePattern, function(match, _, dep) { deps.push(dep); }); diff --git a/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js b/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js index 69354590..c81944d8 100644 --- a/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js +++ b/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js @@ -10,6 +10,7 @@ jest.dontMock('../') .dontMock('q') + .dontMock('../requirePattern') .setMock('../../ModuleDescriptor', function(data) {return data;}); var q = require('q'); @@ -226,11 +227,13 @@ describe('HasteDependencyResolver', function() { }); var depGraph = depResolver._depGraph; - var dependencies = ['x', 'y', 'z']; + var dependencies = ['x', 'y', 'z', 'a', 'b']; var code = [ 'require("x")', 'require("y")', - 'require("z")', + 'require( "z" )', + 'require( "a")', + 'require("b" )', ].join('\n'); depGraph.resolveDependency.mockImpl(function(fromModule, toModuleName) { @@ -255,7 +258,9 @@ describe('HasteDependencyResolver', function() { ' require, requireDynamic, requireLazy, module, exports) {' + ' require(\'changed\')', 'require(\'y\')', - 'require("z")});', + 'require("z")', + 'require("a")', + 'require("b")});', ].join('\n')); }); }); diff --git a/react-packager/src/DependencyResolver/haste/index.js b/react-packager/src/DependencyResolver/haste/index.js index 0e46d5e8..941a687e 100644 --- a/react-packager/src/DependencyResolver/haste/index.js +++ b/react-packager/src/DependencyResolver/haste/index.js @@ -9,8 +9,8 @@ 'use strict'; var path = require('path'); -var FileWatcher = require('../../FileWatcher'); var DependencyGraph = require('./DependencyGraph'); +var requirePattern = require('./requirePattern'); var ModuleDescriptor = require('../ModuleDescriptor'); var declareOpts = require('../../lib/declareOpts'); @@ -25,7 +25,6 @@ var DEFINE_MODULE_CODE = [ ].join(''); var DEFINE_MODULE_REPLACE_RE = /_moduleName_|_code_|_deps_/g; -var REL_REQUIRE_STMT = /require\(['"]([\.\/0-9A-Z_$\-]*)['"]\)/gi; var validateOpts = declareOpts({ projectRoots: { @@ -146,12 +145,12 @@ HasteDependencyResolver.prototype.wrapModule = function(module, code) { } var relativizedCode = - code.replace(REL_REQUIRE_STMT, function(codeMatch, depName) { + code.replace(requirePattern, function(codeMatch, _, depName) { var depId = resolvedDeps[depName]; if (depId != null) { return 'require(\'' + depId + '\')'; } else { - return codeMatch; + return codeMatch.replace(/\s+/g, ''); } }); diff --git a/react-packager/src/DependencyResolver/haste/requirePattern.js b/react-packager/src/DependencyResolver/haste/requirePattern.js new file mode 100644 index 00000000..26d807ff --- /dev/null +++ b/react-packager/src/DependencyResolver/haste/requirePattern.js @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +'use strict'; + +var REQUIRE_RE = /\brequire\s*?\(\s*?([\'"])([^"\']+)\1\s*?\)/g; + +module.exports = REQUIRE_RE;