Skip to content

Commit a66db32

Browse files
authored
fix(tpc): use mlly to resolve third-party-capital (#138)
1 parent 437b3bd commit a66db32

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/tpc/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ExternalScript, Output, Script } from 'third-party-capital'
22
import { genImport, genTypeImport } from 'knitwork'
33
import { useNuxt } from '@nuxt/kit'
44
import type { HeadEntryOptions } from '@unhead/vue'
5+
import { resolvePath } from 'mlly'
56

67
export interface ScriptContentOpts {
78
data: Output
@@ -21,7 +22,7 @@ export interface ScriptContentOpts {
2122
const HEAD_VAR = '__head'
2223
const INJECTHEAD_CODE = `const ${HEAD_VAR} = injectHead()`
2324

24-
export function getTpcScriptContent(input: ScriptContentOpts) {
25+
export async function getTpcScriptContent(input: ScriptContentOpts) {
2526
const nuxt = useNuxt()
2627
if (!input.data.scripts)
2728
throw new Error('input.data has no scripts !')
@@ -47,7 +48,9 @@ export function getTpcScriptContent(input: ScriptContentOpts) {
4748

4849
if (input.tpcTypeImport) {
4950
// TPC type import
50-
imports.add(genTypeImport('third-party-capital', [input.tpcTypeImport]))
51+
imports.add(genTypeImport(await resolvePath('third-party-capital', {
52+
url: import.meta.url,
53+
}), [input.tpcTypeImport]))
5154
}
5255

5356
if (hasParams) {

test/unit/tpc.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe.each([
3838
scriptFunctionName: 'useScriptGoogleAnalytics',
3939
use: () => { },
4040
stub: () => { },
41-
})).toThrowError('no main script found for google-analytics in third-party-capital')
41+
})).rejects.toThrow('no main script found for google-analytics in third-party-capital')
4242
})
4343

4444
describe('script content generation', () => {
@@ -68,8 +68,8 @@ describe.each([
6868
},
6969
}
7070

71-
it(`expect to${isDev ? '' : ' not'} add the schema to the script options`, () => {
72-
const result = getTpcScriptContent(input)
71+
it(`expect to${isDev ? '' : ' not'} add the schema to the script options`, async () => {
72+
const result = await getTpcScriptContent(input)
7373
const returnStatement = getTpcScriptReturnStatement(result, 'useScriptGoogleAnalytics')
7474
if (!returnStatement || returnStatement.argument?.type !== TSESTree.AST_NODE_TYPES.CallExpression || (returnStatement.argument?.callee as TSESTree.Identifier).name !== 'useRegistryScript') {
7575
throw new Error('TPC Scripts must return a call expression of useRegistryScript')
@@ -92,8 +92,8 @@ describe.each([
9292
}
9393
})
9494

95-
it('expect to stringify the use and stub functions', () => {
96-
const result = getTpcScriptContent(input)
95+
it('expect to stringify the use and stub functions', async () => {
96+
const result = await getTpcScriptContent(input)
9797
const returnStatement = getTpcScriptReturnStatement(result, 'useScriptGoogleAnalytics')
9898
if (!returnStatement || returnStatement.argument?.type !== TSESTree.AST_NODE_TYPES.CallExpression || (returnStatement.argument?.callee as TSESTree.Identifier).name !== 'useRegistryScript') {
9999
throw new Error('TPC Scripts must return a call expression of useRegistryScript')
@@ -111,8 +111,8 @@ describe.each([
111111
expect(getCodeFromAst(result, stubFn)).toContain('return []')
112112
})
113113

114-
it('expect to augment window types', () => {
115-
const result = getTpcScriptContent(input)
114+
it('expect to augment window types', async () => {
115+
const result = await getTpcScriptContent(input)
116116
const ast = parse(result, { loc: true, range: true })
117117
const augmentWindowTypes = ast.body.find((node): node is TSESTree.TSModuleDeclaration => node.type === TSESTree.AST_NODE_TYPES.TSModuleDeclaration)
118118
expect(augmentWindowTypes).toBeTruthy()
@@ -145,12 +145,12 @@ describe('script content generation with head positioning', () => {
145145
}
146146

147147
describe('main script', () => {
148-
it('main script post body position', () => {
149-
const scriptOptsAst = getTpcScriptOptsASt(getTpcScriptContent(inputBase), 'useScriptGoogleAnalytics')
150-
expect(getCodeFromAst(getTpcScriptContent(inputBase), scriptOptsAst)).toContain('"tagPosition":"bodyClose"')
148+
it('main script post body position', async () => {
149+
const scriptOptsAst = getTpcScriptOptsASt(await getTpcScriptContent(inputBase), 'useScriptGoogleAnalytics')
150+
expect(getCodeFromAst(await getTpcScriptContent(inputBase), scriptOptsAst)).toContain('"tagPosition":"bodyClose"')
151151
})
152-
it('main script pre body position', () => {
153-
const scriptOptsAst = getTpcScriptOptsASt(getTpcScriptContent({
152+
it('main script pre body position', async () => {
153+
const scriptOptsAst = getTpcScriptOptsASt(await getTpcScriptContent({
154154
...inputBase,
155155
data: {
156156
...inputBase.data,
@@ -160,7 +160,7 @@ describe('script content generation with head positioning', () => {
160160
}],
161161
},
162162
}), 'useScriptGoogleAnalytics')
163-
expect(getCodeFromAst(getTpcScriptContent(inputBase), scriptOptsAst)).toContain('"tagPosition":"bodyClose"')
163+
expect(getCodeFromAst(await getTpcScriptContent(inputBase), scriptOptsAst)).toContain('"tagPosition":"bodyClose"')
164164
})
165165
})
166166
})

0 commit comments

Comments
 (0)