Skip to content

Commit

Permalink
feat: optionally include self dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Mar 11, 2024
1 parent 16e4d5d commit 66a2963
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/license-plugin-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const SCHEMA = {
validators.func(),
validators.object({
includePrivate: validators.boolean(),
includeSelf: validators.boolean(),
multipleVersions: validators.boolean(),

allow: [
Expand Down
18 changes: 17 additions & 1 deletion src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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}`);
}

Expand Down
21 changes: 21 additions & 0 deletions test/integration/it.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Expand Down Expand Up @@ -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'),
Expand Down
14 changes: 14 additions & 0 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 66a2963

Please sign in to comment.