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

Babel ast location, diagnostic, and source location remapping #6238

Merged
merged 40 commits into from May 17, 2021

Conversation

wbinnssmith
Copy link
Contributor

@wbinnssmith wbinnssmith commented May 6, 2021

#5871 applied to #6230.

This also adds a fallback to read and use the original asset's code as sourceContent when an existing sourcemap is not found.

Fixes T-1042

Test Plan:

  • See reproduction in T-1042
  • This should be implemented as an integration test. Added an integration test

# Conflicts:
#	packages/core/core/package.json
#	packages/core/core/src/Transformation.js
#	packages/core/utils/package.json
#	packages/optimizers/cssnano/package.json
#	packages/optimizers/esbuild/package.json
#	packages/optimizers/terser/package.json
#	packages/packagers/css/package.json
#	packages/packagers/js/package.json
#	packages/shared/babel-ast-utils/package.json
#	packages/shared/scope-hoisting/package.json
#	packages/transformers/coffeescript/package.json
#	packages/transformers/css/package.json
#	packages/transformers/less/package.json
#	packages/transformers/sass/package.json
#	packages/transformers/typescript-types/package.json
#	packages/transformers/vue/package.json
@height
Copy link

height bot commented May 6, 2021

This pull request has been linked to and will mark 1 task as "Done" when merged:

💡Tip: You can link multiple Height tasks to a pull request.

let assetGroup = {
code,
mapBuffer: sourcemap.toBuffer(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is still going to be an issue as it (along with the code) gets serialized into the bundlegraph. cc @DeMoorJasper @devongovett

Copy link
Member

Choose a reason for hiding this comment

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

Yes. Do we actually need the sourcemap here? It's empty and only contains the same code as is already passed through the request. If there's no existing map, won't it already be set as the source content during transformation? I guess we should have an integration test for runtimes + sourcemaps.

Copy link
Member

Choose a reason for hiding this comment

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

Another issue is that with empty maps all of the columns mappings are zero when the empty map is extended. If we overwrote the filePath here though it breaks a lot of tests that expect JSRuntime.js as the name. Maybe the test util should just ignore runtime assets?

@wbinnssmith wbinnssmith force-pushed the wbinnssmith/swc-babel-remap-asts branch from 102213a to e0abdbc Compare May 6, 2021 23:50
Base automatically changed from swc-scope-hoisting to v2 May 10, 2021 00:13
@wbinnssmith wbinnssmith force-pushed the wbinnssmith/swc-babel-remap-asts branch from e0abdbc to 6aeb81a Compare May 12, 2021 19:06
@parcel-benchmark
Copy link

parcel-benchmark commented May 12, 2021

Benchmark Results

Kitchen Sink 🚨

Timings

Description Time Difference
Cold FAILED -0.00ms
Cached FAILED -0.00ms

Cold Bundles

No bundles found, this is probably a failed build...

Cached Bundles

No bundles found, this is probably a failed build...

React HackerNews 🚨

Timings

Description Time Difference
Cold FAILED -0.00ms
Cached FAILED -0.00ms

Cold Bundles

No bundles found, this is probably a failed build...

Cached Bundles

No bundles found, this is probably a failed build...

AtlasKit Editor 🚨

Timings

Description Time Difference
Cold FAILED -0.00ms
Cached FAILED -0.00ms

Cold Bundles

No bundles found, this is probably a failed build...

Cached Bundles

No bundles found, this is probably a failed build...

Three.js 🚨

Timings

Description Time Difference
Cold FAILED -0.00ms
Cached FAILED -0.00ms

Cold Bundles

No bundles found, this is probably a failed build...

Cached Bundles

No bundles found, this is probably a failed build...

Click here to view a detailed benchmark overview.

if (sourceMap) {
let promises = [];
// Traverse the bundle to get all source contents
this.bundle.traverseAssets(asset => {
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem quite right. Not all of the assets in the bundle will necessarily be included in the final output. Some might be skipped e.g. due to no used symbols. Also, shouldn't the sourcemap library handle this when merging the source maps from the original asset?

Copy link
Member

Choose a reason for hiding this comment

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

I think you're right the new scope hoisting no longer needs this as now you're using the sourcemap library already and not relying on the AST for all of this.

Previously we were relying on the Babel AST for this and the AST does not keep a reference to the source content so we had to extract that from the maps and manually add this. We can probably refactor this a bit further to make it a bit more lightweight now that the scope-hoisting implementation already supports the source-maps properly.

if (existing == null) {
// If no existing sourcemap was found, initialize sourcesContent with
// the asset's filepath and its original contents.
asset.sourcesContent = {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this stored separately on the asset rather than in the source map? If there's no existing source map, should we just initialize an empty one containing only the original code?

Copy link
Member

Choose a reason for hiding this comment

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

@@ -24,6 +25,42 @@ import {generator, expressionsPrecedence} from './generator';

export {babelErrorEnhancer};

export function remapAstLocations(ast: BabelNodeFile, map: SourceMap) {
// remap ast to original mappings
// This improves sourcemap accuracy and fixes sourcemaps when scope-hoisting
Copy link
Member

Choose a reason for hiding this comment

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

I guess this is only used if you are using babel and there's an existing sourcemap now?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

@devongovett
Copy link
Member

Changed a few things in #6286. LMK if I missed something.

devongovett and others added 2 commits May 16, 2021 21:02
* Simplify source content handling

* Copy over source content from original map in babel transformer

* Don't store a source map in the graph

* Remap diagnostics and other source locations using input source map (#6288)

* Remap diagnostics and other source locations using input source map

* Fix flow

* Ignore test in flow config

* Move to util, apply in CSS transformer as well
@devongovett devongovett changed the title [New scope hoisting] Babel ast location remapping Babel ast location, diagnostic, and source location remapping May 16, 2021
@devongovett devongovett merged commit 06bdb1f into v2 May 17, 2021
@devongovett devongovett deleted the wbinnssmith/swc-babel-remap-asts branch May 17, 2021 01:23
lettertwo added a commit that referenced this pull request Jun 4, 2021
…raph

* bdo/buffer-backed-graph: (37 commits)
  Improve resolver performance (#6328)
  v2.0.0-beta.3.1
  Add it to the nightly release workflow as well
  Configure jemalloc page size for M1 (#6314)
  Update swc (#6307)
  Fix parcelDependencies
  v2.0.0-beta.3
  Update swc (#6289)
  Remove old AdjacencyList, serialize EfficientGraph in Graph, update BundleGraph to use new functions
  Babel ast location, diagnostic, and source location remapping (#6238)
  update contributing guide (#6293)
  Update sourcemap to rc-1.0 (#6279)
  Serve nearest index.html in case the applications has multiple index files (#6250)
  Allow bundling browserify bundles by replacing free requires with undefined (#6260)
  Upgrade flow to 0.151.0 (#6281)
  Use local require for `@babel/core` and `postcss` (#6264)
  Resolve helpers relative to JS transformer (#6278)
  Pure comment for $parcel$interopDefault (#6271)
  Don't transpile spread in JSX with modern targets (#6274)
  Update TS validator assertions (#6272)
  ...
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.

None yet

5 participants