Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
script inject improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Öner Zafer committed Feb 29, 2020
1 parent 4a3dafa commit d60c4d0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
9 changes: 7 additions & 2 deletions lib/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module.exports = ($, extendingAppManifest) => {
utils.injectBundleIntoDom($, extendingAppManifest.bundle);
$('[export]').attr('export', extendingAppManifest.appName);

$('script:not([resource]),style:not([resource]),link:not([resource])').remove();
$(
'script:not([resource]),style:not([resource]),link:not([resource])'
).remove();

if (extendingAppManifest.overrides && extendingAppManifest.overrides.length) {
extendingAppManifest.overrides.forEach(override => {
Expand All @@ -15,7 +17,10 @@ module.exports = ($, extendingAppManifest) => {
if (superTagPattern.test(content)) {
content = content.replace(superTagPattern, $elem.html());
}
const tagName = override.target && override.target !== '' ? override.target : override.tag;
const tagName =
override.target && override.target !== ''
? override.target
: override.tag;
utils.replaceElement($, selector, tagName, content);
});
}
Expand Down
6 changes: 5 additions & 1 deletion lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ module.exports = html => {
};

$('meta[import]').each((index, elem) => {
manifest.uses.push($(elem).attr('import'))});
manifest.uses.push($(elem).attr('import'));
});

$('[resource]').each((index, elem) =>
manifest.bundle.push(utils.toBundleItem($, elem))
);

$('[override]').each((index, elem) =>
manifest.overrides.push(utils.toOverrideItem($, elem))
);

$('[public]').each((index, elem) =>
manifest.publics.push($(elem).attr('public') || elem.name)
);
Expand Down
17 changes: 12 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,18 @@ const injectBundleIntoDom = ($, bundle) => {
.filter(({ type }) => headerTypes.includes(type))
.map(
({ type, content, attributes }) =>
`<${type} resource ${attributesToString(attributes)}>${content}</${type}>`
`<${type} resource ${attributesToString(
attributes
)}>${content}</${type}>`
)
.join('');
const bodyContent = bundle
.filter(({ type }) => bodyTypes.includes(type))
.map(
({ type, content, attributes }) =>
`<${type} resource ${attributesToString(attributes)}>${content}</${type}>`
`<${type} resource ${attributesToString(
attributes
)}>${content}</${type}>`
)
.join('');
$('head').append(headContent);
Expand All @@ -93,9 +97,12 @@ const cleanPrivates = $ => {
};

const injectScript = ($, script) => {
$('script')
.first()
.before(script);
const $firstScriptEl = $('script').first();
if ($firstScriptEl) {
$firstScriptEl.before(script);
} else {
$('header').append(script);
}
};

module.exports = {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
"license": "MIT",
"private": false,
"scripts": {
"test": "mocha --watch --recursive"
"test": "mocha --watch --recursive",
"format": "prettier --write \"lib/**/*.js\" \"test/**/*.js\""
},
"dependencies": {
"cheerio": "^1.0.0-rc.3",
"html-minifier": "^3.5.21"
"html-minifier": "^3.5.21",
"prettier": "^1.19.1"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.0.1"
"mocha": "^6.2.2"
}
}
2 changes: 1 addition & 1 deletion test/mfhtml/mfhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('MFHTML runtime', () => {

it('should throw error on register with wrong html', () => {
expect(() => mfhtml.register(mock.badHtml)).to.throw(
'<meta export> is not available'
'<meta export> is not available'
);
});

Expand Down

0 comments on commit d60c4d0

Please sign in to comment.