Constructor
new Asset(config, assetGraph)
- Source:
Create a new Asset instance.
Most of the time it's unnecessary to create asset objects
directly. When you need to manipulate assets that already exist on
disc or on a web server, the loadAssets
and populate
transforms
are the easiest way to get the objects created. See the section about
transforms below.
Note that the Asset base class is only intended to be used to represent assets for which there's no specific subclass.
Parameters:
Name | Type | Description |
---|---|---|
config |
AssetConfig | Configuration for instantiating an asset |
assetGraph |
AssetGraph | Mandatory AssetGraph instance references |
Members
baseName :String
- Source:
The file name for the asset, excluding the extension. It is automatically kept in sync with the url, but preserved if the asset is inlined or set to a value that ends with a slash.
If updated, the url of the asset will also be updated.
Type:
- String
contentType :String
- Source:
- Default Value:
- 'appliction/octet-stream'
The Content-Type (MIME type) of the asset.
Type:
- String
dataUrl :String
- Source:
The data:
-url of the asset for inlining
Type:
- String
defaultExtension :String
- Source:
The default extension for the asset type, prepended with a dot, eg. .html
, .js
or .png
Type:
- String
extension :String
- Source:
The file name extension for the asset (String). It is automatically kept in sync with the url, but preserved if the asset is inlined or set to a value that ends with a slash.
If updated, the url of the asset will also be updated.
The extension includes the leading dot and is thus kept in the
same format as require('path').extname
and the basename
command line utility use.
Type:
- String
externalRelations :Array.<Relation>
- Source:
The subset of outgoing Relations that point to external (non-inlined) Assets
Type:
- Array.<Relation>
fileName :String
- Source:
The file name for the asset. It is automatically kept in sync with the url, but preserved if the asset is inlined or set to a value that ends with a slash.
If updated, the url of the asset will also be updated.
Type:
- String
hostname :String
- Source:
The hostname part of a URL protocol://username:password@<hostname>:port/
Type:
- String
incomingRelations :Array.<Relation>
- Source:
Get/set the relations pointing to this asset
Caveat: Setting Does not remove/detach other relations already pointing at the asset, but not included in the array, so it's not strictly symmetric with the incomingRelations getter.
Type:
- Array.<Relation>
(constant) isAsset :Boolean
- Source:
- Default Value:
- true
Boolean Property that's true for all Asset instances. Avoids
reliance on the instanceof
operator.
Type:
- Boolean
isExternalizable :Boolean
- Source:
- Default Value:
- true
Whether the asset occurs in a context where it can be made external. If false, the asset will stay inline. Useful for "always inline" assets pointed to by HtmlConditionalComment, HtmlDataBindAttribute, and HtmlKnockoutContainerless relations. Override when creating the asset.
Type:
- Boolean
isInline :Boolean
- Source:
Determine whether the asset is inline (shorthand for checking whether it has a url).
Type:
- Boolean
isLoaded :Boolean
- Source:
The loaded state of the Asset
Type:
- Boolean
lastKnownByteLength :Number
- Source:
Get the last known byt length of the Asset
Doesn't force a serialization of the asset if a value has previously been recorded.
Type:
- Number
md5Hex :String
- Source:
Get the current md5 hex of the asset.
Type:
- String
(nullable) nonInlineAncestor :Asset
- Source:
Get the first non-inline ancestor asset by following the incoming relations, ie. the first asset that has a url. Returns the asset itself if it's not inline, and null if the asset is inline, but not in an AssetGraph.
Type:
origin :String
- Source:
The origin of the asset, protocol://host
Corresponds to new URL(...).origin
For inlined assets, this will contain the origin of the first non-inlined ancestor
Type:
- String
outgoingRelations :Array.<Relation>
- Source:
Get/set the outgoing relations of the asset.
Type:
- Array.<Relation>
parseTree :Oject
- Source:
Some asset classes support inspection and manipulation using a high
level interface. If you modify the parse tree, you have to call
asset.markDirty()
so any cached serializations of the asset are
invalidated.
These are the formats you'll get:
Html
and Xml
:
jsdom document object (https://github.com/tmpvar/jsdom).
Css
CSSOM CSSStyleSheet object (https://github.com/NV/CSSOM).
JavaScript
estree AST object (via acorn).
Json
Regular JavaScript object (the result of JSON.parse on the decoded source).
CacheManifest
A JavaScript object with a key for each section present in the
manifest (CACHE
, NETWORK
, REMOTE
). The value is an array with
an item for each entry in the section. Refer to the source for
details.
Type:
- Oject
password :String
- Source:
The password part of a URL protocol://username:<password>@hostname:port/
Type:
- String
path :String
- Source:
The path of the asset relative to the AssetGraph root.
Corresponds to a new URL(...).pathName
If updated, the url of the asset will also be updated.
Type:
- String
port :Number
- Source:
The port part of a URL protocol://username:password@hostname:<port>/
Type:
- Number
protocol :String
- Source:
The protocol part of a URL <protocol:>//username:password@hostname:port/
.
Includes trailing :
Type:
- String
query :String
- Source:
The query parameters part of the Assets URL.
Can be set with a String
or Object
, but always returns String
in the getters
Type:
- String
rawSrc :Buffer
- Source:
Get or set the raw source of the asset.
If the internal state has been changed since the asset was initialized, it will automatically be reserialized when this property is retrieved.
Type:
- Buffer
Example
const htmlAsset = new AssetGraph().addAsset({
type: 'Html',
rawSrc: new Buffer('<html><body>Hello!</body></html>')
});
htmlAsset.parseTree.body.innerHTML = "Bye!";
htmlAsset.markDirty();
htmlAsset.rawSrc.toString(); // "<html><body>Bye!</body></html>"
type :String
- Source:
The assets defined or inferred type
Type:
- String
url :String
- Source:
Get or set the absolute url of the asset.
The url will use the file:
schema if loaded from disc. Will be
falsy for inline assets.
Type:
- String
urlOrDescription :String
- Source:
A Human readable URL or Asset description if inline.
Paths for file://
URL's are kept relative to the current
working directory for easier copy/paste if needed.
Type:
- String
username :String
- Source:
The username part of a URL protocol://<username>:password@hostname:port/
Type:
- String
Methods
addRelation(relation, positionopt, adjacentRelationopt)
- Source:
Attaches a Relation to the Asset.
The ordering of certain relation types is significant
(HtmlScript
, for instance), so it's important that the order
isn't scrambled in the indices. Therefore the caller must
explicitly specify a position at which to insert the object.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
relation |
Relation | The Relation to attach to the Asset |
||
position |
String |
<optional> |
'last'
|
|
adjacentRelation |
Relation |
<optional> |
The adjacent relation, mandatory if the position is |
clone(incomingRelations) → {Asset}
- Source:
Clone this asset instance and add the clone to the graph if this instance is part of a graph. As an extra service, optionally update some caller-specified relations to point at the clone.
If this instance isn't inline, a url is made up for the clone.
Parameters:
Name | Type | Description |
---|---|---|
incomingRelations |
Array.<Relation> | Relation | (optional) Some incoming relations that should be pointed at the clone. |
Returns:
The cloned asset.
- Type
- Asset
externalize()
- Source:
Externalize an inlined Asset.
This will create an URL from as many available URL parts as possible and auto generate the rest, then assign the URL to the Asset
findOutgoingRelationsInParseTree() → {Array.<Relation>}
- Source:
Parse the Asset for outgoing relations and return them.
Returns:
The Assets outgoing Relations
- Type
- Array.<Relation>
(async) load() → {Promise.<Asset>}
- Source:
Load the Asset
Returns a promise that is resolved when the asset is loaded. This is Asset's only async method, as soon as it is loaded, everything can happen synchronously.
Usually you'll want to use transforms.loadAssets
, which will
handle this automatically.
Returns:
The loaded Asset
- Type
- Promise.<Asset>
markDirty() → {Asset}
- Source:
Sets the dirty
flag of the asset, which is the way to say
that the asset has been manipulated since it was first loaded
(read from disc or loaded via http). For inline assets the flag
is set if the asset has been manipulated since it was last
synchronized with (copied into) its containing asset.
For assets that support a text
or parseTree
property, calling
markDirty()
will invalidate any cached serializations of the
asset.
Returns:
The asset itself (chaining-friendly)
- Type
- Asset
populate()
- Source:
Go through the outgoing relations of the asset and add the ones that refer to assets that are already part of the graph. Recurses into inline assets.
You shouldn't need to call this manually.
removeRelation(relation) → {Asset}
- Source:
Remove an outgoing Relation from the Asset by reference
Parameters:
Name | Type | Description |
---|---|---|
relation |
Relation | Outgoing Relation |
Returns:
The Asset itself
- Type
- Asset
replaceWith(newAsset) → {Asset}
- Source:
Replace the asset in the graph with another asset, then remove it from the graph.
Updates the incoming relations of the old asset to point at the new one and preserves the url of the old asset if it's not inline.
Parameters:
Name | Type | Description |
---|---|---|
newAsset |
Asset | The asset to put replace this one with. |
Returns:
The new asset.
- Type
- Asset
toString() → {String}
- Source:
Get a brief text containing the type, id, and url (if not inline) of the asset.
Returns:
The string, eg. "[JavaScript/141 file:///the/thing.js]"
- Type
- String
unload()
- Source:
Unload the asset body. If the asset is in a graph, also remove the relations from the graph along with any inline assets. Also used internally to clean up before overwriting .rawSrc or .text.