@@ -6,6 +6,7 @@ import { hash } from 'ohash'
66import { hasProtocol , joinURL , withBase } from 'ufo'
77import { beforeEach , describe , expect , it , vi } from 'vitest'
88import { NuxtScriptBundleTransformer } from '../../packages/script/src/plugins/transform'
9+ import { runTransform } from '../utils/unplugin'
910
1011const ohash = ( await vi . importActual < typeof import ( 'ohash' ) > ( 'ohash' ) ) . hash
1112vi . mock ( 'ohash' , async ( og ) => {
@@ -61,16 +62,18 @@ vi.mocked(hasProtocol).mockImplementation(() => true)
6162// hash receive a URL object, we want to mock it to return the pathname by default
6263vi . mocked ( hash ) . mockImplementation ( src => src . pathname )
6364
64- async function transform ( code : string | string [ ] , options ?: AssetBundlerTransformerOptions ) {
65+ async function transformId ( id : string , code : string , options ?: AssetBundlerTransformerOptions ) {
6566const plugin = NuxtScriptBundleTransformer ( { ...options , nuxt : mockNuxt } ) . vite ( ) as any
66- const res = await plugin . transform . handler . call (
67- { } ,
68- Array . isArray ( code ) ? code . join ( '\n' ) : code ,
69- 'file.js' ,
70- )
67+ // Goes through the declared filter, so a filter regression fails here rather than silently
68+ // shipping a plugin that never sees the files it should.
69+ const res = await runTransform ( plugin , { id, code } )
7170return res ?. code
7271}
7372
73+ async function transform ( code : string | string [ ] , options ?: AssetBundlerTransformerOptions ) {
74+ return transformId ( 'file.js' , Array . isArray ( code ) ? code . join ( '\n' ) : code , options )
75+ }
76+
7477describe ( 'nuxtScriptTransformer' , ( ) => {
7578it ( 'string arg' , async ( ) => {
7679vi . mocked ( hash ) . mockImplementationOnce ( ( ) => 'beacon.min' )
@@ -1314,4 +1317,29 @@ const _sfc_main = /* @__PURE__ */ _defineComponent({
13141317expect ( code ) . toContain ( 'bundle.js' )
13151318} )
13161319} )
1320+
1321+ describe ( 'module scope' , ( ) => {
1322+ const bundled = `const instance = useScript('https://example.com/s.js', { bundle: true })`
1323+
1324+ it . each ( [
1325+ 'file.ts' ,
1326+ 'file.mts' ,
1327+ 'file.cts' ,
1328+ 'file.mjs' ,
1329+ 'file.jsx' ,
1330+ 'file.vue' ,
1331+ 'file.vue?vue&type=script&setup=true&lang.ts' ,
1332+ 'file.ts?t=1699999999999' ,
1333+ ] ) ( 'transforms %s' , async ( id ) => {
1334+ vi . mocked ( hash ) . mockImplementationOnce ( ( ) => 's' )
1335+ expect ( await transformId ( id , bundled ) ) . toContain ( '/_scripts/' )
1336+ } )
1337+
1338+ it . each ( [
1339+ 'file.vue?vue&type=style&index=0&lang.css' ,
1340+ 'file.vue?nuxt_component=async' ,
1341+ ] ) ( 'leaves %s alone' , async ( id ) => {
1342+ expect ( await transformId ( id , bundled ) ) . toBeUndefined ( )
1343+ } )
1344+ } )
13171345} )
0 commit comments