Skip to content

chore(sourcemaps): update global script handling#3094

Merged
rwaskiewicz merged 2 commits intorwaskiewicz-rebase-again-sourcemapsfrom
rwaskiewicz/sourcemaps/app-data-plugin
Oct 6, 2021
Merged

chore(sourcemaps): update global script handling#3094
rwaskiewicz merged 2 commits intorwaskiewicz-rebase-again-sourcemapsfrom
rwaskiewicz/sourcemaps/app-data-plugin

Conversation

@rwaskiewicz
Copy link
Copy Markdown
Member

@rwaskiewicz rwaskiewicz commented Oct 6, 2021

Please do not merge, this is targeting a parent branch for ease of review

Pull request checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Build (npm run build) was run locally and any changes were pushed
  • Unit tests (npm test) were run locally and passed
  • E2E Tests (npm run test.karma.prod) were run locally and passed
  • Prettier (npm run prettier) was run locally and passed

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

For global script sourcemap generation, there are a few minor cleanups the team wanted to perform

GitHub Issue Number: N/A

What is the new behavior?

add a check to verify that a global script exists in the moduleMap on
the compiler context before accessing fields on it to prevent compiler
time errors. this is one of the items we discussed as a team in the GH
comments of #3005, and is being implemented here

reworked how sourcemaps are generated. this commit restores the original
code generation where the platform, source code, and default export are
concatenated, which allows us to reserve generating a magic string
until/if we're using source maps

Does this introduce a breaking change?

  • Yes
  • No

Testing

I spun up a basic Stencil component library with npm init stencil. Run npm i in the created project.

I added two files to src/:

// mr-worldwide.ts
import { childGlobal } from "./child-worldwide";
/**
 * prints a name with a greeting
 * @param name the name
 */
const helloGlobal = (name?: string): void => {
    const hello: string = 'hello';
    const globe: string = 'globe';
    const result = `${hello}, ${globe} ${name ? 'and ' + name : ''}!`;
    console.log(result);
}
helloGlobal(childGlobal());
}
// child-worldwide.ts
export const childGlobal = (): string => {
    return 'ryan';
}

Update your stencil.config.ts, setting:

  sourceMap: true,

Checkout the this branch, which allows for sourcemaps to be used. Build stencil with npm ci && npm run build && npm pack and install the tarball in your component library

Finally start the dev server with npm start.

Observe breakpoints in the global script there.

Note that there is a known issue where breakpoints are 'off by one', where the first eligible line my not be able to receive/stop on a breakpoint. I've added something to the Stencil team backlog to investigate further Fixed in 4ca0542

Other information

@rwaskiewicz rwaskiewicz requested a review from a team October 6, 2021 12:36
@johnjenkins
Copy link
Copy Markdown
Contributor

johnjenkins commented Oct 6, 2021

Hey @rwaskiewicz
the "off by one" (in my original working on this, it can be more than that) will be because Magic String is not being used before adding lines of code to the original code < Magic string (and accompanying sourcemap) will have no knowledge of the changes made to the source code.
If you don't use Magic String in the first instance of amending the code, you can remove / stop using Magic string completely plus you can remove the sourceMap merging. See #3005 (comment) for more detailed context

return { code: results.outputText };
}

return { code: results.outputText, map: sourceMapMerge(sourceMap, codeMap) };
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.

sourceMap merging is now redundant - the magic string sourcemap === the sourcemap from transpileModule

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.

Good call! Updated in 4ca0542 which also fixes the off by one error 🎉

Base automatically changed from rwaskiewicz/sourcemaps/minify to rwaskiewicz-rebase-again-sourcemaps October 6, 2021 14:21
add a check to verify that a global script exists in the moduleMap on
the compiler context before accessing fields on it to prevent compiler
time errors. this is one of the items we discussed as a team in the GH
comments of #3005, and is being implemented here

reworked how sourcemaps are generated. this commit restores the original
code generation where the platform, source code, and default export are
concatenated, which allows us to reserve generating a magic string
until/if we're using source maps

remove includeContent field from the generation of the sourcemap, which
does not appear to be needed, strictly speaking
include the source content and remove the merge of sourcemaps to resolve off by one error
@rwaskiewicz rwaskiewicz force-pushed the rwaskiewicz/sourcemaps/app-data-plugin branch from 4ca0542 to c8d029b Compare October 6, 2021 14:37
@rwaskiewicz rwaskiewicz merged commit 80ce208 into rwaskiewicz-rebase-again-sourcemaps Oct 6, 2021
@rwaskiewicz rwaskiewicz deleted the rwaskiewicz/sourcemaps/app-data-plugin branch October 6, 2021 15:54
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.

3 participants