Skip to content

Commit

Permalink
fix: exclude tree shaken modules from dependencies to scan
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Jun 11, 2019
1 parent 4a7c65c commit c62af9c
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 17 deletions.
21 changes: 10 additions & 11 deletions src/index-rollup-stable.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

'use strict';

const _ = require('lodash');
const LicensePlugin = require('./license-plugin.js');

module.exports = (options = {}) => {
Expand All @@ -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.
Expand All @@ -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);
},

Expand Down
18 changes: 16 additions & 2 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
82 changes: 78 additions & 4 deletions test/index-rollup-stable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -57,9 +57,68 @@ 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.renderChunk(code, chunk, outputOptions);
instance.generateBundle();

fs.readFile(thirdPartyOutput, 'utf8', (err, data) => {
if (err) {
done.fail(err);
}

const content = data.toString();
expect(content).toBeDefined();
expect(content).toContain('fake-package');
done();
});
});

it('should scan dependencies when chunk is rendered and skip tree-shaken modules', (done) => {
const thirdPartyOutput = path.join(tmpDir.name, 'dependencies.txt');
const instance = plugin({
thirdParty: {
includePrivate: true,
output: thirdPartyOutput,
},
});

instance.load(id);
const moduleId1 = path.join(__dirname, 'fixtures', 'fake-package', 'src', 'index.js');
const moduleId2 = path.join(__dirname, '..', 'node_modules', 'lodash', 'index.js');

const modules = {
[moduleId1]: {
renderedExports: [],
removedExports: [],
renderedLength: 10,
originalLength: 100,
},

[moduleId2]: {
renderedExports: [],
removedExports: [],
renderedLength: 0,
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) => {
Expand All @@ -70,6 +129,7 @@ describe('rollup-plugin-license [rollup stable]', () => {
const content = data.toString();
expect(content).toBeDefined();
expect(content).toContain('fake-package');
expect(content).not.toContain('lodash');
done();
});
});
Expand Down Expand Up @@ -105,7 +165,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) => {
Expand Down
29 changes: 29 additions & 0 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit c62af9c

Please sign in to comment.