diff --git a/packages/vite/src/assets.ts b/packages/vite/src/assets.ts index 4f1b95b5b2..18055aec06 100644 --- a/packages/vite/src/assets.ts +++ b/packages/vite/src/assets.ts @@ -44,8 +44,9 @@ export function assets(): Plugin { configureServer(server) { return () => { server.middlewares.use((req, res, next) => { - if (req.originalUrl && req.originalUrl.length > 1) { - const assetUrl = findPublicAsset(req.originalUrl.split('?')[0], resolverLoader.resolver); + const originalUrl = req.originalUrl.slice(server.config.base.length - 1); + if (originalUrl && originalUrl.length > 1) { + const assetUrl = findPublicAsset(originalUrl.split('?')[0], resolverLoader.resolver); if (assetUrl) { return send(req as Readable, assetUrl).pipe(res); } diff --git a/packages/vite/src/scripts.ts b/packages/vite/src/scripts.ts index de6e991b6d..f6612edc2a 100644 --- a/packages/vite/src/scripts.ts +++ b/packages/vite/src/scripts.ts @@ -26,11 +26,14 @@ export function scripts(params?: { include?: string[]; exclude?: string[] }): Pl } }); + let config: any = null; + return { name: 'embroider-scripts', enforce: 'pre', configResolved(resolvedConfig) { + config = resolvedConfig; optimizer = new ScriptOptimizer(resolvedConfig.root); }, @@ -47,7 +50,7 @@ export function scripts(params?: { include?: string[]; exclude?: string[] }): Pl // we don't do anything in `vite dev`, we only need to work in `vite // build` if (!context.server) { - return optimizer.transformHTML(htmlIn); + return optimizer.transformHTML(htmlIn, config.base); } }, }; @@ -123,16 +126,25 @@ class ScriptOptimizer { return fileParts.join('.'); } - transformHTML(htmlIn: string) { + transformHTML(htmlIn: string, baseUrl: string) { if (this.transformState?.htmlIn !== htmlIn) { let parsed = new JSDOM(htmlIn); let scriptTags = [...parsed.window.document.querySelectorAll('script')] as HTMLScriptElement[]; + let linkTags = [...parsed.window.document.querySelectorAll('link')] as HTMLLinkElement[]; + for (const linkTag of linkTags) { + if (linkTag.href.startsWith('/') && !linkTag.href.startsWith(baseUrl)) { + linkTag.href = baseUrl + linkTag.href.slice(1); + } + } for (let scriptTag of scriptTags) { if (scriptTag.type !== 'module') { let fingerprinted = this.emitted.get(scriptTag.src); if (fingerprinted) { scriptTag.src = fingerprinted; } + if (scriptTag.src.startsWith('/') && !scriptTag.src.startsWith(baseUrl)) { + scriptTag.src = baseUrl + scriptTag.src.slice(1); + } } } let htmlOut = parsed.serialize(); diff --git a/tests/scenarios/vite-internals-test.ts b/tests/scenarios/vite-internals-test.ts index f1db3bbf01..4afb6b9cf8 100644 --- a/tests/scenarios/vite-internals-test.ts +++ b/tests/scenarios/vite-internals-test.ts @@ -373,7 +373,7 @@ tsAppScenarios module.exports = function (environment) { const ENV = { - modulePrefix: 'app-template', + modulePrefix: 'ts-app-template', environment, rootURL: '/sub-dir/', locationType: 'history', @@ -477,12 +477,12 @@ tsAppScenarios components: { 'example-test.js': ` import { module, test } from 'qunit'; - import { setupRenderingTest } from 'app-template/tests/helpers'; + import { setupRenderingTest } from 'ts-app-template/tests/helpers'; import { render } from '@ember/test-helpers'; import { hbs } from 'ember-cli-htmlbars'; - import { appLibOne as libOneViaAddon, appLibTwo as libTwoViaAddon } from 'app-template/v1-example-addon'; - import appLibOne from 'app-template/lib/app-lib-one'; - import appLibTwo from 'app-template/lib/app-lib-two'; + import { appLibOne as libOneViaAddon, appLibTwo as libTwoViaAddon } from 'ts-app-template/v1-example-addon'; + import appLibOne from 'ts-app-template/lib/app-lib-one'; + import appLibTwo from 'ts-app-template/lib/app-lib-two'; module('Integration | Component | example', function (hooks) { setupRenderingTest(hooks); @@ -555,7 +555,7 @@ tsAppScenarios app: { 'v1-example-addon.js': ` import appLibOne from './lib/app-lib-one'; - import appLibTwo from 'app-template/lib/app-lib-two'; + import appLibTwo from 'ts-app-template/lib/app-lib-two'; export { appLibOne, appLibTwo }; `, templates: {