Skip to content

Commit bad11e1

Browse files
committed
Do the babel import thing on all imports
1 parent 35c32e8 commit bad11e1

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/tsBuilder.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _generator from "@babel/generator"
2-
import parser from "@babel/parser"
3-
import traverse from "@babel/traverse"
4-
import t, {
2+
import _parser from "@babel/parser"
3+
import _traverse from "@babel/traverse"
4+
import _t, {
55
addComment,
66
BlockStatement,
77
Declaration,
@@ -34,6 +34,9 @@ interface NodeConfig {
3434
// Babel is a CJS package and uses `default` as named binding (`exports.default =`).
3535
// https://github.com/babel/babel/issues/15269.
3636
const generator = (_generator as any).default as typeof _generator || _generator
37+
const t = (_t as any).default as typeof _t || _t
38+
const parser = (_parser as any).default as typeof _parser || _parser
39+
const traverse = (_traverse as any).default as typeof _traverse || _traverse
3740

3841
export const builder = (priorSource: string, _opts: {}) => {
3942
const sourceFile = parser.parse(priorSource, { sourceType: "module", plugins: ["jsx", "typescript"] })
@@ -44,7 +47,7 @@ export const builder = (priorSource: string, _opts: {}) => {
4447

4548
const existing = imports.find((i) => i.source.value === source)
4649
if (!existing) {
47-
const imports = [] as (t.ImportSpecifier | t.ImportDefaultSpecifier)[]
50+
const imports = [] as (_t.ImportSpecifier | _t.ImportDefaultSpecifier)[]
4851
if (opts.mainImport) {
4952
imports.push(t.importDefaultSpecifier(t.identifier(opts.mainImport)))
5053
}
@@ -121,11 +124,11 @@ export const builder = (priorSource: string, _opts: {}) => {
121124
}
122125

123126
/** An internal API for describing a new area for inputting template info */
124-
const createScope = (scopeName: string, scopeNode: t.Node, statements: Statement[]) => {
127+
const createScope = (scopeName: string, scopeNode: _t.Node, statements: Statement[]) => {
125128
const addFunction = (name: string) => {
126129
let functionNode = statements.find(
127130
(s) => t.isVariableDeclaration(s) && t.isIdentifier(s.declarations[0].id) && s.declarations[0].id.name === name
128-
) as t.VariableDeclaration | undefined
131+
) as _t.VariableDeclaration | undefined
129132

130133
if (!functionNode) {
131134
functionNode = t.variableDeclaration("const", [
@@ -134,7 +137,7 @@ export const builder = (priorSource: string, _opts: {}) => {
134137
statements.push(functionNode)
135138
}
136139

137-
const arrowFn = functionNode.declarations[0].init as t.ArrowFunctionExpression
140+
const arrowFn = functionNode.declarations[0].init as _t.ArrowFunctionExpression
138141
if (!t.isArrowFunctionExpression(arrowFn)) throw new Error("Expected ArrowFunctionExpression")
139142

140143
return {
@@ -154,7 +157,7 @@ export const builder = (priorSource: string, _opts: {}) => {
154157
}
155158
}
156159

157-
const addVariableDeclaration = (name: string, add: (prior: t.Expression | undefined) => t.Expression) => {
160+
const addVariableDeclaration = (name: string, add: (prior: _t.Expression | undefined) => _t.Expression) => {
158161
const prior = statements.find(
159162
(b) => t.isVariableDeclaration(b) && t.isIdentifier(b.declarations[0].id) && b.declarations[0].id.name === name
160163
)
@@ -256,13 +259,13 @@ export const builder = (priorSource: string, _opts: {}) => {
256259
}
257260

258261
/** Experimental function for parsing out a graphql template tag, and ensuring certain fields have been called */
259-
const updateGraphQLTemplateTag = (expression: t.Expression, path: string, modelFields: string[]) => {
262+
const updateGraphQLTemplateTag = (expression: _t.Expression, path: string, modelFields: string[]) => {
260263
if (path !== ".") throw new Error("Only support updating the root of the graphql tag ATM")
261264
// @ts-expect-error - ts/js babel interop issue
262265
traverse(
263266
expression,
264267
{
265-
TaggedTemplateExpression(path: traverse.NodePath<t.TaggedTemplateExpression>) {
268+
TaggedTemplateExpression(path: _traverse.NodePath<_t.TaggedTemplateExpression>) {
266269
const { tag, quasi } = path.node
267270
if (t.isIdentifier(tag) && tag.name === "graphql") {
268271
// This is the graphql query

0 commit comments

Comments
 (0)