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

Add missing source-map methods #2

Merged
merged 7 commits into from Mar 12, 2024
Merged

Conversation

onigoetz
Copy link
Contributor

@onigoetz onigoetz commented Jun 16, 2022

Hi,

As said in #1 This PR adds some missing methods to source-map, I'm opening it as Draft as it's not yet complete but I would love some feedback on a few things.

Added Methods and fields

  • static SourceMapConsumer.fromSourceMap() will transform a SourceMapGenerator into a SourceMapConsumer. is optimized to use toDecodedMap when available
  • SourceMapConsumer.version
  • SourceMapConsumer.mappings: computes the mappings on demand
  • SourceMapConsumer.generatedPositionFor()
  • SourceMapConsumer.allGeneratedPositionsFor(): Will only return one value from generatedPositionFor
  • SourceMapConsumer.hasContentsOfAllSources()
  • SourceMapConsumer.sourceContentFor()
  • SourceMapConsumer.eachMapping()
    • Doesn't support the "order" argument yet
  • static SourceMapGenerator.fromSourceMap()
  • SourceMapGenerator.applySourceMap
  • src/utils: needed

Tests

Tests come from the source-map-js repository, I've been testing these changes on a PR I'm preparing for postcss I will probably add more tests and examples as I discover and fix issues.

Tests Runtime

I configured the tests to use ts-mocha instead of mocha and changed the paths in the tests to use the source file, as it didn't show the coverage properly. This PR currently has 85% coverage.

---------------|---------|----------|---------|---------|
File           | % Stmts | % Branch | % Funcs | % Lines |              
---------------|---------|----------|---------|---------|
All files      |   85.32 |    78.08 |   88.46 |   85.32 |                                           
 source-map.ts |   89.95 |    90.32 |      85 |   89.95 | 
 util.ts       |   80.64 |    69.04 |     100 |   80.64 |
---------------|---------|----------|---------|---------|

Build

This PR also introduces changes in the build process. It now has three outputs instead of two :

  • source-map.umd.js : For browsers, still same as before
  • source-map.mjs.js: For node with imports, does not include dependencies
  • source-map.cjs: For node with requires, does not include dependencies

I also changed the package.json accordingly.

If you have any early feedback before I go in a wrong direction I'd love to hear it

