Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(build): address issue with dynamic import and vite #5399

Merged
merged 1 commit into from
Feb 23, 2024

Conversation

alicewriteswrongs
Copy link
Contributor

@alicewriteswrongs alicewriteswrongs commented Feb 22, 2024

When we added a script for building the modules in internal/ with Esbuild in #5276 we needed to make a change to the function that Stencil uses at runtime to lazy-load components (in
src/client/client-load-module.ts). Prior to #5276 we had a dynamic import statement which looked like so:

import(
  `./${bundleId}.entry.js${BUILD.hotModuleReplacement && hmrVersionId ? '?s-hmr=' + hmrVersionId : ''}`
)

This constructs a filepath to the module for a given Stencil component, accounting for HMR versioning, and then imports the module. All well and good, but unfortunately this dynamic import does not play well with Esbuild. As described
here when Esbuild is in 'bundle' mode and it encounters an import() and the imported path or identifier looks "analyzable" it will attempt to resolve the corresponding file and incorporate it into the bundle.

This is not always what you want! In particular, in our situation the leading "./" in the template literal we had in client-load-module.ts caused Esbuild to consider the import() an "analyzable" import and it then tried to resolve and bundle the import instead of just leaving the dynamic import in the code (as Rollup does in this case).

This created an issue because at compile time (i.e. when Stencil itself is built) this import does not resolve to anything, so Esbuild would essentially transform that line into an empty import. This caused runtime issues because the side-effect of the dynamic import was no longer happening, so the modules containing Stencil component classes and so on were not longer being loaded in.

To get this working for #5276 we pulled out the "./" string as a separate variable, changing the template literal so it looks something like this:

const MODULE_IMPORT_PREFIX = './';

import(
  `${MODULE_IMPORT_PREFIX}${bundleId}.entry.js${BUILD.hotModuleReplacement && hmrVersionId ? '?s-hmr=' + hmrVersionId : ''}`
)

This causes Esbuild to conclude that the import is "non-analyzable", which addresses the issue and causes both Rollup and Esbuild to emit equivalent code for this snippet, where both retain the dynamic import, allowing for the runtime module resolution that we want here.

However, this broke the ability to use Stencil with Vite, which will complain about non-analyzable imports if it sees a dynamic import which does not begin with "./". See #5389 for details.

So essentially we have a situation where the behavior of Rollup, Esbuild, and Vite is incompatible. The solution is to figure out a way for both the Esbuild and Rollup builds to emit code in this case which retains the dynamic import and retains the leading "./" in the template literal.

This is accomplished by retaining the ${MODULE_IMPORT_PREFIX} in the template literal, so that Esbuild does not attempt to analyze and bundle the import, and adding plugins to both the Rollup and Esbuild bundles to transform the emitted code before it is written to disk.

fixes #5389
STENCIL-1181

What is the current behavior?

GitHub Issue Number: N/A

What is the new behavior?

Documentation

Does this introduce a breaking change?

  • Yes
  • No

Testing

To test this the reproduction case linked in #5389 should be tried with both the ESbuild and Rollup based builds. It should work fine with either one.

The reproduction is here: https://github.com/gregorypratt/stencil-issue

After cloning locally, build and pack with Rollup and test to see that the issue doesn't repro anymore, and then do the same with Esbuild.

Other information

Copy link
Contributor

--strictNullChecks error report

Typechecking with --strictNullChecks resulted in 1150 errors on this branch.

That's the same number of errors on main, so at least we're not creating new ones!

reports and statistics

Our most error-prone files
Path Error Count
src/dev-server/index.ts 37
src/dev-server/server-process.ts 32
src/compiler/prerender/prerender-main.ts 22
src/testing/puppeteer/puppeteer-element.ts 22
src/runtime/client-hydrate.ts 20
src/screenshot/connector-base.ts 19
src/runtime/vdom/vdom-render.ts 17
src/dev-server/request-handler.ts 15
src/compiler/prerender/prerender-optimize.ts 14
src/compiler/sys/stencil-sys.ts 14
src/compiler/transpile/transpile-module.ts 14
src/sys/node/node-sys.ts 14
src/compiler/prerender/prerender-queue.ts 13
src/compiler/sys/in-memory-fs.ts 13
src/runtime/connected-callback.ts 13
src/runtime/set-value.ts 13
src/compiler/output-targets/output-www.ts 12
src/compiler/transformers/test/parse-vdom.spec.ts 12
src/compiler/transformers/transform-utils.ts 12
src/mock-doc/test/attribute.spec.ts 12
Our most common errors
Typescript Error Code Count
TS2322 361
TS2345 350
TS18048 201
TS18047 91
TS2722 37
TS2532 24
TS2531 22
TS2454 14
TS2790 11
TS2352 10
TS2769 8
TS2538 8
TS2416 6
TS2493 3
TS18046 2
TS2684 1
TS2430 1

Unused exports report

