Title: fix(vite): scan top-level imports with Vite's native parser by aleclarson · Pull Request #11634 · NativeScript/NativeScript · GitHub
Open Graph Title: fix(vite): scan top-level imports with Vite's native parser by aleclarson · Pull Request #11634 · NativeScript/NativeScript
X Title: fix(vite): scan top-level imports with Vite's native parser by aleclarson · Pull Request #11634 · NativeScript/NativeScript
Description: What The /ns/m dev route parses each served module with Babel several times just to find its top-level imports (collectTopLevelImportRecords in two places, plus the dev default-import link check). On modules whose code sits on a few very long lines, Babel's skipBlockComment becomes quadratic: for every block comment it runs lineBreakG.test(this.input) from the comment start to the next line break anywhere in the input, not just to the comment's end. Real case: a 3.9 MB dependency chunk (shaders/dist/core/std-*.js) is transformed by Vite's dev pipeline into 225 lines, the longest 1.9 MB with about 2,000 block comments. One Babel parse of that output takes ~1.5 s (0.26 s on the original multi-line file), and a single cold request did about six of them. CPU profiles showed @babel/parser at 8.7 s of an 11.25 s request, 7.2 s of it in skipBlockComment. Because this runs synchronously, the dev server's event loop is blocked for the whole request, and the iOS runtime's module loader timed out during cold boot (NSURLErrorDomain -1001) before the app could start. How New parseTopLevelStatements(code) in websocket-served-module-helpers.ts returns top-level statements from Vite's parseAst(code, { lang: 'tsx' }) (Oxc), or null if Oxc rejects the code. Both collectTopLevelImportRecords implementations use it first and fall back to the unchanged Babel parse. If both fail, they still return []. Node checks are plain type comparisons that work on either AST. Output fields are unchanged. New collectStaticImportDefaultIntents(code) replaces the link check's inline parse and traverse. Static imports can only appear at top level, so scanning top-level ImportDeclarations is equivalent. It falls back to the previous Babel parse and traverse, and still throws on a double failure, so the caller's try/catch behaves as before. Other Babel uses (astExtractImportsAndStripTypes, collectReExportAndDynamicImportSources, the normalizer) are untouched. vite is already a dependency of @nativescript/vite (^8.0.0), so no new dependency. Test plan npx vitest run hmr/server/websocket-served-module-helpers.spec.ts hmr/server/websocket-import-records-fallback.spec.ts: 26 pass. Covers default, named and string-named specifiers, exact text slices after non-ASCII text, import type, TS+JSX input, no imports, a forced Oxc failure falling back to Babel, ensureNativeScriptModuleBindings on both paths, and default-import intents. Long-line regression: one import plus a single line with 12,000 block comments. Babel-only took 10.3 s on this machine; the new path takes about 0.05 s. The spec asserts under 2 s. npx nx run vite:build. Touched files are lint-clean; vite:lint still reports 3 existing errors in untouched files (configuration/base.ts, css-device-module.ts). Applied as a patch to published 8.0.20 and 8.0.21 in a large app. A cold request for the module above went from 11–21 s to 5–9 s on a heavily loaded host (load average 40–190); the rest is untouched Babel work and the Vite transform. The @babel/parser share dropped from 8.7 s to 1.2 s. A serial cold crawl of the app's 1,443-module boot graph went from 241.6 s to 108.9 s, with the slowest module dropping from 4.9 s to 1.4 s. The same app now completes an iOS simulator HMR cycle from a cold dev server: launch, an in-place JS edit on the same process, and a native edit with rebuild and relaunch. Before this change it failed during module loading.
Open Graph Description: What The /ns/m dev route parses each served module with Babel several times just to find its top-level imports (collectTopLevelImportRecords in two places, plus the dev default-import link check). ...
X Description: What The /ns/m dev route parses each served module with Babel several times just to find its top-level imports (collectTopLevelImportRecords in two places, plus the dev default-import link check). ...
Opengraph URL: https://github.com/NativeScript/NativeScript/pull/11634
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:cfb9bb58-4b08-99ae-4c6c-123783e8d655 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | E0A8:305B07:161B0B5:1D2A924:6B01303A |
| html-safe-nonce | 18ca11feddc01749d3cdc7fa1ff5512498783abadb0762f17fff9d44732db648 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMEE4OjMwNUIwNzoxNjFCMEI1OjFEMkE5MjQ6NkIwMTMwM0EiLCJ2aXNpdG9yX2lkIjoiMzk0NjA5MjA2MDczMzQxOTU3OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 05ca31f5cd256420366777bdc9516d9712aacf92073ab357c97f70d086f137cf |
| hovercard-subject-tag | pull_request:5248316771 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/NativeScript/NativeScript/pull/11634/files |
| twitter:image | https://avatars.githubusercontent.com/u/1925840?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1925840?s=400&v=4 |
| og:image:alt | What The /ns/m dev route parses each served module with Babel several times just to find its top-level imports (collectTopLevelImportRecords in two places, plus the dev default-import link check). ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/NativeScript/NativeScript git https://github.com/NativeScript/NativeScript.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 31492490 |
| octolytics-dimension-repository_nwo | NativeScript/NativeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31492490 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/NativeScript |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width