From e7500033924e98a64c98f6e8637feca0c74a0262 Mon Sep 17 00:00:00 2001 From: Mickael Jeanroy Date: Tue, 11 Jun 2019 12:49:04 +0200 Subject: [PATCH] fix: exclude tree shaken modules from dependencies to scan --- src/index-rollup-stable.js | 21 ++++++++++---------- src/license-plugin.js | 18 +++++++++++++++-- test/index-rollup-stable.spec.js | 34 ++++++++++++++++++++++++++++---- test/license-plugin.spec.js | 29 +++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 17 deletions(-) diff --git a/src/index-rollup-stable.js b/src/index-rollup-stable.js index 77415cf8..352a5dcf 100644 --- a/src/index-rollup-stable.js +++ b/src/index-rollup-stable.js @@ -24,6 +24,7 @@ 'use strict'; +const _ = require('lodash'); const LicensePlugin = require('./license-plugin.js'); module.exports = (options = {}) => { @@ -36,17 +37,6 @@ module.exports = (options = {}) => { */ name: plugin.name, - /** - * Function called by rollup when a JS file is loaded: it is used to scan - * third-party dependencies. - * - * @param {string} id JS file path. - * @return {void} - */ - load(id) { - plugin.scanDependency(id); - }, - /** * Function called by rollup when the final bundle is generated: it is used * to prepend the banner file on the generated bundle. @@ -57,6 +47,15 @@ module.exports = (options = {}) => { * @return {void} */ renderChunk(code, chunk, outputOptions = {}) { + plugin.scanDependencies( + _.chain(chunk.modules) + .toPairs() + .reject((mod) => mod[1].isAsset) + .filter((mod) => mod[1].renderedLength > 0) + .map((mod) => mod[0]) + .value() + ); + return plugin.prependBanner(code, outputOptions.sourcemap !== false); }, diff --git a/src/license-plugin.js b/src/license-plugin.js index fffda227..333b5200 100644 --- a/src/license-plugin.js +++ b/src/license-plugin.js @@ -153,10 +153,10 @@ module.exports = class LicensePlugin { if (id.startsWith('\0')) { id = id.replace(/^\0/, ''); this.debug(`scanning internal module ${id}`); - } else { - this.debug(`scanning ${id}`); } + this.debug(`scanning ${id}`); + // Look for the `package.json` file let dir = path.parse(id).dir; let pkg = null; @@ -202,6 +202,20 @@ module.exports = class LicensePlugin { }); } + /** + * Hook triggered by `rollup` to load code from given path file. + * + * @param {Object} dependencies List of modules included in the final bundle. + * @return {void} + */ + scanDependencies(dependencies) { + this.debug(`Scanning: ${dependencies}`); + + _.forEach(dependencies, (dependency) => { + this.scanDependency(dependency); + }); + } + /** * Hook triggered by `rollup` to transform the final generated bundle. * This hook is used here to prepend the license banner to the final bundle. diff --git a/test/index-rollup-stable.spec.js b/test/index-rollup-stable.spec.js index f587e268..9de6d2fc 100644 --- a/test/index-rollup-stable.spec.js +++ b/test/index-rollup-stable.spec.js @@ -48,7 +48,7 @@ describe('rollup-plugin-license [rollup stable]', () => { expect(instance.name).toBe('rollup-plugin-license'); }); - it('should scan dependency on load', (done) => { + it('should scan dependencies when chunk is rendered', (done) => { const thirdPartyOutput = path.join(tmpDir.name, 'dependencies.txt'); const instance = plugin({ thirdParty: { @@ -57,9 +57,21 @@ describe('rollup-plugin-license [rollup stable]', () => { }, }); - const id = path.join(__dirname, 'fixtures', 'fake-package', 'src', 'index.js'); + const moduleId = path.join(__dirname, 'fixtures', 'fake-package', 'src', 'index.js'); + const modules = { + [moduleId]: { + renderedExports: [], + removedExports: [], + renderedLength: 10, + originalLength: 100, + }, + }; + + const code = 'var foo = 0;'; + const chunk = {code, modules}; + const outputOptions = {}; - instance.load(id); + instance.renderChunk(code, chunk, outputOptions); instance.generateBundle(); fs.readFile(thirdPartyOutput, 'utf8', (err, data) => { @@ -105,7 +117,21 @@ describe('rollup-plugin-license [rollup stable]', () => { }, }); - instance.load('lodash'); + const moduleId = path.join(__dirname, 'fixtures', 'fake-package', 'src', 'index.js'); + const modules = { + [moduleId]: { + renderedExports: [], + removedExports: [], + renderedLength: 10, + originalLength: 100, + }, + }; + + const code = 'var foo = 0;'; + const chunk = {code, modules}; + const outputOptions = {}; + + instance.renderChunk(code, chunk, outputOptions); instance.generateBundle(); fs.readFile(thirdPartyOutput, 'utf8', (err, data) => { diff --git a/test/license-plugin.spec.js b/test/license-plugin.spec.js index 15d5a37b..ab9fb3ad 100644 --- a/test/license-plugin.spec.js +++ b/test/license-plugin.spec.js @@ -130,6 +130,35 @@ describe('LicensePlugin', () => { }); }); + it('should load pkg dependencies', () => { + const plugin = new LicensePlugin(); + const modules = [ + path.join(__dirname, 'fixtures', 'fake-package', 'src', 'index.js'), + ]; + + spyOn(plugin, 'scanDependency').and.callThrough(); + spyOn(plugin, 'addDependency').and.callThrough(); + + const result = plugin.scanDependencies(modules); + + expect(result).not.toBeDefined(); + expect(plugin.scanDependency).toHaveBeenCalledWith(modules[0]); + expect(plugin.addDependency).toHaveBeenCalled(); + expect(plugin._dependencies).toEqual({ + 'fake-package': { + name: 'fake-package', + version: '1.0.0', + description: 'Fake package used in unit tests', + license: 'MIT', + private: true, + author: { + name: 'Mickael Jeanroy', + email: 'mickael.jeanroy@gmail.com', + }, + }, + }); + }); + it('should load pkg and stop on cwd', () => { const plugin = new LicensePlugin(); const id = path.join(__dirname, '..', 'src', 'index.js');