Skip to content

Commit

Permalink
feat: OutputBundle Tagged union with 'type = chunk|asset'
Browse files Browse the repository at this point in the history
  • Loading branch information
askbeka committed Aug 24, 2019
1 parent a7e5ff2 commit 7b47ad0
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 25 deletions.
1 change: 1 addition & 0 deletions .eslintignore
@@ -1,3 +1,4 @@
test/*/samples/**/*.js
!test/*/samples/**/_config.js
test/leak/index.js
**/*.ts
10 changes: 5 additions & 5 deletions cli/run/build.ts
@@ -1,7 +1,7 @@
import ms from 'pretty-ms';
import tc from 'turbocolor';
import * as rollup from '../../src/node-entry';
import { InputOptions, OutputAsset, OutputChunk, OutputOptions, RollupBuild, SourceMap } from '../../src/rollup/types';
import { InputOptions, OutputOptions, RollupBuild, SourceMap } from '../../src/rollup/types';
import relativeId from '../../src/utils/relativeId';
import { handleError, stderr } from '../logging';
import SOURCEMAPPING_URL from '../sourceMappingUrl';
Expand Down Expand Up @@ -49,12 +49,12 @@ export default function build(
return bundle.generate(output).then(({ output: outputs }) => {
for (const file of outputs) {
let source: string | Buffer;
if ((file as OutputAsset).isAsset) {
source = (file as OutputAsset).source;
if (file.type === 'asset') {
source = file.source;
} else {
source = (file as OutputChunk).code;
source = file.code;
if (output.sourcemap === 'inline') {
source += `\n//# ${SOURCEMAPPING_URL}=${((file as OutputChunk)
source += `\n//# ${SOURCEMAPPING_URL}=${(file
.map as SourceMap).toUrl()}\n`;
}
}
Expand Down
6 changes: 4 additions & 2 deletions docs/02-javascript-api.md
Expand Up @@ -25,12 +25,13 @@ async function build() {
const { output } = await bundle.generate(outputOptions);

for (const chunkOrAsset of output) {
if (chunkOrAsset.isAsset) {
if (chunkOrAsset.type === 'asset') {
// For assets, this contains
// {
// isAsset: true, // signifies that this is an asset
// isAsset: true, // signifies that this is an asset, !deprecated use type === asset instead
// fileName: string, // the asset file name
// source: string | Buffer // the asset source
// type: 'asset'
// }
console.log('Asset', chunkOrAsset);
} else {
Expand All @@ -54,6 +55,7 @@ async function build() {
// };
// },
// name: string // the name of this chunk as used in naming patterns
// type: 'chunk',
// }
console.log('Chunk', chunkOrAsset.modules);
}
Expand Down
8 changes: 5 additions & 3 deletions docs/05-plugin-development.md
Expand Up @@ -123,8 +123,9 @@ Called at the end of `bundle.generate()` or immediately before the files are wri
// AssetInfo
{
fileName: string,
isAsset: true,
source: string | Buffer
isAsset: true, // deprecated
source: string | Buffer,
type: 'asset',
}
// ChunkInfo
Expand All @@ -146,7 +147,8 @@ Called at the end of `bundle.generate()` or immediately before the files are wri
originalLength: number
},
},
name: string
name: string,
type: 'chunk',
}
```

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/rollup/index.ts
Expand Up @@ -166,6 +166,7 @@ function assignChunksToBundle(
isEntry: facadeModule !== null && facadeModule.isEntryPoint,
map: undefined,
modules: chunk.renderedModules,
type: 'chunk',
get name() {
return chunk.getChunkName();
}
Expand Down Expand Up @@ -301,7 +302,7 @@ export default async function rollup(rawInputOptions: GenericConfigObject): Prom
let chunkCnt = 0;
for (const fileName of Object.keys(bundle)) {
const file = bundle[fileName];
if ((file as OutputAsset).isAsset) continue;
if (file.type === 'asset') continue;
chunkCnt++;
if (chunkCnt > 1) break;
}
Expand Down Expand Up @@ -343,10 +344,10 @@ enum SortingFileType {
}

function getSortingFileType(file: OutputAsset | OutputChunk): SortingFileType {
if ((file as OutputAsset).isAsset) {
if (file.type === 'asset') {
return SortingFileType.ASSET;
}
if ((file as OutputChunk).isEntry) {
if (file.isEntry) {
return SortingFileType.ENTRY_CHUNK;
}
return SortingFileType.SECONDARY_CHUNK;
Expand All @@ -368,7 +369,7 @@ function createOutput(outputBundle: Record<string, OutputChunk | OutputAsset | {
}

function isOutputAsset(file: OutputAsset | OutputChunk): file is OutputAsset {
return (file as OutputAsset).isAsset === true;
return file.type === 'asset';
}

function writeOutputFile(
Expand Down
11 changes: 9 additions & 2 deletions src/rollup/types.d.ts
Expand Up @@ -314,8 +314,13 @@ export interface OutputBundle {
[fileName: string]: OutputAsset | OutputChunk;
}

export interface FilePlaceholder {
isPlaceholder: true;
type: 'placeholder';
}

export interface OutputBundleWithPlaceholders {
[fileName: string]: OutputAsset | OutputChunk | {};
[fileName: string]: OutputAsset | OutputChunk | FilePlaceholder;
}

interface OnGenerateOptions extends OutputOptions {
Expand Down Expand Up @@ -487,9 +492,10 @@ export interface SerializedTimings {
}

export interface OutputAsset {
type: 'asset';
code?: undefined;
fileName: string;
isAsset: true;
isAsset: true; // deprecated
source: string | Buffer;
}

Expand Down Expand Up @@ -518,6 +524,7 @@ export interface RenderedChunk extends PreRenderedChunk {
}

export interface OutputChunk extends RenderedChunk {
type: 'chunk';
code: string;
map?: SourceMap;
}
Expand Down
18 changes: 12 additions & 6 deletions src/utils/FileEmitter.ts
Expand Up @@ -2,7 +2,7 @@ import sha256 from 'hash.js/lib/hash/sha/256';
import Chunk from '../Chunk';
import Graph from '../Graph';
import Module from '../Module';
import { OutputAsset, OutputBundleWithPlaceholders } from '../rollup/types';
import { FilePlaceholder, OutputBundleWithPlaceholders } from '../rollup/types';
import { BuildPhase } from './buildPhase';
import {
errAssetNotFinalisedForFileName,
Expand Down Expand Up @@ -78,8 +78,9 @@ interface EmittedFile {

type ConsumedFile = ConsumedChunk | ConsumedAsset;

export const FILE_PLACEHOLDER = {
isPlaceholder: true
export const FILE_PLACEHOLDER: FilePlaceholder = {
isPlaceholder: true,
type: 'placeholder'
};

function hasValidType(
Expand Down Expand Up @@ -318,17 +319,22 @@ export class FileEmitter {
// We must not modify the original assets to avoid interaction between outputs
const assetWithFileName = { ...consumedFile, source, fileName };
this.filesByReferenceId.set(referenceId, assetWithFileName);
output.bundle[fileName] = { fileName, isAsset: true, source };
output.bundle[fileName] = {
fileName,
isAsset: true, // deprecated
source,
type: 'asset'
};
}

private findExistingAssetFileNameWithSource(
bundle: OutputBundleWithPlaceholders,
source: string | Buffer
): string | null {
for (const fileName of Object.keys(bundle)) {
const outputFile = bundle[fileName] as OutputAsset;
const outputFile = bundle[fileName];
if (
outputFile.isAsset &&
outputFile.type === 'asset' &&
(Buffer.isBuffer(source) && Buffer.isBuffer(outputFile.source)
? source.equals(outputFile.source)
: source === outputFile.source)
Expand Down
1 change: 1 addition & 0 deletions test/form/samples/emit-asset-file/_config.js
Expand Up @@ -27,6 +27,7 @@ module.exports = {
const asset = outputBundle[keys[0]];
assert.strictEqual(asset.fileName, 'assets/logo-25585ac1.svg');
assert.strictEqual(asset.isAsset, true);
assert.strictEqual(asset.type, 'asset');
assert.ok(
asset.source.equals(fs.readFileSync(path.resolve(__dirname, 'logo.svg'))),
'asset has correct source'
Expand Down

0 comments on commit 7b47ad0

Please sign in to comment.