Skip to content

Commit

Permalink
fix: handle package.json used to specify module types being in used
Browse files Browse the repository at this point in the history
Close #550
  • Loading branch information
mjeanroy committed May 15, 2020
1 parent a8d9d2e commit f0efb4f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
31 changes: 20 additions & 11 deletions src/license-plugin.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
Expand Down
25 changes: 25 additions & 0 deletions 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');
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/fake-package-5/esm/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
15 changes: 15 additions & 0 deletions 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 <mickael.jeanroy@gmail.com>",
"license": "MIT",
"private": true,
"dependencies": {
"lodash": "*"
}
}
25 changes: 24 additions & 1 deletion test/license-plugin.spec.js
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 <mickael.jeanroy@gmail.com> (https://mjeanroy.com)',
Expand Down

0 comments on commit f0efb4f

Please sign in to comment.