diff --git a/index.js b/index.js index 97d8935..0b1bf8d 100644 --- a/index.js +++ b/index.js @@ -45,7 +45,11 @@ function find_shims_in_package(pkgJson, cur_path, shims, browser) { // then it just replaces the main entry point if (typeof replacements === 'string') { var key = path.resolve(cur_path, info.main || 'index.js'); - shims[key] = path.resolve(cur_path, replacements); + if (replacements[0] === '.') + shims[key] = path.resolve(cur_path, replacements); + else + shims[cur_path] = replacements; + return; } @@ -164,7 +168,10 @@ function build_resolve_opts(opts, base) { // replace main if (typeof replacements === 'string') { - info.main = replacements; + if (replacements[0] === '.') + info.main = replacements; + else + info.main = resv.sync(replacements, build_resolve_opts(opts, info.__dirname || base)) return info; } diff --git a/test/fixtures/node_modules/module-t/main.js b/test/fixtures/node_modules/module-t/main.js new file mode 100644 index 0000000..b4597a7 --- /dev/null +++ b/test/fixtures/node_modules/module-t/main.js @@ -0,0 +1 @@ +// module-t \ No newline at end of file diff --git a/test/fixtures/node_modules/module-t/package.json b/test/fixtures/node_modules/module-t/package.json new file mode 100644 index 0000000..0aa6a78 --- /dev/null +++ b/test/fixtures/node_modules/module-t/package.json @@ -0,0 +1,4 @@ +{ + "main": "./main.js", + "browser": "module-b" +} diff --git a/test/fixtures/node_modules/module-u/index.js b/test/fixtures/node_modules/module-u/index.js new file mode 100644 index 0000000..7039380 --- /dev/null +++ b/test/fixtures/node_modules/module-u/index.js @@ -0,0 +1 @@ +require('module-t'); \ No newline at end of file diff --git a/test/fixtures/node_modules/module-u/package.json b/test/fixtures/node_modules/module-u/package.json new file mode 100644 index 0000000..a9907d8 --- /dev/null +++ b/test/fixtures/node_modules/module-u/package.json @@ -0,0 +1,3 @@ +{ + "main": "./index.js" +} diff --git a/test/modules-sync.js b/test/modules-sync.js index 914758a..a1f8b42 100644 --- a/test/modules-sync.js +++ b/test/modules-sync.js @@ -33,6 +33,45 @@ test('string browser field as main - require subfile', function() { assert.equal(path, require.resolve('./fixtures/node_modules/module-c/bar')); }); +// package.json has 'browser' field which is a reference to another module +// the replacement should be respected if the module is resolved +test('string browser field as main - alt module', function() { + var parent = { + filename: fixtures_dir + '/module-t/_fake.js', + paths: [ fixtures_dir + '/module-t/node_modules' ], + package: { main: './main.js' } + }; + + var path = resolve.sync('.', parent) + assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main')); +}); + +// package.json has 'browser' field which is a reference to another module +// the replacement should be ignored if the file referenced in main is explicitly resolved +test('string browser field as main - alt module - explicit main', function() { + var parent = { + filename: fixtures_dir + '/module-t/_fake.js', + paths: [ fixtures_dir + '/module-t/node_modules' ], + package: { main: './main.js' } + }; + + var path = resolve.sync('./main.js', parent) + assert.equal(path, require.resolve('./fixtures/node_modules/module-t/main')); +}); + +// package.json has 'browser' field which is a reference to another module +// the replacement should be respected if the module is required from another module +test('string browser field as main - alt module - required from another module', function() { + var parent = { + filename: fixtures_dir + '/module-u/index.js', + paths: [ fixtures_dir ], + package: { main: './index.js' } + }; + + var path = resolve.sync('module-t', parent) + assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main')); +}); + // package.json has browser field as object // one of the keys replaces the main file // this would be done if the user needed to replace main and some other module diff --git a/test/modules.js b/test/modules.js index d1a3814..7d79d89 100644 --- a/test/modules.js +++ b/test/modules.js @@ -33,6 +33,57 @@ test('string browser field as main', function(done) { }); }); +// package.json has 'browser' field which is a reference to another module +// the replacement should be respected if the module is resolved +test('string browser field as main - alt module', function(done) { + var parent = { + filename: fixtures_dir + '/module-t/_fake.js', + paths: [ fixtures_dir + '/module-t/node_modules' ], + package: { main: './main.js' } + }; + + resolve('.', parent, function(err, path, pkg) { + assert.ifError(err); + assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main')); + assert.equal(pkg.main, './main.js'); + done(); + }); +}); + +// package.json has 'browser' field which is a reference to another module +// the replacement should be ignored if the file referenced in main is explicitly resolved +test('string browser field as main - alt module - explicit main', function(done) { + var parent = { + filename: fixtures_dir + '/module-t/_fake.js', + paths: [ fixtures_dir + '/module-t/node_modules' ], + package: { main: './main.js' } + }; + + resolve('./main.js', parent, function(err, path, pkg) { + assert.ifError(err); + assert.equal(path, require.resolve('./fixtures/node_modules/module-t/main')); + assert.equal(pkg.main, './main.js'); + done(); + }); +}); + +// package.json has 'browser' field which is a reference to another module +// the replacement should be respected if the module is required from another module +test('string browser field as main - alt module - required from another module', function(done) { + var parent = { + filename: fixtures_dir + '/module-u/index.js', + paths: [ fixtures_dir ], + package: { main: './index.js' } + }; + + resolve('module-t', parent, function(err, path, pkg) { + assert.ifError(err); + assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main')); + assert.equal(pkg.main, require.resolve('./fixtures/node_modules/module-b/main')); + done(); + }); +}); + // package.json has 'browser' field which is a string test('string browser field as main - require subfile', function(done) { var parent = {