Skip to content

Commit d946aa4

Browse files
committed
fix: ignore and skip virtual modules
1 parent 0b14f37 commit d946aa4

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@rollup/plugin-babel": "6.0.2",
5454
"@rollup/plugin-commonjs": "23.0.2",
5555
"@rollup/plugin-node-resolve": "15.0.1",
56+
"@rollup/plugin-virtual": "3.0.1",
5657
"@typescript-eslint/eslint-plugin": "5.41.0",
5758
"@typescript-eslint/parser": "5.41.0",
5859
"babel-plugin-add-module-exports": "1.0.4",

src/license-plugin.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ class LicensePlugin {
143143
this.debug(`scanning internal module ${id}`);
144144
}
145145

146+
if (id.indexOf('virtual:') === 0) {
147+
this.debug(`skipping virtual module: ${id}`);
148+
return;
149+
}
150+
146151
this.debug(`scanning ${id}`);
147152

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

152157
const scannedDirs = [];
153158

159+
this.debug(`iterative over directory tree, starting with: ${dir}`);
160+
154161
while (dir && dir !== this._cwd && !scannedDirs.includes(dir)) {
155162
// Try the cache.
156163
if (_.has(this._cache, dir)) {
@@ -165,6 +172,7 @@ class LicensePlugin {
165172

166173
scannedDirs.push(dir);
167174

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

209217
// Go up in the directory tree.
210-
dir = path.normalize(path.join(dir, '..'));
218+
const newDir = path.normalize(path.join(dir, '..'));
219+
if (newDir === '.') {
220+
break;
221+
}
222+
223+
this.debug(`going up in the directory tree: ${newDir}`);
224+
dir = newDir;
211225
}
212226

213227
// Update the cache

test/integration/bundle-virtual.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016-2022 Mickael Jeanroy
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
import reduce from 'lodash/reduce';
26+
27+
// eslint-disable-next-line require-jsdoc
28+
export function sum(array) {
29+
return reduce(array, (acc, x) => acc + x, 0);
30+
}

test/integration/it.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import _ from 'lodash';
2929
import * as rollup from 'rollup';
3030
import nodeResolve from '@rollup/plugin-node-resolve';
3131
import commonjs from '@rollup/plugin-commonjs';
32+
import virtual from '@rollup/plugin-virtual';
3233
import licensePlugin from '../../src/index.js';
3334
import {join} from '../utils/join.js';
3435

@@ -181,6 +182,39 @@ describe('rollup-plugin-license', () => {
181182
});
182183
});
183184

185+
it('should generate bundle with license header ignoring virtual modules', (done) => {
186+
const banner = 'test banner';
187+
const rollupConfig = {
188+
input: path.join(__dirname, 'bundle-virtual.js'),
189+
190+
output: {
191+
file: path.join(tmpDir.name, 'bundle-virtual.js'),
192+
format: 'es',
193+
},
194+
195+
plugins: [
196+
virtual({
197+
'lodash/reduce': `
198+
export default () => {};
199+
`,
200+
}),
201+
licensePlugin({
202+
banner,
203+
}),
204+
],
205+
};
206+
207+
writeBundle(rollupConfig).then(() => {
208+
verifyFile(rollupConfig.output.file, done, (data) => {
209+
expect(data.toString()).toContain(join([
210+
`/**`,
211+
` * ${banner}`,
212+
` */`,
213+
]));
214+
});
215+
});
216+
});
217+
184218
it('should generate bundle with license header from content as a raw string', (done) => {
185219
const content = 'Banner from inline content';
186220
const banner = {content};

0 commit comments

Comments
 (0)