data: 导入


data: URL 支持使用以下 MIME 类型导入:

  • text/javascript 用于 ES 模块
  • application/json 用于 JSON
  • application/wasm 用于 Wasm

data: URL 只为内置模块解析裸说明符绝对说明符。 解析相对说明符不起作用,因为 data: 不是特殊协议。 例如,尝试从 data:text/javascript,import "./foo"; 加载 ./foo 无法解析,因为 data: URL 没有相对解析的概念。 正在使用的 data: URL 示例是:

import 'data:text/javascript,console.log("hello!");';
import _ from 'data:application/json,"world!"';

data: URLs are supported for importing with the following MIME types:

  • text/javascript for ES Modules
  • application/json for JSON
  • application/wasm for Wasm

data: URLs only resolve Bare specifiers for builtin modules and Absolute specifiers. Resolving Relative specifiers does not work because data: is not a special scheme. For example, attempting to load ./foo from data:text/javascript,import "./foo"; fails to resolve because there is no concept of relative resolution for data: URLs. An example of a data: URLs being used is:

import 'data:text/javascript,console.log("hello!");';
import _ from 'data:application/json,"world!"';