脚本类型 importmap 有什么用?

What's script type importmap used for?

什么是 <script type="importmap">,为什么我的代码突然需要它才能正常工作?

<script type="importmap">
    {
        "imports": {
            "three": "https://example.com/3dstuff/three.module.js"
        }
    }
</script>

以前,我只写这个 Three.js 就可以了,但是现在这部分没有 importmap 就不能工作了:

<script type="module"> import * as THREE from "https://example.com/3dstuff/three.module.js";

代码中的 importmap 本质上是设置从字符串“three”到实际 .js 文件的快捷方式 URL。你应该在 <script type="module"> 中写的是 import * as three from "three"; 并且由于你之前定义的导入映射,它会自动解析为 URL。

来自https://github.com/WICG/import-maps

By supplying the browser with the following import map

<script type="importmap">
{
  "imports": {
    "moment": "/node_modules/moment/src/moment.js",
    "lodash": "/node_modules/lodash-es/lodash.js"
  }
}
</script>

the above would act as if you had written

import moment from "/node_modules/moment/src/moment.js";
import { partition } from "/node_modules/lodash-es/lodash.js";