Skip to content

Commit

Permalink
Fix: define check (#668)
Browse files Browse the repository at this point in the history
Global define check was causing errors, adding a window.define check and also fixing document viewer to correctly rely on minified pdf.js.
  • Loading branch information
tonyjin authored Feb 16, 2018
1 parent 77fa2e2 commit 2edc0ff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/lib/__tests__/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('lib/util', () => {

return util.get(url, 'any').then((response) => {
expect(fetchMock.called(url)).to.be.true;
expect(response).to.be.an.object;
expect(typeof response === 'object').to.be.true;
});
});
});
Expand Down Expand Up @@ -398,15 +398,15 @@ describe('lib/util', () => {
});

it('should disable AMD until scripts are loaded or fail to load', () => {
const func = () => {};
func.amd = ['jquery'];
window.define = func;
const defineFunc = () => {};
defineFunc.amd = { jquery: '' };
window.define = defineFunc;

const promise = util.loadScripts(['foo', 'bar'], true);
expect(define).to.equal(undefined);

return promise.then(() => {
expect(define).to.equal(func);
expect(define).to.equal(defineFunc);
});
});
});
Expand Down
14 changes: 10 additions & 4 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,10 @@ export function loadScripts(urls, disableAMD = false) {
// scripts are loaded.

/* eslint-disable no-undef */
const defineRef = typeof define === 'function' ? define : undefined;
if (disableAMD && typeof define === 'function' && define.amd) {
const amdPresent = !!window.define && typeof define === 'function' && !!define.amd;
const defineRef = amdPresent ? define : undefined;

if (disableAMD && amdPresent) {
define = undefined;
}

Expand All @@ -509,10 +511,14 @@ export function loadScripts(urls, disableAMD = false) {

return Promise.all(promises)
.then(() => {
define = defineRef || define;
if (disableAMD && amdPresent) {
define = defineRef;
}
})
.catch(() => {
define = defineRef || define;
if (disableAMD && amdPresent) {
define = defineRef;
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/doc/docAssets.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DOC_STATIC_ASSETS_VERSION } from '../../constants';

const STATIC_URI = `third-party/doc/${DOC_STATIC_ASSETS_VERSION}/`;
export const JS = [`${STATIC_URI}pdf.js`, `${STATIC_URI}pdf_viewer.min.js`, `${STATIC_URI}exif.min.js`];
export const JS = [`${STATIC_URI}pdf.min.js`, `${STATIC_URI}pdf_viewer.min.js`, `${STATIC_URI}exif.min.js`];
export const PRELOAD_JS = [`${STATIC_URI}pdf.worker.min.js`];
export const CSS = [`${STATIC_URI}pdf_viewer.min.css`];

0 comments on commit 2edc0ff

Please sign in to comment.