| 7 | * This plugin copies source maps to the build directory and ensures they're accessible |
| 8 | */ |
| 9 | export function sourcemapPlugin(): Plugin { |
| 10 | return { |
| 11 | name: "vite-plugin-sourcemap", |
| 12 | apply: "build", |
| 13 | |
| 14 | // After the build is complete, ensure source maps are included in the build |
| 15 | closeBundle: { |
| 16 | order: "post", |
| 17 | handler: async () => { |
| 18 | console.log("Ensuring source maps are included in build...") |
| 19 | |
| 20 | // Determine the correct output directory based on the build mode |
| 21 | const mode = process.env.NODE_ENV |
| 22 | let outDir |
| 23 | |
| 24 | if (mode === "nightly") { |
| 25 | outDir = path.resolve("../apps/vscode-nightly/build/webview-ui/build") |
| 26 | } else { |
| 27 | outDir = path.resolve("../src/webview-ui/build") |
| 28 | } |
| 29 | |
| 30 | const assetsDir = path.join(outDir, "assets") |
| 31 | |
| 32 | console.log(`Source map processing for ${mode} build in ${outDir}`) |
| 33 | |
| 34 | // Check if build directory exists |
| 35 | if (!fs.existsSync(outDir)) { |
| 36 | console.warn("Build directory not found:", outDir) |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | // Check if assets directory exists |
| 41 | if (!fs.existsSync(assetsDir)) { |
| 42 | console.warn("Assets directory not found:", assetsDir) |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | // Find JS files in the assets directory |
| 47 | const jsFiles = fs.readdirSync(assetsDir).filter((file) => file.endsWith(".js")) |
| 48 | |
| 49 | console.log(`Found ${jsFiles.length} JS files in assets directory`) |
| 50 | |
| 51 | // Check for source maps |
| 52 | for (const jsFile of jsFiles) { |
| 53 | const jsPath = path.join(assetsDir, jsFile) |
| 54 | const mapPath = jsPath + ".map" |
| 55 | |
| 56 | // If source map exists, ensure it's properly referenced in the JS file |
| 57 | if (fs.existsSync(mapPath)) { |
| 58 | console.log(`Source map found for ${jsFile}`) |
| 59 | |
| 60 | // Read the JS file |
| 61 | let jsContent = fs.readFileSync(jsPath, "utf8") |
| 62 | |
| 63 | // Check if the source map is already referenced |
| 64 | if (!jsContent.includes("//# sourceMappingURL=")) { |
| 65 | console.log(`Adding source map reference to ${jsFile}`) |
| 66 | |