-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support unique bundle-time asset emission
- Loading branch information
1 parent
9ffb2ff
commit 53805de
Showing
7 changed files
with
190 additions
and
117 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import error from './error'; | ||
import { randomHexString } from './entryHashing'; | ||
import { isPlainName } from './relativeId'; | ||
import { makeUnique, renderNamePattern } from './renderNamePattern'; | ||
import sha256 from 'hash.js/lib/hash/sha/256'; | ||
import { extname } from './path'; | ||
import { OutputBundle } from '../rollup/types'; | ||
|
||
export interface Asset { | ||
name: string; | ||
source: string | Buffer; | ||
fileName: string; | ||
} | ||
|
||
export function getAssetFileName( | ||
asset: Asset, | ||
existingNames: Record<string, any>, | ||
assetFileNames: string | ||
) { | ||
if (asset.source === undefined) | ||
error({ | ||
code: 'ASSET_SOURCE_NOT_FOUND', | ||
message: `Plugin error creating asset ${asset.name} - no asset source set.` | ||
}); | ||
|
||
if (asset.fileName) return asset.fileName; | ||
|
||
return makeUnique( | ||
renderNamePattern(assetFileNames, 'assetFileNames', name => { | ||
switch (name) { | ||
case 'hash': | ||
const hash = sha256(); | ||
hash.update(name); | ||
hash.update(':'); | ||
hash.update(asset.source); | ||
return hash.digest('hex').substr(0, 8); | ||
case 'name': | ||
return asset.name.substr(0, asset.name.length - extname(asset.name).length); | ||
case 'ext': | ||
return extname(asset.name); | ||
} | ||
}), | ||
existingNames | ||
); | ||
} | ||
|
||
export function createAssetPluginHooks( | ||
assetsById: Map<string, Asset>, | ||
outputBundle?: OutputBundle, | ||
assetFileNames?: string | ||
) { | ||
return { | ||
emitAsset(name: string, source?: string | Buffer) { | ||
if (typeof name !== 'string' || !isPlainName(name)) | ||
error({ | ||
code: 'INVALID_ASSET_NAME', | ||
message: `Plugin error creating asset, name is not a plain (non relative or absolute URL) string name.` | ||
}); | ||
const assetId = randomHexString(8); | ||
const asset: Asset = { name, source, fileName: undefined }; | ||
if (outputBundle) finaliseAsset(asset, outputBundle, assetFileNames); | ||
assetsById.set(assetId, asset); | ||
return assetId; | ||
}, | ||
setAssetSource: (assetId: string, source: string | Buffer) => { | ||
const asset = assetsById.get(assetId); | ||
if (!asset) | ||
error({ | ||
code: 'ASSET_NOT_FOUND', | ||
message: `Plugin error - Unable to set asset source for unknown asset ${assetId}.` | ||
}); | ||
if (asset.source) | ||
error({ | ||
code: 'ASSET_SOURCE_ALREADY_SET', | ||
message: `Plugin error - Unable to set asset source for ${ | ||
asset.name | ||
}, source already set.` | ||
}); | ||
asset.source = source; | ||
if (outputBundle) finaliseAsset(asset, outputBundle, assetFileNames); | ||
}, | ||
getAssetFileName: (assetId: string) => { | ||
const asset = assetsById.get(assetId); | ||
if (!asset) | ||
error({ | ||
code: 'ASSET_NOT_FOUND', | ||
message: `Plugin error - Unable to get asset filename for unknown asset ${assetId}.` | ||
}); | ||
if (asset.fileName === undefined) | ||
error({ | ||
code: 'ASSET_NOT_FINALISED', | ||
message: `Plugin error - Unable to get asset file name for asset ${assetId}. Ensure that the source is set and that generate is called first.` | ||
}); | ||
return asset.fileName; | ||
} | ||
}; | ||
} | ||
|
||
export function finaliseAsset(asset: Asset, outputBundle: OutputBundle, assetFileNames: string) { | ||
const fileName = getAssetFileName(asset, outputBundle, assetFileNames); | ||
asset.fileName = fileName; | ||
outputBundle[fileName] = asset.source; | ||
} |
This file was deleted.
Oops, something went wrong.
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