From f0efb4f699d265331898857423e35dc1a378f188 Mon Sep 17 00:00:00 2001 From: Mickael Jeanroy Date: Fri, 15 May 2020 21:30:31 +0200 Subject: [PATCH] fix: handle package.json used to specify module types being in used Close #550 --- src/license-plugin.js | 31 ++++++++++++------- test/fixtures/fake-package-5/cjs/index.js | 25 +++++++++++++++ test/fixtures/fake-package-5/esm/index.js | 0 test/fixtures/fake-package-5/esm/package.json | 3 ++ test/fixtures/fake-package-5/package.json | 15 +++++++++ test/license-plugin.spec.js | 25 ++++++++++++++- 6 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/fake-package-5/cjs/index.js create mode 100644 test/fixtures/fake-package-5/esm/index.js create mode 100644 test/fixtures/fake-package-5/esm/package.json create mode 100644 test/fixtures/fake-package-5/package.json diff --git a/src/license-plugin.js b/src/license-plugin.js index ba4851de..0848ab4b 100644 --- a/src/license-plugin.js +++ b/src/license-plugin.js @@ -170,21 +170,28 @@ class LicensePlugin { this.debug(`found package.json at: ${pkgPath}, read it`); // Read `package.json` file - pkg = JSON.parse( + const pkgJson = JSON.parse( fs.readFileSync(pkgPath, 'utf-8') ); - // Read license file, if it exists. - const licenseFile = glob.sync(path.join(dir, 'LICENSE*'))[0]; - if (licenseFile) { - pkg.licenseText = fs.readFileSync(licenseFile, 'utf-8'); - } + // We are probably in a package.json specifying the type of package (module, cjs). + // Nevertheless, if the package name is not defined, we must not use this `package.json` descriptor. + if (pkgJson.name) { + // We found it! + pkg = pkgJson; - // Add the new dependency to the set of third-party dependencies. - this.addDependency(pkg); + // Read license file, if it exists. + const licenseFile = glob.sync(path.join(dir, 'LICENSE*'))[0]; + if (licenseFile) { + pkg.licenseText = fs.readFileSync(licenseFile, 'utf-8'); + } - // We can stop now. - break; + // Add the new dependency to the set of third-party dependencies. + this.addDependency(pkg); + + // We can stop now. + break; + } } // Go up in the directory tree. @@ -253,7 +260,9 @@ class LicensePlugin { */ addDependency(pkg) { const name = pkg.name; - if (!_.has(this._dependencies, name)) { + if (!name) { + this.warn('Trying to add dependency without any name, skipping it.'); + } else if (!_.has(this._dependencies, name)) { this._dependencies[name] = new Dependency(pkg); } } diff --git a/test/fixtures/fake-package-5/cjs/index.js b/test/fixtures/fake-package-5/cjs/index.js new file mode 100644 index 00000000..05935b38 --- /dev/null +++ b/test/fixtures/fake-package-5/cjs/index.js @@ -0,0 +1,25 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Mickael Jeanroy + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +console.log('fake-package'); diff --git a/test/fixtures/fake-package-5/esm/index.js b/test/fixtures/fake-package-5/esm/index.js new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/fake-package-5/esm/package.json b/test/fixtures/fake-package-5/esm/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/test/fixtures/fake-package-5/esm/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/test/fixtures/fake-package-5/package.json b/test/fixtures/fake-package-5/package.json new file mode 100644 index 00000000..9e08f39b --- /dev/null +++ b/test/fixtures/fake-package-5/package.json @@ -0,0 +1,15 @@ +{ + "name": "fake-package", + "version": "1.0.0", + "description": "Fake package used in unit tests", + "main": "src/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Mickael Jeanroy ", + "license": "MIT", + "private": true, + "dependencies": { + "lodash": "*" + } +} diff --git a/test/license-plugin.spec.js b/test/license-plugin.spec.js index 815a9777..0d76ca27 100644 --- a/test/license-plugin.spec.js +++ b/test/license-plugin.spec.js @@ -133,6 +133,17 @@ describe('LicensePlugin', () => { }); }); + it('should load pkg and going up directories until a package name is found', () => { + const id = path.join(__dirname, 'fixtures', 'fake-package-5', 'esm', 'index.js'); + const result = plugin.scanDependency(id); + + expect(result).not.toBeDefined(); + expect(addDependency).toHaveBeenCalled(); + expect(plugin._dependencies).toEqual({ + 'fake-package': fakePackage, + }); + }); + it('should load pkg including license text from LICENSE.md file', () => { const id = path.join(__dirname, 'fixtures', 'fake-package-2', 'src', 'index.js'); const result = plugin.scanDependency(id); @@ -159,7 +170,6 @@ describe('LicensePlugin', () => { }); }); - it('should load pkg including license text from LICENSE file', () => { const id = path.join(__dirname, 'fixtures', 'fake-package-4', 'src', 'index.js'); const result = plugin.scanDependency(id); @@ -259,10 +269,12 @@ describe('LicensePlugin', () => { }); describe('when adding dependencies', () => { + let warn; let plugin; let pkg; beforeEach(() => { + warn = spyOn(console, 'warn'); plugin = licensePlugin(); pkg = { name: 'foo', @@ -324,6 +336,17 @@ describe('LicensePlugin', () => { }); }); + it('should warn when adding dependency without name', () => { + plugin.addDependency({ + type: 'module', + }); + + expect(plugin._dependencies).toEqual({}); + expect(warn).toHaveBeenCalledWith( + '[rollup-plugin-license] -- Trying to add dependency without any name, skipping it.' + ); + }); + it('should add dependency and parse author field', () => { const dependency = Object.assign(pkg, { author: 'Mickael Jeanroy (https://mjeanroy.com)',