Skip to content

Commit

Permalink
fix(html-exporter): lazy loaded JS and CSS files work (#1362)
Browse files Browse the repository at this point in the history
* fix(html-exporter): lazy loaded JS and CSS files work

* refactor(html-exporter): separated overrides into own JS file

* fix(h5p-html-exporrter): getLibraryFilePath has fallback

* refactor(h5p-html-exporter): load file override encapsulated

* fix(html-exporter): added library.json vor H5P.Timeline

* fix(h5p-html-exporter): accept versioned paths in further resources

* fix(h5p-html-exporter): corrected filePath

* docs(h5p-html-exporter): added docs
  • Loading branch information
sr258 authored Apr 30, 2021
1 parent 564dbf8 commit 450775b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ node_modules/
packages/h5p-rest-example-client
*.d.ts
.eslintrc.js
.config.js
.config.js
packages/h5p-html-exporter/src/*.js
2 changes: 1 addition & 1 deletion packages/h5p-html-exporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"build:watch": "npx tsc -w -p ./tsconfig.build.json",
"prepare": "npm run build",
"build": "npx tsc",
"build": "npx tsc && cp src/*.js build",
"ci": "npm run build && npm run lint && npm run format:check && npm run test && npm run test:integration && npm run test:e2e",
"clean": "rm -rf build && rm -rf coverage && rm -rf node_modules",
"test:watch": "jest --watch",
Expand Down
34 changes: 16 additions & 18 deletions packages/h5p-html-exporter/src/HtmlExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import postCssClean from 'postcss-clean';
import mimetypes from 'mime-types';
import uglifyJs from 'uglify-js';
import postCssSafeParser from 'postcss-safe-parser';

import {
ContentId,
IContentStorage,
Expand All @@ -21,6 +20,7 @@ import {
LibraryManager,
streamToString
} from '@lumieducation/h5p-server';

import postCssRemoveRedundantUrls from './helpers/postCssRemoveRedundantFontUrls';
import LibrariesFilesList from './helpers/LibrariesFilesList';

Expand All @@ -31,17 +31,9 @@ import LibrariesFilesList from './helpers/LibrariesFilesList';
* can't be modified that way.
*/
const getLibraryFilePathOverrideScript = uglifyJs.minify(
`H5P.ContentType = function (isRootLibrary) {
function ContentType() {}
ContentType.prototype = new H5P.EventDispatcher();
ContentType.prototype.isRoot = function () {
return isRootLibrary;
};
ContentType.prototype.getLibraryFilePath = function (filePath) {
return furtherH5PInlineResources[this.libraryInfo.versionedNameNoSpaces + '/' + filePath];
};
return ContentType;
};`
fsExtra.readFileSync(path.join(__dirname, 'loadFileOverrides.js'), {
encoding: 'utf8'
})
).code;

const getContentPathOverrideScript = uglifyJs.minify(
Expand Down Expand Up @@ -501,7 +493,10 @@ export default class HtmlExporter {
if (
!usedFiles.checkFile(library, filename) &&
!filename.startsWith('language/') &&
filename !== 'library.json' &&
(filename !== 'library.json' ||
// We allow the library.json file for timeline
// as it's needed at runtime.
ubername.startsWith('H5P.Timeline-')) &&
filename !== 'semantics.json' &&
filename !== 'icon.svg' &&
filename !== 'upgrades.js' &&
Expand All @@ -511,11 +506,14 @@ export default class HtmlExporter {
path.basename(filename)
);
if (
mt &&
(mt.startsWith('audio/') ||
mt.startsWith('video/') ||
mt.startsWith('image/')) &&
!filename.includes('font')
filename.endsWith('.js') ||
filename.endsWith('.css') ||
filename.endsWith('.json') ||
(mt &&
(mt.startsWith('audio/') ||
mt.startsWith('video/') ||
mt.startsWith('image/')) &&
!filename.includes('font'))
) {
return true;
}
Expand Down
68 changes: 68 additions & 0 deletions packages/h5p-html-exporter/src/loadFileOverrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* This file is injected into the HTML bundle (in a minified form) if there were
* any files in the library directories which weren't already included.
*/

(function () {
// By providing a custom implementation of H5P.ContentType we can inject the
// resource files when a H5P content type generates the path to a file.
H5P.ContentType = function (isRootLibrary) {
function ContentType() {}
ContentType.prototype = new H5P.EventDispatcher();
ContentType.prototype.isRoot = function () {
return isRootLibrary;
};
ContentType.prototype.getLibraryFilePath = function (filePath) {
return (
furtherH5PInlineResources[
this.libraryInfo.versionedNameNoSpaces + '/' + filePath
] || filePath
);
};
return ContentType;
};

// We override setting <script src="..."/> behavior to be able to inject scripts
// that don't use H5P.ContentType.getLibraryFilePath.
var nativeSetScriptAttribute = HTMLScriptElement.prototype.setAttribute;
HTMLScriptElement.prototype.setAttribute = function (name, value) {
if (name === 'src') {
var match = value.match(/^.\/libraries\/([^?]+)\??.*$/)
if (match) {
const file = match[1];
if (furtherH5PInlineResources[file]) {
value = furtherH5PInlineResources[file];
}
}
}
nativeSetScriptAttribute.call(this, name, value);
};
Object.defineProperty(HTMLScriptElement.prototype, 'src', {
// also apply the modification if the src is set with the src property
set(value) {
this.setAttribute('src', value);
}
});

// We override setting <link href="..."/> to be able to inject styles that don't
// use H5P.ContentType.getLibraryFilePath.
var nativeSetLinkAttribute = HTMLLinkElement.prototype.setAttribute;
HTMLLinkElement.prototype.setAttribute = function (name, value) {
if (name === 'href') {
var match = value.match(/^.\/libraries\/([^?]+)\??.*$/)
if (match) {
const file = match[1];
if (furtherH5PInlineResources[file]) {
value = furtherH5PInlineResources[file];
}
}
}
nativeSetLinkAttribute.call(this, name, value);
};
Object.defineProperty(HTMLLinkElement.prototype, 'src', {
// also apply the modification if the src is set with the src property
set(value) {
this.setAttribute('src', value);
}
});
})();

0 comments on commit 450775b

Please sign in to comment.