From 229e795dc6d67b1862c8651ac73ede1b1aa4b4ac Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 8 Apr 2019 08:01:17 +0200 Subject: [PATCH] Move functionality into default plugin --- docs/05-plugins.md | 3 ++- src/ast/nodes/MetaProperty.ts | 42 ++++++++--------------------------- src/rollup/types.d.ts | 8 ++++++- src/utils/defaultPlugin.ts | 25 +++++++++++++++++++++ 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/docs/05-plugins.md b/docs/05-plugins.md index 0e8ddc4fdc4..907e0f7038d 100644 --- a/docs/05-plugins.md +++ b/docs/05-plugins.md @@ -180,7 +180,7 @@ Kind: `async, parallel` Called initially each time `bundle.generate()` or `bundle.write()` is called. To get notified when generation has completed, use the `generateBundle` and `renderError` hooks. #### `resolveAssetUrl` -Type: `({assetFileName: string, relativeAssetPath: string, chunkId: string, moduleId: string}) => string | null`
+Type: `({assetFileName: string, relativeAssetPath: string, chunkId: string, moduleId: string, format: string}) => string | null`
Kind: `sync, first` Allows to customize how Rollup resolves URLs of assets emitted via `this.emitAsset` by plugins. By default, Rollup will generate code for `import.meta.ROLLUP_ASSET_URL_[assetId]` that should correctly generate absolute URLs of emitted assets independent of the output format and the host system where the code is deployed. @@ -191,6 +191,7 @@ For that, all formats except CommonJS and UMD assume that they run in a browser - `relativeAssetPath`: The path and file name of the emitted asset, relative to the chunk from which the asset is referenced via `import.meta.ROLLUP_ASSET_URL_[assetId]`. This will also contain no leading `./` but may contain a leading `../`. - `moduleId`: The id of the original module this asset is referenced from. Useful for conditionally resolving certain assets differently. - `chunkId`: The id of the chunk this asset is referenced from. +- `format`: The rendered output format. Note that since this hook has access to the filename of the current chunk, its return value will not be considered when generating the hash of this chunk. diff --git a/src/ast/nodes/MetaProperty.ts b/src/ast/nodes/MetaProperty.ts index 7a5c64ca71c..c3c01d15025 100644 --- a/src/ast/nodes/MetaProperty.ts +++ b/src/ast/nodes/MetaProperty.ts @@ -8,30 +8,6 @@ import MemberExpression from './MemberExpression'; import * as NodeType from './NodeType'; import { NodeBase } from './shared/Node'; -const getResolveUrl = (path: string, URL: string = 'URL') => `new ${URL}(${path}).href`; - -const getRelativeUrlFromDocument = (relativePath: string) => - getResolveUrl( - `(document.currentScript && document.currentScript.src || document.baseURI) + '/../${relativePath}'` - ); - -const relativeUrlMechanisms: Record string> = { - amd: relativePath => getResolveUrl(`module.uri + '/../${relativePath}', document.baseURI`), - cjs: relativePath => - `(typeof document === 'undefined' ? ${getResolveUrl( - `'file:' + __dirname + '/${relativePath}'`, - `(require('u' + 'rl').URL)` - )} : ${getRelativeUrlFromDocument(relativePath)})`, - es: relativePath => getResolveUrl(`'${relativePath}', import.meta.url`), - iife: relativePath => getRelativeUrlFromDocument(relativePath), - system: relativePath => getResolveUrl(`'${relativePath}', module.meta.url`), - umd: relativePath => - `(typeof document === 'undefined' ? ${getResolveUrl( - `'file:' + __dirname + '/${relativePath}'`, - `(require('u' + 'rl').URL)` - )} : ${getRelativeUrlFromDocument(relativePath)})` -}; - export default class MetaProperty extends NodeBase { meta: Identifier; property: Identifier; @@ -71,15 +47,15 @@ export default class MetaProperty extends NodeBase { if (importMetaProperty.startsWith('ROLLUP_ASSET_URL_')) { const assetFileName = this.context.getAssetFileName(importMetaProperty.substr(17)); const relativeAssetPath = normalize(relative(dirname(chunkId), assetFileName)); - const replacement = - pluginDriver.hookFirstSync('resolveAssetUrl', [ - { - assetFileName, - chunkId, - moduleId: this.context.module.id, - relativeAssetPath - } - ]) || relativeUrlMechanisms[format](relativeAssetPath); + const replacement = pluginDriver.hookFirstSync('resolveAssetUrl', [ + { + assetFileName, + chunkId, + format, + moduleId: this.context.module.id, + relativeAssetPath + } + ]); code.overwrite(parent.start, parent.end, replacement); return true; diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 90747ca107e..ceff255fcf8 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -198,7 +198,13 @@ export type ResolveImportMetaUrlHook = ( export type ResolveAssetUrlHook = ( this: PluginContext, - options: { assetFileName: string; chunkId: string; moduleId: string; relativeAssetPath: string } + options: { + assetFileName: string; + chunkId: string; + format: string; + moduleId: string; + relativeAssetPath: string; + } ) => string | void; export type AddonHook = string | ((this: PluginContext) => string | Promise); diff --git a/src/utils/defaultPlugin.ts b/src/utils/defaultPlugin.ts index a37a30c8c73..18022924596 100644 --- a/src/utils/defaultPlugin.ts +++ b/src/utils/defaultPlugin.ts @@ -14,6 +14,9 @@ export function getRollupDefaultPlugin(options: InputOptions): Plugin { if (typeof specifier === 'string' && !this.isExternal(specifier, parentId, false)) return >this.resolveId(specifier, parentId); }, + resolveAssetUrl({ relativeAssetPath, format }) { + return relativeUrlMechanisms[format](relativeAssetPath); + }, resolveImportMetaUrl({ chunkId, format }) { return importMetaUrlMechanisms[format] && importMetaUrlMechanisms[format](chunkId); } @@ -91,3 +94,25 @@ const importMetaUrlMechanisms: Record string> = { `(require('u' + 'rl').URL)` )} : ${getUrlFromDocument(chunkId)})` }; + +const getRelativeUrlFromDocument = (relativePath: string) => + getResolveUrl( + `(document.currentScript && document.currentScript.src || document.baseURI) + '/../${relativePath}'` + ); + +const relativeUrlMechanisms: Record string> = { + amd: relativePath => getResolveUrl(`module.uri + '/../${relativePath}', document.baseURI`), + cjs: relativePath => + `(typeof document === 'undefined' ? ${getResolveUrl( + `'file:' + __dirname + '/${relativePath}'`, + `(require('u' + 'rl').URL)` + )} : ${getRelativeUrlFromDocument(relativePath)})`, + es: relativePath => getResolveUrl(`'${relativePath}', import.meta.url`), + iife: relativePath => getRelativeUrlFromDocument(relativePath), + system: relativePath => getResolveUrl(`'${relativePath}', module.meta.url`), + umd: relativePath => + `(typeof document === 'undefined' ? ${getResolveUrl( + `'file:' + __dirname + '/${relativePath}'`, + `(require('u' + 'rl').URL)` + )} : ${getRelativeUrlFromDocument(relativePath)})` +};