diff --git a/package-lock.json b/package-lock.json index 95d18bb..e68a037 100644 --- a/package-lock.json +++ b/package-lock.json @@ -525,6 +525,11 @@ "minimalistic-assert": "^1.0.0" } }, + "detect-node": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.0.4.tgz", + "integrity": "sha512-ZIzRpLJrOj7jjP2miAtgqIfmzbxa4ZOr5jJc601zklsfEx9oTzmmj2nVpIPRpNlRTIh8lc1kyViIY7BWSGNmKw==" + }, "dfa": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/dfa/-/dfa-1.2.0.tgz", diff --git a/package.json b/package.json index 89c8bb8..4d3a5d8 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "dist/main.js" ], "dependencies": { + "detect-node": "^2.0.4", "pdfkit": "git+https://github.com/Xmader/pdfkit.git", "svg-to-pdfkit": "^0.1.8", "webmscore": "^0.10.4" diff --git a/src/mscore.ts b/src/mscore.ts index e9efcfa..2f26308 100644 --- a/src/mscore.ts +++ b/src/mscore.ts @@ -3,6 +3,7 @@ import { fetchMscz } from './mscz' import { fetchData } from './utils' import { ScoreInfo } from './scoreinfo' +import isNodeJs from 'detect-node' const WEBMSCORE_URL = 'https://cdn.jsdelivr.net/npm/webmscore@0.10/webmscore.js' @@ -16,8 +17,8 @@ const SOUND_FONT_LOADED = Symbol('SoundFont loaded') export type WebMscore = import('webmscore').default export type WebMscoreConstr = typeof import('webmscore').default -const initMscore = async (w?: Window): Promise => { - if (w !== undefined) { // attached to a page +const initMscore = async (w: Window): Promise => { + if (!isNodeJs) { // attached to a page if (!w['WebMscore']) { // init webmscore (https://github.com/LibreScore/webmscore) const script = w.document.createElement('script') @@ -32,11 +33,11 @@ const initMscore = async (w?: Window): Promise => { } let fonts: Promise | undefined -const initFonts = (nodeJs: boolean) => { +const initFonts = () => { // load CJK fonts // CJK (East Asian) characters will be rendered as "tofu" if there is no font if (!fonts) { - if (nodeJs) { + if (isNodeJs) { // module.exports.CN = ..., module.exports.KR = ... const FONTS = Object.values(require('@librescore/fonts')) @@ -65,8 +66,8 @@ export const loadSoundFont = (score: WebMscore): Promise => { } export const loadMscore = async (scoreinfo: ScoreInfo, w?: Window): Promise => { - initFonts(w === undefined) - const WebMscore = await initMscore(w) + initFonts() + const WebMscore = await initMscore(w!) // parse mscz data const data = new Uint8Array(