Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Nov 5, 2024
1 parent 3f10875 commit bc6cfdf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/vite/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 14 additions & 2 deletions packages/vite/src/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

Expand All @@ -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);
}
},
};
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions tests/scenarios/vite-internals-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ tsAppScenarios
module.exports = function (environment) {
const ENV = {
modulePrefix: 'app-template',
modulePrefix: 'ts-app-template',
environment,
rootURL: '/sub-dir/',
locationType: 'history',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit bc6cfdf

Please sign in to comment.