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

Fix a component namespace and name parsing issue #5117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Mr-VincentW
Copy link

@Mr-VincentW Mr-VincentW commented Jan 9, 2025

As per issue #5113 :

The original approach cannot correctly parse namespace and name from file path in Windows as the path may contain \ as delimeters.

It doesn't work well for relative paths start with ./ either which derives from implicit reference to CSS files from HTML templates.

The fix still cannot extract the namespace for relative paths since there's only a ./. However, it does correctly identify the name for the component.

Details

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.

The original approach cannot correctly parse `namespace` and `name` from file path in Windows as the path may contain `\` as delimeters.

It doesn't work well for relative paths start with './' either which derives from implicit reference to CSS files.
@Mr-VincentW Mr-VincentW requested a review from a team as a code owner January 9, 2025 01:00
Copy link

salesforce-cla bot commented Jan 9, 2025

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

Copy link
Contributor

@cardoso cardoso left a comment

Choose a reason for hiding this comment

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

Thank you for looking into this!

In the original issue you mentioned:

Using /[\/]/ here solves the problem for me.

Can you simplify this PR to fix just that and provide a minimal repo that demonstrates this issue?

I gather you're trying to solve what you also mentioned:

It doesn't work well for relative paths start with ./ either which derives from implicit reference to CSS files from HTML templates.

I think this should be done as a separate PR once this one is merged.

--

PS: I'm only a contributor, not a maintainer, so this is just my advice based on a past windows-related issue and how tricky it can be to diagnose, validate the fix and ensure no breaking changes:

cardoso/vite-plugin-lwc#1 -> danjunger/vite-repro#5 -> #4825 (comment) -> #5038

@@ -332,7 +332,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
const [namespace, name] =
// Note we do not need to use path.sep here because this filename contains
// a '/' regardless of Windows vs Unix, since it comes from the Rollup `id`
specifier?.split('/') ?? path.dirname(filename).split('/').slice(-2);
specifier?.split('/') ?? /(?:([^\\/]+?)[\\/])?([^\\/]+?)(?:[\\/]\2)?(?:\.scoped)?\.[^.]+$/.exec(filename)?.slice(1);
Copy link
Contributor

@cardoso cardoso Jan 9, 2025

Choose a reason for hiding this comment

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

I don't think using a regex here is the right approach.

However, in my case (on Windows), I found them cometimes containing \ as well.

Are you able to debug what specifier and filename are here in this case you mentioned?

Ideally this line remains unchanged, and the filename is normalized via simple String.replace previous to it.

The assumption is that rollup already does this normalization, which is why providing a log of the variables as well a minimal repo would be needed to figure out the proper fix.

Copy link
Author

@Mr-VincentW Mr-VincentW Jan 9, 2025

Choose a reason for hiding this comment

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

I've updated my fix to only fix the delimiter issue.

BTW, rollup does normalize the ids but not the importer, and the LWC rollup plugin has a custom resolveId hook in the plugin to resolves ids based on both importee (id) and importer, which is where the issue derives.

Copy link
Author

Choose a reason for hiding this comment

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

Here's a screenshot of what the specifire and filename look like:
image

The specifier is always null for "directory modules" as it's parsed from a URL search param which is only appended to "alias modules" by the resolveId hook.

Simplified the fix as only having replaced the path delimiter for parsing `namespace` and `name` from filenames.
@Mr-VincentW
Copy link
Author

Can you simplify this PR to fix just that and provide a minimal repo that demonstrates this issue?

Hey, thank you for the feedback. I've simplified my fix and created a demo repo to showcase the issue.

Now the fix only solves the path delimiter issue but doesn't handle the relative path problem.

Cheers.

@cardoso
Copy link
Contributor

cardoso commented Jan 10, 2025

@Mr-VincentW thanks a lot for the demo repo!

Does the error still happen if you remove the html plugin and create the index.html file yourself? Example.

If the error still happens, I suggest further simplifying the repo by removing the html plugin. If the error goes away, we need to understand why and figure out the best way to deal with it.

@Mr-VincentW
Copy link
Author

Mr-VincentW commented Jan 10, 2025

@cardoso Yes, the issue continues.

I tend not to remove the plugin in the repo, or you'll need to either copy the index.html from src to dist or create one there (which is what the html plugin does) in order to run the demo.
(You could run it likely because you've already got one there, the playground repo as your example won't be able to run if you clear the dist folder.)

Anyway, it has nothing to do with the html. No matter if the demo can run or not in the browser, the issue happens when the package is built. Also, I have already explained the reason why this issue happens here.

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.

2 participants