Skip to content

Commit c45eaa8

Browse files
authored
fix(umd): fix the UMD bundle export names so they don't conflict and overwrite each other (#318)
Importing multiple UMD bundles into the same page was causing the global Ix object to be replaced each time, rather than augmented. Exporting them to distinct global objects based on the file name causes them not to conflict.
1 parent 8893e2c commit c45eaa8

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

gulp/closure-task.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
targetDir,
2020
mainExport,
2121
esmRequire,
22+
getUMDExportName,
2223
gCCLanguageNames,
2324
observableFromStreams,
2425
shouldRunInChildProcess,
@@ -58,7 +59,7 @@ const closureTask = ((cache) => memoizeTask(cache, async function closure(target
5859
const externsPath = path.join(out, `${entry}.externs.js`);
5960

6061
await Promise.all([
61-
fs.promises.writeFile(entry_point, generateUMDExportAssignnent(entry)),
62+
fs.promises.writeFile(entry_point, generateUMDExportAssignment(entry)),
6263
fs.promises.writeFile(externsPath, generateExternsFile(path.resolve(`${src}/${entry}.js`)))
6364
]);
6465

@@ -73,15 +74,15 @@ const closureTask = ((cache) => memoizeTask(cache, async function closure(target
7374
`${src}/**/*.js` /* <-- then sources globs */
7475
], { base: `./` }),
7576
sourcemaps.init(),
76-
closureCompiler(createClosureArgs(entry_point, entry, externsPath)),
77+
closureCompiler(createClosureArgs(entry_point, entry, externsPath, getUMDExportName(entry))),
7778
// rename the sourcemaps from *.js.map files to *.min.js.map
7879
sourcemaps.write(`.`, { mapFile: (mapPath) => mapPath.replace(`.js.map`, `.${target}.min.js.map`) }),
7980
gulp.dest(out)
8081
).toPromise();
8182
}
8283
}))({});
8384

84-
const createClosureArgs = (entry_point, output, externs) => ({
85+
const createClosureArgs = (entry_point, output, externs, libraryName) => ({
8586
externs,
8687
entry_point,
8788
third_party: true,
@@ -100,15 +101,15 @@ const createClosureArgs = (entry_point, output, externs) => ({
100101
language_out: gCCLanguageNames[`esnext`],
101102
output_wrapper: `(function (global, factory) {
102103
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
103-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
104-
(factory(global.Ix = global.Ix || {}));
104+
typeof define === 'function' && define.amd ? define(['${libraryName}'], factory) :
105+
(factory(global.${libraryName} = global.${libraryName} || {}));
105106
}(this, (function (exports) {%output%}.bind(this))));`
106107
});
107108

108109
module.exports = closureTask;
109110
module.exports.closureTask = closureTask;
110111

111-
function generateUMDExportAssignnent(entry) {
112+
function generateUMDExportAssignment(entry) {
112113
return [`
113114
import { __await } from 'tslib';
114115
__await.prototype[Symbol.toStringTag] = '__await';

gulp/minify-task.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
const {
1919
targetDir,
2020
mainExport,
21+
getUMDExportName,
2122
UMDSourceTargets,
2223
terserLanguageNames,
2324
shouldRunInChildProcess,
@@ -57,6 +58,10 @@ const minifyTask = ((cache, commonConfig) => memoizeTask(cache, function minifyJ
5758
].map((entry) => ({
5859
...targetConfig,
5960
name: entry,
61+
output: {
62+
...targetConfig.output,
63+
library: getUMDExportName(entry)
64+
},
6065
entry: { [entry]: path.resolve(`${src}/${entry}.js`) },
6166
plugins: [
6267
...(targetConfig.plugins || []),

gulp/util.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ const esmRequire = require(`esm`)(module, {
216216
}
217217
});
218218

219+
const getUMDExportName = (umdEntryFileName) => umdEntryFileName
220+
.split('.')
221+
.filter((x) => x != 'dom')
222+
.map((x) => x[0].toUpperCase() + x.slice(1))
223+
.join('');
224+
219225
module.exports = {
220226

221227
mainExport, npmPkgName, npmOrgName, metadataFiles, packageJSONFields,
@@ -224,5 +230,5 @@ module.exports = {
224230
gCCLanguageNames, UMDSourceTargets, terserLanguageNames,
225231

226232
taskName, packageName, tsconfigName, targetDir, combinations, observableFromStreams,
227-
ESKeywords, esmRequire, shouldRunInChildProcess, spawnGulpCommandInChildProcess
233+
ESKeywords, esmRequire, shouldRunInChildProcess, spawnGulpCommandInChildProcess, getUMDExportName
228234
};

0 commit comments

Comments
 (0)