From 50970d051dd35fe902c96dd2c5c379937cd75bc2 Mon Sep 17 00:00:00 2001 From: harlan Date: Thu, 21 Mar 2024 14:33:00 +1100 Subject: [PATCH] chore: fix tests --- package.json | 1 + pnpm-lock.yaml | 10 ++++++++++ src/plugins/transform.ts | 28 ++++++++++++++-------------- test/unit/transform.test.ts | 22 +++++++++++++++++++--- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 3a28fdd4..8664f283 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d1b49809..5d808efa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -72,6 +72,9 @@ importers: '@nuxt/test-utils': specifier: 3.12.0 version: 3.12.0(h3@1.11.1)(playwright-core@1.42.1)(rollup@3.29.4)(vite@5.1.6)(vitest@1.4.0)(vue-router@4.3.0)(vue@3.4.21) + acorn-loose: + specifier: ^8.4.0 + version: 8.4.0 bumpp: specifier: ^9.4.0 version: 9.4.0 @@ -3780,6 +3783,13 @@ packages: dependencies: acorn: 8.11.3 + /acorn-loose@8.4.0: + resolution: {integrity: sha512-M0EUka6rb+QC4l9Z3T0nJEzNOO7JcoJlYMrBlyBCiFSXRyxjLKayd4TbQs2FDRWQU1h9FR7QVNHt+PEaoNL5rQ==} + engines: {node: '>=0.4.0'} + dependencies: + acorn: 8.11.3 + dev: true + /acorn-walk@8.3.2: resolution: {integrity: sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A==} engines: {node: '>=0.4.0'} diff --git a/src/plugins/transform.ts b/src/plugins/transform.ts index 808b64ba..bdf7b164 100644 --- a/src/plugins/transform.ts +++ b/src/plugins/transform.ts @@ -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 } @@ -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 @@ -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}'`) diff --git a/test/unit/transform.test.ts b/test/unit/transform.test.ts index 7fedbe5d..30193638 100644 --- a/test/unit/transform.test.ts +++ b/test/unit/transform.test.ts @@ -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, @@ -28,7 +28,7 @@ describe('nuxtScriptTransformer', () => { ) expect(code).toMatchInlineSnapshot(` "const instance = useScript('/_scripts/beacon.min.js', { - assetStrategy: 'bundle', + })" `) }) @@ -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' })"`) + }) })