1- import type { BuildOptions as ESBuildOptions } from 'esbuild' ;
1+ import type { BuildOptions as ESBuildOptions , Plugin } from 'esbuild' ;
22import { replace } from 'esbuild-plugin-replace' ;
33import fs from 'fs-extra' ;
44import { glob } from 'glob' ;
@@ -47,6 +47,9 @@ export async function getInternalClientBundles(opts: BuildOptions): Promise<ESBu
4747 ...getBaseEsbuildOptions ( ) ,
4848 entryPoints : [ join ( inputClientDir , 'index.ts' ) ] ,
4949 format : 'esm' ,
50+ // we do 'write: false' here because we write the build to disk in our
51+ // `findAndReplaceLoadModule` plugin below
52+ write : false ,
5053 outfile : join ( outputInternalClientDir , 'index.js' ) ,
5154 platform : 'node' ,
5255 external : clientExternal ,
@@ -59,6 +62,7 @@ export async function getInternalClientBundles(opts: BuildOptions): Promise<ESBu
5962 externalAlias ( '@app-data' , '@stencil/core/internal/app-data' ) ,
6063 externalAlias ( '@app-globals' , '@stencil/core/internal/app-globals' ) ,
6164 externalAlias ( '@utils/shadow-css' , './shadow-css.js' ) ,
65+ findAndReplaceLoadModule ( ) ,
6266 ] ,
6367 } ;
6468
@@ -93,6 +97,31 @@ export async function getInternalClientBundles(opts: BuildOptions): Promise<ESBu
9397 return [ internalClientBundle , internalClientPatchBrowserBundle ] ;
9498}
9599
100+ /**
101+ * We need to manually find-and-replace a bit of code in
102+ * `client-load-module.ts` in order to prevent Esbuild from analyzing /
103+ * transforming the input by ensuring it does not start with `"./"`. However
104+ * some _other_ bundlers will _not_ work with such an import if it _lacks_ a
105+ * leading `"./"`, so we thus we have to do a little dance where we manually
106+ * replace it here after it's been run through Esbuild.
107+ *
108+ * @returns an Esbuild plugin
109+ */
110+ export function findAndReplaceLoadModule ( ) : Plugin {
111+ return {
112+ name : 'findAndReplaceLoadModule' ,
113+ setup ( build ) {
114+ build . onEnd ( async ( result ) => {
115+ for ( const file of result . outputFiles ! ) {
116+ const { path, text } = file ;
117+
118+ await fs . writeFile ( path , text . replace ( / \$ { MODULE_ I M P O R T _ P R E F I X } / , './' ) ) ;
119+ }
120+ } ) ;
121+ } ,
122+ } ;
123+ }
124+
96125async function copyPolyfills ( opts : BuildOptions , outputInternalClientPolyfillsDir : string ) {
97126 const srcPolyfillsDir = join ( opts . srcDir , 'client' , 'polyfills' ) ;
98127
0 commit comments