diff --git a/docs/05-plugins.md b/docs/05-plugins.md index 7d3ee34d7a5..9fbf1f28b17 100644 --- a/docs/05-plugins.md +++ b/docs/05-plugins.md @@ -179,6 +179,31 @@ 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, 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. + +For that, all formats except CommonJS and UMD assume that they run in a browser environment where `URL` and `document` are available. In case that fails or to generate more optimized code, this hook can be used to customize this behaviour. To do that, the following information is available: + +- `assetFileName`: The path and file name of the emitted asset, relative to `output.dir` without a leading `./`. +- `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. + +The following plugin will always resolve all assets relative to the current document: + +```javascript +// rollup.config.js +resolveAssetUrl({assetFileName}) { + return `new URL('${assetFileName}', document.baseURI).href`; +} +``` + #### `resolveDynamicImport` Type: `(specifier: string | ESTree.Node, importer: string) => string | false | null`
Kind: `async, first` @@ -348,14 +373,36 @@ The `position` argument is a character index where the warning was raised. If pr ### Asset URLs -To reference an asset URL reference from within JS code, use the `import.meta.ROLLUP_ASSET_URL_[assetId]` replacement. The following example represents emitting a CSS file for a module that then exports a URL that is constructed to correctly point to the emitted file from the target runtime environment. +To reference an asset URL reference from within JS code, use the `import.meta.ROLLUP_ASSET_URL_[assetId]` replacement. This will generate code that depends on the output format and generates a URL that points to the emitted file in the target environment. Note that all formats except CommonJS and UMD assume that they run in a browser environment where `URL` and `document` are available. +The following example will detect imports of `.svg` files, emit the imported files as assets, and return their URLs to be used e.g. as the `src` attribute of an `img` tag: ```js -load (id) { - const assetId = this.emitAsset('style.css', fs.readFileSync(path.resolve(assets, 'style.css'))); - return `export default import.meta.ROLLUP_ASSET_URL_${assetId}`; +// plugin +export default function svgResolverPlugin () { + return ({ + resolveId(id, importee) { + if (id.endsWith('.svg')) { + return path.resolve(path.dirname(importee), id); + } + }, + load(id) { + if (id.endsWith('.svg')) { + const assetId = this.emitAsset( + path.basename(id), + fs.readFileSync(id) + ); + return `export default import.meta.ROLLUP_ASSET_URL_${assetId};`; + } + } + }); } + +// usage +import logo from '../images/logo.svg'; +const image = document.createElement('img'); +image.src = logo; +document.body.appendChild(image); ``` ### Advanced Loaders diff --git a/package-lock.json b/package-lock.json index 97e7835f0c6..5ea1b958322 100644 --- a/package-lock.json +++ b/package-lock.json @@ -110,9 +110,9 @@ "dev": true }, "@types/node": { - "version": "11.13.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.2.tgz", - "integrity": "sha512-HOtU5KqROKT7qX/itKHuTtt5fV0iXbheQvrgbLNXFJQBY/eh+VS5vmmTAVlo3qIGMsypm0G4N1t2AXjy1ZicaQ==" + "version": "11.13.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.4.tgz", + "integrity": "sha512-+rabAZZ3Yn7tF/XPGHupKIL5EcAbrLxnTr/hgQICxbeuAfWtT0UZSfULE+ndusckBItcv4o6ZeOJplQikVcLvQ==" }, "@types/pretty-ms": { "version": "4.0.0", @@ -3327,7 +3327,7 @@ "glob": "7.1.3", "growl": "1.10.5", "he": "1.2.0", - "js-yaml": "^3.13.0", + "js-yaml": "3.13.0", "log-symbols": "2.2.0", "minimatch": "3.0.4", "mkdirp": "0.5.1", @@ -4277,13 +4277,13 @@ } }, "rollup": { - "version": "1.9.1", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.9.1.tgz", - "integrity": "sha512-eqID6rAL7R9Jyrt10TvZr1/NUNesGmUlMcDoiPX9e7YK9Sjqi2oTRV1YuV5UFZ4WeJ0u0R8kk8JWjWWdRFia7A==", + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-1.9.3.tgz", + "integrity": "sha512-20iIOjee5n3H6W6CXsVdYs2xw86j4l+LQLM6yACynt+YJCwkqaYNHAjQ/dhVBIKsFpHwPqHamn/GHq+3Zp8ybQ==", "dev": true, "requires": { "@types/estree": "0.0.39", - "@types/node": "^11.13.0", + "@types/node": "^11.13.2", "acorn": "^6.1.1" } }, @@ -4339,13 +4339,13 @@ } }, "rollup-plugin-node-resolve": { - "version": "4.2.2", - "resolved": "https://registry.npmjs.org/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-4.2.2.tgz", - "integrity": "sha512-fkTHihF4Tzc95ZotKJNZZgxZPzslj+twk6UNWSBn3ln1mSV55atjsi7CDODdw/NNlteaf/jjjvrAj62p7OQjaQ==", + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-4.2.3.tgz", + "integrity": "sha512-r+WaesPzdGEynpLZLALFEDugA4ACa5zn7bc/+LVX4vAXQQ8IgDHv0xfsSvJ8tDXUtprfBtrDtRFg27ifKjcJTg==", "dev": true, "requires": { "@types/resolve": "0.0.8", - "builtin-modules": "^3.0.0", + "builtin-modules": "^3.1.0", "is-module": "^1.0.0", "resolve": "^1.10.0" }, @@ -4938,9 +4938,9 @@ "dev": true }, "systemjs": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/systemjs/-/systemjs-3.1.0.tgz", - "integrity": "sha512-91NK7BcuqGbfP/CjL4N3uaT+pupmNp7nCtEQr0gzCfn7SrpPpl6j0EIidLGnuO/E4NfWIDbQkTK2X2Pmdfw+kg==", + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/systemjs/-/systemjs-3.1.1.tgz", + "integrity": "sha512-Sfbn/RwJjEb88kQDbLlsj7vxuYwt1eFOd3rXD3olxATmrNSwBAuwwt/3H/2kj6hGHiw0kqywqeYw0bXfUe+KNg==", "dev": true }, "table": { diff --git a/package.json b/package.json index fc1fdd23935..f5bc9c0b563 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "homepage": "https://github.com/rollup/rollup", "dependencies": { "@types/estree": "0.0.39", - "@types/node": "^11.13.2", + "@types/node": "^11.13.4", "acorn": "^6.1.1" }, "devDependencies": { @@ -98,12 +98,12 @@ "remap-istanbul": "^0.13.0", "require-relative": "^0.8.7", "requirejs": "^2.3.6", - "rollup": "^1.9.1", + "rollup": "^1.9.3", "rollup-plugin-alias": "^1.5.1", "rollup-plugin-buble": "^0.19.6", "rollup-plugin-commonjs": "^9.3.4", "rollup-plugin-json": "^4.0.0", - "rollup-plugin-node-resolve": "^4.2.2", + "rollup-plugin-node-resolve": "^4.2.3", "rollup-plugin-replace": "^2.2.0", "rollup-plugin-string": "^3.0.0", "rollup-plugin-terser": "^4.0.4", @@ -115,7 +115,7 @@ "source-map": "^0.6.1", "source-map-support": "^0.5.12", "sourcemap-codec": "^1.4.4", - "systemjs": "^3.1.0", + "systemjs": "^3.1.1", "terser": "^3.17.0", "tslib": "^1.9.3", "tslint": "^5.15.0", diff --git a/src/Chunk.ts b/src/Chunk.ts index f150f22d74b..2e2fb2da111 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -795,7 +795,7 @@ export default class Chunk { } private finaliseImportMetas(options: OutputOptions): boolean { - let usesMechanism = false; + let needsAmdModule = false; for (let i = 0; i < this.orderedModules.length; i++) { const module = this.orderedModules[i]; const code = this.renderedModuleSources[i]; @@ -803,11 +803,11 @@ export default class Chunk { if ( importMeta.renderFinalMechanism(code, this.id, options.format, this.graph.pluginDriver) ) { - usesMechanism = true; + needsAmdModule = true; } } } - return usesMechanism; + return needsAmdModule; } private getChunkDependencyDeclarations( diff --git a/src/Module.ts b/src/Module.ts index fe3585c600a..a122ad6b877 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -499,8 +499,8 @@ export default class Module { this.resolvedIds = resolvedIds || Object.create(null); - // By default, `id` is the filename. Custom resolvers and loaders - // can change that, but it makes sense to use it for the source filename + // By default, `id` is the file name. Custom resolvers and loaders + // can change that, but it makes sense to use it for the source file name const fileName = this.id; this.magicString = new MagicString(code, { diff --git a/src/ast/nodes/MetaProperty.ts b/src/ast/nodes/MetaProperty.ts index e532a8a0adb..4bbbc30eace 100644 --- a/src/ast/nodes/MetaProperty.ts +++ b/src/ast/nodes/MetaProperty.ts @@ -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 amdModuleUrl = `(typeof process !== 'undefined' && process.versions && process.versions.node ? 'file:' : '') + module.uri`; - -const globalRelUrlMechanism = (relPath: string) => { - return getResolveUrl( - `(typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : 'file:' + __filename) + '/../${relPath}'`, - `(typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)` - ); -}; - -const relUrlMechanisms: Record string> = { - amd: (relPath: string) => getResolveUrl(`${amdModuleUrl} + '/../${relPath}'`), - cjs: (relPath: string) => - getResolveUrl( - `(process.browser ? '' : 'file:') + __dirname + '/${relPath}', process.browser && document.baseURI`, - `(typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)` - ), - es: (relPath: string) => getResolveUrl(`'../${relPath}', import.meta.url`), - iife: globalRelUrlMechanism, - system: (relPath: string) => getResolveUrl(`'../${relPath}', module.url`), - umd: globalRelUrlMechanism -}; - export default class MetaProperty extends NodeBase { meta: Identifier; property: Identifier; @@ -58,11 +34,21 @@ export default class MetaProperty extends NodeBase { // support import.meta.ROLLUP_ASSET_URL_[ID] if (importMetaProperty && importMetaProperty.startsWith('ROLLUP_ASSET_URL_')) { const assetFileName = this.context.getAssetFileName(importMetaProperty.substr(17)); - const relPath = normalize(relative(dirname(chunkId), assetFileName)); + const relativeAssetPath = normalize(relative(dirname(chunkId), assetFileName)); + const replacement = pluginDriver.hookFirstSync('resolveAssetUrl', [ + { + assetFileName, + chunkId, + format, + moduleId: this.context.module.id, + relativeAssetPath + } + ]); + code.overwrite( (parent as MemberExpression).start, (parent as MemberExpression).end, - relUrlMechanisms[format](relPath) + replacement ); return true; } diff --git a/src/rollup/index.ts b/src/rollup/index.ts index 1f1e69f3171..134e5da40db 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -382,7 +382,7 @@ function writeOutputFile( outputFile: OutputAsset | OutputChunk, outputOptions: OutputOptions ): Promise { - const filename = resolve(outputOptions.dir || dirname(outputOptions.file), outputFile.fileName); + const fileName = resolve(outputOptions.dir || dirname(outputOptions.file), outputFile.fileName); let writeSourceMapPromise: Promise; let source: string | Buffer; if (isOutputAsset(outputFile)) { @@ -395,13 +395,13 @@ function writeOutputFile( url = outputFile.map.toUrl(); } else { url = `${basename(outputFile.fileName)}.map`; - writeSourceMapPromise = writeFile(`${filename}.map`, outputFile.map.toString()); + writeSourceMapPromise = writeFile(`${fileName}.map`, outputFile.map.toString()); } source += `//# ${SOURCEMAPPING_URL}=${url}\n`; } } - return writeFile(filename, source) + return writeFile(fileName, source) .then(() => writeSourceMapPromise) .then( () => diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 316666f3c1f..86b5fffe558 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -197,6 +197,17 @@ export type ResolveImportMetaHook = ( options: { chunkId: string; format: string; moduleId: string } ) => string | void; +export type ResolveAssetUrlHook = ( + this: PluginContext, + options: { + assetFileName: string; + chunkId: string; + format: string; + moduleId: string; + relativeAssetPath: string; + } +) => string | void; + export type AddonHook = string | ((this: PluginContext) => string | Promise); /** @@ -248,6 +259,7 @@ export interface Plugin { renderChunk?: RenderChunkHook; renderError?: (this: PluginContext, err?: Error) => Promise | void; renderStart?: (this: PluginContext) => Promise | void; + resolveAssetUrl?: ResolveAssetUrlHook; resolveDynamicImport?: ResolveDynamicImportHook; resolveId?: ResolveIdHook; resolveImportMeta?: ResolveImportMetaHook; @@ -331,6 +343,7 @@ export interface OutputOptions { format?: ModuleFormat; freeze?: boolean; globals?: GlobalsOption; + importMetaUrl?: (chunkId: string, moduleId: string) => string; indent?: boolean; interop?: boolean; intro?: string | (() => string | Promise); diff --git a/src/utils/defaultPlugin.ts b/src/utils/defaultPlugin.ts index 4cc1c4bda4f..b8e4d8e84ba 100644 --- a/src/utils/defaultPlugin.ts +++ b/src/utils/defaultPlugin.ts @@ -14,10 +14,13 @@ 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); + }, resolveImportMeta(prop, { chunkId, format }) { - const mechanism = importMetaUrlMechanisms[format] && importMetaUrlMechanisms[format](chunkId); + const mechanism = importMetaMechanisms[format] && importMetaMechanisms[format](prop, chunkId); if (mechanism) { - return prop === null ? `({ url: ${mechanism} })` : prop === 'url' ? mechanism : 'undefined'; + return mechanism; } } }; @@ -79,18 +82,52 @@ const getResolveUrl = (path: string, URL: string = 'URL') => `new ${URL}(${path} const getUrlFromDocument = (chunkId: string) => `(document.currentScript && document.currentScript.src || new URL('${chunkId}', document.baseURI).href)`; -const importMetaUrlMechanisms: Record string> = { - amd: () => getResolveUrl(`module.uri, document.baseURI`), - cjs: chunkId => +const getGenericImportMetaMechanism = (getUrl: (chunkId: string) => string) => ( + prop: string | null, + chunkId: string +) => { + const urlMechanism = getUrl(chunkId); + return prop === null ? `({ url: ${urlMechanism} })` : prop === 'url' ? urlMechanism : 'undefined'; +}; + +const importMetaMechanisms: Record string> = { + amd: getGenericImportMetaMechanism(() => getResolveUrl(`module.uri, document.baseURI`)), + cjs: getGenericImportMetaMechanism( + chunkId => + `(typeof document === 'undefined' ? ${getResolveUrl( + `'file:' + __filename`, + `(require('u' + 'rl').URL)` + )} : ${getUrlFromDocument(chunkId)})` + ), + iife: getGenericImportMetaMechanism(chunkId => getUrlFromDocument(chunkId)), + system: prop => (prop === null ? `module.meta` : `module.meta.${prop}`), + umd: getGenericImportMetaMechanism( + chunkId => + `(typeof document === 'undefined' ? ${getResolveUrl( + `'file:' + __filename`, + `(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:' + __filename`, + `'file:' + __dirname + '/${relativePath}'`, `(require('u' + 'rl').URL)` - )} : ${getUrlFromDocument(chunkId)})`, - iife: chunkId => getUrlFromDocument(chunkId), - system: () => `module.meta.url`, - umd: chunkId => + )} : ${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:' + __filename`, + `'file:' + __dirname + '/${relativePath}'`, `(require('u' + 'rl').URL)` - )} : ${getUrlFromDocument(chunkId)})` + )} : ${getRelativeUrlFromDocument(relativePath)})` }; diff --git a/test/chunking-form/samples/asset-emission/_config.js b/test/chunking-form/samples/asset-emission/_config.js index c430dd28952..9784a297436 100644 --- a/test/chunking-form/samples/asset-emission/_config.js +++ b/test/chunking-form/samples/asset-emission/_config.js @@ -1,11 +1,26 @@ +const fs = require('fs'); +const path = require('path'); + module.exports = { description: 'supports emitting assets from plugin hooks', options: { input: ['main.js'], + output: { + chunkFileNames: 'nested/chunk.js' + }, plugins: { - transform() { - const assetId = this.emitAsset('test.ext', 'hello world'); - return `export default import.meta.ROLLUP_ASSET_URL_${assetId};`; + resolveId(id, importee) { + if (id.endsWith('.svg')) { + return path.resolve(path.dirname(importee), id); + } + }, + load(id) { + if (id.endsWith('.svg')) { + return `export default import.meta.ROLLUP_ASSET_URL_${this.emitAsset( + path.basename(id), + fs.readFileSync(id) + )};`; + } } } } diff --git a/test/chunking-form/samples/asset-emission/_expected/amd/assets/logo1-25253976.svg b/test/chunking-form/samples/asset-emission/_expected/amd/assets/logo1-25253976.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/amd/assets/logo1-25253976.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/amd/assets/logo2-25253976.svg b/test/chunking-form/samples/asset-emission/_expected/amd/assets/logo2-25253976.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/amd/assets/logo2-25253976.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/amd/assets/test-19916f7d.ext b/test/chunking-form/samples/asset-emission/_expected/amd/assets/test-19916f7d.ext deleted file mode 100644 index 95d09f2b101..00000000000 --- a/test/chunking-form/samples/asset-emission/_expected/amd/assets/test-19916f7d.ext +++ /dev/null @@ -1 +0,0 @@ -hello world \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/amd/main.js b/test/chunking-form/samples/asset-emission/_expected/amd/main.js index 1b97643a906..88a74ef7790 100644 --- a/test/chunking-form/samples/asset-emission/_expected/amd/main.js +++ b/test/chunking-form/samples/asset-emission/_expected/amd/main.js @@ -1,7 +1,8 @@ -define(['module'], function (module) { 'use strict'; +define(['module', 'require', './nested/chunk.js'], function (module, require, __chunk_1) { 'use strict'; - var main = new URL((typeof process !== 'undefined' && process.versions && process.versions.node ? 'file:' : '') + module.uri + '/../assets/test-19916f7d.ext').href; + var logo = new URL(module.uri + '/../assets/logo1-25253976.svg', document.baseURI).href; - return main; + __chunk_1.showImage(logo); + new Promise(function (resolve, reject) { require(['./nested/chunk2.js'], resolve, reject) }); }); diff --git a/test/chunking-form/samples/asset-emission/_expected/amd/nested/chunk.js b/test/chunking-form/samples/asset-emission/_expected/amd/nested/chunk.js new file mode 100644 index 00000000000..37057bda9a9 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/amd/nested/chunk.js @@ -0,0 +1,14 @@ +define(['exports'], function (exports) { 'use strict'; + + function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } + } + + exports.showImage = showImage; + +}); diff --git a/test/chunking-form/samples/asset-emission/_expected/amd/nested/chunk2.js b/test/chunking-form/samples/asset-emission/_expected/amd/nested/chunk2.js new file mode 100644 index 00000000000..f4f035d353e --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/amd/nested/chunk2.js @@ -0,0 +1,7 @@ +define(['module', './chunk.js'], function (module, __chunk_1) { 'use strict'; + + var logo = new URL(module.uri + '/../../assets/logo2-25253976.svg', document.baseURI).href; + + __chunk_1.showImage(logo); + +}); diff --git a/test/chunking-form/samples/asset-emission/_expected/cjs/assets/logo1-25253976.svg b/test/chunking-form/samples/asset-emission/_expected/cjs/assets/logo1-25253976.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/cjs/assets/logo1-25253976.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/cjs/assets/logo2-25253976.svg b/test/chunking-form/samples/asset-emission/_expected/cjs/assets/logo2-25253976.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/cjs/assets/logo2-25253976.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/cjs/assets/test-19916f7d.ext b/test/chunking-form/samples/asset-emission/_expected/cjs/assets/test-19916f7d.ext deleted file mode 100644 index 95d09f2b101..00000000000 --- a/test/chunking-form/samples/asset-emission/_expected/cjs/assets/test-19916f7d.ext +++ /dev/null @@ -1 +0,0 @@ -hello world \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/cjs/main.js b/test/chunking-form/samples/asset-emission/_expected/cjs/main.js index b26ac590cd4..e6461f5d2b4 100644 --- a/test/chunking-form/samples/asset-emission/_expected/cjs/main.js +++ b/test/chunking-form/samples/asset-emission/_expected/cjs/main.js @@ -1,5 +1,8 @@ 'use strict'; -var main = new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)((process.browser ? '' : 'file:') + __dirname + '/assets/test-19916f7d.ext', process.browser && document.baseURI).href; +var __chunk_1 = require('./nested/chunk.js'); -module.exports = main; +var logo = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/logo1-25253976.svg').href : new URL((document.currentScript && document.currentScript.src || document.baseURI) + '/../assets/logo1-25253976.svg').href); + +__chunk_1.showImage(logo); +Promise.resolve(require('./nested/chunk2.js')); diff --git a/test/chunking-form/samples/asset-emission/_expected/cjs/nested/chunk.js b/test/chunking-form/samples/asset-emission/_expected/cjs/nested/chunk.js new file mode 100644 index 00000000000..ddb8d67c1f2 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/cjs/nested/chunk.js @@ -0,0 +1,12 @@ +'use strict'; + +function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } +} + +exports.showImage = showImage; diff --git a/test/chunking-form/samples/asset-emission/_expected/cjs/nested/chunk2.js b/test/chunking-form/samples/asset-emission/_expected/cjs/nested/chunk2.js new file mode 100644 index 00000000000..195d71e0356 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/cjs/nested/chunk2.js @@ -0,0 +1,7 @@ +'use strict'; + +var __chunk_1 = require('./chunk.js'); + +var logo = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/../assets/logo2-25253976.svg').href : new URL((document.currentScript && document.currentScript.src || document.baseURI) + '/../../assets/logo2-25253976.svg').href); + +__chunk_1.showImage(logo); diff --git a/test/chunking-form/samples/asset-emission/_expected/es/assets/logo1-25253976.svg b/test/chunking-form/samples/asset-emission/_expected/es/assets/logo1-25253976.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/es/assets/logo1-25253976.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/es/assets/logo2-25253976.svg b/test/chunking-form/samples/asset-emission/_expected/es/assets/logo2-25253976.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/es/assets/logo2-25253976.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/es/assets/test-19916f7d.ext b/test/chunking-form/samples/asset-emission/_expected/es/assets/test-19916f7d.ext deleted file mode 100644 index 95d09f2b101..00000000000 --- a/test/chunking-form/samples/asset-emission/_expected/es/assets/test-19916f7d.ext +++ /dev/null @@ -1 +0,0 @@ -hello world \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/es/main.js b/test/chunking-form/samples/asset-emission/_expected/es/main.js index 715ecc0722d..cd37d610503 100644 --- a/test/chunking-form/samples/asset-emission/_expected/es/main.js +++ b/test/chunking-form/samples/asset-emission/_expected/es/main.js @@ -1,3 +1,6 @@ -var main = new URL('../assets/test-19916f7d.ext', import.meta.url).href; +import { a as showImage } from './nested/chunk.js'; -export default main; +var logo = new URL('assets/logo1-25253976.svg', import.meta.url).href; + +showImage(logo); +import('./nested/chunk2.js'); diff --git a/test/chunking-form/samples/asset-emission/_expected/es/nested/chunk.js b/test/chunking-form/samples/asset-emission/_expected/es/nested/chunk.js new file mode 100644 index 00000000000..614dfc331dc --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/es/nested/chunk.js @@ -0,0 +1,10 @@ +function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } +} + +export { showImage as a }; diff --git a/test/chunking-form/samples/asset-emission/_expected/es/nested/chunk2.js b/test/chunking-form/samples/asset-emission/_expected/es/nested/chunk2.js new file mode 100644 index 00000000000..fe6455a36b8 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/es/nested/chunk2.js @@ -0,0 +1,5 @@ +import { a as showImage } from './chunk.js'; + +var logo = new URL('../assets/logo2-25253976.svg', import.meta.url).href; + +showImage(logo); diff --git a/test/chunking-form/samples/asset-emission/_expected/system/assets/logo1-25253976.svg b/test/chunking-form/samples/asset-emission/_expected/system/assets/logo1-25253976.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/system/assets/logo1-25253976.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/system/assets/logo2-25253976.svg b/test/chunking-form/samples/asset-emission/_expected/system/assets/logo2-25253976.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/system/assets/logo2-25253976.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/system/assets/test-19916f7d.ext b/test/chunking-form/samples/asset-emission/_expected/system/assets/test-19916f7d.ext deleted file mode 100644 index 95d09f2b101..00000000000 --- a/test/chunking-form/samples/asset-emission/_expected/system/assets/test-19916f7d.ext +++ /dev/null @@ -1 +0,0 @@ -hello world \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/_expected/system/main.js b/test/chunking-form/samples/asset-emission/_expected/system/main.js index c6b29b073e2..92e22a533e7 100644 --- a/test/chunking-form/samples/asset-emission/_expected/system/main.js +++ b/test/chunking-form/samples/asset-emission/_expected/system/main.js @@ -1,9 +1,16 @@ -System.register([], function (exports, module) { +System.register(['./nested/chunk.js'], function (exports, module) { 'use strict'; + var showImage; return { + setters: [function (module) { + showImage = module.a; + }], execute: function () { - var main = exports('default', new URL('../assets/test-19916f7d.ext', module.url).href); + var logo = new URL('assets/logo1-25253976.svg', module.meta.url).href; + + showImage(logo); + module.import('./nested/chunk2.js'); } }; diff --git a/test/chunking-form/samples/asset-emission/_expected/system/nested/chunk.js b/test/chunking-form/samples/asset-emission/_expected/system/nested/chunk.js new file mode 100644 index 00000000000..6e733ba1c92 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/system/nested/chunk.js @@ -0,0 +1,19 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + exports('a', showImage); + + function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } + } + + } + }; +}); diff --git a/test/chunking-form/samples/asset-emission/_expected/system/nested/chunk2.js b/test/chunking-form/samples/asset-emission/_expected/system/nested/chunk2.js new file mode 100644 index 00000000000..95cb8f90a22 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/_expected/system/nested/chunk2.js @@ -0,0 +1,16 @@ +System.register(['./chunk.js'], function (exports, module) { + 'use strict'; + var showImage; + return { + setters: [function (module) { + showImage = module.a; + }], + execute: function () { + + var logo = new URL('../assets/logo2-25253976.svg', module.meta.url).href; + + showImage(logo); + + } + }; +}); diff --git a/test/chunking-form/samples/asset-emission/index-amd.html b/test/chunking-form/samples/asset-emission/index-amd.html new file mode 100644 index 00000000000..476b9ce3df1 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/index-amd.html @@ -0,0 +1,10 @@ + + + + + AMD + + + + + diff --git a/test/chunking-form/samples/asset-emission/index.html b/test/chunking-form/samples/asset-emission/index-es.html similarity index 100% rename from test/chunking-form/samples/asset-emission/index.html rename to test/chunking-form/samples/asset-emission/index-es.html diff --git a/test/chunking-form/samples/asset-emission/index-system.html b/test/chunking-form/samples/asset-emission/index-system.html new file mode 100644 index 00000000000..96c687c1eb7 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/index-system.html @@ -0,0 +1,11 @@ + + + + + SystemJS + + + + + + diff --git a/test/chunking-form/samples/asset-emission/logo1.svg b/test/chunking-form/samples/asset-emission/logo1.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/logo1.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/logo2.svg b/test/chunking-form/samples/asset-emission/logo2.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/logo2.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/chunking-form/samples/asset-emission/main.js b/test/chunking-form/samples/asset-emission/main.js index e69de29bb2d..5c1787b77b4 100644 --- a/test/chunking-form/samples/asset-emission/main.js +++ b/test/chunking-form/samples/asset-emission/main.js @@ -0,0 +1,5 @@ +import logo from './logo1.svg'; +import { showImage } from './show-image'; + +showImage(logo); +import('./nested.js'); diff --git a/test/chunking-form/samples/asset-emission/nested.js b/test/chunking-form/samples/asset-emission/nested.js new file mode 100644 index 00000000000..ceb0a68e6e1 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/nested.js @@ -0,0 +1,4 @@ +import logo from './logo2.svg'; +import { showImage } from './show-image'; + +showImage(logo); diff --git a/test/chunking-form/samples/asset-emission/show-image.js b/test/chunking-form/samples/asset-emission/show-image.js new file mode 100644 index 00000000000..28c706ab24b --- /dev/null +++ b/test/chunking-form/samples/asset-emission/show-image.js @@ -0,0 +1,8 @@ +export function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } +} diff --git a/test/chunking-form/samples/configure-asset-url/_config.js b/test/chunking-form/samples/configure-asset-url/_config.js new file mode 100644 index 00000000000..5bdb9770752 --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_config.js @@ -0,0 +1,41 @@ +module.exports = { + description: 'allows to configure asset urls', + options: { + output: { + chunkFileNames: 'nested/chunk.js' + }, + plugins: [ + { + resolveId(id) { + if (id.endsWith('solved')) { + return id; + } + }, + load(id) { + if (id.endsWith('solved')) { + const assetId = this.emitAsset(`asset-${id}.txt`, `Asset for: ${id}`); + return `export default import.meta.ROLLUP_ASSET_URL_${assetId};`; + } + }, + resolveAssetUrl({ chunkId, moduleId, assetFileName, relativeAssetPath }) { + if (!moduleId.endsWith('resolved')) { + return `'${chunkId}:${moduleId + .replace(/\\/g, '/') + .split('/') + .slice(-2) + .join('/')}:${assetFileName}:${relativeAssetPath}'`; + } + return null; + } + }, + { + resolveAssetUrl({ moduleId }) { + if (moduleId === 'resolved') { + return `'resolved'`; + } + return null; + } + } + ] + } +}; diff --git a/test/chunking-form/samples/configure-asset-url/_expected/amd/assets/asset-resolved-dfc93baf.txt b/test/chunking-form/samples/configure-asset-url/_expected/amd/assets/asset-resolved-dfc93baf.txt new file mode 100644 index 00000000000..31fb300f988 --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/amd/assets/asset-resolved-dfc93baf.txt @@ -0,0 +1 @@ +Asset for: resolved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/amd/assets/asset-solved-9b321da2.txt b/test/chunking-form/samples/configure-asset-url/_expected/amd/assets/asset-solved-9b321da2.txt new file mode 100644 index 00000000000..07c7a096c7e --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/amd/assets/asset-solved-9b321da2.txt @@ -0,0 +1 @@ +Asset for: solved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/amd/assets/asset-unresolved-9548436d.txt b/test/chunking-form/samples/configure-asset-url/_expected/amd/assets/asset-unresolved-9548436d.txt new file mode 100644 index 00000000000..fd3ad6261cc --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/amd/assets/asset-unresolved-9548436d.txt @@ -0,0 +1 @@ +Asset for: unresolved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/amd/main.js b/test/chunking-form/samples/configure-asset-url/_expected/amd/main.js new file mode 100644 index 00000000000..3d245c68235 --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/amd/main.js @@ -0,0 +1,9 @@ +define(['module', 'require'], function (module, require) { 'use strict'; + + var asset2 = 'resolved'; + + var asset3 = new URL(module.uri + '/../assets/asset-unresolved-9548436d.txt', document.baseURI).href; + + new Promise(function (resolve, reject) { require(['./nested/chunk.js'], resolve, reject) }).then(result => console.log(result, asset2, asset3)); + +}); diff --git a/test/chunking-form/samples/configure-asset-url/_expected/amd/nested/chunk.js b/test/chunking-form/samples/configure-asset-url/_expected/amd/nested/chunk.js new file mode 100644 index 00000000000..bdbd7f73c64 --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/amd/nested/chunk.js @@ -0,0 +1,7 @@ +define(['module', 'exports'], function (module, exports) { 'use strict'; + + var solved = 'nested/chunk.js:solved:assets/asset-solved-9b321da2.txt:../assets/asset-solved-9b321da2.txt'; + + exports.default = solved; + +}); diff --git a/test/chunking-form/samples/configure-asset-url/_expected/cjs/assets/asset-resolved-dfc93baf.txt b/test/chunking-form/samples/configure-asset-url/_expected/cjs/assets/asset-resolved-dfc93baf.txt new file mode 100644 index 00000000000..31fb300f988 --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/cjs/assets/asset-resolved-dfc93baf.txt @@ -0,0 +1 @@ +Asset for: resolved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/cjs/assets/asset-solved-9b321da2.txt b/test/chunking-form/samples/configure-asset-url/_expected/cjs/assets/asset-solved-9b321da2.txt new file mode 100644 index 00000000000..07c7a096c7e --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/cjs/assets/asset-solved-9b321da2.txt @@ -0,0 +1 @@ +Asset for: solved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/cjs/assets/asset-unresolved-9548436d.txt b/test/chunking-form/samples/configure-asset-url/_expected/cjs/assets/asset-unresolved-9548436d.txt new file mode 100644 index 00000000000..fd3ad6261cc --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/cjs/assets/asset-unresolved-9548436d.txt @@ -0,0 +1 @@ +Asset for: unresolved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/cjs/main.js b/test/chunking-form/samples/configure-asset-url/_expected/cjs/main.js new file mode 100644 index 00000000000..02ac6ba542d --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/cjs/main.js @@ -0,0 +1,7 @@ +'use strict'; + +var asset2 = 'resolved'; + +var asset3 = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-unresolved-9548436d.txt').href : new URL((document.currentScript && document.currentScript.src || document.baseURI) + '/../assets/asset-unresolved-9548436d.txt').href); + +Promise.resolve(require('./nested/chunk.js')).then(result => console.log(result, asset2, asset3)); diff --git a/test/chunking-form/samples/configure-asset-url/_expected/cjs/nested/chunk.js b/test/chunking-form/samples/configure-asset-url/_expected/cjs/nested/chunk.js new file mode 100644 index 00000000000..42f557796ee --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/cjs/nested/chunk.js @@ -0,0 +1,5 @@ +'use strict'; + +var solved = 'nested/chunk.js:solved:assets/asset-solved-9b321da2.txt:../assets/asset-solved-9b321da2.txt'; + +exports.default = solved; diff --git a/test/chunking-form/samples/configure-asset-url/_expected/es/assets/asset-resolved-dfc93baf.txt b/test/chunking-form/samples/configure-asset-url/_expected/es/assets/asset-resolved-dfc93baf.txt new file mode 100644 index 00000000000..31fb300f988 --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/es/assets/asset-resolved-dfc93baf.txt @@ -0,0 +1 @@ +Asset for: resolved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/es/assets/asset-solved-9b321da2.txt b/test/chunking-form/samples/configure-asset-url/_expected/es/assets/asset-solved-9b321da2.txt new file mode 100644 index 00000000000..07c7a096c7e --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/es/assets/asset-solved-9b321da2.txt @@ -0,0 +1 @@ +Asset for: solved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/es/assets/asset-unresolved-9548436d.txt b/test/chunking-form/samples/configure-asset-url/_expected/es/assets/asset-unresolved-9548436d.txt new file mode 100644 index 00000000000..fd3ad6261cc --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/es/assets/asset-unresolved-9548436d.txt @@ -0,0 +1 @@ +Asset for: unresolved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/es/main.js b/test/chunking-form/samples/configure-asset-url/_expected/es/main.js new file mode 100644 index 00000000000..f061c14feed --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/es/main.js @@ -0,0 +1,5 @@ +var asset2 = 'resolved'; + +var asset3 = new URL('assets/asset-unresolved-9548436d.txt', import.meta.url).href; + +import('./nested/chunk.js').then(result => console.log(result, asset2, asset3)); diff --git a/test/chunking-form/samples/configure-asset-url/_expected/es/nested/chunk.js b/test/chunking-form/samples/configure-asset-url/_expected/es/nested/chunk.js new file mode 100644 index 00000000000..70feca23425 --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/es/nested/chunk.js @@ -0,0 +1,3 @@ +var solved = 'nested/chunk.js:solved:assets/asset-solved-9b321da2.txt:../assets/asset-solved-9b321da2.txt'; + +export default solved; diff --git a/test/chunking-form/samples/configure-asset-url/_expected/system/assets/asset-resolved-dfc93baf.txt b/test/chunking-form/samples/configure-asset-url/_expected/system/assets/asset-resolved-dfc93baf.txt new file mode 100644 index 00000000000..31fb300f988 --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/system/assets/asset-resolved-dfc93baf.txt @@ -0,0 +1 @@ +Asset for: resolved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/system/assets/asset-solved-9b321da2.txt b/test/chunking-form/samples/configure-asset-url/_expected/system/assets/asset-solved-9b321da2.txt new file mode 100644 index 00000000000..07c7a096c7e --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/system/assets/asset-solved-9b321da2.txt @@ -0,0 +1 @@ +Asset for: solved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/system/assets/asset-unresolved-9548436d.txt b/test/chunking-form/samples/configure-asset-url/_expected/system/assets/asset-unresolved-9548436d.txt new file mode 100644 index 00000000000..fd3ad6261cc --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/system/assets/asset-unresolved-9548436d.txt @@ -0,0 +1 @@ +Asset for: unresolved \ No newline at end of file diff --git a/test/chunking-form/samples/configure-asset-url/_expected/system/main.js b/test/chunking-form/samples/configure-asset-url/_expected/system/main.js new file mode 100644 index 00000000000..ee045a93e6e --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/system/main.js @@ -0,0 +1,14 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + var asset2 = 'resolved'; + + var asset3 = new URL('assets/asset-unresolved-9548436d.txt', module.meta.url).href; + + module.import('./nested/chunk.js').then(result => console.log(result, asset2, asset3)); + + } + }; +}); diff --git a/test/chunking-form/samples/configure-asset-url/_expected/system/nested/chunk.js b/test/chunking-form/samples/configure-asset-url/_expected/system/nested/chunk.js new file mode 100644 index 00000000000..59d15ec5e1b --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/_expected/system/nested/chunk.js @@ -0,0 +1,10 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + var solved = exports('default', 'nested/chunk.js:solved:assets/asset-solved-9b321da2.txt:../assets/asset-solved-9b321da2.txt'); + + } + }; +}); diff --git a/test/chunking-form/samples/configure-asset-url/main.js b/test/chunking-form/samples/configure-asset-url/main.js new file mode 100644 index 00000000000..b3f01707026 --- /dev/null +++ b/test/chunking-form/samples/configure-asset-url/main.js @@ -0,0 +1,4 @@ +import asset2 from 'resolved'; +import asset3 from 'unresolved'; + +import('solved').then(result => console.log(result, asset2, asset3)); diff --git a/test/form/samples/asset-emission/_config.js b/test/form/samples/asset-emission/_config.js new file mode 100644 index 00000000000..bdf8e5fdfde --- /dev/null +++ b/test/form/samples/asset-emission/_config.js @@ -0,0 +1,23 @@ +const fs = require('fs'); +const path = require('path'); + +module.exports = { + description: 'supports emitting assets from plugin hooks', + options: { + plugins: { + resolveId(id, importee) { + if (id.endsWith('.svg')) { + return path.resolve(path.dirname(importee), id); + } + }, + load(id) { + if (id.endsWith('.svg')) { + return `export default import.meta.ROLLUP_ASSET_URL_${this.emitAsset( + path.basename(id), + fs.readFileSync(id) + )};`; + } + } + } + } +}; diff --git a/test/form/samples/asset-emission/_expected/amd.js b/test/form/samples/asset-emission/_expected/amd.js new file mode 100644 index 00000000000..e981d0ded96 --- /dev/null +++ b/test/form/samples/asset-emission/_expected/amd.js @@ -0,0 +1,16 @@ +define(['module'], function (module) { 'use strict'; + + var logo = new URL(module.uri + '/../assets/logo-25253976.svg', document.baseURI).href; + + function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } + } + + showImage(logo); + +}); diff --git a/test/form/samples/asset-emission/_expected/assets/logo-25253976.svg b/test/form/samples/asset-emission/_expected/assets/logo-25253976.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/form/samples/asset-emission/_expected/assets/logo-25253976.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/form/samples/asset-emission/_expected/cjs.js b/test/form/samples/asset-emission/_expected/cjs.js new file mode 100644 index 00000000000..55eb8a4a004 --- /dev/null +++ b/test/form/samples/asset-emission/_expected/cjs.js @@ -0,0 +1,14 @@ +'use strict'; + +var logo = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/logo-25253976.svg').href : new URL((document.currentScript && document.currentScript.src || document.baseURI) + '/../assets/logo-25253976.svg').href); + +function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } +} + +showImage(logo); diff --git a/test/form/samples/asset-emission/_expected/es.js b/test/form/samples/asset-emission/_expected/es.js new file mode 100644 index 00000000000..6ad86e3a083 --- /dev/null +++ b/test/form/samples/asset-emission/_expected/es.js @@ -0,0 +1,12 @@ +var logo = new URL('assets/logo-25253976.svg', import.meta.url).href; + +function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } +} + +showImage(logo); diff --git a/test/form/samples/asset-emission/_expected/iife.js b/test/form/samples/asset-emission/_expected/iife.js new file mode 100644 index 00000000000..561aff639dc --- /dev/null +++ b/test/form/samples/asset-emission/_expected/iife.js @@ -0,0 +1,17 @@ +(function () { + 'use strict'; + + var logo = new URL((document.currentScript && document.currentScript.src || document.baseURI) + '/../assets/logo-25253976.svg').href; + + function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } + } + + showImage(logo); + +}()); diff --git a/test/form/samples/asset-emission/_expected/system.js b/test/form/samples/asset-emission/_expected/system.js new file mode 100644 index 00000000000..075df5052ee --- /dev/null +++ b/test/form/samples/asset-emission/_expected/system.js @@ -0,0 +1,21 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + var logo = new URL('assets/logo-25253976.svg', module.meta.url).href; + + function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } + } + + showImage(logo); + + } + }; +}); diff --git a/test/form/samples/asset-emission/_expected/umd.js b/test/form/samples/asset-emission/_expected/umd.js new file mode 100644 index 00000000000..4bae7b42681 --- /dev/null +++ b/test/form/samples/asset-emission/_expected/umd.js @@ -0,0 +1,19 @@ +(function (factory) { + typeof define === 'function' && define.amd ? define(factory) : + factory(); +}(function () { 'use strict'; + + var logo = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/logo-25253976.svg').href : new URL((document.currentScript && document.currentScript.src || document.baseURI) + '/../assets/logo-25253976.svg').href); + + function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } + } + + showImage(logo); + +})); diff --git a/test/form/samples/asset-emission/index-amd.html b/test/form/samples/asset-emission/index-amd.html new file mode 100644 index 00000000000..e89cb396ea2 --- /dev/null +++ b/test/form/samples/asset-emission/index-amd.html @@ -0,0 +1,10 @@ + + + + + AMD + + + + + diff --git a/test/form/samples/asset-emission/index-cjs.html b/test/form/samples/asset-emission/index-cjs.html new file mode 100644 index 00000000000..c5b18ec148f --- /dev/null +++ b/test/form/samples/asset-emission/index-cjs.html @@ -0,0 +1,10 @@ + + + + + CommonJS + + + + + diff --git a/test/form/samples/asset-emission/index-es.html b/test/form/samples/asset-emission/index-es.html new file mode 100644 index 00000000000..5079fce53d8 --- /dev/null +++ b/test/form/samples/asset-emission/index-es.html @@ -0,0 +1,10 @@ + + + + + ES Module + + + + + diff --git a/test/form/samples/asset-emission/index-iife.html b/test/form/samples/asset-emission/index-iife.html new file mode 100644 index 00000000000..aaf475f1505 --- /dev/null +++ b/test/form/samples/asset-emission/index-iife.html @@ -0,0 +1,10 @@ + + + + + IIFE + + + + + diff --git a/test/form/samples/asset-emission/index-system.html b/test/form/samples/asset-emission/index-system.html new file mode 100644 index 00000000000..bef9897957b --- /dev/null +++ b/test/form/samples/asset-emission/index-system.html @@ -0,0 +1,11 @@ + + + + + SystemJS + + + + + + diff --git a/test/form/samples/asset-emission/index-umd-amd.html b/test/form/samples/asset-emission/index-umd-amd.html new file mode 100644 index 00000000000..e644ea36370 --- /dev/null +++ b/test/form/samples/asset-emission/index-umd-amd.html @@ -0,0 +1,10 @@ + + + + + UMD (AMD) + + + + + diff --git a/test/form/samples/asset-emission/index-umd-iife.html b/test/form/samples/asset-emission/index-umd-iife.html new file mode 100644 index 00000000000..202c1dad8f0 --- /dev/null +++ b/test/form/samples/asset-emission/index-umd-iife.html @@ -0,0 +1,10 @@ + + + + + UMD (IIFE) + + + + + diff --git a/test/form/samples/asset-emission/logo.svg b/test/form/samples/asset-emission/logo.svg new file mode 100644 index 00000000000..20bb5dfe639 --- /dev/null +++ b/test/form/samples/asset-emission/logo.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/form/samples/asset-emission/main.js b/test/form/samples/asset-emission/main.js new file mode 100644 index 00000000000..c63771af02d --- /dev/null +++ b/test/form/samples/asset-emission/main.js @@ -0,0 +1,12 @@ +import logo from './logo.svg'; + +function showImage(url) { + console.log(url); + if (typeof document !== 'undefined') { + const image = document.createElement('img'); + image.src = url; + document.body.appendChild(image); + } +} + +showImage(logo); diff --git a/test/form/samples/configure-asset-url/_config.js b/test/form/samples/configure-asset-url/_config.js new file mode 100644 index 00000000000..022a748f957 --- /dev/null +++ b/test/form/samples/configure-asset-url/_config.js @@ -0,0 +1,38 @@ +module.exports = { + description: 'allows to configure asset urls', + options: { + plugins: [ + { + resolveId(id) { + if (id.endsWith('solved')) { + return id; + } + }, + load(id) { + if (id.endsWith('solved')) { + const assetId = this.emitAsset(`asset-${id}.txt`, `Asset for: ${id}`); + return `export default import.meta.ROLLUP_ASSET_URL_${assetId};`; + } + }, + resolveAssetUrl({ chunkId, moduleId, assetFileName, relativeAssetPath }) { + if (!moduleId.endsWith('resolved')) { + return `'${chunkId}:${moduleId + .replace(/\\/g, '/') + .split('/') + .slice(-2) + .join('/')}:${assetFileName}:${relativeAssetPath}'`; + } + return null; + } + }, + { + resolveAssetUrl({ moduleId }) { + if (moduleId === 'resolved') { + return `'resolved'`; + } + return null; + } + } + ] + } +}; diff --git a/test/form/samples/configure-asset-url/_expected/amd.js b/test/form/samples/configure-asset-url/_expected/amd.js new file mode 100644 index 00000000000..c8302dda1c6 --- /dev/null +++ b/test/form/samples/configure-asset-url/_expected/amd.js @@ -0,0 +1,11 @@ +define(['module'], function (module) { 'use strict'; + + var asset1 = 'amd.js:solved:assets/asset-solved-9b321da2.txt:assets/asset-solved-9b321da2.txt'; + + var asset2 = 'resolved'; + + var asset3 = new URL(module.uri + '/../assets/asset-unresolved-9548436d.txt', document.baseURI).href; + + console.log(asset1, asset2, asset3); + +}); diff --git a/test/form/samples/configure-asset-url/_expected/assets/asset-resolved-dfc93baf.txt b/test/form/samples/configure-asset-url/_expected/assets/asset-resolved-dfc93baf.txt new file mode 100644 index 00000000000..31fb300f988 --- /dev/null +++ b/test/form/samples/configure-asset-url/_expected/assets/asset-resolved-dfc93baf.txt @@ -0,0 +1 @@ +Asset for: resolved \ No newline at end of file diff --git a/test/form/samples/configure-asset-url/_expected/assets/asset-solved-9b321da2.txt b/test/form/samples/configure-asset-url/_expected/assets/asset-solved-9b321da2.txt new file mode 100644 index 00000000000..07c7a096c7e --- /dev/null +++ b/test/form/samples/configure-asset-url/_expected/assets/asset-solved-9b321da2.txt @@ -0,0 +1 @@ +Asset for: solved \ No newline at end of file diff --git a/test/form/samples/configure-asset-url/_expected/assets/asset-unresolved-9548436d.txt b/test/form/samples/configure-asset-url/_expected/assets/asset-unresolved-9548436d.txt new file mode 100644 index 00000000000..fd3ad6261cc --- /dev/null +++ b/test/form/samples/configure-asset-url/_expected/assets/asset-unresolved-9548436d.txt @@ -0,0 +1 @@ +Asset for: unresolved \ No newline at end of file diff --git a/test/form/samples/configure-asset-url/_expected/cjs.js b/test/form/samples/configure-asset-url/_expected/cjs.js new file mode 100644 index 00000000000..34e6af70544 --- /dev/null +++ b/test/form/samples/configure-asset-url/_expected/cjs.js @@ -0,0 +1,9 @@ +'use strict'; + +var asset1 = 'cjs.js:solved:assets/asset-solved-9b321da2.txt:assets/asset-solved-9b321da2.txt'; + +var asset2 = 'resolved'; + +var asset3 = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-unresolved-9548436d.txt').href : new URL((document.currentScript && document.currentScript.src || document.baseURI) + '/../assets/asset-unresolved-9548436d.txt').href); + +console.log(asset1, asset2, asset3); diff --git a/test/form/samples/configure-asset-url/_expected/es.js b/test/form/samples/configure-asset-url/_expected/es.js new file mode 100644 index 00000000000..91697ce916a --- /dev/null +++ b/test/form/samples/configure-asset-url/_expected/es.js @@ -0,0 +1,7 @@ +var asset1 = 'es.js:solved:assets/asset-solved-9b321da2.txt:assets/asset-solved-9b321da2.txt'; + +var asset2 = 'resolved'; + +var asset3 = new URL('assets/asset-unresolved-9548436d.txt', import.meta.url).href; + +console.log(asset1, asset2, asset3); diff --git a/test/form/samples/configure-asset-url/_expected/iife.js b/test/form/samples/configure-asset-url/_expected/iife.js new file mode 100644 index 00000000000..23d28d32452 --- /dev/null +++ b/test/form/samples/configure-asset-url/_expected/iife.js @@ -0,0 +1,12 @@ +(function () { + 'use strict'; + + var asset1 = 'iife.js:solved:assets/asset-solved-9b321da2.txt:assets/asset-solved-9b321da2.txt'; + + var asset2 = 'resolved'; + + var asset3 = new URL((document.currentScript && document.currentScript.src || document.baseURI) + '/../assets/asset-unresolved-9548436d.txt').href; + + console.log(asset1, asset2, asset3); + +}()); diff --git a/test/form/samples/configure-asset-url/_expected/system.js b/test/form/samples/configure-asset-url/_expected/system.js new file mode 100644 index 00000000000..1b2643e4385 --- /dev/null +++ b/test/form/samples/configure-asset-url/_expected/system.js @@ -0,0 +1,16 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + var asset1 = 'system.js:solved:assets/asset-solved-9b321da2.txt:assets/asset-solved-9b321da2.txt'; + + var asset2 = 'resolved'; + + var asset3 = new URL('assets/asset-unresolved-9548436d.txt', module.meta.url).href; + + console.log(asset1, asset2, asset3); + + } + }; +}); diff --git a/test/form/samples/configure-asset-url/_expected/umd.js b/test/form/samples/configure-asset-url/_expected/umd.js new file mode 100644 index 00000000000..9a058fff2d7 --- /dev/null +++ b/test/form/samples/configure-asset-url/_expected/umd.js @@ -0,0 +1,14 @@ +(function (factory) { + typeof define === 'function' && define.amd ? define(factory) : + factory(); +}(function () { 'use strict'; + + var asset1 = 'umd.js:solved:assets/asset-solved-9b321da2.txt:assets/asset-solved-9b321da2.txt'; + + var asset2 = 'resolved'; + + var asset3 = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/asset-unresolved-9548436d.txt').href : new URL((document.currentScript && document.currentScript.src || document.baseURI) + '/../assets/asset-unresolved-9548436d.txt').href); + + console.log(asset1, asset2, asset3); + +})); diff --git a/test/form/samples/configure-asset-url/main.js b/test/form/samples/configure-asset-url/main.js new file mode 100644 index 00000000000..74a56461e3a --- /dev/null +++ b/test/form/samples/configure-asset-url/main.js @@ -0,0 +1,5 @@ +import asset1 from 'solved'; +import asset2 from 'resolved'; +import asset3 from 'unresolved'; + +console.log(asset1, asset2, asset3); diff --git a/test/form/samples/configure-import-meta-url/_expected/system.js b/test/form/samples/configure-import-meta-url/_expected/system.js index 0023ad18505..fd3c4bfbd11 100644 --- a/test/form/samples/configure-import-meta-url/_expected/system.js +++ b/test/form/samples/configure-import-meta-url/_expected/system.js @@ -8,8 +8,8 @@ System.register([], function (exports, module) { console.log('resolved'); console.log(module.meta.url); - console.log(undefined); - console.log(({ url: module.meta.url })); + console.log(module.meta.privateProp); + console.log(module.meta); console.log('url=system.js:configure-import-meta-url/main.js'); console.log('privateProp=system.js:configure-import-meta-url/main.js'); diff --git a/test/hooks/index.js b/test/hooks/index.js index dd26ae632c5..6568c68e24c 100644 --- a/test/hooks/index.js +++ b/test/hooks/index.js @@ -385,7 +385,7 @@ describe('hooks', () => { assert.equal(output[0].isEntry, true); assert.equal( output[0].code, - `var input = new URL('../assets/test-19916f7d.ext', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('assets/test-19916f7d.ext', import.meta.url).href;\n\nexport default input;\n` ); }); }); @@ -412,7 +412,7 @@ describe('hooks', () => { .then(({ output }) => { assert.equal( output[0].code, - `var input = new URL('../assets/test-19916f7d.ext', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('assets/test-19916f7d.ext', import.meta.url).href;\n\nexport default input;\n` ); assert.equal(output[1].fileName, 'assets/test-19916f7d.ext'); assert.equal(output[1].source, 'hello world'); @@ -438,7 +438,7 @@ describe('hooks', () => { .then(({ output }) => { assert.equal( output[0].code, - `var input = new URL('../assets/test-19916f7d.ext', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('assets/test-19916f7d.ext', import.meta.url).href;\n\nexport default input;\n` ); assert.equal(output[1].fileName, 'assets/test-19916f7d.ext'); assert.equal(output[1].source, 'hello world'); @@ -473,7 +473,7 @@ describe('hooks', () => { .then(({ output }) => { assert.equal( output[0].code, - `var input = new URL('../assets/test-19916f7d.ext', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('assets/test-19916f7d.ext', import.meta.url).href;\n\nexport default input;\n` ); assert.equal(output[1].fileName, 'assets/test-19916f7d.ext'); assert.equal(output[1].source, 'hello world'); @@ -527,7 +527,7 @@ describe('hooks', () => { code, `'use strict'; -var input = new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)((process.browser ? '' : 'file:') + __dirname + '/assets/test-19916f7d.ext', process.browser && document.baseURI).href; +var input = (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __dirname + '/assets/test-19916f7d.ext').href : new URL((document.currentScript && document.currentScript.src || document.baseURI) + '/../assets/test-19916f7d.ext').href); module.exports = input; ` @@ -793,7 +793,7 @@ module.exports = input; assert.equal(outputBundle['assets/test-19916f7d.ext'].source, 'hello world'); assert.equal( outputBundle['input.js'].code, - `var input = new URL('../assets/test-19916f7d.ext', import.meta.url).href;\n\nexport default input;\n` + `var input = new URL('assets/test-19916f7d.ext', import.meta.url).href;\n\nexport default input;\n` ); } }