Skip to content

Commit 87b0fd7

Browse files
authored
Revert "fix(compiler): move dynamic import path into a variable (#6452)" (#6464)
This reverts commit 52bdcb8.
1 parent c6bb6f1 commit 87b0fd7

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

scripts/esbuild/internal-platform-client.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BuildOptions as ESBuildOptions } from 'esbuild';
1+
import type { BuildOptions as ESBuildOptions, Plugin } from 'esbuild';
22
import { replace } from 'esbuild-plugin-replace';
33
import fs from 'fs-extra';
44
import { 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_IMPORT_PREFIX}/, './'));
119+
}
120+
});
121+
},
122+
};
123+
}
124+
96125
async function copyPolyfills(opts: BuildOptions, outputInternalClientPolyfillsDir: string) {
97126
const srcPolyfillsDir = join(opts.srcDir, 'client', 'polyfills');
98127

src/client/client-load-module.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ import { consoleDevError, consoleError } from './client-log';
55

66
export const cmpModules = /*@__PURE__*/ new Map<string, { [exportName: string]: d.ComponentConstructor }>();
77

8+
/**
9+
* We need to separate out this prefix so that Esbuild doesn't try to resolve
10+
* the below, but instead retains a dynamic `import()` statement in the
11+
* emitted code.
12+
*
13+
* See here for details https://esbuild.github.io/api/#non-analyzable-imports
14+
*
15+
* We need to do this in order to prevent Esbuild from analyzing / transforming
16+
* the input. However some _other_ bundlers will _not_ work with such an import
17+
* if it _lacks_ a leading `"./"`, so we thus we have to do a little dance
18+
* where here in the source code it must be like this, so that an undesirable
19+
* transformation that Esbuild would otherwise carry out doesn't occur, but we
20+
* actually need to then manually edit the bundled Esbuild code later on to fix
21+
* that. We do this with plugins in the Esbuild and Rollup bundles which
22+
* include this file.
23+
*/
24+
const MODULE_IMPORT_PREFIX = './';
25+
826
export const loadModule = (
927
cmpMeta: d.ComponentRuntimeMeta,
1028
hostRef: d.HostRef,
@@ -26,20 +44,14 @@ export const loadModule = (
2644
return module[exportName];
2745
}
2846
/*!__STENCIL_STATIC_IMPORT_SWITCH__*/
29-
30-
/**
31-
* Esbuild will try to statically match a path string inside an import statement.
32-
* By putting dynamicImportPath into a variable, it can stay dynamic.
33-
* For details: https://esbuild.github.io/api/#non-analyzable-imports
34-
*/
35-
const hmr = BUILD.hotModuleReplacement && hmrVersionId ? '?s-hmr=' + hmrVersionId : '';
36-
const dynamicImportPath = `./${bundleId}.entry.js${hmr}`;
3747
return import(
3848
/* @vite-ignore */
3949
/* webpackInclude: /\.entry\.js$/ */
4050
/* webpackExclude: /\.system\.entry\.js$/ */
4151
/* webpackMode: "lazy" */
42-
dynamicImportPath
52+
`${MODULE_IMPORT_PREFIX}${bundleId}.entry.js${
53+
BUILD.hotModuleReplacement && hmrVersionId ? '?s-hmr=' + hmrVersionId : ''
54+
}`
4355
).then(
4456
(importedModule) => {
4557
if (!BUILD.hotModuleReplacement) {

0 commit comments

Comments
 (0)