-
-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
025c423
commit e6598af
Showing
5 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './watch-rebuild-plugin'; | ||
export * from './make-entry-point-plugin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as fs from 'fs'; | ||
import path from 'path'; | ||
import type { PluginOption } from 'vite'; | ||
|
||
/** | ||
* make entry point file for content script cache busting | ||
*/ | ||
|
||
export function makeEntryPointPlugin(): PluginOption { | ||
const cleanupTargets = new Set<string>(); | ||
return { | ||
name: 'make-entry-point-plugin', | ||
generateBundle(options, bundle) { | ||
const outputDir = options.dir; | ||
if (!outputDir) { | ||
throw new Error('Output directory not found'); | ||
} | ||
for (const module of Object.values(bundle)) { | ||
const fileName = path.basename(module.fileName); | ||
const newFileName = fileName.replace('.js', '_dev.js'); | ||
switch (module.type) { | ||
case 'asset': | ||
// map file | ||
if (fileName.endsWith('.map')) { | ||
cleanupTargets.add(path.resolve(outputDir, fileName)); | ||
const originalFileName = fileName.replace('.map', ''); | ||
const replacedSource = String(module.source).replaceAll(originalFileName, newFileName); | ||
module.source = ''; | ||
fs.writeFileSync(path.resolve(outputDir, newFileName), replacedSource); | ||
break; | ||
} | ||
break; | ||
case 'chunk': { | ||
fs.writeFileSync(path.resolve(outputDir, newFileName), module.code); | ||
module.code = `import('./${newFileName}');`; | ||
break; | ||
} | ||
} | ||
} | ||
}, | ||
closeBundle() { | ||
cleanupTargets.forEach(target => { | ||
fs.unlinkSync(target); | ||
}); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters