Skip to content

Commit 2da5f1c

Browse files
authored
Fix bug where css files in node_modules were pre-bundled (#142)
1 parent 0f91d62 commit 2da5f1c

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

.changeset/wet-snails-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pleasantest': patch
3+
---
4+
5+
Fix bug where css files in node_modules were pre-bundled

src/module-server/middleware/js.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface JSMiddlewareOpts {
1717
}
1818

1919
// TODO: make this configurable
20-
const jsExts = /\.(?:[jt]sx?|[cm]js)$/;
20+
export const jsExts = /\.(?:[jt]sx?|[cm]js)$/;
2121

2222
// Minimal version of https://github.com/preactjs/wmr/blob/main/packages/wmr/src/wmr-middleware.js
2323

src/module-server/plugins/npm-plugin.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import * as esbuild from 'esbuild';
99
import { parse } from 'cjs-module-lexer';
1010
import MagicString from 'magic-string';
1111
import { fileURLToPath } from 'url';
12+
import { jsExts } from '../middleware/js';
13+
import { changeErrorMessage } from '../../utils';
1214

1315
// This is the folder that Pleasantest is installed in (e.g. <something>/node_modules/pleasantest)
1416
const installFolder = dirname(dirname(dirname(fileURLToPath(import.meta.url))));
@@ -51,13 +53,28 @@ export const npmPlugin = ({ root }: { root: string }): Plugin => {
5153
return {
5254
name: 'npm',
5355
// Rewrite bare imports to have @npm/ prefix
54-
resolveId(id) {
55-
if (isBareImport(id)) return prefix + id;
56+
async resolveId(id, importer) {
57+
if (!isBareImport(id)) return;
58+
const resolved = await nodeResolve(id, root).catch((error) => {
59+
throw importer
60+
? changeErrorMessage(
61+
error,
62+
(msg) => `${msg} (imported by ${importer})`,
63+
)
64+
: error;
65+
});
66+
if (!jsExts.test(resolved.path))
67+
// Don't pre-bundle, use the full path to the file in node_modules
68+
// (ex: CSS files in node_modules)
69+
return resolved.path;
70+
71+
return prefix + id;
5672
},
5773
async load(id) {
5874
if (!id.startsWith(prefix)) return null;
5975
id = id.slice(prefix.length);
6076
const resolved = await nodeResolve(id, root);
77+
if (!jsExts.test(resolved.path)) return null; // Don't pre-bundle
6178
const cachePath = join(cacheDir, '@npm', `${resolved.idWithVersion}.js`);
6279
const cached = await getFromCache(cachePath);
6380
if (cached) return cached;

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ export const assertElementHandle: (
5757
}
5858
};
5959

60+
export const changeErrorMessage = (
61+
error: Error,
62+
changeMessage: (originalMessage: string) => string,
63+
) => {
64+
const newMessage = changeMessage(error.message);
65+
if (error.stack) error.stack = error.stack.replace(error.message, newMessage);
66+
error.message = newMessage;
67+
return error;
68+
};
69+
6070
/**
6171
* Manipulate the stack trace and remove fn from it
6272
* That way jest will show a code frame from the user's code, not ours

0 commit comments

Comments
 (0)