-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the
tailwindcss/plugin
export (#14173)
This PR adds support for the `tailwindcss/plugin` import which has historically been used to define custom plugins: ```js import plugin from "tailwindcss/plugin"; export default plugin(function ({ addBase }) { addBase({ // ... }); }); ``` This also adds support for `plugin.withOptions` which was used to define plugins that took optional initilization options when they were registered in your `tailwind.config.js` file: ```js import plugin from "tailwindcss/plugin"; export default plugin.withOptions((options = {}) => { return function ({ addBase }) { addBase({ // ... }); }; }); ``` We've stubbed out support for the `config` argument but we're not actually doing anything with it at the time of this PR. The scope of this PR is just to allow people to create plugins that currently work using the raw function syntax but using the `plugin` and `plugin.withOptions` APIs. Support for `config` will land separately. --------- Co-authored-by: Adam Wathan <[email protected]>
- Loading branch information
1 parent
9ab4732
commit e299ea3
Showing
10 changed files
with
191 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// This file exists so that `plugin.ts` can be written one time but be compatible with both CJS and | ||
// ESM. Without it we get a `.default` export when using `require` in CJS. | ||
|
||
// @ts-ignore | ||
module.exports = require('./plugin.ts').default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { test } from 'vitest' | ||
import { compile } from '.' | ||
import plugin from './plugin' | ||
|
||
const css = String.raw | ||
|
||
test('plugin', async ({ expect }) => { | ||
let input = css` | ||
@plugin "my-plugin"; | ||
` | ||
|
||
let compiler = await compile(input, { | ||
loadPlugin: async () => { | ||
return plugin(function ({ addBase }) { | ||
addBase({ | ||
body: { | ||
margin: '0', | ||
}, | ||
}) | ||
}) | ||
}, | ||
}) | ||
|
||
expect(compiler.build([])).toMatchInlineSnapshot(` | ||
"@layer base { | ||
body { | ||
margin: 0; | ||
} | ||
} | ||
" | ||
`) | ||
}) | ||
|
||
test('plugin.withOptions', async ({ expect }) => { | ||
let input = css` | ||
@plugin "my-plugin"; | ||
` | ||
|
||
let compiler = await compile(input, { | ||
loadPlugin: async () => { | ||
return plugin.withOptions(function (opts = { foo: '1px' }) { | ||
return function ({ addBase }) { | ||
addBase({ | ||
body: { | ||
margin: opts.foo, | ||
}, | ||
}) | ||
} | ||
}) | ||
}, | ||
}) | ||
|
||
expect(compiler.build([])).toMatchInlineSnapshot(` | ||
"@layer base { | ||
body { | ||
margin: 1px; | ||
} | ||
} | ||
" | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Config, PluginFn, PluginWithConfig, PluginWithOptions } from './plugin-api' | ||
|
||
function createPlugin(handler: PluginFn, config?: Partial<Config>): PluginWithConfig { | ||
return { | ||
handler, | ||
config, | ||
} | ||
} | ||
|
||
createPlugin.withOptions = function <T>( | ||
pluginFunction: (options?: T) => PluginFn, | ||
configFunction: (options?: T) => Partial<Config> = () => ({}), | ||
): PluginWithOptions<T> { | ||
function optionsFunction(options: T): PluginWithConfig { | ||
return { | ||
handler: pluginFunction(options), | ||
config: configFunction(options), | ||
} | ||
} | ||
|
||
optionsFunction.__isOptionsFunction = true as const | ||
|
||
return optionsFunction as PluginWithOptions<T> | ||
} | ||
|
||
export default createPlugin |
Oops, something went wrong.