Last active
May 26, 2022 09:34
-
-
Save evisong/0e70d3ee5215d751e7d57d8378de9c96 to your computer and use it in GitHub Desktop.
ESM self import: different behaviors between browser native and es-module-shims
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const b = { | |
| m: () => console.log('Run m') | |
| }; | |
| import { b as c } from './module-self-import.mjs'; | |
| const m = c.m; | |
| export default function a() { | |
| console.log('Run a'); | |
| m(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <body> | |
| <script type="module"> | |
| import a from './module-self-import.mjs'; | |
| console.log('Run inline'); | |
| a(); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "self-import", | |
| "version": "1.0.0", | |
| "private": true, | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "start": "serve -C" | |
| }, | |
| "author": "", | |
| "license": "ISC", | |
| "devDependencies": { | |
| "serve": "^13.0.2" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script async src="https://ga.jspm.io/npm:es-module-shims@1.5.5/dist/es-module-shims.js"></script> | |
| </head> | |
| <body> | |
| <script type="module-shim"> | |
| import a from './module-self-import.mjs'; | |
| console.log('Run inline'); | |
| a(); | |
| </script> | |
| </body> | |
| </html> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Download and run:
Visit http://localhost:3000/native.html or http://localhost:3000/shims.html in browser.
For
native.html, the browser console shows:For
shims.html, the browser console shows:FYI, a similar self-import pattern in module-self-import.mjs can be found in Webpack 5 generated ESM bundles (with code-splitting).