-
Notifications
You must be signed in to change notification settings - Fork 647
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
Open
joshblack
wants to merge
6
commits into
main
Choose a base branch
from
refactor/update-react-to-rolldown
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11d744f
refactor: move rollup-plugin-import-css to rolldown-plugin-import-css
joshblack 4466aa7
refactor(rolldown-plugin): update to rolldown plugin, remove build step
joshblack 1dbef54
refactor(rolldown-plugin): update to new syntax for better rust compat
joshblack a002443
refactor(react): use rolldown over rollup
joshblack ac69729
docs: update README to reference package name
joshblack 0953b87
Apply suggestions from code review
joshblack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
8 changes: 4 additions & 4 deletions
8
packages/rollup-plugin-import-css/README.md → ...ages/rolldown-plugin-import-css/README.md
This file contains hidden or 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 hidden or 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,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 | ||
| } | ||
This file contains hidden or 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,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) | ||
| } |
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.