Skip to content

Commit

Permalink
feat: add cache busting plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonghakseo committed May 11, 2024
1 parent 025c423 commit e6598af
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/hmr/lib/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './watch-rebuild-plugin';
export * from './make-entry-point-plugin';
47 changes: 47 additions & 0 deletions packages/hmr/lib/plugins/make-entry-point-plugin.ts
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);
});
},
};
}
4 changes: 2 additions & 2 deletions pages/content-ui/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { resolve } from 'path';
import { watchRebuildPlugin } from '@chrome-extension-boilerplate/hmr';
import { makeEntryPointPlugin, watchRebuildPlugin } from '@chrome-extension-boilerplate/hmr';

const rootDir = resolve(__dirname);
const srcDir = resolve(rootDir, 'src');
Expand All @@ -16,7 +16,7 @@ export default defineConfig({
},
},
base: '',
plugins: [react(), isDev && watchRebuildPlugin({ refresh: true })],
plugins: [react(), isDev && watchRebuildPlugin({ refresh: true }), isDev && makeEntryPointPlugin()],
publicDir: resolve(rootDir, 'public'),
build: {
lib: {
Expand Down
4 changes: 2 additions & 2 deletions pages/content/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineConfig } from 'vite';
import { resolve } from 'path';
import { watchRebuildPlugin } from '@chrome-extension-boilerplate/hmr';
import { makeEntryPointPlugin, watchRebuildPlugin } from '@chrome-extension-boilerplate/hmr';

const rootDir = resolve(__dirname);
const libDir = resolve(rootDir, 'lib');
Expand All @@ -14,7 +14,7 @@ export default defineConfig({
'@lib': libDir,
},
},
plugins: [isDev && watchRebuildPlugin({ refresh: true })],
plugins: [isDev && watchRebuildPlugin({ refresh: true }), isDev && makeEntryPointPlugin()],
publicDir: resolve(rootDir, 'public'),
build: {
lib: {
Expand Down
1 change: 1 addition & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"dev": {
"dependsOn": ["^build"],
"outputs": ["dist/**", "build/**"],
"persistent": true
},
Expand Down

0 comments on commit e6598af

Please sign in to comment.