Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Mar 21, 2024
1 parent cb75361 commit 50970d0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@nuxt/devtools-ui-kit": "^1.1.1",
"@nuxt/module-builder": "^0.5.5",
"@nuxt/test-utils": "3.12.0",
"acorn-loose": "^8.4.0",
"bumpp": "^9.4.0",
"eslint": "8.57.0",
"nuxt": "^3.11.1",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions src/plugins/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { SimpleCallExpression } from 'estree'
import type { ModuleOptions } from '../module'

export interface AssetBundlerTransformerOptions {
overrides: ModuleOptions['overrides']
overrides?: ModuleOptions['overrides']
resolveScript: (src: string) => string
}

Expand Down Expand Up @@ -50,8 +50,6 @@ export function NuxtScriptAssetBundlerTransformer(options: AssetBundlerTransform
&& _node.callee.type === 'Identifier'
&& _node.callee?.name === 'useScript') {
const node = _node as SimpleCallExpression
if (node.arguments[1].type !== 'ObjectExpression')
return
let scriptKey: string | undefined
let scriptNode: any | undefined
// do easy case first where first argument is a literal
Expand All @@ -73,19 +71,21 @@ export function NuxtScriptAssetBundlerTransformer(options: AssetBundlerTransform
if (scriptNode) {
const src = scriptNode.value
if (src) {
// second node needs to be an object with an property of assetStrategy and a value of 'bundle'
const assetStrategyProperty = node.arguments[1].properties.find(
(p: any) => p.key?.name === 'assetStrategy' || p.key?.value === 'assetStrategy',
)
let hasAssetStrategy = false
if (assetStrategyProperty) {
if (assetStrategyProperty?.value?.value !== 'bundle')
return
// remove the property
s.remove(assetStrategyProperty.start, assetStrategyProperty.end + 1) // need to strip the comma
hasAssetStrategy = true
if (node.arguments[1]?.type === 'ObjectExpression') {
// second node needs to be an object with an property of assetStrategy and a value of 'bundle'
const assetStrategyProperty = node.arguments[1]?.properties.find(
(p: any) => p.key?.name === 'assetStrategy' || p.key?.value === 'assetStrategy',
)
if (assetStrategyProperty) {
if (assetStrategyProperty?.value?.value !== 'bundle')
return
// remove the property
s.remove(assetStrategyProperty.start, assetStrategyProperty.end + 1) // need to strip the comma
hasAssetStrategy = true
}
}
hasAssetStrategy = options.overrides?.[scriptKey]?.assetStrategy === 'bundle'
hasAssetStrategy = hasAssetStrategy || options.overrides?.[scriptKey]?.assetStrategy === 'bundle'
if (hasAssetStrategy) {
const newSrc = options.resolveScript(src)
s.overwrite(scriptNode.start, scriptNode.end, `'${newSrc}'`)
Expand Down
22 changes: 19 additions & 3 deletions test/unit/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { AssetBundlerTransformerOptions } from '../../src/plugins/transform
import { NuxtScriptAssetBundlerTransformer } from '../../src/plugins/transform'

async function transform(code: string | string[], options: AssetBundlerTransformerOptions) {
const plugin = NuxtScriptAssetBundlerTransformer.vite(options) as any
const plugin = NuxtScriptAssetBundlerTransformer(options).vite() as any
const res = await plugin.transform.call(
{ parse: (code: string) => parse(code, { ecmaVersion: 2022, sourceType: 'module', allowImportExportEverywhere: true, allowAwaitOutsideFunction: true }) },
Array.isArray(code) ? code.join('\n') : code,
Expand All @@ -28,7 +28,7 @@ describe('nuxtScriptTransformer', () => {
)
expect(code).toMatchInlineSnapshot(`
"const instance = useScript('/_scripts/beacon.min.js', {
assetStrategy: 'bundle',
})"
`)
})
Expand All @@ -46,8 +46,24 @@ describe('nuxtScriptTransformer', () => {
)
expect(code).toMatchInlineSnapshot(`
"const instance = useScript({ defer: true, src: '/_scripts/beacon.min.js' }, {
assetStrategy: 'bundle',
})"
`)
})
it('overrides', async () => {
const code = await transform(
`const instance = useScript({ key: 'cloudflareAnalytics', src: 'https://static.cloudflareinsights.com/beacon.min.js' })`,
{
overrides: {
cloudflareAnalytics: {
assetStrategy: 'bundle',
},
},
resolveScript(src) {
return `/_scripts${parseURL(src).pathname}`
},
},
)
expect(code).toMatchInlineSnapshot(`"const instance = useScript({ key: 'cloudflareAnalytics', src: '/_scripts/beacon.min.js' })"`)
})
})

0 comments on commit 50970d0

Please sign in to comment.