Skip to content
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

feat(macro): macro for vite #386

Merged
merged 31 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2cd2549
feat: init
Aslemammad Mar 6, 2022
0a01569
remove unused
Aslemammad Mar 6, 2022
c80b38b
feat: use unreleased code
Aslemammad Mar 6, 2022
13189af
update package.json
Aslemammad Mar 6, 2022
b88f637
relint
Aslemammad Mar 6, 2022
70be59b
use node 14 for github actions
Aslemammad Mar 6, 2022
9853d38
add macro-vite to build
Aslemammad Mar 6, 2022
40b2533
add chokidar
Aslemammad Mar 6, 2022
f410386
relint
Aslemammad Mar 6, 2022
978ed75
macro vite in exports
Aslemammad Mar 6, 2022
7f16d74
use aslemammad-vite-plugin-macro
Aslemammad Mar 13, 2022
53638ee
lint
Aslemammad Mar 13, 2022
e46d1b1
add readme
Aslemammad Mar 13, 2022
cfde197
Update package.json
Aslemammad Mar 14, 2022
618cd6f
rollup
Aslemammad Mar 14, 2022
f2088b0
Merge branch 'feat/vite-macro' of github.com:Aslemammad/valtio into f…
Aslemammad Mar 14, 2022
d39150d
revert eslint
Aslemammad Mar 14, 2022
6cd5ad7
update tsconfig
Aslemammad Mar 14, 2022
6d0480c
change vite version
Aslemammad Mar 15, 2022
4a0ad46
remove skipLibCheck
Aslemammad Mar 15, 2022
3a8779e
update yarn lock
Aslemammad Mar 15, 2022
0bb36a2
add ts-ignore
Aslemammad Mar 15, 2022
385f581
add eslint comment
Aslemammad Mar 15, 2022
cad1453
revert
Aslemammad Mar 15, 2022
faec07f
patch
Aslemammad Mar 15, 2022
5a3f72e
Update package.json
Aslemammad Mar 15, 2022
6d3387e
Update package.json
Aslemammad Mar 15, 2022
417a13b
Update package.json
Aslemammad Mar 15, 2022
2a40e21
update lock
Aslemammad Mar 15, 2022
68220ea
add postinstall
Aslemammad Mar 15, 2022
0588310
remove chokidar
Aslemammad Mar 15, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-multiple-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '12'
node-version: '14'
cache: yarn
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- run: yarn install --frozen-lockfile --check-files
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-multiple-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '12'
node-version: '14'
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
cache: yarn
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- run: yarn install --frozen-lockfile --check-files
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '12'
node-version: '14'
cache: yarn
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- run: yarn install --frozen-lockfile --check-files
Expand Down
22 changes: 20 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
"module": "./esm/macro.js",
"import": "./esm/macro.mjs",
"default": "./macro.js"
},
"./macro/vite": {
"types": "./esm/macro/vite.d.ts",
"module": "./esm/macro/vite.js",
"import": "./esm/macro/vite.mjs",
"default": "./macro/vite.js"
}
},
"files": [
Expand All @@ -54,6 +60,7 @@
"build:vanilla": "rollup -c --config-vanilla",
"build:utils": "rollup -c --config-utils",
"build:macro": "rollup -c --config-macro",
"build:macro-vite": "rollup -c --config-macro-vite",
Aslemammad marked this conversation as resolved.
Show resolved Hide resolved
"postbuild": "yarn copy",
"eslint": "eslint --fix '*.{js,json}' '{src,tests}/**/*.{ts,tsx}'",
"eslint:ci": "eslint '*.{js,json}' '{src,tests}/**/*.{ts,tsx}'",
Expand Down Expand Up @@ -156,8 +163,10 @@
"@types/react-dom": "^17.0.11",
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"aslemammad-vite-plugin-macro": "^1.0.0-alpha.1",
"babel-plugin-macros": "^3.1.0",
"babel-plugin-tester": "^10.1.0",
"chokidar": "^3.5.3",
"concurrently": "^7.0.0",
"downlevel-dts": "^0.8.0",
"esbuild": "^0.14.23",
Expand All @@ -181,7 +190,8 @@
"rollup-plugin-terser": "^7.0.2",
"shx": "^0.3.4",
"tslib": "^2.3.1",
"typescript": "^4.5.5"
"typescript": "^4.5.5",
"vite": "^2.8.6"
},
"resolutions": {
"date-fns": "2.27.0"
Expand All @@ -190,9 +200,17 @@
"@babel/helper-module-imports": ">=7.12",
"@babel/types": ">=7.13",
"babel-plugin-macros": ">=3.0",
"react": ">=16.8"
"react": ">=16.8",
"vite": "*",
"aslemammad-vite-plugin-macro": "*"
},
"peerDependenciesMeta": {
"vite": {
"optional": true
},
"aslemammad-vite-plugin-macro": {
"optional": true
},
"@babel/helper-module-imports": {
"optional": true
},
Expand Down
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ const Component = () => {
}
```

##### vite
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
```
npm i --save-dev aslemammad-vite-plugin-macro babel-plugin-macros
```

And in your `vite.config.js`
```js
import { defineConfig } from "vite";
import macro from 'valtio/macro/vite'

export default defineConfig({
plugins: [macro],
});
```

#### `derive` util

You can subscribe to some proxies and create a derived proxy.
Expand Down
4 changes: 4 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,13 @@ export default function (args) {
let c = Object.keys(args).find((key) => key.startsWith('config-'))
if (c) {
c = c.slice('config-'.length).replace(/_/g, '/')
if (c === 'macro-vite') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't config-macro_vite work??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try that (I'm having some issues with types/exports, when importing valtio/macro/vite)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean L164 does the conversion, no?

Copy link
Member Author

@Aslemammad Aslemammad Mar 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I thought you mean the cli command? yea, it does! in the end, it's valtio/macro/vite.js

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove L165-L167,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove L171 empty line to to remove the changes in this file.

c = 'macro/vite'
}
} else {
c = 'index'
}

return [
...(c === 'index' ? [createDeclarationConfig(`src/${c}.ts`, 'dist')] : []),
createCommonJSConfig(`src/${c}.ts`, `dist/${c}`),
Expand Down
63 changes: 63 additions & 0 deletions src/macro/vite.ts
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
34 changes: 34 additions & 0 deletions tests/__snapshots__/macro-vite.test.ts.snap
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>);

};"
`;
64 changes: 64 additions & 0 deletions tests/macro-vite.test.ts
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()
})
2 changes: 0 additions & 2 deletions website/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
{
"root": true,
"extends": "next/core-web-vitals"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't remove this, right??

}
Loading