diff --git a/src/index.d.ts b/src/index.d.ts index 1743a6a3..9202ff3e 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -285,6 +285,13 @@ interface ThirdPartyOptions { */ includePrivate?: boolean; + /** + * If "self" should be checked and included in the output. + * In this context, "self" means the package being built. + * @default false + */ + includeSelf?: boolean; + /** * Ensures that dependencies does not violate any license restriction. * diff --git a/src/license-plugin-option.js b/src/license-plugin-option.js index 418c0498..8890f330 100644 --- a/src/license-plugin-option.js +++ b/src/license-plugin-option.js @@ -62,6 +62,7 @@ const SCHEMA = { validators.func(), validators.object({ includePrivate: validators.boolean(), + includeSelf: validators.boolean(), multipleVersions: validators.boolean(), allow: [ diff --git a/src/license-plugin.js b/src/license-plugin.js index f5d24071..e5ec30f2 100644 --- a/src/license-plugin.js +++ b/src/license-plugin.js @@ -154,11 +154,17 @@ class LicensePlugin { let dir = path.resolve(path.parse(id).dir); let pkg = null; + const includeSelf = !!this._options.thirdParty?.includeSelf; const scannedDirs = new Set(); this.debug(`iterative over directory tree, starting with: ${dir}`); - while (dir && dir !== this._cwd && !scannedDirs.has(dir)) { + while (dir) { + if (!includeSelf && dir === this._cwd) { + // No need to scan "self" if it's not explicitly allowed. + break; + } + // Try the cache. if (this._cache.has(dir)) { pkg = this._cache.get(dir); @@ -215,8 +221,18 @@ class LicensePlugin { } } + if (dir === this._cwd) { + // If "self" has been scanned, no need to go up in the directory tree. + break; + } + // Go up in the directory tree. dir = path.resolve(path.join(dir, '..')); + + if (!dir || scannedDirs.has(dir)) { + break; + } + this.debug(`going up in the directory tree: ${dir}`); } diff --git a/test/integration/it.spec.js b/test/integration/it.spec.js index 1aa4effd..0f5b5992 100644 --- a/test/integration/it.spec.js +++ b/test/integration/it.spec.js @@ -62,6 +62,7 @@ describe('rollup-plugin-license', () => { writeBundle(rollupConfig).then(() => { verifyFile(thirdPartyOutput, done, (data) => { expect(data.toString()).toContain('lodash'); + expect(data.toString()).not.toContain('rollup-plugin-license'); }); }); }); @@ -304,6 +305,26 @@ describe('rollup-plugin-license', () => { }); }); + it('should include self dependency', (done) => { + const thirdPartyOutput = path.join(tmpDir.name, 'dependencies.txt'); + const rollupConfig = createRollupConfig({ + thirdParty: { + includeSelf: true, + output: { + file: thirdPartyOutput, + }, + }, + }); + + writeBundle(rollupConfig).then(() => { + verifyFile(thirdPartyOutput, done, (data) => { + expect(warn).not.toHaveBeenCalled(); + expect(data.toString()).toContain('rollup-plugin-license'); + expect(data.toString()).toContain('lodash'); + }); + }); + }); + function createRollupConfig(licensePluginOptions) { return { input: path.join(__dirname, 'bundle.js'), diff --git a/test/license-plugin.spec.js b/test/license-plugin.spec.js index 04d20a0b..30bc962c 100644 --- a/test/license-plugin.spec.js +++ b/test/license-plugin.spec.js @@ -324,6 +324,20 @@ describe('LicensePlugin', () => { expect(plugin._dependencies.size).toBe(0); }); + it('should load pkg and include self dependency if specified', () => { + const id = path.join(__dirname, '..', 'src', 'index.js'); + + plugin._options.thirdParty = { + includeSelf: true, + }; + + plugin.scanDependency(id); + + expect(addDependency).toHaveBeenCalled(); + expect(plugin._dependencies.size).toBe(1); + expect(plugin._dependencies.has('rollup-plugin-license')).toBe(true); + }); + it('should load pkg and update cache', () => { const pkgPath = path.join(__dirname, 'fixtures', 'fake-package-1'); const id = path.join(pkgPath, 'src', 'index.js');