Skip to content

Commit

Permalink
Move functionality into default plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 11, 2019
1 parent 96a6105 commit b602fc5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 37 deletions.
3 changes: 2 additions & 1 deletion docs/05-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`<br>
Type: `({assetFileName: string, relativeAssetPath: string, chunkId: string, moduleId: string, format: string}) => string | null`<br>
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.
Expand All @@ -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.

Expand Down
48 changes: 13 additions & 35 deletions src/ast/nodes/MetaProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,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, (relativePath: string) => 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;
Expand Down Expand Up @@ -59,19 +35,21 @@ export default class MetaProperty extends NodeBase {
if (importMetaProperty && importMetaProperty.startsWith('ROLLUP_ASSET_URL_')) {
const assetFileName = this.context.getAssetFileName(importMetaProperty.substr(17));
const relativeAssetPath = normalize(relative(dirname(chunkId), assetFileName));
const replacement =
pluginDriver.hookFirstSync<string | void>('resolveAssetUrl', [
{
assetFileName,
chunkId,
moduleId: this.context.module.id,
relativeAssetPath
}
]) || relativeUrlMechanisms[format](relativeAssetPath);
const replacement = pluginDriver.hookFirstSync<string>('resolveAssetUrl', [
{
assetFileName,
chunkId,
format,
moduleId: this.context.module.id,
relativeAssetPath
}
]);

code.overwrite((parent as MemberExpression).start,
code.overwrite(
(parent as MemberExpression).start,
(parent as MemberExpression).end,
replacement);
replacement
);
return true;
}

Expand Down
8 changes: 7 additions & 1 deletion src/rollup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ export type ResolveImportMetaHook = (

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<string>);
Expand Down
25 changes: 25 additions & 0 deletions src/utils/defaultPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export function getRollupDefaultPlugin(options: InputOptions): Plugin {
if (typeof specifier === 'string' && !this.isExternal(specifier, parentId, false))
return <Promise<string>>this.resolveId(specifier, parentId);
},
resolveAssetUrl({ relativeAssetPath, format }) {
return relativeUrlMechanisms[format](relativeAssetPath);
},
resolveImportMeta(prop, { chunkId, format }) {
const mechanism = importMetaUrlMechanisms[format] && importMetaUrlMechanisms[format](chunkId);
if (mechanism) {
Expand Down Expand Up @@ -94,3 +97,25 @@ const importMetaUrlMechanisms: Record<string, (chunkId: string) => string> = {
`(require('u' + 'rl').URL)`
)} : ${getUrlFromDocument(chunkId)})`
};

const getRelativeUrlFromDocument = (relativePath: string) =>
getResolveUrl(
`(document.currentScript && document.currentScript.src || document.baseURI) + '/../${relativePath}'`
);

const relativeUrlMechanisms: Record<string, (relativePath: string) => 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)})`
};

0 comments on commit b602fc5

Please sign in to comment.