|
1 | 1 | const path = require('path');
|
2 | 2 | const { createHash } = require('crypto');
|
3 |
| -const { readFile, writeFile, unlink } = require('fs/promises'); |
| 3 | +const { readFile, writeFile, unlink, appendFile } = require('fs/promises'); |
4 | 4 | const {
|
5 | 5 | getLogger,
|
6 | 6 | buildInjectCode,
|
@@ -315,15 +315,16 @@ const onEnd = async (build, options, result) => {
|
315 | 315 | };
|
316 | 316 |
|
317 | 317 | const buildJs = async (entryName, entryPath, jsCode) => {
|
318 |
| - const r = (p) => `./${path.relative(absOutdir, p).split(path.sep).join(path.posix.sep)}`; |
319 |
| - const imports = `import "${r(entryPath)}";`; |
320 |
| - if ((sourceRoot || publicPath) && sourcemap) { |
321 |
| - const sp = sourceRoot || publicPath; |
322 |
| - const fixedSourceRoot = sp.endsWith('/') ? sp : sp + '/'; |
| 318 | + const r = (p) => path.relative(absOutdir, p).split(path.sep).join(path.posix.sep); |
| 319 | + const imports = `import "./${r(entryPath)}";`; |
| 320 | + if (sourcemap === 'external') { |
| 321 | + await appendFile(entryPath, `\n//# sourceMappingURL=${r(entryPath)}.map`, { encoding: 'utf8' }); |
| 322 | + } else if (publicPath && sourcemap) { |
| 323 | + const fixedPublicPath = publicPath.endsWith('/') ? publicPath : publicPath + '/'; |
323 | 324 | const entryContent = await readFile(entryPath, { encoding: 'utf8' });
|
324 | 325 | await writeFile(
|
325 | 326 | entryPath,
|
326 |
| - entryContent.replace(`sourceMappingURL=${fixedSourceRoot}`, 'sourceMappingURL='), |
| 327 | + entryContent.replace(`sourceMappingURL=${fixedPublicPath}`, 'sourceMappingURL='), |
327 | 328 | { encoding: 'utf8' }
|
328 | 329 | );
|
329 | 330 | }
|
|
0 commit comments