Skip to content

Commit 9953532

Browse files
lpsingerpcattoriMichaelDeBoey
authored
fix(dev): treat imported assets from node_modules as external (#6813)
Co-authored-by: Pedro Cattori <pcattori@gmail.com> Co-authored-by: Michaël De Boey <info@michaeldeboey.be>
1 parent 23f27ef commit 9953532

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { test, expect } from "@playwright/test";
2+
3+
import { PlaywrightFixture } from "./helpers/playwright-fixture";
4+
import type { Fixture, AppFixture } from "./helpers/create-fixture";
5+
import { createAppFixture, createFixture, js } from "./helpers/create-fixture";
6+
7+
let fixture: Fixture;
8+
let appFixture: AppFixture;
9+
10+
test.beforeEach(async ({ context }) => {
11+
await context.route(/_data/, async (route) => {
12+
await new Promise((resolve) => setTimeout(resolve, 50));
13+
route.continue();
14+
});
15+
});
16+
17+
test.beforeAll(async () => {
18+
fixture = await createFixture({
19+
files: {
20+
"app/routes/_index.tsx": js`
21+
import imgSrc from "getos/imgs/logo.svg";
22+
23+
export default function () {
24+
return (
25+
<div>
26+
<img src={imgSrc} data-testid="example-svg" alt="example img"/>
27+
</div>
28+
)
29+
}
30+
`,
31+
},
32+
});
33+
34+
appFixture = await createAppFixture(fixture);
35+
});
36+
37+
test.afterAll(() => {
38+
appFixture.close();
39+
});
40+
41+
test("renders SVG images imported from node_modules", async ({ page }) => {
42+
let app = new PlaywrightFixture(appFixture, page);
43+
// You can test any request your app might get using `fixture`.
44+
await app.goto("/");
45+
expect(await page.getByTestId("example-svg").getAttribute("src")).toMatch(
46+
/\/build\/_assets\/logo-.*\.svg/
47+
);
48+
});

‎packages/remix-dev/compiler/server/plugins/bareImports.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { isCssSideEffectImportPath } from "../../plugins/cssSideEffectImports";
1010
import { createMatchPath } from "../../utils/tsconfig";
1111
import { detectPackageManager } from "../../../cli/detectPackageManager";
1212
import type { Context } from "../../context";
13+
import { getLoaderForFile } from "../../utils/loaders";
1314

1415
/**
1516
* A plugin responsible for resolving bare module ids based on server target.
@@ -59,8 +60,23 @@ export function serverBareModulesPlugin(ctx: Context): Plugin {
5960
return undefined;
6061
}
6162

62-
// Always bundle CSS files so we get immutable fingerprinted asset URLs.
63-
if (path.endsWith(".css")) {
63+
// Skip assets that are treated as files (.css, .svg, .png, etc.).
64+
// Otherwise, esbuild would emit code that would attempt to require()
65+
// or import these files --- which aren't JavaScript!
66+
let loader;
67+
try {
68+
loader = getLoaderForFile(path);
69+
} catch (e) {
70+
if (
71+
!(
72+
e instanceof Error &&
73+
e.message.startsWith("Cannot get loader for file")
74+
)
75+
) {
76+
throw e;
77+
}
78+
}
79+
if (loader === "file") {
6480
return undefined;
6581
}
6682

0 commit comments

Comments
 (0)