src/source-map.ts Outdated Show resolved Hide resolved
src/source-map.ts Outdated Show resolved Hide resolved
Comment on lines +90 to +93
allGeneratedPositionsFor(
originalPosition: Parameters<typeof generatedPositionFor>[1],
): ReturnType<typeof generatedPositionFor>[] {
// This doesn't map exactly to the same feature
return [generatedPositionFor(this._map, originalPosition)];
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems there is no equivalent in trace-mapping I'm not sure to know how important it is or not

Copy link
Owner

Choose a reason for hiding this comment

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

Not yet, but the instanbul project might want to use it eventually to fix their coverage cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Documented for now that it only returns one mapping

Comment on lines 114 to 120
const sourceContent = sourceContentFor(this._map, aSource);
if (sourceContent != null) {
return sourceContent;
}

const resolvedSource = resolve(aSource, this.sourceRoot);
if (this.sources.indexOf(resolvedSource) > -1) {
return this.sourcesContent[this.sources.indexOf(resolvedSource)];
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should this logic be moved to trace-mapping ?

Copy link
Owner

Choose a reason for hiding this comment

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

It's already there. 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only partially though.

the implementation in trace-mapping looks up a static list of sources, whereas the original Mozilla implementation resolves the sourceFile and accepts some relative paths.

Removed the extended part for now and will look into a future PR for trace-mapping


eachMapping(
aCallback: Parameters<typeof eachMapping>[1],
aContext?: any /*, aOrder?: number*/,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know how important that order argument is, should it be implemented ? is it even possible in trace-mapping ?

Copy link
Owner

Choose a reason for hiding this comment

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

This is possible in trace-mapping, but it's not implemented. We generate the "original order" in generatedPositionFor, and you would need to walk that. Rather than adding a parameter to trace-mapping, I would expose a new function that did eachMappingBySource, and call the appropriate function here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Documented that this isn't supported for now

src/source-map.ts Outdated Show resolved Hide resolved
rollup.config.js Show resolved Hide resolved
src/source-map.ts Outdated Show resolved Hide resolved
Comment on lines +90 to +93
allGeneratedPositionsFor(
originalPosition: Parameters<typeof generatedPositionFor>[1],
): ReturnType<typeof generatedPositionFor>[] {
// This doesn't map exactly to the same feature
return [generatedPositionFor(this._map, originalPosition)];
}
Copy link
Owner

Choose a reason for hiding this comment

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

Not yet, but the instanbul project might want to use it eventually to fix their coverage cases.

src/source-map.ts Show resolved Hide resolved
src/source-map.ts Show resolved Hide resolved
this._map = new GenMapping(opts);
constructor(opts: ConstructorParameters<typeof GenMapping>[0] | GenMapping) {
// TODO :: should this be duck-typed ?
this._map = opts instanceof GenMapping ? opts : new GenMapping(opts);
Copy link
Owner

Choose a reason for hiding this comment

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

Does SourceMapGenerator support this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't but I use this as a workaround to make it possible to call the constructor in the fromSourceMap below that is using fromMap() and passing that to SourceMapGenerator's constructor

}

// This is a fallback for `source-map` and `source-map-js`
return new SourceMapConsumer(map.toJSON() as SectionedSourceMapInput, mapUrl);
Copy link
Owner

Choose a reason for hiding this comment

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

Do we need to support the old consumer instances? I'd think most would be from the JSON string/Object forms that are serialized into a file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we do, I honestly don't know in which contexts this package is used and sometimes compilation processes go through multiple libraries which could use any source map library

src/source-map.ts Outdated Show resolved Hide resolved
src/source-map.ts Outdated Show resolved Hide resolved
src/source-map.ts Outdated Show resolved Hide resolved
@TylorS
Copy link

TylorS commented Jun 25, 2022

Hello @onigoetz and @jridgewell! Just came across these packages and this PR and just wanted to thank you both for doing amazing work to dramatically improve the codebase and size of source-map!

It's a lot harder to justify the bundle size of source-map in the browser for small features like enhancing error messages as I'm keen to do, and this seems incredibly promising. Please let me know if I can be of any help

@jridgewell
Copy link
Owner

@TylorS from you're comment, I imagine you're doing unminification of stack trace line/columns using the sourcemap? I'd suggest using @jridgewell/trace-mapping directly instead of this package, so that unused code can be eliminated from your bundle. That won't be fully possible with this package, because using the SourceMapConsumer class will hold all of trace-mapping's functions regardless of whether you actually use them.

@TylorS
Copy link

TylorS commented Jun 27, 2022

Thanks for the advice @jridgewell! I'll have to poke around at it.

I was initially going to use github.com/xpl/stacktracey, but it has a direct dep on source-map, and I was considering forking it to have a dependency on this library instead

@onigoetz
Copy link
Contributor Author

Hi @jridgewell

I've been fairly inactive on my JS projects lately but would like to pick up where I left off.
How can I help to get this PR moving ?

I'm still very motivated to get this PR merged and get more tools over to your implementation of source-map to make JS tooling faster.

@onigoetz onigoetz marked this pull request as ready for review March 4, 2024 20:20
@onigoetz
Copy link
Contributor Author

onigoetz commented Mar 4, 2024

Hi @jridgewell

I think I addressed all comments from the PR except the ones that I left unresolved.
I removed the applySourceMaps implementation for now and some methods are not 1:1 equivalents to source-map or source-map-js yet. I documented that as well.

I'm ready to make adjustments wherever is needed

@jridgewell
Copy link
Owner

I'll take a look at this this weekend.

@jridgewell jridgewell merged commit 410f3e3 into jridgewell:main Mar 12, 2024
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

3 participants