From 5665701d9ba0992a85a33563307113d27190b18b Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sun, 31 Mar 2019 18:51:21 +0200 Subject: [PATCH] Refactor import.meta.url handling --- package-lock.json | 6 +++ package.json | 1 + src/ast/nodes/MetaProperty.ts | 46 +++++++++---------- .../samples/asset-emission/_config.js | 1 - .../samples/asset-emission/index.html | 10 ++++ .../samples/import-meta-url/_config.js | 8 ++++ .../import-meta-url/_expected/amd/main.js | 6 +++ .../_expected/amd/nested/chunk.js | 13 ++++++ .../_expected/amd/nested/chunk2.js | 5 ++ .../import-meta-url/_expected/cjs/main.js | 6 +++ .../_expected/cjs/nested/chunk.js | 11 +++++ .../_expected/cjs/nested/chunk2.js | 5 ++ .../import-meta-url/_expected/es/main.js | 4 ++ .../_expected/es/nested/chunk.js | 9 ++++ .../_expected/es/nested/chunk2.js | 3 ++ .../import-meta-url/_expected/system/main.js | 15 ++++++ .../_expected/system/nested/chunk.js | 18 ++++++++ .../_expected/system/nested/chunk2.js | 14 ++++++ .../samples/import-meta-url/index-amd.html | 10 ++++ .../samples/import-meta-url/index-es.html | 10 ++++ .../samples/import-meta-url/index-system.html | 11 +++++ .../samples/import-meta-url/log.js | 7 +++ .../samples/import-meta-url/main.js | 4 ++ .../samples/import-meta-url/nested.js | 3 ++ test/form/samples/import-meta-url/_config.js | 3 +- .../samples/import-meta-url/_expected/amd.js | 10 +++- .../samples/import-meta-url/_expected/cjs.js | 10 +++- .../samples/import-meta-url/_expected/es.js | 10 +++- .../samples/import-meta-url/_expected/iife.js | 10 +++- .../import-meta-url/_expected/system.js | 10 +++- .../samples/import-meta-url/_expected/umd.js | 10 +++- .../samples/import-meta-url/index-amd.html | 2 +- .../samples/import-meta-url/index-cjs.html | 10 ++++ .../samples/import-meta-url/index-es.html | 2 +- .../samples/import-meta-url/index-iife.html | 10 ++++ .../samples/import-meta-url/index-system.html | 11 +++++ .../import-meta-url/index-umd-amd.html | 10 ++++ .../import-meta-url/index-umd-iife.html | 10 ++++ test/form/samples/import-meta-url/main.js | 10 +++- .../import-meta-url-compact/_config.js | 18 -------- .../samples/import-meta-url-compact/main.js | 1 - .../samples/import-meta-url/_config.js | 12 +++++ test/function/samples/import-meta-url/main.js | 1 + 43 files changed, 332 insertions(+), 54 deletions(-) create mode 100644 test/chunking-form/samples/asset-emission/index.html create mode 100644 test/chunking-form/samples/import-meta-url/_config.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/amd/main.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/amd/nested/chunk.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/amd/nested/chunk2.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/cjs/main.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/cjs/nested/chunk.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/cjs/nested/chunk2.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/es/main.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/es/nested/chunk.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/es/nested/chunk2.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/system/main.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/system/nested/chunk.js create mode 100644 test/chunking-form/samples/import-meta-url/_expected/system/nested/chunk2.js create mode 100644 test/chunking-form/samples/import-meta-url/index-amd.html create mode 100644 test/chunking-form/samples/import-meta-url/index-es.html create mode 100644 test/chunking-form/samples/import-meta-url/index-system.html create mode 100644 test/chunking-form/samples/import-meta-url/log.js create mode 100644 test/chunking-form/samples/import-meta-url/main.js create mode 100644 test/chunking-form/samples/import-meta-url/nested.js create mode 100644 test/form/samples/import-meta-url/index-cjs.html create mode 100644 test/form/samples/import-meta-url/index-iife.html create mode 100644 test/form/samples/import-meta-url/index-system.html create mode 100644 test/form/samples/import-meta-url/index-umd-amd.html create mode 100644 test/form/samples/import-meta-url/index-umd-iife.html delete mode 100644 test/function/samples/import-meta-url-compact/_config.js delete mode 100644 test/function/samples/import-meta-url-compact/main.js create mode 100644 test/function/samples/import-meta-url/_config.js create mode 100644 test/function/samples/import-meta-url/main.js diff --git a/package-lock.json b/package-lock.json index 63bdaca8a28..1fade978805 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5245,6 +5245,12 @@ "integrity": "sha512-TyOuWLwkmtPL49LHCX1caIwHjRzcVd62+GF6h8W/jHOeZUFHpnd2XJDVuUlaTaLPH1nuu2M69mfHr5XbQJnf/g==", "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==", + "dev": true + }, "table": { "version": "5.2.3", "resolved": "https://registry.npmjs.org/table/-/table-5.2.3.tgz", diff --git a/package.json b/package.json index af186435133..a7a151e9362 100644 --- a/package.json +++ b/package.json @@ -115,6 +115,7 @@ "source-map": "^0.6.1", "source-map-support": "^0.5.11", "sourcemap-codec": "^1.4.4", + "systemjs": "^3.1.0", "terser": "^3.17.0", "tslib": "^1.9.3", "tslint": "^5.14.0", diff --git a/src/ast/nodes/MetaProperty.ts b/src/ast/nodes/MetaProperty.ts index 5ef89f2a4e1..233aaae0d16 100644 --- a/src/ast/nodes/MetaProperty.ts +++ b/src/ast/nodes/MetaProperty.ts @@ -7,32 +7,32 @@ import MemberExpression from './MemberExpression'; import * as NodeType from './NodeType'; import { NodeBase } from './shared/Node'; -// TODO Lukas make relative mechanism more uniform -// TODO Lukas reference absolute mechanism in relative mechanism - const getResolveUrl = (path: string, URL: string = 'URL') => `new ${URL}(${path}).href`; + +const getUrlFromDocument = (chunkId: string) => + `(document.currentScript && document.currentScript.src || new URL('${chunkId}', document.baseURI).href)`; + const amdModuleUrl = `(typeof process !== 'undefined' && process.versions && process.versions.node ? 'file:' : '') + module.uri`; -const getURLFromGlobalOrCjs = `(typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)`; - -const globalImportMetaUrlMechanism = `(typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : ${getResolveUrl( - `'file:' + __filename`, - getURLFromGlobalOrCjs -)})`; - -const importMetaUrlMechanisms: Record = { - amd: getResolveUrl(amdModuleUrl), - cjs: getResolveUrl( - `(process.browser ? '' : 'file:') + __filename, process.browser && document.baseURI`, - getURLFromGlobalOrCjs - ), - iife: globalImportMetaUrlMechanism, - umd: globalImportMetaUrlMechanism + +const importMetaUrlMechanisms: Record string> = { + amd: () => getResolveUrl(`module.uri, document.baseURI`), + cjs: chunkId => + `(typeof document === 'undefined' ? ${getResolveUrl( + `'file:' + __filename`, + `(require('u' + 'rl').URL)` + )} : ${getUrlFromDocument(chunkId)})`, + iife: chunkId => getUrlFromDocument(chunkId), + umd: chunkId => + `(typeof document === 'undefined' ? ${getResolveUrl( + `'file:' + __filename`, + `(require('u' + 'rl').URL)` + )} : ${getUrlFromDocument(chunkId)})` }; const globalRelUrlMechanism = (relPath: string) => { return getResolveUrl( `(typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : 'file:' + __filename) + '/../${relPath}'`, - getURLFromGlobalOrCjs + `(typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)` ); }; @@ -41,9 +41,9 @@ const relUrlMechanisms: Record string> = { cjs: (relPath: string) => getResolveUrl( `(process.browser ? '' : 'file:') + __dirname + '/${relPath}', process.browser && document.baseURI`, - getURLFromGlobalOrCjs + `(typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)` ), - es: (relPath: string) => getResolveUrl(`'../${relPath}', import.meta.url`), // TODO Lukas does this pattern make sense in more situations? + es: (relPath: string) => getResolveUrl(`'../${relPath}', import.meta.url`), iife: globalRelUrlMechanism, system: (relPath: string) => getResolveUrl(`'../${relPath}', module.url`), umd: globalRelUrlMechanism @@ -69,7 +69,6 @@ export default class MetaProperty extends NodeBase { } renderFinalMechanism(code: MagicString, chunkId: string, format: string): boolean { - // TODO Lukas why? if (!this.rendered) return false; if (this.parent instanceof MemberExpression === false) return false; @@ -94,7 +93,8 @@ export default class MetaProperty extends NodeBase { code.overwrite(this.meta.start, this.meta.end, 'module'); } else if (importMetaProperty === 'url') { const importMetaUrlMechanism = importMetaUrlMechanisms[format]; - if (importMetaUrlMechanism) code.overwrite(parent.start, parent.end, importMetaUrlMechanism); + if (importMetaUrlMechanism) + code.overwrite(parent.start, parent.end, importMetaUrlMechanism(chunkId)); return true; } diff --git a/test/chunking-form/samples/asset-emission/_config.js b/test/chunking-form/samples/asset-emission/_config.js index 625b607dad6..c430dd28952 100644 --- a/test/chunking-form/samples/asset-emission/_config.js +++ b/test/chunking-form/samples/asset-emission/_config.js @@ -1,5 +1,4 @@ module.exports = { - solo: true, description: 'supports emitting assets from plugin hooks', options: { input: ['main.js'], diff --git a/test/chunking-form/samples/asset-emission/index.html b/test/chunking-form/samples/asset-emission/index.html new file mode 100644 index 00000000000..58ae4a61527 --- /dev/null +++ b/test/chunking-form/samples/asset-emission/index.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + diff --git a/test/chunking-form/samples/import-meta-url/_config.js b/test/chunking-form/samples/import-meta-url/_config.js new file mode 100644 index 00000000000..0de1384302d --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_config.js @@ -0,0 +1,8 @@ +module.exports = { + description: 'supports import.meta.url', + options: { + output: { + chunkFileNames: 'nested/chunk.js' + } + } +}; diff --git a/test/chunking-form/samples/import-meta-url/_expected/amd/main.js b/test/chunking-form/samples/import-meta-url/_expected/amd/main.js new file mode 100644 index 00000000000..d95c0efb5a9 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/amd/main.js @@ -0,0 +1,6 @@ +define(['module', 'require', './nested/chunk.js'], function (module, require, __chunk_1) { 'use strict'; + + __chunk_1.log('main: ' + new URL(module.uri, document.baseURI).href); + new Promise(function (resolve, reject) { require(['./nested/chunk2.js'], resolve, reject) }); + +}); diff --git a/test/chunking-form/samples/import-meta-url/_expected/amd/nested/chunk.js b/test/chunking-form/samples/import-meta-url/_expected/amd/nested/chunk.js new file mode 100644 index 00000000000..8df72136505 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/amd/nested/chunk.js @@ -0,0 +1,13 @@ +define(['exports'], function (exports) { 'use strict'; + + function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerHTML += url + '
'; + } + } + + exports.log = log; + +}); diff --git a/test/chunking-form/samples/import-meta-url/_expected/amd/nested/chunk2.js b/test/chunking-form/samples/import-meta-url/_expected/amd/nested/chunk2.js new file mode 100644 index 00000000000..0c78bb0ac8b --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/amd/nested/chunk2.js @@ -0,0 +1,5 @@ +define(['module', './chunk.js'], function (module, __chunk_1) { 'use strict'; + + __chunk_1.log('nested: ' + new URL(module.uri, document.baseURI).href); + +}); diff --git a/test/chunking-form/samples/import-meta-url/_expected/cjs/main.js b/test/chunking-form/samples/import-meta-url/_expected/cjs/main.js new file mode 100644 index 00000000000..d4619267ada --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/cjs/main.js @@ -0,0 +1,6 @@ +'use strict'; + +var __chunk_1 = require('./nested/chunk.js'); + +__chunk_1.log('main: ' + (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href))); +Promise.resolve(require('./nested/chunk2.js')); diff --git a/test/chunking-form/samples/import-meta-url/_expected/cjs/nested/chunk.js b/test/chunking-form/samples/import-meta-url/_expected/cjs/nested/chunk.js new file mode 100644 index 00000000000..b1d04a7cdab --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/cjs/nested/chunk.js @@ -0,0 +1,11 @@ +'use strict'; + +function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerHTML += url + '
'; + } +} + +exports.log = log; diff --git a/test/chunking-form/samples/import-meta-url/_expected/cjs/nested/chunk2.js b/test/chunking-form/samples/import-meta-url/_expected/cjs/nested/chunk2.js new file mode 100644 index 00000000000..ad2c0bb4da0 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/cjs/nested/chunk2.js @@ -0,0 +1,5 @@ +'use strict'; + +var __chunk_1 = require('./chunk.js'); + +__chunk_1.log('nested: ' + (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('nested/chunk2.js', document.baseURI).href))); diff --git a/test/chunking-form/samples/import-meta-url/_expected/es/main.js b/test/chunking-form/samples/import-meta-url/_expected/es/main.js new file mode 100644 index 00000000000..85486cf6309 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/es/main.js @@ -0,0 +1,4 @@ +import { a as log } from './nested/chunk.js'; + +log('main: ' + import.meta.url); +import('./nested/chunk2.js'); diff --git a/test/chunking-form/samples/import-meta-url/_expected/es/nested/chunk.js b/test/chunking-form/samples/import-meta-url/_expected/es/nested/chunk.js new file mode 100644 index 00000000000..b666269f35d --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/es/nested/chunk.js @@ -0,0 +1,9 @@ +function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerHTML += url + '
'; + } +} + +export { log as a }; diff --git a/test/chunking-form/samples/import-meta-url/_expected/es/nested/chunk2.js b/test/chunking-form/samples/import-meta-url/_expected/es/nested/chunk2.js new file mode 100644 index 00000000000..4dc8950a08f --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/es/nested/chunk2.js @@ -0,0 +1,3 @@ +import { a as log } from './chunk.js'; + +log('nested: ' + import.meta.url); diff --git a/test/chunking-form/samples/import-meta-url/_expected/system/main.js b/test/chunking-form/samples/import-meta-url/_expected/system/main.js new file mode 100644 index 00000000000..b0fd79d49ee --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/system/main.js @@ -0,0 +1,15 @@ +System.register(['./nested/chunk.js'], function (exports, module) { + 'use strict'; + var log; + return { + setters: [function (module) { + log = module.a; + }], + execute: function () { + + log('main: ' + module.meta.url); + module.import('./nested/chunk2.js'); + + } + }; +}); diff --git a/test/chunking-form/samples/import-meta-url/_expected/system/nested/chunk.js b/test/chunking-form/samples/import-meta-url/_expected/system/nested/chunk.js new file mode 100644 index 00000000000..37d15663c1f --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/system/nested/chunk.js @@ -0,0 +1,18 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + exports('a', log); + + function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerHTML += url + '
'; + } + } + + } + }; +}); diff --git a/test/chunking-form/samples/import-meta-url/_expected/system/nested/chunk2.js b/test/chunking-form/samples/import-meta-url/_expected/system/nested/chunk2.js new file mode 100644 index 00000000000..4c893092da0 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/_expected/system/nested/chunk2.js @@ -0,0 +1,14 @@ +System.register(['./chunk.js'], function (exports, module) { + 'use strict'; + var log; + return { + setters: [function (module) { + log = module.a; + }], + execute: function () { + + log('nested: ' + module.meta.url); + + } + }; +}); diff --git a/test/chunking-form/samples/import-meta-url/index-amd.html b/test/chunking-form/samples/import-meta-url/index-amd.html new file mode 100644 index 00000000000..476b9ce3df1 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/index-amd.html @@ -0,0 +1,10 @@ + + + + + AMD + + + + + diff --git a/test/chunking-form/samples/import-meta-url/index-es.html b/test/chunking-form/samples/import-meta-url/index-es.html new file mode 100644 index 00000000000..413e6fb0801 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/index-es.html @@ -0,0 +1,10 @@ + + + + + ES Module + + + + + diff --git a/test/chunking-form/samples/import-meta-url/index-system.html b/test/chunking-form/samples/import-meta-url/index-system.html new file mode 100644 index 00000000000..96c687c1eb7 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/index-system.html @@ -0,0 +1,11 @@ + + + + + SystemJS + + + + + + diff --git a/test/chunking-form/samples/import-meta-url/log.js b/test/chunking-form/samples/import-meta-url/log.js new file mode 100644 index 00000000000..569e3e6f390 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/log.js @@ -0,0 +1,7 @@ +export function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerHTML += url + '
'; + } +} diff --git a/test/chunking-form/samples/import-meta-url/main.js b/test/chunking-form/samples/import-meta-url/main.js new file mode 100644 index 00000000000..7cacbf31f83 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/main.js @@ -0,0 +1,4 @@ +import {log} from './log'; + +log('main: ' + import.meta.url); +import('./nested.js'); diff --git a/test/chunking-form/samples/import-meta-url/nested.js b/test/chunking-form/samples/import-meta-url/nested.js new file mode 100644 index 00000000000..6c04c450934 --- /dev/null +++ b/test/chunking-form/samples/import-meta-url/nested.js @@ -0,0 +1,3 @@ +import {log} from './log'; + +log('nested: ' + import.meta.url); diff --git a/test/form/samples/import-meta-url/_config.js b/test/form/samples/import-meta-url/_config.js index 2328aa134ee..7acc8dce0e5 100644 --- a/test/form/samples/import-meta-url/_config.js +++ b/test/form/samples/import-meta-url/_config.js @@ -1,4 +1,3 @@ module.exports = { - solo: true, - description: 'import.meta.url support' + description: 'supports import.meta.url' }; diff --git a/test/form/samples/import-meta-url/_expected/amd.js b/test/form/samples/import-meta-url/_expected/amd.js index 40cb580c725..3ea98d3db9b 100644 --- a/test/form/samples/import-meta-url/_expected/amd.js +++ b/test/form/samples/import-meta-url/_expected/amd.js @@ -1,5 +1,13 @@ define(['module'], function (module) { 'use strict'; - document.body.innerText = new URL((typeof process !== 'undefined' && process.versions && process.versions.node ? 'file:' : '') + module.uri).href; + function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerText = url; + } + } + + log(new URL(module.uri, document.baseURI).href); }); diff --git a/test/form/samples/import-meta-url/_expected/cjs.js b/test/form/samples/import-meta-url/_expected/cjs.js index d128c8f5a4d..58fb168b483 100644 --- a/test/form/samples/import-meta-url/_expected/cjs.js +++ b/test/form/samples/import-meta-url/_expected/cjs.js @@ -1,3 +1,11 @@ 'use strict'; -document.body.innerText = new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)((process.browser ? '' : 'file:') + __filename, process.browser && document.baseURI).href; +function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerText = url; + } +} + +log((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('cjs.js', document.baseURI).href))); diff --git a/test/form/samples/import-meta-url/_expected/es.js b/test/form/samples/import-meta-url/_expected/es.js index db58671cfe6..9390079070b 100644 --- a/test/form/samples/import-meta-url/_expected/es.js +++ b/test/form/samples/import-meta-url/_expected/es.js @@ -1 +1,9 @@ -document.body.innerText = import.meta.url; +function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerText = url; + } +} + +log(import.meta.url); diff --git a/test/form/samples/import-meta-url/_expected/iife.js b/test/form/samples/import-meta-url/_expected/iife.js index 7b316441284..2ceafdeef6b 100644 --- a/test/form/samples/import-meta-url/_expected/iife.js +++ b/test/form/samples/import-meta-url/_expected/iife.js @@ -1,6 +1,14 @@ (function () { 'use strict'; - document.body.innerText = (typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)('file:' + __filename).href); + function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerText = url; + } + } + + log((document.currentScript && document.currentScript.src || new URL('iife.js', document.baseURI).href)); }()); diff --git a/test/form/samples/import-meta-url/_expected/system.js b/test/form/samples/import-meta-url/_expected/system.js index fdcfbe36f6d..85f9c9532f7 100644 --- a/test/form/samples/import-meta-url/_expected/system.js +++ b/test/form/samples/import-meta-url/_expected/system.js @@ -3,7 +3,15 @@ System.register([], function (exports, module) { return { execute: function () { - document.body.innerText = module.meta.url; + function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerText = url; + } + } + + log(module.meta.url); } }; diff --git a/test/form/samples/import-meta-url/_expected/umd.js b/test/form/samples/import-meta-url/_expected/umd.js index fdc8d689138..8ea4b1f845b 100644 --- a/test/form/samples/import-meta-url/_expected/umd.js +++ b/test/form/samples/import-meta-url/_expected/umd.js @@ -3,6 +3,14 @@ factory(); }(function () { 'use strict'; - document.body.innerText = (typeof document !== 'undefined' ? document.currentScript && document.currentScript.src || document.baseURI : new (typeof URL !== 'undefined' ? URL : require('ur'+'l').URL)('file:' + __filename).href); + function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerText = url; + } + } + + log((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('umd.js', document.baseURI).href))); })); diff --git a/test/form/samples/import-meta-url/index-amd.html b/test/form/samples/import-meta-url/index-amd.html index 609103dda38..e89cb396ea2 100644 --- a/test/form/samples/import-meta-url/index-amd.html +++ b/test/form/samples/import-meta-url/index-amd.html @@ -2,7 +2,7 @@ - Title + AMD diff --git a/test/form/samples/import-meta-url/index-cjs.html b/test/form/samples/import-meta-url/index-cjs.html new file mode 100644 index 00000000000..c5b18ec148f --- /dev/null +++ b/test/form/samples/import-meta-url/index-cjs.html @@ -0,0 +1,10 @@ + + + + + CommonJS + + + + + diff --git a/test/form/samples/import-meta-url/index-es.html b/test/form/samples/import-meta-url/index-es.html index eae8d0c2729..5079fce53d8 100644 --- a/test/form/samples/import-meta-url/index-es.html +++ b/test/form/samples/import-meta-url/index-es.html @@ -2,7 +2,7 @@ - Title + ES Module diff --git a/test/form/samples/import-meta-url/index-iife.html b/test/form/samples/import-meta-url/index-iife.html new file mode 100644 index 00000000000..aaf475f1505 --- /dev/null +++ b/test/form/samples/import-meta-url/index-iife.html @@ -0,0 +1,10 @@ + + + + + IIFE + + + + + diff --git a/test/form/samples/import-meta-url/index-system.html b/test/form/samples/import-meta-url/index-system.html new file mode 100644 index 00000000000..bef9897957b --- /dev/null +++ b/test/form/samples/import-meta-url/index-system.html @@ -0,0 +1,11 @@ + + + + + SystemJS + + + + + + diff --git a/test/form/samples/import-meta-url/index-umd-amd.html b/test/form/samples/import-meta-url/index-umd-amd.html new file mode 100644 index 00000000000..e644ea36370 --- /dev/null +++ b/test/form/samples/import-meta-url/index-umd-amd.html @@ -0,0 +1,10 @@ + + + + + UMD (AMD) + + + + + diff --git a/test/form/samples/import-meta-url/index-umd-iife.html b/test/form/samples/import-meta-url/index-umd-iife.html new file mode 100644 index 00000000000..202c1dad8f0 --- /dev/null +++ b/test/form/samples/import-meta-url/index-umd-iife.html @@ -0,0 +1,10 @@ + + + + + UMD (IIFE) + + + + + diff --git a/test/form/samples/import-meta-url/main.js b/test/form/samples/import-meta-url/main.js index db58671cfe6..9390079070b 100644 --- a/test/form/samples/import-meta-url/main.js +++ b/test/form/samples/import-meta-url/main.js @@ -1 +1,9 @@ -document.body.innerText = import.meta.url; +function log(url) { + if (typeof document === 'undefined') { + console.log(url); + } else { + document.body.innerText = url; + } +} + +log(import.meta.url); diff --git a/test/function/samples/import-meta-url-compact/_config.js b/test/function/samples/import-meta-url-compact/_config.js deleted file mode 100644 index bd433231ebe..00000000000 --- a/test/function/samples/import-meta-url-compact/_config.js +++ /dev/null @@ -1,18 +0,0 @@ -const assert = require('assert'); -const path = require('path'); -const URL = global.URL || require('url-parse'); - -module.exports = { - description: 'import.meta.url support', - options: { output: { compact: true } }, - context: { - __filename: path.resolve(__dirname, 'test.js'), - require: name => { - assert.equal(name, 'url'); - return { URL }; - } - }, - exports(exports) { - assert.equal(exports, new URL('file:' + path.resolve(__dirname, 'test.js')).href); - } -}; diff --git a/test/function/samples/import-meta-url-compact/main.js b/test/function/samples/import-meta-url-compact/main.js deleted file mode 100644 index faad9bdcec9..00000000000 --- a/test/function/samples/import-meta-url-compact/main.js +++ /dev/null @@ -1 +0,0 @@ -export default import.meta.url; \ No newline at end of file diff --git a/test/function/samples/import-meta-url/_config.js b/test/function/samples/import-meta-url/_config.js new file mode 100644 index 00000000000..96626daee58 --- /dev/null +++ b/test/function/samples/import-meta-url/_config.js @@ -0,0 +1,12 @@ +const path = require('path'); +const assert = require('assert'); + +module.exports = { + description: 'resolves import.meta.url', + context: { + __filename: path.resolve(__dirname, 'main.js') + }, + exports(exports) { + assert.strictEqual(exports, 'file://' + path.resolve(__dirname, 'main.js')); + } +}; diff --git a/test/function/samples/import-meta-url/main.js b/test/function/samples/import-meta-url/main.js new file mode 100644 index 00000000000..37b3f8565ea --- /dev/null +++ b/test/function/samples/import-meta-url/main.js @@ -0,0 +1 @@ +export default import.meta.url;