-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: init * remove unused * feat: use unreleased code * update package.json * relint * use node 14 for github actions * add macro-vite to build * add chokidar * relint * macro vite in exports * use aslemammad-vite-plugin-macro * lint * add readme * Update package.json Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com> * rollup * revert eslint * update tsconfig * change vite version * remove skipLibCheck * update yarn lock * add ts-ignore * add eslint comment * revert * patch * Update package.json Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com> * Update package.json Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com> * Update package.json Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com> * update lock * add postinstall * remove chokidar Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
- Loading branch information
1 parent
9643aa6
commit 0fcb5aa
Showing
9 changed files
with
638 additions
and
24 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
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
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,13 @@ | ||
diff --git a/node_modules/vite/dist/node/index.d.ts b/node_modules/vite/dist/node/index.d.ts | ||
index cb3a834..a253ff0 100644 | ||
--- a/node_modules/vite/dist/node/index.d.ts | ||
+++ b/node_modules/vite/dist/node/index.d.ts | ||
@@ -1175,7 +1175,7 @@ export declare interface PackageData { | ||
* If a plugin should be applied only for server or build, a function format | ||
* config file can be used to conditional determine the plugins to use. | ||
*/ | ||
-export declare interface Plugin extends Plugin_2 { | ||
+export declare interface Plugin { | ||
/** | ||
* Enforce plugin invocation tier similar to webpack loaders. | ||
* |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import { addNamed } from '@babel/helper-module-imports' | ||
import * as t from '@babel/types' | ||
import { | ||
createMacroPlugin, | ||
defineMacro, | ||
defineMacroProvider, | ||
} from 'aslemammad-vite-plugin-macro' | ||
import { MacroError } from 'babel-plugin-macros' | ||
|
||
export const valtioMacro = defineMacro(`useProxy`) | ||
.withSignature(`<T extends object>(proxyObject: T): void`) | ||
.withHandler((ctx) => { | ||
const { path, args } = ctx | ||
const hook = addNamed(path, 'useSnapshot', 'valtio') | ||
const proxy = args[0]?.node | ||
|
||
if (!t.isIdentifier(proxy)) throw new MacroError('no proxy object') | ||
|
||
const snap = t.identifier(`valtio_macro_snap_${proxy.name}`) | ||
path.parentPath?.replaceWith( | ||
t.variableDeclaration('const', [ | ||
t.variableDeclarator(snap, t.callExpression(hook, [proxy])), | ||
]) | ||
) | ||
|
||
let inFunction = 0 | ||
path.parentPath?.getFunctionParent()?.traverse({ | ||
Identifier(p) { | ||
if ( | ||
inFunction === 0 && // in render | ||
p.node !== proxy && | ||
p.node.name === proxy.name | ||
) { | ||
p.node.name = snap.name | ||
} | ||
}, | ||
Function: { | ||
enter() { | ||
++inFunction | ||
}, | ||
exit() { | ||
--inFunction | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
export function provideValtioMacro() { | ||
return defineMacroProvider({ | ||
id: 'valtio/macro', | ||
exports: { | ||
'valtio/macro': { | ||
macros: [valtioMacro], | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const macroPlugin = createMacroPlugin({}).use(provideValtioMacro()) | ||
|
||
export default macroPlugin |
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,34 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`basic 1`] = ` | ||
"import { useSnapshot as _useSnapshot } from \\"valtio\\"; | ||
const Component = () => {const valtio_macro_snap_state = _useSnapshot( | ||
state); | ||
return ( | ||
<div> | ||
{valtio_macro_snap_state.count} | ||
<button onClick={() => ++state.count}>inc</button> | ||
</div>); | ||
};" | ||
`; | ||
|
||
exports[`complex 1`] = ` | ||
"import { useSnapshot as _useSnapshot } from \\"valtio\\"; | ||
const Component = () => {const valtio_macro_snap_state = _useSnapshot( | ||
state); | ||
return ( | ||
<div> | ||
<button onClick={() => { | ||
;(() => ++state.count)(); | ||
++state.count; | ||
}}>inc</button> | ||
{valtio_macro_snap_state.count} | ||
</div>); | ||
};" | ||
`; |
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,64 @@ | ||
import { EnvContext } from '@typed-macro/core' | ||
import { createTransformer } from '@typed-macro/runtime' | ||
import { valtioMacro } from 'valtio/macro/vite' | ||
|
||
const env: EnvContext = { | ||
host: 'test', | ||
packageManager: 'test', | ||
projectPath: [''], | ||
dev: true, | ||
ssr: false, | ||
} | ||
|
||
it('basic', async () => { | ||
const transform = createTransformer({}) | ||
transform.appendMacros('valtio/macro', [valtioMacro]) | ||
|
||
expect( | ||
transform.transform( | ||
` | ||
import { useProxy } from 'valtio/macro' | ||
const Component = () => { | ||
useProxy(state) | ||
return ( | ||
<div> | ||
{state.count} | ||
<button onClick={() => ++state.count}>inc</button> | ||
</div> | ||
) | ||
} | ||
`, | ||
'test.ts', | ||
env | ||
) | ||
).toMatchSnapshot() | ||
}) | ||
|
||
it('complex', async () => { | ||
const transform = createTransformer({}) | ||
transform.appendMacros('valtio/macro', [valtioMacro]) | ||
|
||
expect( | ||
transform.transform( | ||
` | ||
import { useProxy } from 'valtio/macro' | ||
const Component = () => { | ||
useProxy(state) | ||
return ( | ||
<div> | ||
<button onClick={() => { | ||
;(() => ++state.count)() | ||
++state.count | ||
}}>inc</button> | ||
{state.count} | ||
</div> | ||
) | ||
} | ||
`, | ||
'test.ts', | ||
env | ||
) | ||
).toMatchSnapshot() | ||
}) |
Oops, something went wrong.
0fcb5aa
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.
Successfully deployed to the following URLs: