mirror of https://github.com/status-im/metro.git
Bugfix/require module regexp
Summary: Resolves https://github.com/facebook/react-native/issues/316. Also updated the spec for the Haste Dependency Resolver. Not sure if these changes are the ones desired so feedback would be welcome! Closes https://github.com/facebook/react-native/pull/368 Github Author: daviskoh <koh.davis.0@gmail.com> Test Plan: ./runJestTests
This commit is contained in:
parent
2c4e9aba80
commit
28a0a2586d
|
@ -14,6 +14,7 @@ jest
|
|||
.dontMock('path')
|
||||
.dontMock('absolute-path')
|
||||
.dontMock('../docblock')
|
||||
.dontMock('../../requirePattern')
|
||||
.setMock('../../../ModuleDescriptor', function(data) {return data;});
|
||||
|
||||
describe('DependencyGraph', function() {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
|
@ -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( "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'));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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, '');
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -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;
|
Loading…
Reference in New Issue