这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: added option to allow alternative postcss file extensions #1307

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export async function runEsbuild(
css,
inject: options.injectStyle,
cssLoader: loader['.css'],
fileExtensions: options.postcssFileExtensions
}),
sveltePlugin({ css }),
...(options.esbuildPlugins || []),
Expand Down
6 changes: 4 additions & 2 deletions src/esbuild/postcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export const postcssPlugin = ({
css,
inject,
cssLoader,
fileExtensions = ["css"]
}: {
css?: Map<string, string>
inject?: boolean | ((css: string, fileId: string) => string | Promise<string>)
cssLoader?: Loader
cssLoader?: Loader,
fileExtensions?: string[]
}): Plugin => {
return {
name: 'postcss',
Expand Down Expand Up @@ -77,7 +79,7 @@ export const postcssPlugin = ({
},
)

build.onLoad({ filter: /\.css$/ }, async (args) => {
build.onLoad({ filter: new RegExp(fileExtensions.map(ext => `\\.${ext}$`).join("|")) }, async (args) => {
let contents: string

if (css && args.path.endsWith('.svelte.css')) {
Expand Down
5 changes: 5 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ export type Options = {
injectStyle?:
| boolean
| ((css: string, fileId: string) => string | Promise<string>)
/**
* Allowed postcss file extensions
* @default ["css"]
*/
postcssFileExtensions?: string[]
/**
* Inject cjs and esm shims if needed
* @default false
Expand Down
5 changes: 5 additions & 0 deletions test/__snapshots__/css.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ exports[`import css in --dts 1`] = `
"
`;

exports[`support alternative postcss file extension 1`] = `
""use strict";
"
`;

exports[`support tailwindcss postcss plugin 1`] = `
""use strict";
"
Expand Down
36 changes: 35 additions & 1 deletion test/css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, test } from 'vitest'
import { getTestName, run } from './utils'

test('import css', async () => {
const { output, outFiles } = await run(getTestName(), {
const { output, outFiles, getFileContent } = await run(getTestName(), {
'input.ts': `
import './foo.css'
`,
Expand All @@ -21,6 +21,40 @@ test('import css', async () => {
})

expect(output, `""`).toMatchSnapshot()
expect(await getFileContent("dist/input.css")).not.toContain("$color")
expect(outFiles).toEqual(['input.css', 'input.js'])
})

test('support alternative postcss file extension', async () => {
const { output, outFiles, getFileContent } = await run(getTestName(), {
'input.ts': `
import './foo.postcss'
`,
'postcss.config.js': `
module.exports = {
plugins: [require('postcss-simple-vars')()]
}
`,
'foo.postcss': `
$color: blue;

.foo {
color: $color;
}
`,
'tsup.config.ts': `
export default {
loader: {
'.postcss': 'css'
},
postcssFileExtensions: ['postcss']
}
`,
})


expect(output, `""`).toMatchSnapshot()
expect(await getFileContent("dist/input.css")).not.toContain("$color")
expect(outFiles).toEqual(['input.css', 'input.js'])
})

Expand Down