-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Upgrade: Rewrite imports of relative files to use relative file paths #14755
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
Merged
adamwathan
merged 5 commits into
next
from
10-22-upgrade_rewrite_imports_of_relative_files_to_use_relative_file_paths
Oct 22, 2024
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3b86506
Upgrade: Rewrite imports of relative files to use relative file paths
philipp-spiess e076a5c
Merge branch 'next' into 10-22-upgrade_rewrite_imports_of_relative_fi…
RobinMalfait 4105468
Update CHANGELOG.md
adamwathan 4d7379c
Merge branch 'next' into 10-22-upgrade_rewrite_imports_of_relative_fi…
adamwathan b5a7f8c
Merge branch 'next' into 10-22-upgrade_rewrite_imports_of_relative_fi…
adamwathan 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .foo { | ||
| color: red; | ||
| } |
102 changes: 102 additions & 0 deletions
102
packages/@tailwindcss-upgrade/src/codemods/migrate-import.test.ts
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,102 @@ | ||
| import { __unstable__loadDesignSystem } from '@tailwindcss/node' | ||
| import dedent from 'dedent' | ||
| import postcss from 'postcss' | ||
| import { expect, it } from 'vitest' | ||
| import type { UserConfig } from '../../../tailwindcss/src/compat/config/types' | ||
| import { migrateImport } from './migrate-import' | ||
|
|
||
| const css = dedent | ||
|
|
||
| async function migrate(input: string, userConfig: UserConfig = {}) { | ||
| return postcss() | ||
| .use( | ||
| migrateImport({ | ||
| designSystem: await __unstable__loadDesignSystem(`@import 'tailwindcss';`, { | ||
| base: __dirname, | ||
| }), | ||
| userConfig, | ||
| }), | ||
| ) | ||
| .process(input, { from: expect.getState().testPath }) | ||
| .then((result) => result.css) | ||
| } | ||
|
|
||
| it('prints relative file imports as relative paths', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @import 'fixtures/test'; | ||
RobinMalfait marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @import 'fixtures/test.css'; | ||
| @import './fixtures/test.css'; | ||
| @import './fixtures/test'; | ||
|
|
||
| @import 'fixtures/test' screen; | ||
| @import 'fixtures/test.css' screen; | ||
| @import './fixtures/test.css' screen; | ||
| @import './fixtures/test' screen; | ||
|
|
||
| @import 'fixtures/test' supports(display: grid); | ||
| @import 'fixtures/test.css' supports(display: grid); | ||
| @import './fixtures/test.css' supports(display: grid); | ||
| @import './fixtures/test' supports(display: grid); | ||
|
|
||
| @import 'fixtures/test' layer(utilities); | ||
| @import 'fixtures/test.css' layer(utilities); | ||
| @import './fixtures/test.css' layer(utilities); | ||
| @import './fixtures/test' layer(utilities); | ||
|
|
||
| @import 'fixtures/test' theme(inline); | ||
| @import 'fixtures/test.css' theme(inline); | ||
| @import './fixtures/test.css' theme(inline); | ||
| @import './fixtures/test' theme(inline); | ||
|
|
||
| @import 'fixtures/test' layer(utilities) supports(display: grid) screen and (min-width: 600px); | ||
| @import 'fixtures/test.css' layer(utilities) supports(display: grid) screen and | ||
| (min-width: 600px); | ||
| @import './fixtures/test.css' layer(utilities) supports(display: grid) screen and | ||
| (min-width: 600px); | ||
| @import './fixtures/test' layer(utilities) supports(display: grid) screen and | ||
| (min-width: 600px); | ||
|
|
||
| @import 'tailwindcss'; | ||
| @import 'tailwindcss/theme.css'; | ||
| @import 'tailwindcss/theme'; | ||
| `), | ||
| ).toMatchInlineSnapshot(` | ||
| "@import './fixtures/test.css'; | ||
| @import './fixtures/test.css'; | ||
| @import './fixtures/test.css'; | ||
| @import './fixtures/test.css'; | ||
|
|
||
| @import './fixtures/test.css' screen; | ||
| @import './fixtures/test.css' screen; | ||
| @import './fixtures/test.css' screen; | ||
| @import './fixtures/test.css' screen; | ||
|
|
||
| @import './fixtures/test.css' supports(display: grid); | ||
| @import './fixtures/test.css' supports(display: grid); | ||
| @import './fixtures/test.css' supports(display: grid); | ||
| @import './fixtures/test.css' supports(display: grid); | ||
|
|
||
| @import './fixtures/test.css' layer(utilities); | ||
| @import './fixtures/test.css' layer(utilities); | ||
| @import './fixtures/test.css' layer(utilities); | ||
| @import './fixtures/test.css' layer(utilities); | ||
|
|
||
| @import './fixtures/test.css' theme(inline); | ||
| @import './fixtures/test.css' theme(inline); | ||
| @import './fixtures/test.css' theme(inline); | ||
| @import './fixtures/test.css' theme(inline); | ||
|
|
||
| @import './fixtures/test.css' layer(utilities) supports(display: grid) screen and (min-width: 600px); | ||
| @import './fixtures/test.css' layer(utilities) supports(display: grid) screen and | ||
| (min-width: 600px); | ||
| @import './fixtures/test.css' layer(utilities) supports(display: grid) screen and | ||
| (min-width: 600px); | ||
| @import './fixtures/test.css' layer(utilities) supports(display: grid) screen and | ||
| (min-width: 600px); | ||
|
|
||
| @import 'tailwindcss'; | ||
| @import 'tailwindcss/theme.css'; | ||
| @import 'tailwindcss/theme';" | ||
| `) | ||
| }) | ||
45 changes: 45 additions & 0 deletions
45
packages/@tailwindcss-upgrade/src/codemods/migrate-import.ts
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,45 @@ | ||
| import fs from 'node:fs/promises' | ||
| import { dirname, resolve } from 'node:path' | ||
| import { type Plugin, type Root } from 'postcss' | ||
| import { parseImportParams } from '../../../tailwindcss/src/at-import' | ||
| import { segment } from '../../../tailwindcss/src/utils/segment' | ||
| import * as ValueParser from '../../../tailwindcss/src/value-parser' | ||
|
|
||
| export function migrateImport(): Plugin { | ||
| async function migrate(root: Root) { | ||
| let file = root.source?.input.file | ||
| if (!file) return | ||
|
|
||
| let promises: Promise<void>[] = [] | ||
| root.walkAtRules('import', (rule) => { | ||
| let [firstParam, ...rest] = segment(rule.params, ' ') | ||
|
|
||
| let params = parseImportParams(ValueParser.parse(firstParam)) | ||
|
|
||
| let isRelative = params.uri[0] === '.' | ||
| let hasCssExtension = params.uri.endsWith('.css') | ||
|
|
||
| if (isRelative && hasCssExtension) { | ||
| return | ||
| } | ||
|
|
||
| let fullPath = resolve(dirname(file), params.uri) | ||
| if (!hasCssExtension) fullPath += '.css' | ||
|
|
||
| promises.push( | ||
| fs.stat(fullPath).then(() => { | ||
| let ext = hasCssExtension ? '' : '.css' | ||
| let path = isRelative ? params.uri : `./${params.uri}` | ||
| rule.params = [`'${path}${ext}'`, ...rest].join(' ') | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| await Promise.allSettled(promises) | ||
| } | ||
|
|
||
| return { | ||
| postcssPlugin: '@tailwindcss/upgrade/migrate-import', | ||
| OnceExit: migrate, | ||
| } | ||
| } |
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
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.