|
| 1 | +import { mkdir, writeFile } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { pathToFileURL } from 'node:url'; |
| 4 | + |
| 5 | +import type { Rspack } from '@rsbuild/core'; |
| 6 | + |
| 7 | +/** |
| 8 | + * The options for {@link TailwindRspackPlugin}. |
| 9 | + * |
| 10 | + * @public |
| 11 | + */ |
| 12 | +interface TailwindRspackPluginOptions { |
| 13 | + /** |
| 14 | + * The path to the configuration of Tailwind CSS. |
| 15 | + * |
| 16 | + * @remarks |
| 17 | + * |
| 18 | + * The default value is `tailwind.config.js`. |
| 19 | + * |
| 20 | + * @example |
| 21 | + * |
| 22 | + * Use absolute path: |
| 23 | + * |
| 24 | + * ```js |
| 25 | + * // rspack.config.js |
| 26 | + * import path from 'node:path' |
| 27 | + * import { fileURLToPath } from 'node:url' |
| 28 | + * |
| 29 | + * import { TailwindRspackPlugin } from '@rsbuild/plugin-tailwindcss' |
| 30 | + * |
| 31 | + * const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 32 | + * |
| 33 | + * export default { |
| 34 | + * plugins: [ |
| 35 | + * new TailwindRspackPlugin({ |
| 36 | + * config: path.resolve(__dirname, './config/tailwind.config.js'), |
| 37 | + * }), |
| 38 | + * ], |
| 39 | + * } |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * @example |
| 43 | + * |
| 44 | + * Use relative path: |
| 45 | + * |
| 46 | + * ```js |
| 47 | + * // rspack.config.js |
| 48 | + * import { TailwindRspackPlugin } from '@rsbuild/plugin-tailwindcss' |
| 49 | + * |
| 50 | + * export default { |
| 51 | + * plugins: [ |
| 52 | + * new TailwindRspackPlugin({ |
| 53 | + * config: './config/tailwind.config.js', |
| 54 | + * }), |
| 55 | + * ], |
| 56 | + * } |
| 57 | + * ``` |
| 58 | + */ |
| 59 | + config?: string; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * The Rspack plugin for Tailwind integration. |
| 64 | + * |
| 65 | + * @public |
| 66 | + */ |
| 67 | +class TailwindRspackPlugin { |
| 68 | + constructor( |
| 69 | + private readonly options?: TailwindRspackPluginOptions | undefined, |
| 70 | + ) {} |
| 71 | + |
| 72 | + /** |
| 73 | + * `defaultOptions` is the default options that the {@link TailwindRspackPlugin} uses. |
| 74 | + * |
| 75 | + * @public |
| 76 | + */ |
| 77 | + static defaultOptions: Readonly<Required<TailwindRspackPluginOptions>> = |
| 78 | + Object.freeze<Required<TailwindRspackPluginOptions>>({ |
| 79 | + config: 'tailwind.config.js', |
| 80 | + }); |
| 81 | + |
| 82 | + /** |
| 83 | + * The entry point of a Rspack plugin. |
| 84 | + * @param compiler - the Rspack compiler |
| 85 | + */ |
| 86 | + apply(compiler: Rspack.Compiler): void { |
| 87 | + new TailwindRspackPluginImpl( |
| 88 | + compiler, |
| 89 | + Object.assign({}, TailwindRspackPlugin.defaultOptions, this.options), |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +export { TailwindRspackPlugin }; |
| 95 | +export type { TailwindRspackPluginOptions }; |
| 96 | + |
| 97 | +type NoUndefinedField<T> = { |
| 98 | + [P in keyof T]-?: NoUndefinedField<NonNullable<T[P]>>; |
| 99 | +}; |
| 100 | + |
| 101 | +class TailwindRspackPluginImpl { |
| 102 | + name = 'TailwindRspackPlugin'; |
| 103 | + |
| 104 | + constructor( |
| 105 | + private compiler: Rspack.Compiler, |
| 106 | + private options: NoUndefinedField<TailwindRspackPluginOptions>, |
| 107 | + ) { |
| 108 | + const { RawSource } = compiler.webpack.sources; |
| 109 | + compiler.hooks.thisCompilation.tap(this.name, (compilation) => { |
| 110 | + compilation.hooks.processAssets.tapPromise(this.name, async () => { |
| 111 | + await Promise.all( |
| 112 | + [...compilation.entrypoints.entries()].map( |
| 113 | + async ([entryName, entrypoint]) => { |
| 114 | + const cssFiles = entrypoint |
| 115 | + .getFiles() |
| 116 | + .filter((file) => file.endsWith('.css')) |
| 117 | + .map((file) => compilation.getAsset(file)) |
| 118 | + .filter((file) => !!file); |
| 119 | + |
| 120 | + if (cssFiles.length === 0) { |
| 121 | + // Ignore entrypoint without CSS files. |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + // collect all the modules corresponding to specific entry |
| 126 | + const entryModules = new Set<string>(); |
| 127 | + |
| 128 | + for (const chunk of entrypoint.chunks) { |
| 129 | + const modules = |
| 130 | + compilation.chunkGraph.getChunkModulesIterable(chunk); |
| 131 | + for (const module of modules) { |
| 132 | + collectModules(module, entryModules); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + const [ |
| 137 | + { default: postcss }, |
| 138 | + { default: tailwindcss }, |
| 139 | + configPath, |
| 140 | + ] = await Promise.all([ |
| 141 | + import('postcss'), |
| 142 | + import('tailwindcss'), |
| 143 | + this.#prepareTailwindConfig(entryName, entryModules), |
| 144 | + ]); |
| 145 | + |
| 146 | + const postcssTransform = postcss([ |
| 147 | + // We use a config path to avoid performance issue of TailwindCSS |
| 148 | + // See: https://github.com/tailwindlabs/tailwindcss/issues/14229 |
| 149 | + tailwindcss({ |
| 150 | + config: configPath, |
| 151 | + }), |
| 152 | + ]); |
| 153 | + |
| 154 | + // iterate all css asset in entry and inject entry modules into tailwind content |
| 155 | + await Promise.all( |
| 156 | + cssFiles.map(async (asset) => { |
| 157 | + const content = asset.source.source(); |
| 158 | + // transform .css which contains tailwind mixin |
| 159 | + // FIXME: add custom postcss config |
| 160 | + const transformResult = await postcssTransform.process( |
| 161 | + content, |
| 162 | + { from: asset.name }, |
| 163 | + ); |
| 164 | + // FIXME: add sourcemap support |
| 165 | + compilation.updateAsset( |
| 166 | + asset.name, |
| 167 | + new RawSource(transformResult.css), |
| 168 | + ); |
| 169 | + }), |
| 170 | + ); |
| 171 | + }, |
| 172 | + ), |
| 173 | + ); |
| 174 | + }); |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + async #prepareTailwindConfig( |
| 179 | + entryName: string, |
| 180 | + entryModules: Set<string>, |
| 181 | + ): Promise<string> { |
| 182 | + const userConfig = path.isAbsolute(this.options.config) |
| 183 | + ? this.options.config |
| 184 | + : // biome-ignore lint/style/noNonNullAssertion: should have context |
| 185 | + path.resolve(this.compiler.options.context!, this.options.config); |
| 186 | + |
| 187 | + const outputDir = path.resolve( |
| 188 | + // biome-ignore lint/style/noNonNullAssertion: should have `output.path` |
| 189 | + this.compiler.options.output.path!, |
| 190 | + '.rsbuild', |
| 191 | + entryName, |
| 192 | + ); |
| 193 | + await mkdir(outputDir, { recursive: true }); |
| 194 | + |
| 195 | + const configPath = path.resolve(outputDir, 'tailwind.config.mjs'); |
| 196 | + |
| 197 | + await writeFile( |
| 198 | + configPath, |
| 199 | + `\ |
| 200 | +import config from '${pathToFileURL(userConfig)}' |
| 201 | +export default { |
| 202 | + ...config, |
| 203 | + content: ${JSON.stringify(Array.from(entryModules))} |
| 204 | +}`, |
| 205 | + ); |
| 206 | + |
| 207 | + return configPath; |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +function collectModules( |
| 212 | + module: Rspack.Module, |
| 213 | + entryModules: Set<string>, |
| 214 | +): void { |
| 215 | + if (module.modules) { |
| 216 | + for (const innerModule of module.modules) { |
| 217 | + collectModules(innerModule, entryModules); |
| 218 | + } |
| 219 | + } else if (module.resource) { |
| 220 | + entryModules.add(module.resource); |
| 221 | + } |
| 222 | +} |
0 commit comments