Skip to content

Commit ac88f20

Browse files
committed
fix(no-missing-import): Support data imports
Imported prefixed with 'data:' are not modules to be resolved, but the direct data that should be imported (e.g., JavaScript code, JSON or WASM). This PR adds support for such imports in the no-missing-import rule. Note: With this change, the rule doesn't attempt to parse the data being imported, it merely does not try to resolve it to a path (which would fail), and thus allows those imports in the user's code. Fixes #464
1 parent 047d914 commit ac88f20

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/util/import-target.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function getTSConfigAliases(context) {
7474
* @property {Partial<import('enhanced-resolve').ResolveOptions>} [resolverConfig]
7575
* @property {string} basedir
7676
*/
77-
/** @typedef { 'unknown' | 'relative' | 'absolute' | 'node' | 'npm' | 'http' } ModuleType */
77+
/** @typedef { 'unknown' | 'relative' | 'absolute' | 'node' | 'npm' | 'http' | 'data' } ModuleType */
7878
/** @typedef { 'import' | 'require' | 'type' } ModuleStyle */
7979

8080
/**
@@ -176,6 +176,10 @@ module.exports = class ImportTarget {
176176
return "node"
177177
}
178178

179+
if (/^data:/.test(this.name)) {
180+
return "data"
181+
}
182+
179183
if (/^(@[\w~-][\w.~-]*\/)?[\w~-][\w.~-]*/.test(this.name)) {
180184
return "npm"
181185
}
@@ -242,7 +246,7 @@ module.exports = class ImportTarget {
242246
* @returns {string | undefined}
243247
*/
244248
getModuleName() {
245-
if (this.moduleType === "relative") return
249+
if (this.moduleType === "relative" || this.moduleType === "data") return
246250

247251
if (this.moduleType === "npm") {
248252
if (this.name.startsWith("@")) {
@@ -349,6 +353,10 @@ module.exports = class ImportTarget {
349353
? this.context.settings?.cwd
350354
: process.cwd()
351355

356+
if (this.moduleType === "data") {
357+
return this.name
358+
}
359+
352360
for (const directory of this.getPaths()) {
353361
const baseDir = resolve(cwd, directory)
354362

tests/lib/rules/no-missing-import.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ ruleTester.run("no-missing-import", rule, {
366366
options: [{ ignoreTypeImport: true }],
367367
},
368368

369+
// data import
370+
{
371+
filename: fixture("test.js"),
372+
code: "import 'data:text/javascript,const x = 123;';",
373+
},
374+
369375
// import()
370376
...(DynamicImportSupported
371377
? [

0 commit comments

Comments
 (0)