-
-
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.
Implement pipelines to compose multiple asset types together (#1065)
- Loading branch information
1 parent
3ddaec9
commit 8a95f70
Showing
13 changed files
with
239 additions
and
65 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
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,102 @@ | ||
const Parser = require('./Parser'); | ||
const path = require('path'); | ||
const md5 = require('./utils/md5'); | ||
|
||
/** | ||
* A Pipeline composes multiple Asset types together. | ||
*/ | ||
class Pipeline { | ||
constructor(options) { | ||
this.options = options; | ||
this.parser = new Parser(options); | ||
} | ||
|
||
async process(path, pkg, options) { | ||
let asset = this.parser.getAsset(path, pkg, options); | ||
let generated = await this.processAsset(asset); | ||
let generatedMap = {}; | ||
for (let rendition of generated) { | ||
generatedMap[rendition.type] = rendition.value; | ||
} | ||
|
||
return { | ||
dependencies: Array.from(asset.dependencies.values()), | ||
generated: generatedMap, | ||
hash: asset.hash, | ||
cacheData: asset.cacheData | ||
}; | ||
} | ||
|
||
async processAsset(asset) { | ||
try { | ||
await asset.process(); | ||
} catch (err) { | ||
throw asset.generateErrorMessage(err); | ||
} | ||
|
||
let inputType = path.extname(asset.name).slice(1); | ||
let generated = []; | ||
|
||
for (let rendition of this.iterateRenditions(asset)) { | ||
let {type, value} = rendition; | ||
if (typeof value !== 'string' || rendition.final) { | ||
generated.push(rendition); | ||
continue; | ||
} | ||
|
||
// Find an asset type for the rendition type. | ||
// If the asset is not already an instance of this asset type, process it. | ||
let AssetType = this.parser.findParser( | ||
asset.name.slice(0, -inputType.length) + type | ||
); | ||
if (!(asset instanceof AssetType)) { | ||
let opts = Object.assign({rendition}, asset.options); | ||
let subAsset = new AssetType(asset.name, asset.package, opts); | ||
subAsset.contents = value; | ||
subAsset.dependencies = asset.dependencies; | ||
|
||
let processed = await this.processAsset(subAsset); | ||
generated = generated.concat(processed); | ||
Object.assign(asset.cacheData, subAsset.cacheData); | ||
asset.hash = md5(asset.hash + subAsset.hash); | ||
} else { | ||
generated.push(rendition); | ||
} | ||
} | ||
|
||
// Post process. This allows assets a chance to modify the output produced by sub-asset types. | ||
asset.generated = generated; | ||
try { | ||
generated = await asset.postProcess(generated); | ||
} catch (err) { | ||
throw asset.generateErrorMessage(err); | ||
} | ||
|
||
return generated; | ||
} | ||
|
||
*iterateRenditions(asset) { | ||
if (Array.isArray(asset.generated)) { | ||
return yield* asset.generated; | ||
} | ||
|
||
if (typeof asset.generated === 'string') { | ||
return yield { | ||
type: asset.type, | ||
value: asset.generated | ||
}; | ||
} | ||
|
||
// Backward compatibility support for the old API. | ||
// Assume all renditions are final - don't compose asset types together. | ||
for (let type in asset.generated) { | ||
yield { | ||
type, | ||
value: asset.generated[type], | ||
final: true | ||
}; | ||
} | ||
} | ||
} | ||
|
||
module.exports = Pipeline; |
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
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
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
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.