File size: 714 Bytes
6a2bc3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { readFileSync } from 'node:fs';

/**
 * Bundler plugin that lets files be imported as raw strings:
 *
 *   import description from './grep.md?raw';
 *
 * The file content is inlined into the bundle at build time, so prompt
 * source files never ship separately in `dist`. Vitest handles the same
 * `?raw` imports through Vite's built-in asset loader.
 */
export function rawTextPlugin() {
  return {
    name: 'raw-text',
    enforce: 'pre',
    load(id) {
      const [path, query = ''] = id.split('?', 2);
      if (!query.split('&').includes('raw')) return null;
      const text = readFileSync(path, 'utf-8');
      return { code: `export default ${JSON.stringify(text)};`, map: null };
    },
  };
}