Skip to content

Commit 5498f93

Browse files
committed
fix(karma): load scripts in strict mode
Replace the eval() with <script> tags for each script, so that they still create global variables Similar to g3 cl/198411444 Fixes #922
1 parent 7163571 commit 5498f93

1 file changed

Lines changed: 41 additions & 16 deletions

File tree

packages/karma/src/index.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,50 @@ function initConcatJs(logger, emitter, basePath, hostname, port) {
3636
content: '',
3737
encodings: {},
3838
} as any;
39-
const included = [];
39+
// Preserve all non-JS that were there in the included list.
40+
const included = files.included.filter(f => path.extname(f.originalPath) !== '.js');
41+
const bundledFiles =
42+
files.included.filter(f => path.extname(f.originalPath) === '.js').map((file) => {
43+
const relativePath = path.relative(basePath, file.originalPath).replace(/\\/g, '/');
4044

41-
files.included.forEach(file => {
42-
if (path.extname(file.originalPath) !== '.js') {
43-
// Preserve all non-JS that were there in the included list.
44-
included.push(file);
45-
} else {
46-
const relativePath = path.relative(basePath, file.originalPath).replace(/\\/g, '/');
45+
let content = file.content + `\n//# sourceURL=http://${hostname}:${port}/base/` +
46+
relativePath + '\n';
4747

48-
// Remove 'use strict'.
49-
let content = file.content.replace(/('use strict'|"use strict");?/, '');
50-
content = JSON.stringify(
51-
`${content}\n//# sourceURL=http://${hostname}:${port}/base/` +
52-
`${relativePath}\n`);
53-
content = `//${relativePath}\neval(${content});\n`;
54-
bundleFile.content += content;
55-
}
56-
});
48+
return `
49+
loadFile(
50+
${JSON.stringify(relativePath)},
51+
${JSON.stringify(content)});`;
52+
});
5753

54+
// Execute each file by putting it in a <script> tag. This makes them create
55+
// global variables, even with 'use strict'; (unlike eval).
56+
bundleFile.content = `
57+
(function() { // Hide local variables
58+
// IE 8 and below do not support document.head.
59+
var parent = document.getElementsByTagName('head')[0] ||
60+
document.documentElement;
61+
function loadFile(path, src) {
62+
try {
63+
var script = document.createElement('script');
64+
if ('textContent' in script) {
65+
script.textContent = src;
66+
} else {
67+
// This is for IE 8 and below.
68+
script.text = src;
69+
}
70+
parent.appendChild(script);
71+
// Don't pollute the DOM with hundreds of <script> tags.
72+
parent.removeChild(script);
73+
} catch(err) {
74+
window.__karma__ && window.__karma__.error(
75+
'An error occurred while loading ' + path + ':\\n' +
76+
(err.stack || err.message || err.toString()));
77+
console.error('An error occurred while loading ' + path, err);
78+
throw err;
79+
}
80+
}
81+
${bundledFiles.join('')}
82+
})();`;
5883
bundleFile.sha = sha1(Buffer.from(bundleFile.content));
5984
bundleFile.mtime = new Date();
6085
included.unshift(bundleFile);

0 commit comments

Comments
 (0)