Skip to content

Commit

Permalink
fix: 🐛 warp import (#405)
Browse files Browse the repository at this point in the history
Closes: #404
  • Loading branch information
maoxiaoke authored Sep 27, 2021
1 parent bd9241d commit 7e8d3f4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

See [https://github.com/ice-lab/icestark/releases](https://github.com/ice-lab/icestark/releases) for what has changed in each version of icestark.

## 2.6.1

- [fix] wrap `import` using `new Function` to avoid compiler error under chrome61 & ie. ([#404](https://github.com/ice-lab/icestark/issues/404))

## 2.6.0

- [feat] support native es module micro-applications. ([#346, #260](https://github.com/ice-lab/icestark/issues/346))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/stark",
"version": "2.6.0",
"version": "2.6.1",
"description": "Icestark is a JavaScript library for multiple projects, Ice workbench solution.",
"scripts": {
"install:deps": "rm -rf node_modules && rm -rf ./packages/*/node_modules && yarn install && lerna exec -- npm install",
Expand Down
22 changes: 18 additions & 4 deletions src/util/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,24 @@ export async function loadScriptByImport(jsList: Asset[]): Promise<null | Module
id: `${PREFIX}-js-module-${index}`,
});
} else {
const { mount: maybeMount, unmount: maybeUnmount } = await import(/* webpackIgnore: true */ /* @vite-ignore */js.content);
if (maybeMount && maybeUnmount) {
mount = maybeMount;
unmount = maybeUnmount;
try {
/**
* `import` will cause error under chrome 61 and ie.
* Then use `new Function` to escape compile error.
* Inspired by [dynamic-import-polyfill](https://github.com/GoogleChromeLabs/dynamic-import-polyfill)
*/
// eslint-disable-next-line no-new-func
const dynamicImport = new Function('url', 'return import(url)');
const { mount: maybeMount, unmount: maybeUnmount } = await dynamicImport(js.content);

if (maybeMount && maybeUnmount) {
mount = maybeMount;
unmount = maybeUnmount;
}
} catch (e) {
Promise.reject(
new Error('[icestark] You are not support to use `loadScriptMode = import` where dynamic import is not supported by browsers.'),
);
}
}
});
Expand Down

0 comments on commit 7e8d3f4

Please sign in to comment.