Skip to content

Commit 52e1fcb

Browse files
committed
fix: insert script src with app.baseURL
Fixes #230
1 parent 8b46f30 commit 52e1fcb

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/plugins/transform.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { hasProtocol, parseURL, joinURL } from 'ufo'
1010
import { hash as ohash } from 'ohash'
1111
import { join } from 'pathe'
1212
import { colors } from 'consola/utils'
13-
import { useNuxt } from '@nuxt/kit'
13+
import { tryUseNuxt, useNuxt } from '@nuxt/kit'
1414
import { logger } from '../logger'
1515
import { storage } from '../assets'
1616
import { isJS, isVue } from './util'
@@ -41,7 +41,8 @@ function normalizeScriptData(src: string, assetsBaseURL: string = '/_scripts'):
4141
const file = [
4242
`${ohash(url)}.js`, // force an extension
4343
].filter(Boolean).join('-')
44-
return { url: joinURL(assetsBaseURL, file), filename: file }
44+
const nuxt = tryUseNuxt()
45+
return { url: joinURL(joinURL(nuxt?.options.app.baseURL || '', assetsBaseURL), file), filename: file }
4546
}
4647
return { url: src }
4748
}

test/e2e/base.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { createResolver } from '@nuxt/kit'
3+
import { createPage, setup } from '@nuxt/test-utils/e2e'
4+
5+
const { resolve } = createResolver(import.meta.url)
6+
7+
await setup({
8+
rootDir: resolve('../fixtures/basic'),
9+
// dev: true,
10+
browser: true,
11+
nuxtConfig: {
12+
app: {
13+
baseURL: '/foo',
14+
},
15+
},
16+
})
17+
18+
describe('base', () => {
19+
it('bundle', async () => {
20+
const page = await createPage('/foo/bundle-use-script')
21+
await page.waitForTimeout(500)
22+
// get content of #script-src
23+
const text = await page.$eval('#script-src', el => el.textContent)
24+
expect(text).toMatchInlineSnapshot(`"/foo/_scripts/6nd5bD9YCW.js"`)
25+
})
26+
})

0 commit comments

Comments
 (0)