Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for async injectStyle #1193

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/esbuild/postcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const postcssPlugin = ({
cssLoader,
}: {
css?: Map<string, string>
inject?: boolean | ((css: string, fileId: string) => string)
inject?: boolean | ((css: string, fileId: string) => string | Promise<string>)
cssLoader?: Loader
}): Plugin => {
return {
Expand Down Expand Up @@ -122,12 +122,11 @@ export const postcssPlugin = ({
})
).code

contents =
typeof inject === 'function'
? inject(JSON.stringify(contents), args.path)
: `import styleInject from '#style-inject';styleInject(${JSON.stringify(
contents,
)})`
contents = typeof inject === 'function'
? await Promise.resolve(inject(JSON.stringify(contents), args.path))
: `import styleInject from '#style-inject';styleInject(${JSON.stringify(
contents,
)})`

return {
contents,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export async function build(_options: Options) {
configName: item?.name,
options: {
...options, // functions cannot be cloned
injectStyle: typeof options.injectStyle === 'function' ? undefined : options.injectStyle,
banner: undefined,
footer: undefined,
esbuildPlugins: undefined,
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export type Options = {
* Inject CSS as style tags to document head
* @default {false}
*/
injectStyle?: boolean | ((css: string, fileId: string) => string)
injectStyle?: boolean | ((css: string, fileId: string) => string | Promise<string>)
/**
* Inject cjs and esm shims if needed
* @default false
Expand Down
26 changes: 25 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ test(`should generate export {} when there are no exports in source file`, async
expect(await getFileContent('dist/input.d.mts')).toMatch(/export {\s*}/)
})

test('custom inject style function', async () => {
test('custom inject style function - sync', async () => {
const { outFiles, getFileContent } = await run(getTestName(), {
'input.ts': `import './style.css'`,
'style.css': `.hello { color: red }`,
Expand All @@ -829,6 +829,30 @@ test('custom inject style function', async () => {
)
})

test('custom inject style function - async', async () => {
const { outFiles, getFileContent } = await run(getTestName(), {
'input.ts': `import './style.css'`,
'style.css': `.hello { color: red }`,
'tsup.config.ts': `
export default {
entry: ['src/input.ts'],
minify: true,
format: ['esm', 'cjs'],
injectStyle: async (css) => {
await new Promise(resolve => setTimeout(resolve, 100));
return "__custom_async_inject_style__(" + css +")";
}
}`,
})
expect(outFiles).toEqual(['input.js', 'input.mjs'])
expect(await getFileContent('dist/input.mjs')).toContain(
'__custom_async_inject_style__(`.hello{color:red}\n`)',
)
expect(await getFileContent('dist/input.js')).toContain(
'__custom_async_inject_style__(`.hello{color:red}\n`)',
)
})

test('preserve top-level variable for IIFE format', async () => {
const { outFiles, getFileContent } = await run(getTestName(), {
'input.ts': `export default 'foo'`,
Expand Down
Loading