Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
refactor(react): use rolldown over rollup#7300
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
11d744f4466aa71dbef54a002443ac697290953b87275fc52File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "rolldown-plugin-import-css", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "exports": "./src/index.ts", | ||
| "scripts": { | ||
| "type-check": "tsc --noEmit" | ||
| }, | ||
| "peerDependencies": { | ||
| "postcss": "^8.4.38", | ||
| "postcss-modules": "^6.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "postcss": "^8.4.38", | ||
| "postcss-modules": "^6.0.0", | ||
| "rolldown": "^1.0.0-beta.53", | ||
| "typescript": "^5.9.2" | ||
| }, | ||
| "sideEffects": false | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import type {Plugin} from 'rolldown' | ||
| import fs from 'node:fs/promises' | ||
| import path from 'node:path' | ||
| import {createHash} from 'node:crypto' | ||
| import postcss from 'postcss' | ||
| import postcssModules from 'postcss-modules' | ||
| interface ImportCSSOptions { | ||
| /** | ||
| * Provide the root directory for your package. This is used to calculate the | ||
| * relative path for generated CSS files | ||
| */ | ||
| modulesRoot: string | ||
| /** | ||
| * Optionally provide an array of postcss plugins to use during CSS | ||
| * compilation. | ||
| */ | ||
| postcssPlugins?: Array<postcss.AcceptedPlugin> | ||
| /** | ||
| * Optionally provide options to pass forward to `postcss-modules` when | ||
| * compiling CSS | ||
| */ | ||
| postcssModulesOptions?: Parameters<typeof postcssModules>[0] | ||
| } | ||
| export function importCSS(options: ImportCSSOptions): Plugin { | ||
| const {modulesRoot, postcssPlugins = [], postcssModulesOptions = {}} = options | ||
| const rootDirectory = path.isAbsolute(modulesRoot) ? modulesRoot : path.resolve(process.cwd(), modulesRoot) | ||
| return { | ||
| name: 'import-css', | ||
| resolveId: { | ||
| filter: { | ||
| id: /\.css$/, | ||
| }, | ||
| handler(source, importer) { | ||
| if (!importer) { | ||
| return | ||
| } | ||
| const moduleInfo = this.getModuleInfo(importer) | ||
| if (moduleInfo?.meta['import-css']?.source === source) { | ||
| return { | ||
| id: source, | ||
| external: true, | ||
| moduleSideEffects: true, | ||
| } | ||
| } | ||
| const id = path.resolve(path.dirname(importer), source) | ||
| return path.format({ | ||
| dir: path.dirname(id), | ||
| base: `${path.basename(id)}.js`, | ||
| }) | ||
| }, | ||
| }, | ||
| load: { | ||
| filter: { | ||
| id: /\.css\.js$/, | ||
| }, | ||
| handler() { | ||
| return '' | ||
| }, | ||
| }, | ||
| transform: { | ||
| filter: { | ||
| id: /\.module\.css\.js$/, | ||
| }, | ||
| async handler(_code, id) { | ||
| const sourceId = path.join(path.dirname(id), path.basename(id, '.js')) | ||
| const code = await fs.readFile(sourceId, 'utf8') | ||
| const hash = getSourceHash(code) | ||
| const relativePath = path.relative(rootDirectory, sourceId) | ||
| const name = path.basename(relativePath, relativePath.endsWith('.module.css') ? '.module.css' : '.css') | ||
| const fileName = path.join( | ||
| path.dirname(relativePath), | ||
| path.format({ | ||
| name: `${name}-${hash}`, | ||
| ext: '.css', | ||
| }), | ||
| ) | ||
| // When transforming CSS modules, we want to emit the generated CSS as an | ||
| // asset and include the generated file in our generated CSS Modules file | ||
| // which contains the classes. This makes sure that if the file containing | ||
| // the classes is used, then the associated styles for those classes is | ||
| // also included | ||
| let cssModuleClasses: {[name: string]: string} | null = null | ||
| const result = await postcss([ | ||
| ...postcssPlugins, | ||
| postcssModules({ | ||
| ...postcssModulesOptions, | ||
| getJSON(filename, json) { | ||
| if (postcssModulesOptions.getJSON) { | ||
| postcssModulesOptions.getJSON(filename, json) | ||
| } | ||
| cssModuleClasses = json | ||
| }, | ||
| }), | ||
| ]).process(code, { | ||
| from: id, | ||
| to: fileName, | ||
| map: { | ||
| inline: false, | ||
| }, | ||
| }) | ||
| this.emitFile({ | ||
| type: 'asset', | ||
| source: result.css, | ||
| fileName, | ||
| }) | ||
| this.emitFile({ | ||
| type: 'asset', | ||
| source: result.map.toString(), | ||
| fileName: `${fileName}.map`, | ||
| }) | ||
| const moduleInfo = this.getModuleInfo(id) | ||
| const cssSource = `./${path.basename(fileName)}` | ||
| if (moduleInfo) { | ||
| moduleInfo.meta['import-css'] = { | ||
| source: cssSource, | ||
| } | ||
| } | ||
| return { | ||
| code: ` | ||
| import '${cssSource}'; | ||
| ${ | ||
| // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
| cssModuleClasses !== null ? `export default ${JSON.stringify(cssModuleClasses)}` : '' | ||
| } | ||
| `, | ||
| moduleSideEffects: 'no-treeshake', | ||
| } | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
| const DEFAULT_HASH_SIZE = 8 | ||
| function getSourceHash(source: string) { | ||
| return createHash('sha256').update(source).digest('hex').slice(0, DEFAULT_HASH_SIZE) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.