-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeScript plugin to add macro import attribute to auto imports (#7141)
* TypeScript plugin to add macro import attribute to auto imports * fix lockfile
- Loading branch information
1 parent
454da05
commit 8a7cb15
Showing
4 changed files
with
74 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "@react-spectrum/ts-plugin", | ||
"version": "0.1.0", | ||
"main": "./src/index.js", | ||
"license": "Apache-2.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/adobe/react-spectrum" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"rsp": { | ||
"type": "cli" | ||
} | ||
} |
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,49 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
module.exports = function () { | ||
/** @param info {import('typescript').server.PluginCreateInfo} */ | ||
function create(info) { | ||
const proxy = Object.create(null); | ||
for (let k of Object.keys(info.languageService)) { | ||
const x = info.languageService[k]; | ||
proxy[k] = (...args) => x.apply(info.languageService, args); | ||
} | ||
|
||
proxy.getCompletionEntryDetails = (...args) => { | ||
let result = info.languageService.getCompletionEntryDetails(...args); | ||
if (!result.codeActions) { | ||
return result; | ||
} | ||
|
||
// Override auto import of style macro to add `with {type: 'macro'}` automatically. | ||
for (let action of result.codeActions) { | ||
for (let change of action.changes) { | ||
for (let textChange of change.textChanges) { | ||
if (change.fileName.includes('@react-spectrum/s2')) { | ||
// For files inside S2 itself, import specifier will be '../style', not '@react-spectrum/s2/style'. | ||
textChange.newText = textChange.newText.replace(/(import\s*\{.*?\}\s*from\s*['"]\.\.\/style['"]);/g, '$1 with {type: \'macro\'};'); | ||
} else { | ||
textChange.newText = textChange.newText.replace(/(import\s*\{.*?\}\s*from\s*['"]@react-spectrum\/s2\/style['"]);/g, '$1 with {type: \'macro\'};'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
return proxy; | ||
} | ||
|
||
return {create}; | ||
}; |
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
8a7cb15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verdaccio builds:
CRA Test App
NextJS Test App
RAC Tailwind Example
RAC Spectrum + Tailwind Example
S2 Parcel Example
S2 Webpack Example
CRA Test App Size
NextJS App Size
Publish stats
Size diff since last release
Docs
Storybook
S2 Storybook