Skip to content

Commit 61b3fd9

Browse files
committed
init commit
0 parents  commit 61b3fd9

18 files changed

+421
-0
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.yml]
11+
indent_style = space
12+
indent_size = 2

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
yarn.lock
3+
4+
dist

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- '12'
4+
- '10'
5+
- '8'

babel.config.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"node": "8"
8+
},
9+
"useBuiltIns": "usage",
10+
"corejs": 3
11+
}
12+
],
13+
"@babel/preset-typescript"
14+
]
15+
}

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/plugin.js');

jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
testMatch: ['**/test/*.js'],
3+
preset: 'ts-jest',
4+
testEnvironment: 'node'
5+
};

license

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) Thomas Sileghem <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "babel-plugin-portal",
3+
"version": "0.1.0",
4+
"description": "",
5+
"license": "MIT",
6+
"repository": "twotwentythree/babel-plugin-portal",
7+
"author": {
8+
"name": "Thomas Sileghem",
9+
"email": "[email protected]"
10+
},
11+
"engines": {
12+
"node": ">=8"
13+
},
14+
"scripts": {
15+
"build": "babel --out-dir dist -x .ts src",
16+
"link": "npm link && npm link babel-plugin-portal",
17+
"test": "xo && jest test/index.js"
18+
},
19+
"files": [
20+
"index.js",
21+
"dist"
22+
],
23+
"keywords": [
24+
""
25+
],
26+
"dependencies": {
27+
"core-js": "^3.6.5"
28+
},
29+
"devDependencies": {
30+
"@babel/cli": "^7.8.4",
31+
"@babel/core": "^7.9.0",
32+
"@babel/helper-plugin-test-runner": "^7.8.3",
33+
"@babel/preset-env": "^7.9.5",
34+
"@babel/preset-typescript": "^7.9.0",
35+
"@types/jest": "^25.2.1",
36+
"jest": "^25.3.0",
37+
"ts-jest": "^25.4.0",
38+
"xo": "^0.24.0"
39+
}
40+
}

readme.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# babel-plugin-portal [![Build Status](https://travis-ci.com/twotwentythree/babel-plugin-portal.svg?branch=master)](https://travis-ci.com/twotwentythree/babel-plugin-portal)
2+
3+
4+
## Install
5+
6+
```
7+
$ npm install babel-plugin-portal
8+
```
9+
10+
11+
## Usage
12+
13+
```json
14+
{
15+
"plugins": [
16+
[
17+
"portal",
18+
{
19+
"portals": [
20+
"create-fetcher.js"
21+
]
22+
}
23+
]
24+
]
25+
}
26+
27+
```

src/plugin.ts

