Skip to content

Commit

Permalink
Add referenced files to bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jul 5, 2020
1 parent 8339c37 commit 23b4a6c
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 24 deletions.
3 changes: 3 additions & 0 deletions docs/02-javascript-api.md
Expand Up @@ -44,9 +44,11 @@ async function build() {
// exports: string[], // exported variable names
// facadeModuleId: string | null, // the id of a module that this chunk corresponds to
// fileName: string, // the chunk file name
// implicitlyLoadedBefore: string[]; // entries that should only be loaded after this chunk
// imports: string[], // external modules imported statically by the chunk
// isDynamicEntry: boolean, // is this chunk a dynamic entry point
// isEntry: boolean, // is this chunk a static entry point
// isImplicitEntry: boolean, // should this chunk only be loaded after other chunks
// map: string | null, // sourcemaps if present
// modules: { // information about the modules in this chunk
// [id: string]: {
Expand All @@ -57,6 +59,7 @@ async function build() {
// };
// },
// name: string // the name of this chunk as used in naming patterns
// referencedFiles: string[] // files referenced via import.meta.ROLLUP_FILE_URL_<id>
// type: 'chunk', // signifies that this is a chunk
// }
console.log('Chunk', chunkOrAsset.modules);
Expand Down
1 change: 1 addition & 0 deletions docs/05-plugin-development.md
Expand Up @@ -308,6 +308,7 @@ Called at the end of `bundle.generate()` or immediately before the files are wri
},
},
name: string,
referencedFiles: string[],
type: 'chunk',
}
```
Expand Down
1 change: 1 addition & 0 deletions src/Bundle.ts
Expand Up @@ -86,6 +86,7 @@ export default class Bundle {
chunk.id!
] = chunk.getPrerenderedChunk() as OutputChunk);
chunkDescription.fileName = chunk.id!;
chunkDescription.referencedFiles = chunk.getReferencedFiles(this.pluginDriver);
}
await Promise.all(
chunks.map(chunk => {
Expand Down
13 changes: 13 additions & 0 deletions src/Chunk.ts
Expand Up @@ -468,6 +468,19 @@ export default class Chunk {
};
}

getReferencedFiles(outputPluginDriver: PluginDriver): string[] {
const referencedFiles: string[] = [];
for (const module of this.orderedModules) {
for (const meta of module.importMetas) {
const fileName = meta.getReferencedFileName(outputPluginDriver);
if (fileName) {
referencedFiles.push(fileName);
}
}
}
return referencedFiles;
}

getRenderedHash(outputPluginDriver: PluginDriver): string {
if (this.renderedHash) return this.renderedHash;
const hash = createHash();
Expand Down
46 changes: 25 additions & 21 deletions src/ast/nodes/MetaProperty.ts
Expand Up @@ -20,6 +20,14 @@ export default class MetaProperty extends NodeBase {

private metaProperty?: string | null;

getReferencedFileName(outputPluginDriver: PluginDriver): string | null {
const metaProperty = this.metaProperty as string | null;
if (metaProperty && metaProperty.startsWith(FILE_PREFIX)) {
return outputPluginDriver.getFileName(metaProperty.substr(FILE_PREFIX.length));
}
return null;
}

hasEffects(): boolean {
return false;
}
Expand All @@ -31,37 +39,33 @@ export default class MetaProperty extends NodeBase {
include() {
if (!this.included) {
this.included = true;
const parent = this.parent;
const metaProperty = (this.metaProperty =
parent instanceof MemberExpression && typeof parent.propertyKey === 'string'
? parent.propertyKey
: null);
if (
metaProperty &&
(metaProperty.startsWith(FILE_PREFIX) ||
metaProperty.startsWith(ASSET_PREFIX) ||
metaProperty.startsWith(CHUNK_PREFIX))
) {
this.scope.addAccessedGlobalsByFormat(accessedFileUrlGlobals);
} else {
this.scope.addAccessedGlobalsByFormat(accessedMetaUrlGlobals);
if (this.meta.name === 'import') {
this.context.addImportMeta(this);
const parent = this.parent;
const metaProperty = (this.metaProperty =
parent instanceof MemberExpression && typeof parent.propertyKey === 'string'
? parent.propertyKey
: null);
if (
metaProperty &&
(metaProperty.startsWith(FILE_PREFIX) ||
metaProperty.startsWith(ASSET_PREFIX) ||
metaProperty.startsWith(CHUNK_PREFIX))
) {
this.scope.addAccessedGlobalsByFormat(accessedFileUrlGlobals);
} else {
this.scope.addAccessedGlobalsByFormat(accessedMetaUrlGlobals);
}
}
}
}

initialise() {
if (this.meta.name === 'import') {
this.context.addImportMeta(this);
}
}

renderFinalMechanism(
code: MagicString,
chunkId: string,
format: InternalModuleFormat,
outputPluginDriver: PluginDriver
): void {
if (!this.included) return;
const parent = this.parent;
const metaProperty = this.metaProperty as string | null;

Expand Down
2 changes: 2 additions & 0 deletions src/rollup/types.d.ts
Expand Up @@ -673,11 +673,13 @@ export interface PreRenderedChunk {
[id: string]: RenderedModule;
};
name: string;
referencedFiles?: string[];
type: 'chunk';
}

export interface RenderedChunk extends PreRenderedChunk {
fileName: string;
referencedFiles: string[];
}

export interface OutputChunk extends RenderedChunk {
Expand Down

This file was deleted.

This file was deleted.

@@ -0,0 +1,39 @@
const assert = require('assert');

module.exports = {
description: 'lists referenced files in the bundle',
options: {
input: 'main',
plugins: {
transform() {
return `export const asset = import.meta.ROLLUP_FILE_URL_${this.emitFile({
type: 'asset',
name: 'asset.txt',
source: 'asset'
})};\nexport const chunk = import.meta.ROLLUP_FILE_URL_${this.emitFile({
type: 'chunk',
id: 'ref.js'
})}`;
},
generateBundle(options, bundle) {
assert.deepStrictEqual(bundle['main.js'].referencedFiles, [
'assets/asset.txt',
'chunks/ref.js'
]);
}
},
output: {
assetFileNames: 'assets/[name][extname]',
chunkFileNames: 'chunks/[name].js'
}
},
context: {
__dirname: 'dir'
},
exports(exports) {
assert.deepStrictEqual(exports, {
asset: 'file:///dir/assets/asset.txt',
chunk: 'file:///dir/chunks/ref.js'
});
}
};
@@ -0,0 +1 @@
throw new Error('not executed');
@@ -0,0 +1 @@
console.log('Hello');
2 changes: 1 addition & 1 deletion test/watch/index.js
Expand Up @@ -1243,7 +1243,7 @@ describe('rollup.watch', () => {
'END',
() => {
assert.strictEqual(run('../_tmp/output/bundle.js'), 42);
assert.strictEqual(watchChangeIds.size, 0);
assert.deepStrictEqual([...watchChangeIds], []);
for (const file of watchFiles) sander.writeFileSync(file, 'changed');
},
'START',
Expand Down

0 comments on commit 23b4a6c

Please sign in to comment.