-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split InternalAsset into UncommittedAsset and CommittedAsset (Cache A…
…STs followup) (#4378) * Split InternalAsset into UncommittedAsset and CommittedAsset * Babel cleanup * Have babel transformer depend on @parcel/babel-ast-utils
- Loading branch information
Will Binns-Smith
committed
Mar 27, 2020
1 parent
a1a2515
commit c690f91
Showing
16 changed files
with
498 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// @flow strict-local | ||
|
||
import type { | ||
AST, | ||
Blob, | ||
ConfigResult, | ||
File, | ||
FilePath, | ||
PackageJSON, | ||
} from '@parcel/types'; | ||
import type {Asset, Dependency, ParcelOptions} from './types'; | ||
|
||
import v8 from 'v8'; | ||
import {Readable} from 'stream'; | ||
import SourceMap from '@parcel/source-map'; | ||
import {bufferStream, blobToStream, streamFromPromise} from '@parcel/utils'; | ||
import {getConfig, generateFromAST} from './assetUtils'; | ||
|
||
export default class CommittedAsset { | ||
value: Asset; | ||
options: ParcelOptions; | ||
content: ?Promise<Buffer | string>; | ||
mapBuffer: ?Promise<?Buffer>; | ||
map: ?Promise<?SourceMap>; | ||
ast: ?Promise<AST>; | ||
idBase: ?string; | ||
generatingPromise: ?Promise<void>; | ||
|
||
constructor(value: Asset, options: ParcelOptions) { | ||
this.value = value; | ||
this.options = options; | ||
} | ||
|
||
getContent(): Blob | Promise<Buffer | string> { | ||
if (this.content == null) { | ||
if (this.value.contentKey != null) { | ||
return this.options.cache.getStream(this.value.contentKey); | ||
} else if (this.value.astKey != null) { | ||
return streamFromPromise( | ||
generateFromAST(this).then(({code}) => { | ||
if (!(code instanceof Readable)) { | ||
this.content = Promise.resolve(code); | ||
} | ||
return code; | ||
}), | ||
); | ||
} else { | ||
throw new Error('Asset has no content'); | ||
} | ||
} | ||
|
||
return this.content; | ||
} | ||
|
||
async getCode(): Promise<string> { | ||
let content = await this.getContent(); | ||
|
||
if (typeof content === 'string' || content instanceof Buffer) { | ||
return content.toString(); | ||
} else if (content != null) { | ||
this.content = bufferStream(content); | ||
return (await this.content).toString(); | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
async getBuffer(): Promise<Buffer> { | ||
let content = await this.getContent(); | ||
|
||
if (content == null) { | ||
return Buffer.alloc(0); | ||
} else if (typeof content === 'string' || content instanceof Buffer) { | ||
return Buffer.from(content); | ||
} | ||
|
||
this.content = bufferStream(content); | ||
return this.content; | ||
} | ||
|
||
getStream(): Readable { | ||
let content = this.getContent(); | ||
return content instanceof Promise | ||
? streamFromPromise(content) | ||
: blobToStream(content); | ||
} | ||
|
||
getMapBuffer(): Promise<?Buffer> { | ||
let mapKey = this.value.mapKey; | ||
if (mapKey != null && this.mapBuffer == null) { | ||
this.mapBuffer = (async () => { | ||
try { | ||
return await this.options.cache.getBlob(mapKey); | ||
} catch (err) { | ||
if (err.code === 'ENOENT' && this.value.astKey != null) { | ||
return (await generateFromAST(this)).map?.toBuffer(); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
})(); | ||
} | ||
|
||
return this.mapBuffer ?? Promise.resolve(); | ||
} | ||
|
||
getMap(): Promise<?SourceMap> { | ||
if (this.map == null) { | ||
this.map = (async () => { | ||
let mapBuffer = await this.getMapBuffer(); | ||
if (mapBuffer) { | ||
// Get sourcemap from flatbuffer | ||
let map = new SourceMap(); | ||
map.addBufferMappings(mapBuffer); | ||
return map; | ||
} | ||
})(); | ||
} | ||
|
||
return this.map; | ||
} | ||
|
||
getAST(): Promise<AST> { | ||
if (this.value.astKey == null) { | ||
throw new Error('Asset does not have an AST'); | ||
} | ||
|
||
if (this.ast == null) { | ||
this.ast = this.options.cache | ||
.getBlob(this.value.astKey) | ||
.then(serializedAst => | ||
// $FlowFixMe | ||
v8.deserialize(serializedAst), | ||
); | ||
} | ||
|
||
return this.ast; | ||
} | ||
|
||
getIncludedFiles(): Array<File> { | ||
return Array.from(this.value.includedFiles.values()); | ||
} | ||
|
||
getDependencies(): Array<Dependency> { | ||
return Array.from(this.value.dependencies.values()); | ||
} | ||
|
||
async getConfig( | ||
filePaths: Array<FilePath>, | ||
options: ?{| | ||
packageKey?: string, | ||
parse?: boolean, | ||
|}, | ||
): Promise<ConfigResult | null> { | ||
return (await getConfig(this, filePaths, options))?.config; | ||
} | ||
|
||
getPackage(): Promise<PackageJSON | null> { | ||
return this.getConfig(['package.json']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.