From 0bcc7f17f1cb9fc34f4c7b375f5795b1ef3efb29 Mon Sep 17 00:00:00 2001 From: Mickael Jeanroy Date: Mon, 31 Oct 2022 09:45:23 +0100 Subject: [PATCH] fix: ignore and skip virtual modules --- package.json | 1 + src/license-plugin.js | 16 +++++++++++++- test/integration/bundle-virtual.js | 30 ++++++++++++++++++++++++++ test/integration/it.spec.js | 34 ++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 test/integration/bundle-virtual.js diff --git a/package.json b/package.json index bc51f530..7e521454 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/license-plugin.js b/src/license-plugin.js index a9a1e189..a668201e 100644 --- a/src/license-plugin.js +++ b/src/license-plugin.js @@ -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 @@ -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)) { @@ -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) { @@ -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 diff --git a/test/integration/bundle-virtual.js b/test/integration/bundle-virtual.js new file mode 100644 index 00000000..40a01c6e --- /dev/null +++ b/test/integration/bundle-virtual.js @@ -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); +} diff --git a/test/integration/it.spec.js b/test/integration/it.spec.js index 9784d7b4..a9f04114 100644 --- a/test/integration/it.spec.js +++ b/test/integration/it.spec.js @@ -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'; @@ -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};