Skip to content

Commit

Permalink
fix: insert script src with app.baseURL
Browse files Browse the repository at this point in the history
Fixes #230
  • Loading branch information
harlan-zw committed Sep 15, 2024
1 parent 8b46f30 commit 52e1fcb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/plugins/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { hasProtocol, parseURL, joinURL } from 'ufo'
import { hash as ohash } from 'ohash'
import { join } from 'pathe'
import { colors } from 'consola/utils'
import { useNuxt } from '@nuxt/kit'
import { tryUseNuxt, useNuxt } from '@nuxt/kit'
import { logger } from '../logger'
import { storage } from '../assets'
import { isJS, isVue } from './util'
Expand Down Expand Up @@ -41,7 +41,8 @@ function normalizeScriptData(src: string, assetsBaseURL: string = '/_scripts'):
const file = [
`${ohash(url)}.js`, // force an extension
].filter(Boolean).join('-')
return { url: joinURL(assetsBaseURL, file), filename: file }
const nuxt = tryUseNuxt()
return { url: joinURL(joinURL(nuxt?.options.app.baseURL || '', assetsBaseURL), file), filename: file }
}
return { url: src }
}
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from 'vitest'
import { createResolver } from '@nuxt/kit'
import { createPage, setup } from '@nuxt/test-utils/e2e'

const { resolve } = createResolver(import.meta.url)

await setup({
rootDir: resolve('../fixtures/basic'),
// dev: true,
browser: true,
nuxtConfig: {
app: {
baseURL: '/foo',
},
},
})

describe('base', () => {
it('bundle', async () => {
const page = await createPage('/foo/bundle-use-script')
await page.waitForTimeout(500)
// get content of #script-src
const text = await page.$eval('#script-src', el => el.textContent)
expect(text).toMatchInlineSnapshot(`"/foo/_scripts/6nd5bD9YCW.js"`)
})
})

0 comments on commit 52e1fcb

Please sign in to comment.