-
Notifications
You must be signed in to change notification settings - Fork 27.3k
/
commonjs.ts
32 lines (30 loc) · 952 Bytes
/
commonjs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import {PluginObj} from '@babel/core'
import {NodePath} from '@babel/traverse'
import {Program} from '@babel/types'
import commonjsPlugin from '@babel/plugin-transform-modules-commonjs'
// Rewrite imports using next/<something> to next-server/<something>
export default function NextToNextServer (...args: any): PluginObj {
const commonjs = commonjsPlugin(...args)
return {
visitor: {
Program: {
exit (path: NodePath<Program>, state) {
let foundModuleExports = false
path.traverse({
MemberExpression (
path: any
) {
if (path.node.object.name !== 'module') return
if (path.node.property.name !== 'exports') return
foundModuleExports = true
}
})
if (!foundModuleExports) {
return
}
commonjs.visitor.Program.exit.call(this, path, state)
}
}
}
}
}