Skip to content

Bugfix/require module regexp #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest
.dontMock('path')
.dontMock('absolute-path')
.dontMock('../docblock')
.dontMock('../regex')
.setMock('../../../ModuleDescriptor', function(data) {return data;});

describe('DependencyGraph', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var ModuleDescriptor = require('../../ModuleDescriptor');
var q = require('q');
var fs = require('fs');
var docblock = require('./docblock');
var REQUIRE_RE = require('./regex').REQUIRE_RE;
var path = require('path');
var isAbsolutePath = require('absolute-path');
var debug = require('debug')('DependecyGraph');
Expand Down Expand Up @@ -600,7 +601,7 @@ 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) {
Expand All @@ -609,7 +610,7 @@ function extractRequires(code) {
code
.replace(blockCommentRe, '')
.replace(lineCommentRe, '')
.replace(requireRe, function(match, dep) {
.replace(REQUIRE_RE, function(match, _, dep) {
deps.push(dep);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

exports.REQUIRE_RE = REQUIRE_RE;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

jest.dontMock('../')
.dontMock('q')
.dontMock('../DependencyGraph/regex')
.setMock('../../ModuleDescriptor', function(data) {return data;});

var q = require('q');
Expand Down Expand Up @@ -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) {
Expand All @@ -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'));
});
});
Expand Down
6 changes: 3 additions & 3 deletions packager/react-packager/src/DependencyResolver/haste/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var path = require('path');
var FileWatcher = require('../../FileWatcher');
var DependencyGraph = require('./DependencyGraph');
var REQUIRE_RE = require('./DependencyGraph/regex').REQUIRE_RE;
var ModuleDescriptor = require('../ModuleDescriptor');
var declareOpts = require('../../lib/declareOpts');

Expand All @@ -25,7 +26,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: {
Expand Down Expand Up @@ -146,12 +146,12 @@ HasteDependencyResolver.prototype.wrapModule = function(module, code) {
}

var relativizedCode =
code.replace(REL_REQUIRE_STMT, function(codeMatch, depName) {
code.replace(REQUIRE_RE, function(codeMatch, _, depName) {
var depId = resolvedDeps[depName];
if (depId != null) {
return 'require(\'' + depId + '\')';
} else {
return codeMatch;
return codeMatch.replace(/\s+/g, '');
}
});

Expand Down