diff --git a/src/finalisers/amd.ts b/src/finalisers/amd.ts index 879fdfca090..5b2aa11da2b 100644 --- a/src/finalisers/amd.ts +++ b/src/finalisers/amd.ts @@ -3,18 +3,9 @@ import { NormalizedOutputOptions } from '../rollup/types'; import { FinaliserOptions } from './index'; import { getExportBlock, getNamespaceMarkers } from './shared/getExportBlock'; import getInteropBlock from './shared/getInteropBlock'; +import removeExtensionFromRelativeAmdId from './shared/removeExtensionFromRelativeAmdId'; import warnOnBuiltins from './shared/warnOnBuiltins'; -// AMD resolution will only respect the AMD baseUrl if the .js extension is omitted. -// The assumption is that this makes sense for all relative ids: -// https://requirejs.org/docs/api.html#jsfiles -function removeExtensionFromRelativeAmdId(id: string) { - if (id[0] === '.' && id.endsWith('.js')) { - return id.slice(0, -3); - } - return id; -} - export default function amd( magicString: MagicStringBundle, { diff --git a/src/finalisers/shared/removeExtensionFromRelativeAmdId.ts b/src/finalisers/shared/removeExtensionFromRelativeAmdId.ts new file mode 100644 index 00000000000..e18b64c73cd --- /dev/null +++ b/src/finalisers/shared/removeExtensionFromRelativeAmdId.ts @@ -0,0 +1,9 @@ +// AMD resolution will only respect the AMD baseUrl if the .js extension is omitted. +// The assumption is that this makes sense for all relative ids: +// https://requirejs.org/docs/api.html#jsfiles +export default function removeExtensionFromRelativeAmdId(id: string) { + if (id[0] === '.' && id.endsWith('.js')) { + return id.slice(0, -3); + } + return id; +} diff --git a/src/finalisers/umd.ts b/src/finalisers/umd.ts index a6b980b1c1c..bf9efad0d76 100644 --- a/src/finalisers/umd.ts +++ b/src/finalisers/umd.ts @@ -1,9 +1,11 @@ import { Bundle as MagicStringBundle } from 'magic-string'; import { NormalizedOutputOptions } from '../rollup/types'; import { error } from '../utils/error'; +import { renderNamePattern } from '../utils/renderNamePattern'; import { FinaliserOptions } from './index'; import { getExportBlock, getNamespaceMarkers } from './shared/getExportBlock'; import getInteropBlock from './shared/getInteropBlock'; +import removeExtensionFromRelativeAmdId from './shared/removeExtensionFromRelativeAmdId'; import { keypath, property } from './shared/sanitize'; import { assignToDeepVariable } from './shared/setupNamespace'; import trimEmptyImports from './shared/trimEmptyImports'; @@ -66,7 +68,7 @@ export default function umd( warnOnBuiltins(warn, dependencies); - const amdDeps = dependencies.map(m => `'${m.id}'`); + const amdDeps = dependencies.map(m => `'${removeExtensionFromRelativeAmdId(m.id)}'`); const cjsDeps = dependencies.map(m => `require('${m.id}')`); const trimmedImports = trimEmptyImports(dependencies); diff --git a/test/form/samples/guessed-global-names/_expected/umd.js b/test/form/samples/guessed-global-names/_expected/umd.js index 9e3a3dd1847..212291e1020 100644 --- a/test/form/samples/guessed-global-names/_expected/umd.js +++ b/test/form/samples/guessed-global-names/_expected/umd.js @@ -1,5 +1,5 @@ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('unchanged'), require('changed'), require('special-character'), require('with/slash'), require('./relative.js')) : + typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('unchanged'), require('changed'), require('special-character'), require('with/slash'), require('./relative')) : typeof define === 'function' && define.amd ? define(['unchanged', 'changed', 'special-character', 'with/slash', './relative.js'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.unchanged, global.changedName, global.specialCharacter, global.slash, global.relative_js)); }(this, (function (unchanged, changedName, specialCharacter, slash, relative_js) { 'use strict'; diff --git a/test/form/samples/relative-external-with-global/_expected/umd.js b/test/form/samples/relative-external-with-global/_expected/umd.js index 7cfe226ab68..4e56304a94f 100644 --- a/test/form/samples/relative-external-with-global/_expected/umd.js +++ b/test/form/samples/relative-external-with-global/_expected/umd.js @@ -1,6 +1,6 @@ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('./lib/throttle.js')) : - typeof define === 'function' && define.amd ? define(['./lib/throttle.js'], factory) : + typeof define === 'function' && define.amd ? define(['./lib/throttle'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Lib.throttle)); }(this, (function (throttle) { 'use strict';