@@ -9,6 +9,8 @@ import * as esbuild from 'esbuild';
99import { parse } from 'cjs-module-lexer' ;
1010import MagicString from 'magic-string' ;
1111import { 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)
1416const 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 ;
0 commit comments