Skip to content

Commit

Permalink
also removeExtensionFromRelativeAmdId in UMD finaliser
Browse files Browse the repository at this point in the history
  • Loading branch information
tjenkinson committed Nov 15, 2020
1 parent fd809d5 commit 3bcd1cd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
11 changes: 1 addition & 10 deletions src/finalisers/amd.ts
Expand Up @@ -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,
{
Expand Down
9 changes: 9 additions & 0 deletions 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;
}
3 changes: 2 additions & 1 deletion src/finalisers/umd.ts
Expand Up @@ -4,6 +4,7 @@ import { error } from '../utils/error';
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';
Expand Down Expand Up @@ -66,7 +67,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);
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/guessed-global-names/_expected/umd.js
@@ -1,6 +1,6 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('unchanged'), require('changed'), require('special-character'), require('with/slash'), require('./relative.js')) :
typeof define === 'function' && define.amd ? define(['unchanged', 'changed', 'special-character', 'with/slash', './relative.js'], factory) :
typeof define === 'function' && define.amd ? define(['unchanged', 'changed', 'special-character', 'with/slash', './relative'], 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';

Expand Down
@@ -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';

Expand Down

0 comments on commit 3bcd1cd

Please sign in to comment.