|
| 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 | +} |
0 commit comments