|
1 | 1 | /* eslint @stylistic/quote-props: ["error", "consistent"] */ |
2 | 2 |
|
3 | 3 | import { defineConfig, Options } from 'tsup' |
4 | | -import { prependDirective } from './scripts/prependDirectivePlugin.ts' |
| 4 | +import { readFileSync, writeFileSync } from 'fs' |
5 | 5 | import { ExpectedBuildOutput, validateBuildFiles } from './scripts/validateBuild.ts' |
6 | 6 |
|
7 | 7 | const expectedBuildOutput: ExpectedBuildOutput = { |
@@ -104,3 +104,25 @@ process.on('beforeExit', (code) => { |
104 | 104 | validateBuildFiles(expectedBuildOutput) |
105 | 105 | console.log('[POST-BUILD] Validation successful! Build complete.') |
106 | 106 | }) |
| 107 | + |
| 108 | +function prependDirective(directive: string, filePatterns: string[]): NonNullable<Options['plugins']>[number] { |
| 109 | + if (!Array.isArray(filePatterns)) { |
| 110 | + throw Error('FilePatterns given to prependDirective plugin must be an array.') |
| 111 | + } |
| 112 | + return { |
| 113 | + name: 'prepend-directive', |
| 114 | + // At the end of the build step, directly prepend the specified directive to files with specified pattern |
| 115 | + |
| 116 | + buildEnd(ctx) { |
| 117 | + for (const file of ctx.writtenFiles) { |
| 118 | + for (const filePattern of filePatterns) { |
| 119 | + if (file.name.startsWith(filePattern)) { |
| 120 | + const fileContent = readFileSync(file.name, 'utf8') |
| 121 | + writeFileSync(file.name, `${directive};${fileContent}`) |
| 122 | + console.log(`Prepended ${directive} directive to ${file.name}`) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + }, |
| 127 | + } |
| 128 | +} |
0 commit comments