+255
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
// Based on https://github.com/zeit/next.js/blob/canary/packages/next/build/babel/plugins/next-ssg-transform.ts
2+
3+
import pathLib from 'path'
4+
5+
import { NodePath, PluginObj, parse } from '@babel/core'
6+
import * as BabelTypes from '@babel/types'
7+
8+
export const EXPORT_NAME_GET_STATIC_PROPS = 'getStaticProps'
9+
export const EXPORT_NAME_GET_STATIC_PATHS = 'getStaticPaths'
10+
export const EXPORT_NAME_GET_SERVER_PROPS = 'getServerSideProps'
11+
12+
const ssgExports = new Set([
13+
EXPORT_NAME_GET_STATIC_PROPS,
14+
EXPORT_NAME_GET_STATIC_PATHS,
15+
EXPORT_NAME_GET_SERVER_PROPS,
16+
17+
// legacy methods added so build doesn't fail from importing
18+
// server-side only methods
19+
`unstable_getStaticProps`,
20+
`unstable_getStaticPaths`,
21+
`unstable_getServerProps`,
22+
`unstable_getServerSideProps`,
23+
])
24+
25+
type PluginState = {
26+
refs: Set<NodePath<BabelTypes.Identifier>>
27+
portalIdentifierName: Set<string>
28+
cwd: string
29+
filename: string,
30+
opts?: {
31+
portals?: string[]
32+
}
33+
}
34+
35+
export default function nextTransformSsg({
36+
types: t,
37+
}: {
38+
types: typeof BabelTypes
39+
}): PluginObj<PluginState> {
40+
function getIdentifier(
41+
path: NodePath<
42+
| BabelTypes.FunctionDeclaration
43+
| BabelTypes.FunctionExpression
44+
| BabelTypes.ArrowFunctionExpression
45+
>
46+
): NodePath<BabelTypes.Identifier> | null {
47+
const parentPath = path.parentPath
48+
if (parentPath.type === 'VariableDeclarator') {
49+
const pp = parentPath as NodePath<BabelTypes.VariableDeclarator>
50+
const name = pp.get('id')
51+
return name.node.type === 'Identifier'
52+
? (name as NodePath<BabelTypes.Identifier>)
53+
: null
54+
}
55+
56+
if (parentPath.type === 'AssignmentExpression') {
57+
const pp = parentPath as NodePath<BabelTypes.AssignmentExpression>
58+
const name = pp.get('left')
59+
return name.node.type === 'Identifier'
60+
? (name as NodePath<BabelTypes.Identifier>)
61+
: null
62+
}
63+
64+
if (path.node.type === 'ArrowFunctionExpression') {
65+
return null
66+
}
67+
68+
return path.node.id && path.node.id.type === 'Identifier'
69+
? (path.get('id') as NodePath<BabelTypes.Identifier>)
70+
: null
71+
}
72+
73+
function isIdentifierReferenced(
74+
ident: NodePath<BabelTypes.Identifier>
75+
): boolean {
76+
const b = ident.scope.getBinding(ident.node.name)
77+
return b != null && b.referenced
78+
}
79+
80+
function markFunction(
81+
path: NodePath<
82+
| BabelTypes.FunctionDeclaration
83+
| BabelTypes.FunctionExpression
84+
| BabelTypes.ArrowFunctionExpression
85+
>,
86+
state: PluginState
87+
) {
88+
const ident = getIdentifier(path)
89+
if (ident?.node && isIdentifierReferenced(ident)) {
90+
state.refs.add(ident)
91+
}
92+
}
93+
94+
function markImport(
95+
path: NodePath<
96+
| BabelTypes.ImportSpecifier
97+
| BabelTypes.ImportDefaultSpecifier
98+
| BabelTypes.ImportNamespaceSpecifier
99+
>,
100+
state: PluginState
101+
) {
102+
const local = path.get('local')
103+
104+
if (path.node.type === 'ImportDefaultSpecifier' && path.parent.type === 'ImportDeclaration') {
105+
const sourceValue = path.parent.source.value
106+
if (['/', '../', './'].some(x => sourceValue.startsWith(x))) {
107+
const importFullPath = pathLib.resolve(pathLib.dirname(state.filename), removeFileExt(sourceValue))
108+
109+
const isPortal = state.opts?.portals?.some(x => pathLib.join(state.cwd, removeFileExt(x)) === importFullPath)
110+
111+
if (local.node.name === 'useQueryFactory') {
112+
console.log(importFullPath, state.cwd, state.opts?.portals)
113+
}
114+
115+
if (isPortal) {
116+
state.portalIdentifierName.add(local.node.name)
117+
console.log(local.node.name)
118+
}
119+
}
120+
}
121+
122+
if (isIdentifierReferenced(local)) {
123+
state.refs.add(local)
124+
}
125+
}
126+
127+
return {
128+
visitor: {
129+
Program: {
130+
enter(_path, state) {
131+
state.refs = new Set<NodePath<BabelTypes.Identifier>>()
132+
state.portalIdentifierName = new Set<string>()
133+
},
134+
exit(path, state) {
135+
const refs = state.refs
136+
let count: number
137+
138+
function sweepFunction(
139+
path: NodePath<
140+
| BabelTypes.FunctionDeclaration
141+
| BabelTypes.FunctionExpression
142+
| BabelTypes.ArrowFunctionExpression
143+
>
144+
) {
145+
const ident = getIdentifier(path)
146+
if (
147+
ident?.node &&
148+
refs.has(ident) &&
149+
!isIdentifierReferenced(ident)
150+
) {
151+
++count
152+
153+
if (
154+
t.isAssignmentExpression(path.parentPath) ||
155+
t.isVariableDeclarator(path.parentPath)
156+
) {
157+
path.parentPath.remove()
158+
} else {
159+
path.remove()
160+
}
161+
}
162+
}
163+
164+
function sweepImport(
165+
path: NodePath<
166+
| BabelTypes.ImportSpecifier
167+
| BabelTypes.ImportDefaultSpecifier
168+
| BabelTypes.ImportNamespaceSpecifier
169+
>
170+
) {
171+
const local = path.get('local')
172+
if (refs.has(local) && !isIdentifierReferenced(local)) {
173+
++count
174+
path.remove()
175+
if (
176+
(path.parent as BabelTypes.ImportDeclaration).specifiers
177+
.length === 0
178+
) {
179+
path.parentPath.remove()
180+
}
181+
}
182+
}
183+
184+
path.traverse<PluginState>({
185+
CallExpression(path, state) {
186+
const ident = getIdentifier(path);
187+
188+
const calleeIdent = path.get('callee')
189+
190+
191+
192+
if (state.portalIdentifierName.has(calleeIdent.node.name)) {
193+
path.replaceWith(t.callExpression(
194+
t.memberExpression(t.identifier(calleeIdent.node.name), t.identifier('register')),
195+
[
196+
t.stringLiteral(pathLib.relative(state.cwd, removeFileExt(state.filename))),
197+
t.stringLiteral(ident.get('name').node)
198+
]
199+
))
200+
}
201+
},
202+
}, state)
203+
204+
do {
205+
;(path.scope as any).crawl()
206+
count = 0
207+
208+
path.traverse({
209+
// eslint-disable-next-line no-loop-func
210+
VariableDeclarator(path) {
211+
if (path.node.id.type !== 'Identifier') {
212+
return
213+
}
214+
215+
const local = path.get('id') as NodePath<BabelTypes.Identifier>
216+
if (refs.has(local) && !isIdentifierReferenced(local)) {
217+
++count
218+
path.remove()
219+
}
220+
},
221+
FunctionDeclaration: sweepFunction,
222+
FunctionExpression: sweepFunction,
223+
ArrowFunctionExpression: sweepFunction,
224+
ImportSpecifier: sweepImport,
225+
ImportDefaultSpecifier: sweepImport,
226+
ImportNamespaceSpecifier: sweepImport,
227+
})
228+
} while (count)
229+
},
230+
},
231+
VariableDeclarator(path, state) {
232+
if (path.node.id.type !== 'Identifier') {
233+
return
234+
}
235+
236+
const local = path.get('id') as NodePath<BabelTypes.Identifier>
237+
if (isIdentifierReferenced(local)) {
238+
state.refs.add(local)
239+
}
240+
},
241+
FunctionDeclaration: markFunction,
242+
FunctionExpression: markFunction,
243+
ArrowFunctionExpression: markFunction,
244+
ImportSpecifier: markImport,
245+
ImportDefaultSpecifier: markImport,
246+
ImportNamespaceSpecifier: markImport,
247+
},
248+
}
249+
}
250+
251+
function removeFileExt(fileName: string): string {
252+
const parsed = pathLib.parse(fileName)
253+
254+
return pathLib.join(parsed.dir, parsed.name)
255+
}

test/fixtures/portal/basic/create-fetcher.js.

Whitespace-only changes.

0 commit comments

Comments
 (0)