{"record":{"id":"039820caeedc2f1f","repo":"babalae/better-genshin-impact","slug":"specifier-js","errorCode":null,"errorMessage":"不支持的模块导入类型: '{specifier}' (仅支持 .js 文件)","messagePattern":"不支持的模块导入类型: '(.+?)' \\(仅支持 \\.js 文件\\)","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Script/PackageDocumentLoader.cs","lineNumber":60,"sourceCode":"                throw new FileNotFoundException($\"无法解析模块导入路径: '{specifier}'\", specifier);\n            }\n\n            // 处理 JS 文件的重写\n            if (Path.GetExtension(targetPath).ToLower() == \".js\")\n            {\n                var uri = new Uri(targetPath);\n\n                // 检查缓存\n                var cached = GetCachedDocument(uri);\n                if (cached != null) return cached;\n\n                string content = await File.ReadAllTextAsync(targetPath);\n                string processedCode = RewriteScriptCode(content, targetPath);\n                var documentInfo = new DocumentInfo(uri) { Category = ModuleCategory.Standard };\n                return CacheDocument(new StringDocument(documentInfo, processedCode), false);\n            }\n\n            throw new FileNotFoundException($\"不支持的模块导入类型: '{specifier}' (仅支持 .js 文件)\", specifier);\n        }\n\n        /// <summary>\n        /// js重写\n        /// </summary>\n        public string RewriteScriptCode(string code, string currentFilePath)\n        {\n            if (string.IsNullOrEmpty(code)) return code;\n\n            string result = code.Replace(\"../../../packages\", \"packages\");\n\n            // 拦截资源导入\n            var resourceRegex = new Regex(@\"import\\s+([\\w\\d_*$]+|[\\s\\S]*?)\\s+from\\s+(['\"\"])([^'\"\"\\n]+)(['\"\"])\");\n            result = resourceRegex.Replace(result, match =>\n            {\n                string importPart = match.Groups[1].Value.Trim();\n                string quote = match.Groups[2].Value;\n                string path = match.Groups[3].Value.Replace(\"../../../packages\", \"packages\");","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Script/PackageDocumentLoader.cs#L42-L78","documentation":"Thrown by PackageDocumentLoader.LoadDocumentAsync when a module import path is resolved successfully but the file extension is not .js. The document loader only supports JavaScript module imports — JSON, CSS, images, and other file types cannot be imported as ES modules through this path (though images are handled specially via RewriteScriptCode for resource interception).","triggerScenarios":"A JS script attempts to import a non-.js file as an ES module, e.g. import config from './config.json' or import styles from './styles.css'. The loader resolves the file path but rejects it because only .js files are valid module entry points in this system.","commonSituations":"Script author tries to use Node.js-style JSON imports (require('./config.json')) which aren't supported in this ClearScript-based environment. Importing TypeScript files directly. Attempting to import a .mjs or .cjs file.","solutions":["Only import .js files as ES modules in this script system.","For JSON data, read it at runtime using the file API (e.g. file.readText()) and JSON.parse() instead of import.","Rename the target file to .js and export its contents as a JS module.","For image resources, use the supported resource import pattern handled by RewriteScriptCode."],"exampleFix":"// before\nimport config from './config.json';\n// after\nconst config = JSON.parse(file.readText('./config.json'));","handlingStrategy":"validation","validationCode":"if (!specifier.EndsWith(\".js\", StringComparison.OrdinalIgnoreCase))\n{\n    throw new NotSupportedException($\"Only .js imports are supported, got: {specifier}\");\n}","typeGuard":"static bool IsJsModule(string specifier) =>\n    specifier.EndsWith(\".js\", StringComparison.OrdinalIgnoreCase);","tryCatchPattern":null,"preventionTips":["Only use import statements for .js files in this ClearScript environment.","Load JSON and other data files via runtime file APIs and JSON.parse().","Document supported import types clearly for script package authors."],"tags":["module-resolution","javascript","clearscript","unsupported-type","import"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}