Skip to content
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

feature: resolve main reference to other modules #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/node_modules/module-t/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/fixtures/node_modules/module-t/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/node_modules/module-u/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/node_modules/module-u/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions test/modules-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions test/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down