Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose rendered module code to generateBundle hook #4028

Merged
merged 4 commits into from Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/02-javascript-api.md
@@ -1,3 +1,4 @@

---
title: JavaScript API
---
Expand Down Expand Up @@ -59,6 +60,7 @@ async function build() {
// removedExports: string[]; // exported variable names that were removed
// renderedLength: number; // the length of the remaining code in this module
// originalLength: number; // the original length of the code in this module
// code: string | null; // remaining code in this module
// };
// },
// name: string // the name of this chunk as used in naming patterns
Expand Down
3 changes: 2 additions & 1 deletion docs/05-plugin-development.md
Expand Up @@ -339,7 +339,8 @@ Called at the end of `bundle.generate()` or immediately before the files are wri
renderedExports: string[],
removedExports: string[],
renderedLength: number,
originalLength: number
originalLength: number,
code: string | null
},
},
name: string,
Expand Down
6 changes: 5 additions & 1 deletion src/Chunk.ts
Expand Up @@ -611,11 +611,15 @@ export default class Chunk {
}
}
const { renderedExports, removedExports } = module.getRenderedExports();
const chunk = this;
renderedModules[module.id] = {
originalLength: module.originalCode.length,
removedExports,
renderedExports,
renderedLength
renderedLength,
get code() {
return chunk.renderedModuleSources.get(module)?.toString() ?? null;
}
};
}

Expand Down
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Expand Up @@ -727,6 +727,7 @@ export interface RenderedModule {
removedExports: string[];
renderedExports: string[];
renderedLength: number;
code: string | null;
}

export interface PreRenderedChunk {
Expand Down
13 changes: 13 additions & 0 deletions test/misc/bundle-information.js
Expand Up @@ -89,6 +89,7 @@ describe('The bundle object', () => {
[
{
input1: {
code: 'console.log("input1", used, shared);const out = true;',
originalLength: 96,
removedExports: [],
renderedExports: ['out'],
Expand All @@ -97,6 +98,7 @@ describe('The bundle object', () => {
},
{
input2: {
code: 'console.log("input2");var input2 = 42;',
originalLength: 55,
removedExports: [],
renderedExports: ['default'],
Expand All @@ -105,6 +107,7 @@ describe('The bundle object', () => {
},
{
shared: {
code: 'console.log("shared");const used = "used"; var shared = "stuff";',
originalLength: 100,
removedExports: ['unused'],
renderedExports: ['used', 'default'],
Expand Down Expand Up @@ -194,6 +197,7 @@ describe('The bundle object', () => {
[
{
input: {
code: 'console.log(external2);',
originalLength: 137,
removedExports: [],
renderedExports: [],
Expand Down Expand Up @@ -438,6 +442,7 @@ describe('The bundle object', () => {
[
{
input: {
code: 'var input = null;',
originalLength: 47,
removedExports: [],
renderedExports: ['default'],
Expand Down Expand Up @@ -525,6 +530,7 @@ console.log(other);Promise.all([import('./dynamic1'), import('./dynamic2')]).the
[
{
input: {
code: 'console.log(other);Promise.all([import(\'./dynamic1\'), import(\'./dynamic2\')]).then(([{dynamic1}, {dynamic2}]) => console.log(dynamic1, dynamic2));',
originalLength: 169,
removedExports: [],
renderedExports: [],
Expand All @@ -533,6 +539,7 @@ console.log(other);Promise.all([import('./dynamic1'), import('./dynamic2')]).the
},
{
dynamic1: {
code: 'const dynamic1 = "dynamic1";',
originalLength: 34,
removedExports: [],
renderedExports: ['dynamic1'],
Expand All @@ -541,6 +548,7 @@ console.log(other);Promise.all([import('./dynamic1'), import('./dynamic2')]).the
},
{
other: {
code: 'const other = "other";',
originalLength: 28,
removedExports: [],
renderedExports: ['other'],
Expand All @@ -549,6 +557,7 @@ console.log(other);Promise.all([import('./dynamic1'), import('./dynamic2')]).the
},
{
dynamic2: {
code: 'const dynamic2 = "dynamic2";',
originalLength: 34,
removedExports: [],
renderedExports: ['dynamic2'],
Expand Down Expand Up @@ -606,12 +615,14 @@ console.log(other);Promise.all([import('./dynamic1'), import('./dynamic2')]).the
output.modules,
{
code: {
code: 'function renderedFn() {}\nclass renderedClass {}\nconst renderedConst = 1;',
originalLength: 184,
removedExports: ['removedFn', 'removedClass', 'removedConst'],
renderedExports: ['renderedFn', 'renderedClass', 'renderedConst'],
renderedLength: 72
},
input: {
code: null,
originalLength: 84,
removedExports: [],
renderedExports: [],
Expand Down Expand Up @@ -658,12 +669,14 @@ console.log(other);Promise.all([import('./dynamic1'), import('./dynamic2')]).the
output.modules,
{
code: {
code: 'function renderedFn() {}\nclass renderedClass {}\nconst renderedConst = 1;',
originalLength: 233,
removedExports: ['removedFn', 'removedClass', 'removedConst'],
renderedExports: ['renderedFn', 'renderedClass', 'renderedConst'],
renderedLength: 72
},
input: {
code: null,
originalLength: 63,
removedExports: [],
renderedExports: [],
Expand Down