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 5851035
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/index.d.ts
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
Expand Up @@ -62,6 +62,7 @@ const SCHEMA = {
validators.func(),
validators.object({
includePrivate: validators.boolean(),
includeSelf: validators.boolean(),
multipleVersions: validators.boolean(),

allow: [
Expand Down
13 changes: 12 additions & 1 deletion src/license-plugin.js
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 @@ -217,6 +223,11 @@ class LicensePlugin {

// 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
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
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 5851035

Please sign in to comment.