2020-11-06 03:33:14 +01:00
|
|
|
import { NodePath, PluginObj, types } from 'next/dist/compiled/babel/core'
|
|
|
|
import commonjsPlugin from 'next/dist/compiled/babel/plugin-transform-modules-commonjs'
|
2019-11-21 03:53:12 +01:00
|
|
|
|
2018-12-05 14:37:26 +01:00
|
|
|
// Rewrite imports using next/<something> to next-server/<something>
|
2019-05-29 13:57:26 +02:00
|
|
|
export default function NextToNextServer(...args: any): PluginObj {
|
2018-12-05 14:37:26 +01:00
|
|
|
const commonjs = commonjsPlugin(...args)
|
|
|
|
return {
|
|
|
|
visitor: {
|
|
|
|
Program: {
|
2020-04-22 07:25:43 +02:00
|
|
|
exit(path: NodePath<types.Program>, state) {
|
2018-12-05 14:37:26 +01:00
|
|
|
let foundModuleExports = false
|
|
|
|
path.traverse({
|
2020-06-01 23:00:22 +02:00
|
|
|
MemberExpression(expressionPath: any) {
|
|
|
|
if (expressionPath.node.object.name !== 'module') return
|
|
|
|
if (expressionPath.node.property.name !== 'exports') return
|
2018-12-05 14:37:26 +01:00
|
|
|
foundModuleExports = true
|
2019-05-29 13:57:26 +02:00
|
|
|
},
|
2018-12-05 14:37:26 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!foundModuleExports) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
commonjs.visitor.Program.exit.call(this, path, state)
|
2019-05-29 13:57:26 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-12-05 14:37:26 +01:00
|
|
|
}
|
|
|
|
}
|