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

engine-server/template-compiler: remove parse5 utils #3785

Merged
merged 5 commits into from
Oct 11, 2023

Conversation

43081j
Copy link
Contributor

@43081j 43081j commented Oct 10, 2023

Details

I had a branch hanging around waiting for inikulin/parse5#996, which upgraded parse5 in this repo to 7.x

While waiting, this was partially done in #3602 (partially because we're still waiting for the parse5 update, just using ts-ignore meanwhile).

One thing that branch didn't have, which would be good to still sort: removal of the parse5 utils in LWC.

Basically, LWC ships its own parse5 utils for things like quick access to tree adapter types, etc.

All of this has been available for a while in a @parse5/tools package.

So i've rebased my original PR back onto master, which has left us with the migration to @parse5/tools.

Failing build

Currently, it won't build as @parse5/tools is ESM-only and our target appears to be commonjs. How did you solve this elsewhere? @nolanlawson i saw you did some stuff around this for parse5 itself, could you point me in the right direction?

Does this pull request introduce a breaking change?

  • ✅ No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • ✅ No, it does not introduce an observable change.

@salesforce-cla
Copy link

Thanks for the contribution! Before we can merge this, we need @43081j to sign the Salesforce Inc. Contributor License Agreement.

@43081j
Copy link
Contributor Author

43081j commented Oct 10, 2023

i did sign the CLA btw but seems to have not picked it up 🤔

@nolanlawson
Copy link
Collaborator

@43081j For ESM dependencies, we are currently pre-bundling those into our own commonjs/ESM outputs, to avoid passing on the "type": "module" requirement to our own consumers.

To bundle up @parse5/utils, you will need to add it to the allowlist here:

nodeResolve({
// These are the devDeps that may be inlined into the dist/ bundles
// These include packages owned by us (LWC, observable-membrane), as well as parse5
// and its single dependency, which are bundled because it makes it simpler to distribute
resolveOnly: [/^@lwc\//, 'observable-membrane', /^parse5($|\/)/, 'entities'],
}),

It will also need to be added to the devDependencies in @lwc/template-compiler/package.json:

"devDependencies": {
"@types/estree": "1.0.2",
"@types/he": "^1.2.1",
"@types/source-map": "0.5.7"
}

If you do this, then the dep should be bundled into packages/@lwc/template-compiler/dist/index.cjs.js rather than require()d.

I just realized though that we missed this step in #3602; we'll open up a separate PR to fix that.

As for the CLA – you may have to push an empty commit to the branch to re-trigger the CLA check. 🙂

@nolanlawson
Copy link
Collaborator

Fixing the devDeps issue here, you may have some merge conflicts after it gets merged: #3786

Copy link
Collaborator

@nolanlawson nolanlawson left a comment

Choose a reason for hiding this comment

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

This LGTM except for the merge conflict/devDeps issue that needs to be resolved, but I would like @divmain to take a look too.

Thank you so much for the PR!! 🙇

@43081j
Copy link
Contributor Author

43081j commented Oct 10, 2023

no worries, i'll update per the comments 👍

LWC is one of the last few major projects i've been updating parse5 and moving to p5 tools in. so will be good to get it done!

This does a couple of things:

- Introduces `@parse5/tools` to replace any parse5 AST utilities we had
- Removes custom parse5 types (since they are now shipped)
@43081j
Copy link
Contributor Author

43081j commented Oct 10, 2023

@nolanlawson i made the changes but, locally at least, build fails still when trying to build the playground:

Error [ERR_REQUIRE_ESM]: require() of ES Module /lwc/node_modules/@parse5/tools/lib/main.js from /lwc/packages/@lwc/template-compiler/dist/index.cjs.js not supported.

@43081j 43081j marked this pull request as ready for review October 10, 2023 18:34
@43081j 43081j requested a review from a team as a code owner October 10, 2023 18:34
@nolanlawson
Copy link
Collaborator

@43081j When I build your branch locally, it works fine for me. I can also see that packages/@lwc/template-compiler/dist/index.cjs.js correctly has parse5 and parse5-utils inlined.

I will kick off a CI test of your code!

@nolanlawson nolanlawson mentioned this pull request Oct 10, 2023
Copy link
Contributor

@divmain divmain left a comment

Choose a reason for hiding this comment

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

This is fantastic! Thanks again for the contribution and for reading my mind in the parse5 PR!

@@ -6,11 +6,11 @@
*/

import * as parse5 from 'parse5';
import type { DefaultTreeAdapterMap } from 'parse5';
import {DocumentFragment} from '@parse5/tools';
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
import {DocumentFragment} from '@parse5/tools';
import { DocumentFragment } from '@parse5/tools';

scripts/rollup/rollup.config.js Show resolved Hide resolved
@43081j
Copy link
Contributor Author

43081j commented Oct 10, 2023

@nolanlawson it was some nx caching stuff, yarn build --skip-nx-cache sorted it (i guess the rollup file change didn't invalidate the cache)

@divmain no worries, thanks for sorting the parse5 upgrade too. once the new version is published, we can remove the ts-ignore flags too

@43081j
Copy link
Contributor Author

43081j commented Oct 10, 2023

the build failure seems like it has transformed to cjs but kept the export * 🤔

not too sure whats going on there

@divmain
Copy link
Contributor

divmain commented Oct 10, 2023

@43081j , feel free to pull in commit 728fb57. This should resolve the issue with Jest. We may follow up and make changes later, but don't worry about that for now.

You'll also find a single failing test related to your changes :)

@43081j
Copy link
Contributor Author

43081j commented Oct 10, 2023

interesting, do you know why that solves the problem? why doesn't ts-jest figure out ESM?

are we basically working around ts-jest by using babel on esm dependencies separately?

sorry for so many questions, just making sure i understand the root of the problem


as for the failing test... its an awkward one.

parse5 parses this as a template node (i.e. it has content):

<template>
  foo
</template>

however, it parses this an element with the nodename/tagname template but no content (so it isn't a template element, its an element named template... 😬 ):

<svg>
  <template>
    foo
  </template>
</svg>

i suspect this is because a template tag in svg is nonsense as far as the spec is concerned (it is a HTML element, doesn't exist in SVG).

i've worked around it by only using content if it actually has child nodes.

@nolanlawson
Copy link
Collaborator

why doesn't ts-jest figure out ESM?

I think it's that Jest just has problems with Native Node ESM (.mjs/"type": "module"): https://gist.github.com/rstacruz/511f43265de4939f6ca729a3df7b001c

@divmain
Copy link
Contributor

divmain commented Oct 11, 2023

Also, ts-jest doesn't operate on .js files (which is what is found in the published version of @parse5/tools), nor does it operate by default on node_modules. Another way to resolve this may have been to try to configure ts-jest differently, but that wasn't what occurred to me at the time.

@nolanlawson
Copy link
Collaborator

/nucleus test

@nolanlawson nolanlawson merged commit 7883f7d into salesforce:master Oct 11, 2023
11 checks passed
@nolanlawson
Copy link
Collaborator

Thank you again!

@43081j 43081j deleted the parse5-upgrade branch October 12, 2023 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants