Skip to content

chore(types): updating typings for merge-source-map#3040

Merged
rwaskiewicz merged 3 commits intorwaskiewicz-rebase-again-sourcemapsfrom
rwaskiewicz/sourcemaps/merge-types
Sep 3, 2021
Merged

chore(types): updating typings for merge-source-map#3040
rwaskiewicz merged 3 commits intorwaskiewicz-rebase-again-sourcemapsfrom
rwaskiewicz/sourcemaps/merge-types

Conversation

@rwaskiewicz
Copy link
Copy Markdown
Member

@rwaskiewicz rwaskiewicz commented Aug 31, 2021

this PR makes changes to the Stencil-defined type declaration file for merge-source-map

merge-source-map does not offer any d.ts file, nor does it have a third party offering on Definitely Typed, so the intent of this effort was to see "how correct is our type definition for our use case, while not deviating from the actual library's implicit typings".

I did try to ask the compiler to generate the d.ts file for me with tsc -d node_modules/merge-source-map/index.js --allowJs --outDir ./out, which generated invalid TS:

export = merge;
/**
 * Merge old source map and new source map and return merged.
 * If old or new source map value is falsy, return another one as it is.
 *
 * @param {object|string} [oldMap] old source map object
 * @param {object|string} [newmap] new source map object
 * @return {object|undefined} merged source map object, or undefined when both old and new source map are undefined
 */
declare function merge(oldMap?: object | string, newMap: any): object | undefined;

(the invalid part being you can't have a non-optional type following an optional one). I can always tweak that and use that if folks prefer it.

document the typings for merge-source-map
there exist many sourcemap types within this PR. This attempts to
eliminate one of them
// need to merge sourcemaps at this point
const mergeMap = sourceMapMerge(
(minifyOpts.sourceMap as SourceMapOptions).content as SourceMap,
(minifyOpts.sourceMap as SourceMapOptions)?.content as SourceMap,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

minifyOpts.sourceMap is typed as sourceMap?: boolean | SourceMapOptions;. This doesn't fix the type assertion usage (that's for a later day), but it does prevent accessing a field on an undefined value

* @returns the merged sourcemap, or undefined if both maps are falsy
*/
function merge(
oldMap: import('../src/declarations').SourceMap,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If there's a better way to do this, I'm very much in favor of it (even if reintroducing the type declaration duplication is the route we take)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm curious how this affects the browser... seems unrelated but I've been bitten by this import syntax. Take a peek at the /test/browser-compile directory and if that doesn't have any errors, I'm good!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks good! Gonna put up a separate PR to add a reminder to TAL at that when doing a release until we get testing underway there

* recreated here for type safety purposes.
*/
declare module 'merge-source-map' {
interface SourceMap {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There are so many representations of SourceMaps in the codebase. One from:

  • terser
  • MagicString
  • Stencil
  • rollup

This is an attempt to coalesce them, because honestly it gets confusing really quickly which definition you're dealing with. I don't want to combine them all into one definition, but this particular definition seemed extraneous

@rwaskiewicz rwaskiewicz marked this pull request as ready for review August 31, 2021 14:11
@rwaskiewicz rwaskiewicz requested a review from a team August 31, 2021 14:11
Copy link
Copy Markdown
Contributor

@splitinfinities splitinfinities left a comment

Choose a reason for hiding this comment

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

LGTM if the /test/browser-compile directory doesn't have any errors.

@rwaskiewicz rwaskiewicz merged commit e8cab1d into rwaskiewicz-rebase-again-sourcemaps Sep 3, 2021
@rwaskiewicz rwaskiewicz deleted the rwaskiewicz/sourcemaps/merge-types branch September 3, 2021 20:17
rwaskiewicz added a commit that referenced this pull request Sep 20, 2021
this PR makes changes to the Stencil-defined type declaration file for `merge-source-map`

`merge-source-map` does not offer any `d.ts` file, nor does it have a third party offering on Definitely Typed, so the intent of this effort was to see "how correct is our type definition for our use case, while not deviating from the actual library's implicit typings".
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.

2 participants