Skip to content

Commit

Permalink
Fix <script> merge with "src" #80
Browse files Browse the repository at this point in the history
  • Loading branch information
maltsev committed Sep 2, 2019
1 parent 14c0d33 commit edb5eae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/modules/mergeScripts.es6
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* Merge multiple <script> into one */
export default function mergeScripts(tree) {
let scriptNodesIndex = {};
let scriptSrcIndex = 1;

tree.match({tag: 'script'}, node => {
const nodeAttrs = node.attrs || {};
if (nodeAttrs.src) {
scriptSrcIndex++;
return node;
}

Expand All @@ -18,7 +20,8 @@ export default function mergeScripts(tree) {
class: nodeAttrs.class,
type: scriptType,
defer: nodeAttrs.defer !== undefined,
async: nodeAttrs.async !== undefined
async: nodeAttrs.async !== undefined,
index: scriptSrcIndex,
});
if (! scriptNodesIndex[scriptKey]) {
scriptNodesIndex[scriptKey] = [];
Expand Down
14 changes: 14 additions & 0 deletions test/modules/mergeScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,18 @@ describe('mergeScripts', () => {
options
);
});


it('should not change order of JS code', () => {
return init(
`<script>window.foo1 = 'foo'</script><script>window.foo2 = 'foo'</script>
<script src="./script-need-foo-variable.js"></script>
<script>window.bar1 = 'foo'</script><script>window.bar2 = 'bar'</script>`,

`<script>window.foo1 = 'foo';window.foo2 = 'foo'</script>
<script src="./script-need-foo-variable.js"></script>
<script>window.bar1 = 'foo';window.bar2 = 'bar'</script>`,
options
);
});
});

0 comments on commit edb5eae

Please sign in to comment.