Skip to content

Commit bd82c6e

Browse files
author
PS
committed
feat: 🎸 Scripts without source will cause promise rejection
1 parent 75501c9 commit bd82c6e

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

Diff for: packages/integration/src/__tests__/indexSpec.js

+30
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,35 @@ describe('Merkur component', () => {
282282
done('promise was rejected');
283283
});
284284
});
285+
286+
it('should throw an error, if script have no source', (done) => {
287+
loadScriptAssets(
288+
[
289+
{
290+
name: 'optional.js',
291+
type: 'script',
292+
source: {},
293+
optional: true,
294+
},
295+
{
296+
name: 'nosource.js',
297+
type: 'script',
298+
source: {},
299+
},
300+
],
301+
rootElement
302+
)
303+
.then(() => {
304+
done('did not reject');
305+
})
306+
.catch((error) => {
307+
expect(error.message).toBe(
308+
"Asset 'nosource.js' is missing ES variant and could not be loaded."
309+
);
310+
expect(error.asset).toBeTruthy();
311+
312+
done();
313+
});
314+
});
285315
});
286316
});

Diff for: packages/integration/src/index.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function loadStyleAssets(assets, root = document.head) {
9090
return Promise.all(stylesToRender.map((asset) => _loadStyle(asset, root)));
9191
}
9292

93-
function loadScriptAssets(assets, root = document.head) {
93+
async function loadScriptAssets(assets, root = document.head) {
9494
const scriptElements = root.querySelectorAll('script');
9595
const scriptsToRender = assets.reduce((scripts, asset) => {
9696
const { source } = asset;
@@ -110,9 +110,16 @@ function loadScriptAssets(assets, root = document.head) {
110110
}
111111

112112
if (!_asset.source) {
113-
console.warn(
114-
`Asset '${_asset.name}' is missing ES variant and could not be loaded.`
115-
);
113+
const message = `Asset '${_asset.name}' is missing ES variant and could not be loaded.`;
114+
115+
if (!_asset.optional) {
116+
const error = new Error(message);
117+
error.asset = _asset;
118+
119+
throw error;
120+
}
121+
122+
console.warn(message);
116123
return scripts;
117124
}
118125
}

0 commit comments

Comments
 (0)