There are 14 unused exports on this PR. That's the same number of errors on main, so at least we're not creating new ones!

Unused exports
File Line Identifier
src/runtime/bootstrap-lazy.ts 21 setNonce
src/screenshot/screenshot-fs.ts 18 readScreenshotData
src/testing/testing-utils.ts 198 withSilentWarn
src/utils/index.ts 145 CUSTOM
src/utils/index.ts 269 normalize
src/utils/index.ts 7 escapeRegExpSpecialCharacters
src/compiler/app-core/app-data.ts 25 BUILD
src/compiler/app-core/app-data.ts 115 Env
src/compiler/app-core/app-data.ts 117 NAMESPACE
src/compiler/fs-watch/fs-watch-rebuild.ts 123 updateCacheFromRebuild
src/compiler/types/validate-primary-package-output-target.ts 61 satisfies
src/compiler/types/validate-primary-package-output-target.ts 61 Record
src/testing/puppeteer/puppeteer-declarations.ts 485 WaitForEventOptions
src/compiler/sys/fetch/write-fetch-success.ts 7 writeFetchSuccessSync

Copy link
Contributor

github-actions bot commented Feb 22, 2024

PR built and packed!

Download the tarball here: https://github.com/ionic-team/stencil/actions/runs/8020920200/artifacts/1269983155

If your browser saves files to ~/Downloads you can install it like so:

npm install ~/Downloads/stencil-core-4.12.3-dev.1708699805.ae14941.tgz

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to test this?

LGTM otherwise 👍

scripts/esbuild/internal-platform-client.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@rwaskiewicz rwaskiewicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @christian-bromann's comments here, nothing blocking on my end.

Copy link
Contributor

@rwaskiewicz rwaskiewicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops - meant to 'approve'

When we added a script for building the modules in `internal/` with
Esbuild in #5276 we needed to make a change to the function that Stencil
uses at runtime to lazy-load components (in
`src/client/client-load-module.ts`). Prior to #5276 we had a dynamic
import statement which looked like so:

```ts
import(
  `./${bundleId}.entry.js${BUILD.hotModuleReplacement && hmrVersionId ? '?s-hmr=' + hmrVersionId : ''}`
)
```

This constructs a filepath to the module for a given Stencil component,
accounting for HMR versioning, and then imports the module. All well and
good, but unfortunately this dynamic import does not play well with
Esbuild. As described
[here](https://esbuild.github.io/api/#non-analyzable-imports) when
Esbuild is in 'bundle' mode and it encounters an `import()` _and_ the
imported path or identifier looks "analyzable" it will attempt to
resolve the corresponding file and incorporate it into the bundle.

This is not always what you want! In particular, in our situation the
leading `"./"` in the template literal we had in `client-load-module.ts`
caused Esbuild to consider the `import()` an "analyzable" import and it
then tried to resolve and bundle the import instead of just leaving the
dynamic import in the code (as Rollup does in this case).

This created an issue because at _compile time_ (i.e. when Stencil
itself is built) this import does not resolve to anything, so Esbuild
would essentially transform that line into an empty import. This caused
runtime issues because the side-effect of the dynamic import was no
longer happening, so the modules containing Stencil component classes
and so on were not longer being loaded in.

To get this working for #5276 we pulled out the `"./"` string as a
separate variable, changing the template literal so it looks something
like this:

```ts
const MODULE_IMPORT_PREFIX = './';

import(
  `${MODULE_IMPORT_PREFIX}${bundleId}.entry.js${BUILD.hotModuleReplacement && hmrVersionId ? '?s-hmr=' + hmrVersionId : ''}`
)
```

This causes Esbuild to conclude that the import is "non-analyzable",
which addresses the issue and causes both Rollup and Esbuild to emit
equivalent code for this snippet, where both retain the dynamic import,
allowing for the runtime module resolution that we want here.

_However_, this broke the ability to use Stencil with Vite, which will
complain about non-analyzable imports if it sees a dynamic import which
does _not_ begin with `"./"`. See #5389 for details.

So essentially we have a situation where the behavior of Rollup,
Esbuild, and Vite is incompatible. The solution is to figure out a way
for both the Esbuild and Rollup builds to emit code in this case which
retains the dynamic import _and_ retains the leading `"./"` in the
template literal.

This is accomplished by retaining the `${MODULE_IMPORT_PREFIX}` in the
template literal, so that Esbuild does not attempt to analyze and bundle
the import, and adding plugins to both the Rollup and Esbuild bundles to
transform the emitted code before it is written to disk.

fixes #5389
STENCIL-1181
@alicewriteswrongs alicewriteswrongs added this pull request to the merge queue Feb 23, 2024
Merged via the queue into main with commit 8ebacae Feb 23, 2024
121 checks passed
@alicewriteswrongs alicewriteswrongs deleted the ap/fix-vite-issue branch February 23, 2024 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: latest v4.12.3 causes: "dynamic import cannot be analyzed by Vite." error with Storybook
3 participants