From a6fc07bffa1873010044f241d7f3c86ae4cb923c Mon Sep 17 00:00:00 2001 From: Peng Wang Date: Fri, 17 Feb 2017 14:43:59 -0600 Subject: [PATCH 1/2] read main from alternatives's main as well --- lib/detect-dependencies.js | 42 ++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/lib/detect-dependencies.js b/lib/detect-dependencies.js index aec3f1e..46a4425 100644 --- a/lib/detect-dependencies.js +++ b/lib/detect-dependencies.js @@ -47,6 +47,15 @@ function detectDependencies(config) { return config; } +/** + * does the config object have the proper main we need + * + * @param config + * @returns {Boolean} + */ +function isValidConfig(config) { + return $._.isObject(config) && config.main && config.main.length > 0; +} /** * Find the component's JSON configuration file. @@ -62,15 +71,32 @@ function findComponentConfigFile(config, component) { return config.get('bower.json'); } - ['bower.json', '.bower.json', 'component.json', 'package.json']. - forEach(function (configFile) { - configFile = $.path.join(config.get('bower-directory'), component, configFile); - - if (!$._.isObject(componentConfigFile) && $.fs.existsSync(configFile)) { - componentConfigFile = JSON.parse($.fs.readFileSync(configFile)); + var candidates = ['bower.json', '.bower.json', 'component.json', 'package.json']; + for (var i = 0, len = candidates.length; i < len; i++) { + if (isValidConfig(componentConfigFile)) { + break; + } + var configFile = $.path.join(config.get('bower-directory'), component, candidates[i]); + if ($.fs.existsSync(configFile) === false) { + continue; + } else { + var curConfigFile = JSON.parse($.fs.readFileSync(configFile)); + if ($._.isObject(componentConfigFile) === false) { + componentConfigFile = curConfigFile; + //current configFile match what we need + if (isValidConfig(curConfigFile)) { + break; + } + } else { + if (!componentConfigFile.main || componentConfigFile.main.length === 0) { + if (curConfigFile.main && curConfigFile.main.length > 0) { + componentConfigFile.main = curConfigFile.main; + break; + } + } } - }); - + } + } return componentConfigFile; } From bcb9ca360c487823f99ffa508d5dafb743151472 Mon Sep 17 00:00:00 2001 From: Peng Wang Date: Fri, 17 Feb 2017 15:20:44 -0600 Subject: [PATCH 2/2] add test case for pacakge.json alternative --- .../fake.js | 0 .../package.json | 5 +++++ test/wiredep_test.js | 20 ++++++++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 test/fixture/bower_components/fake-package-only-has-package-json/fake.js create mode 100644 test/fixture/bower_components/fake-package-only-has-package-json/package.json diff --git a/test/fixture/bower_components/fake-package-only-has-package-json/fake.js b/test/fixture/bower_components/fake-package-only-has-package-json/fake.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixture/bower_components/fake-package-only-has-package-json/package.json b/test/fixture/bower_components/fake-package-only-has-package-json/package.json new file mode 100644 index 0000000..f64a8c8 --- /dev/null +++ b/test/fixture/bower_components/fake-package-only-has-package-json/package.json @@ -0,0 +1,5 @@ +{ + "name": "fake-package-only-has-package-json", + "version": "1.0.0", + "main": "fake.js" +} diff --git a/test/wiredep_test.js b/test/wiredep_test.js index 7ab11a6..7ec22a9 100644 --- a/test/wiredep_test.js +++ b/test/wiredep_test.js @@ -1,5 +1,5 @@ /*jshint latedef:false */ -/*global after, describe, it, before, beforeEach */ +/*global after, describe, it, before, beforeEach, afterEach*/ 'use strict'; @@ -29,10 +29,10 @@ require.uncache = function (moduleName) { describe('wiredep', function () { beforeEach(function () { wiredep = require('../wiredep'); - }) + }); afterEach(function () { require.uncache('../wiredep'); - }) + }); before(function() { fs.copySync('test/fixture', '.tmp'); process.chdir('.tmp'); @@ -470,6 +470,20 @@ describe('wiredep', function () { assert.equal(filePaths.read('actual'), filePaths.read('expected')); }); + it('should read main field from package.json when main is missing from bower.json .bower.json',function(){ + var bowerObj = { + name: 'test', + dependencies: { + 'fake-package-only-has-package-json': '*' + } + }; + var obj = wiredep({ + bowerJson: bowerObj + }); + assert.isTrue(obj.packages['fake-package-only-has-package-json'].main.length > 0); + assert.isTrue(obj.packages['fake-package-only-has-package-json'].main[0].indexOf('fake.js') > -1); + }); + it('should support inclusion of main files from some other dir with manually loaded bower.json', function () { var filePaths = getFilePaths('index-cwd-include-self', 'html');