-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Parcel 2: Asset methods #2335
Merged
Merged
Parcel 2: Asset methods #2335
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,150 @@ | ||
// @flow | ||
import type { | ||
Asset as IAsset, | ||
TransformerResult, | ||
DependencyOptions, | ||
Dependency, | ||
FilePath, | ||
File, | ||
Environment, | ||
JSONObject, | ||
AST, | ||
AssetOutput | ||
} from '@parcel/types'; | ||
import md5 from '@parcel/utils/md5'; | ||
import config from '@parcel/utils/config'; | ||
import createDependency from './createDependency'; | ||
|
||
type AssetOptions = { | ||
id?: string, | ||
hash?: string, | ||
filePath: FilePath, | ||
type: string, | ||
code?: string, | ||
ast?: ?AST, | ||
dependencies?: Array<Dependency>, | ||
connectedFiles?: Array<File>, | ||
output?: AssetOutput, | ||
env: Environment, | ||
meta?: JSONObject | ||
}; | ||
|
||
export default class Asset implements IAsset { | ||
id: string; | ||
hash: string; | ||
filePath: FilePath; | ||
type: string; | ||
code: string; | ||
ast: ?AST; | ||
dependencies: Array<Dependency>; | ||
connectedFiles: Array<File>; | ||
output: AssetOutput; | ||
env: Environment; | ||
meta: JSONObject; | ||
|
||
constructor(options: AssetOptions) { | ||
this.id = | ||
options.id || | ||
md5(options.filePath + options.type + JSON.stringify(options.env)); | ||
this.hash = options.hash || ''; | ||
this.filePath = options.filePath; | ||
this.type = options.type; | ||
this.code = options.code || (options.output ? options.output.code : ''); | ||
this.ast = options.ast || null; | ||
this.dependencies = options.dependencies | ||
? options.dependencies.slice() | ||
: []; | ||
this.connectedFiles = options.connectedFiles | ||
? options.connectedFiles.slice() | ||
: []; | ||
this.output = options.output || {code: this.code}; | ||
this.env = options.env; | ||
this.meta = options.meta || {}; | ||
} | ||
|
||
toJSON(): AssetOptions { | ||
// Exclude `code` and `ast` from cache | ||
return { | ||
id: this.id, | ||
hash: this.hash, | ||
filePath: this.filePath, | ||
type: this.type, | ||
dependencies: this.dependencies, | ||
connectedFiles: this.connectedFiles, | ||
output: this.output, | ||
env: this.env, | ||
meta: this.meta | ||
}; | ||
} | ||
|
||
addDependency(opts: DependencyOptions) { | ||
let dep = createDependency( | ||
{ | ||
...opts, | ||
env: mergeEnvironment(this.env, opts.env) | ||
}, | ||
this.filePath | ||
); | ||
|
||
this.dependencies.push(dep); | ||
return dep.id; | ||
} | ||
|
||
async addConnectedFile(file: File) { | ||
if (!file.hash) { | ||
file.hash = await md5.file(file.filePath); | ||
} | ||
|
||
this.connectedFiles.push(file); | ||
} | ||
|
||
createChildAsset(result: TransformerResult) { | ||
let code = result.code || (result.output && result.output.code) || ''; | ||
let opts: AssetOptions = { | ||
hash: this.hash || md5(code), | ||
filePath: this.filePath, | ||
type: result.type, | ||
code, | ||
ast: result.ast, | ||
env: mergeEnvironment(this.env, result.env), | ||
dependencies: this.dependencies, | ||
connectedFiles: this.connectedFiles, | ||
meta: Object.assign({}, this.meta, result.meta) | ||
}; | ||
|
||
let asset = new Asset(opts); | ||
|
||
if (result.dependencies) { | ||
for (let dep of result.dependencies) { | ||
asset.addDependency(dep); | ||
} | ||
} | ||
|
||
if (result.connectedFiles) { | ||
for (let file of result.connectedFiles) { | ||
asset.addConnectedFile(file); | ||
} | ||
} | ||
|
||
return asset; | ||
} | ||
|
||
async getOutput() { | ||
return this.output; | ||
} | ||
|
||
async getConfig(filePaths: Array<FilePath>) { | ||
devongovett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return config.load(filePaths); | ||
} | ||
|
||
async getPackage() { | ||
return { | ||
name: 'foo', | ||
version: '1.2.3' | ||
}; | ||
} | ||
} | ||
|
||
function mergeEnvironment(a: Environment, b: ?Environment): Environment { | ||
return Object.assign({}, a, b); | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to also set code to null when the asset is finalized in TransformerRunner? I noticed these fields are getting removed when the asset is stringified going into the cache, but that's not what is passed back on a cache miss run, so some "code" is being stored in memory in the graph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably. That's true, although when run in the worker farm the assets are converted to JSON to pass over the IPC back to the main process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I guess it wouldn't be every cache miss, just the cache misses before the workers warm up. Still probably better to guarantee it's removed once finalized though.