Skip to content

Commit 01169b3

Browse files
authored
feat: add function type for options.injectStyle (#855)
1 parent 97f3abf commit 01169b3

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

src/esbuild/postcss.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import fs from 'fs'
22
import path from 'path'
33
import { Loader, Plugin, transform } from 'esbuild'
44
import { getPostcss } from '../utils'
5-
import type { Result } from 'postcss-load-config';
5+
import type { Result } from 'postcss-load-config'
66

77
export const postcssPlugin = ({
88
css,
99
inject,
1010
cssLoader,
1111
}: {
1212
css?: Map<string, string>
13-
inject?: boolean
13+
inject?: boolean | ((css: string, fileId: string) => string)
1414
cssLoader?: Loader
1515
}): Plugin => {
1616
return {
@@ -28,7 +28,7 @@ export const postcssPlugin = ({
2828

2929
try {
3030
const result = await loadConfig({}, process.cwd())
31-
configCache = result
31+
configCache = result
3232
return result
3333
} catch (error: any) {
3434
if (error.message.includes('No PostCSS Config found in')) {
@@ -123,9 +123,12 @@ export const postcssPlugin = ({
123123
})
124124
).code
125125

126-
contents = `import styleInject from '#style-inject';styleInject(${JSON.stringify(
127-
contents
128-
)})`
126+
contents =
127+
typeof inject === 'function'
128+
? inject(JSON.stringify(contents), args.path)
129+
: `import styleInject from '#style-inject';styleInject(${JSON.stringify(
130+
contents
131+
)})`
129132

130133
return {
131134
contents,

src/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export type Options = {
197197
* Inject CSS as style tags to document head
198198
* @default {false}
199199
*/
200-
injectStyle?: boolean
200+
injectStyle?: boolean | ((css: string, fileId: string) => string)
201201
/**
202202
* Inject cjs and esm shims if needed
203203
* @default false

test/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,3 +1245,25 @@ test(`should generate export {} when there are no exports in source file`, async
12451245
expect(outFiles).toEqual(['input.d.ts', 'input.mjs'])
12461246
expect(await getFileContent('dist/input.d.ts')).toContain('export { }')
12471247
})
1248+
1249+
test('custom inject style function', async () => {
1250+
const { outFiles, getFileContent } = await run(
1251+
getTestName(),
1252+
{
1253+
'input.ts': `import './style.css'`,
1254+
'style.css': `.hello { color: red }`,
1255+
'tsup.config.ts': `
1256+
export default {
1257+
entry: ['src/input.ts'],
1258+
minify: true,
1259+
format: ['esm', 'cjs'],
1260+
injectStyle: (css) => {
1261+
return "__custom_inject_style__(" + css +")";
1262+
}
1263+
}`,
1264+
},
1265+
)
1266+
expect(outFiles).toEqual(['input.js', 'input.mjs'])
1267+
expect(await getFileContent('dist/input.mjs')).toContain('__custom_inject_style__(`.hello{color:red}\n`)')
1268+
expect(await getFileContent('dist/input.js')).toContain('__custom_inject_style__(`.hello{color:red}\n`)')
1269+
})

0 commit comments

Comments
 (0)