Skip to content

Commit

Permalink
fix: ignore and skip virtual modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Oct 31, 2022
1 parent bdfd236 commit 0bcc7f1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@rollup/plugin-babel": "6.0.2",
"@rollup/plugin-commonjs": "23.0.2",
"@rollup/plugin-node-resolve": "15.0.1",
"@rollup/plugin-virtual": "3.0.1",
"@typescript-eslint/eslint-plugin": "5.41.0",
"@typescript-eslint/parser": "5.41.0",
"babel-plugin-add-module-exports": "1.0.4",
Expand Down
16 changes: 15 additions & 1 deletion src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ class LicensePlugin {
this.debug(`scanning internal module ${id}`);
}

if (id.indexOf('virtual:') === 0) {
this.debug(`skipping virtual module: ${id}`);
return;
}

this.debug(`scanning ${id}`);

// Look for the `package.json` file
Expand All @@ -151,6 +156,8 @@ class LicensePlugin {

const scannedDirs = [];

this.debug(`iterative over directory tree, starting with: ${dir}`);

while (dir && dir !== this._cwd && !scannedDirs.includes(dir)) {
// Try the cache.
if (_.has(this._cache, dir)) {
Expand All @@ -165,6 +172,7 @@ class LicensePlugin {

scannedDirs.push(dir);

this.debug(`looking for package.json file in: ${dir}`);
const pkgPath = path.join(dir, 'package.json');
const exists = fs.existsSync(pkgPath);
if (exists) {
Expand Down Expand Up @@ -207,7 +215,13 @@ class LicensePlugin {
}

// Go up in the directory tree.
dir = path.normalize(path.join(dir, '..'));
const newDir = path.normalize(path.join(dir, '..'));
if (newDir === '.') {
break;
}

this.debug(`going up in the directory tree: ${newDir}`);
dir = newDir;
}

// Update the cache
Expand Down
30 changes: 30 additions & 0 deletions test/integration/bundle-virtual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 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.
*/

import reduce from 'lodash/reduce';

// eslint-disable-next-line require-jsdoc
export function sum(array) {
return reduce(array, (acc, x) => acc + x, 0);
}
34 changes: 34 additions & 0 deletions test/integration/it.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import _ from 'lodash';
import * as rollup from 'rollup';
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import virtual from '@rollup/plugin-virtual';
import licensePlugin from '../../src/index.js';
import {join} from '../utils/join.js';

Expand Down Expand Up @@ -181,6 +182,39 @@ describe('rollup-plugin-license', () => {
});
});

it('should generate bundle with license header ignoring virtual modules', (done) => {
const banner = 'test banner';
const rollupConfig = {
input: path.join(__dirname, 'bundle-virtual.js'),

output: {
file: path.join(tmpDir.name, 'bundle-virtual.js'),
format: 'es',
},

plugins: [
virtual({
'lodash/reduce': `
export default () => {};
`,
}),
licensePlugin({
banner,
}),
],
};

writeBundle(rollupConfig).then(() => {
verifyFile(rollupConfig.output.file, done, (data) => {
expect(data.toString()).toContain(join([
`/**`,
` * ${banner}`,
` */`,
]));
});
});
});

it('should generate bundle with license header from content as a raw string', (done) => {
const content = 'Banner from inline content';
const banner = {content};
Expand Down

0 comments on commit 0bcc7f1

Please sign in to comment.