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: consider deep imports in isBuiltIn #5248

Merged
merged 1 commit into from Oct 21, 2021
Merged

fix: consider deep imports in isBuiltIn #5248

merged 1 commit into from Oct 21, 2021

Conversation

ygj6
Copy link
Member

@ygj6 ygj6 commented Oct 11, 2021

Description

see detail: https://github.com/vitejs/vite/pull/5017/files#r725839810

...trying to add a test case
done

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Niputi
Copy link
Contributor

Niputi commented Oct 11, 2021

would this also fix #4037?

@ygj6
Copy link
Member Author

ygj6 commented Oct 11, 2021

@patak-js @Shinigami92 I don’t see e2e tests for other utils functions, so I’m a bit confused whether I need to add test cases

@ygj6
Copy link
Member Author

ygj6 commented Oct 11, 2021

would this also fix #4037?

Yes i think it will

@Shinigami92
Copy link
Member

@patak-js @Shinigami92 I don’t see e2e tests for other utils functions, so I’m a bit confused whether I need to add test cases

As far as I know, maybe we don't have directly tests for the utils, cause they are indirectly covered and tested 🤔

@bluwy
Copy link
Member

bluwy commented Oct 11, 2021

I'm curious though, should we use import { builtinModules } from 'module' instead of the builtin-modules package?

@ygj6
Copy link
Member Author

ygj6 commented Oct 11, 2021

@patak-js @Shinigami92 I don’t see e2e tests for other utils functions, so I’m a bit confused whether I need to add test cases

As far as I know, maybe we don't have directly tests for the utils, cause they are indirectly covered and tested 🤔

OK, I will try another way tomorrow, thx

@ygj6
Copy link
Member Author

ygj6 commented Oct 12, 2021

Some counterintuitive things blocked me. I tried to add a test case to the ssr project, and the module loaded via ssrLoadModule raised the error ReferenceError: __dirname is not defined. Not sure what I am missing, can anyone provide an explanation or help?

Use PR code first, reproduction: https://stackblitz.com/edit/vite-boyvb7?file=entry-server.js

@ygj6 ygj6 added the help wanted Extra attention is needed label Oct 12, 2021
@ygj6 ygj6 force-pushed the dev branch 3 times, most recently from 48e6887 to 86619aa Compare October 14, 2021 03:23
@ygj6
Copy link
Member Author

ygj6 commented Oct 14, 2021

Well, let me explain the current situation🤔

I simply want to add a test case to ssr-vue, try to use readFile(path.resolve(__dirname,'./src/message'),'utf-8') in entry-server.js to read the file, and then encountered the error ReferenceError: __dirname is not defined.

To solve the error, I used readFile(new URL('./src/message', import.meta.url)) to read the file according to the nodejs issue and its docs, but this caused the test to fail.

The only way I can think of is to pass in __dirname to entry-server.js. This works when node version >=14 but fails on version 12 because 'fs/promises' API is available since version 14.

Additionally, I added process.versions.node to conditionally get fs to make the test pass. I’m not sure if this is appropriate, please review.

@ygj6 ygj6 removed the help wanted Extra attention is needed label Oct 14, 2021
@ygj6 ygj6 requested a review from patak-dev October 14, 2021 04:13
@ygj6 ygj6 assigned Shinigami92 and unassigned Shinigami92 Oct 14, 2021
@ygj6 ygj6 requested a review from Shinigami92 October 14, 2021 04:14
@ygj6 ygj6 marked this pull request as ready for review October 14, 2021 04:14
Shinigami92
Shinigami92 previously approved these changes Oct 14, 2021
@ygj6 ygj6 requested a review from antfu October 16, 2021 02:16
@patak-dev
Copy link
Member

@ygj6 could the problem you encountered with import.meta.url be solved now that we merged #5268? Maybe you could rebase to main and see if the workaround is still needed

@ygj6
Copy link
Member Author

ygj6 commented Oct 16, 2021

Oops, the error still exists. For some reasons, the use of new URL is not allowed in the SSR phase.

if (ssr) {
this.error(
`\`new URL(url, import.meta.url)\` is not supported in SSR.`,
index
)
}

I try to avoid it, but it seems infeasible. @patak-js

@patak-dev
Copy link
Member

Ok, thanks for testing!

fix(isBuiltIn): consider the case of deep import
@ygj6
Copy link
Member Author

ygj6 commented Oct 16, 2021

I rebase to executable code. Thanks for your review 👍

@ygj6 ygj6 requested a review from Shinigami92 October 16, 2021 12:36
@patak-dev
Copy link
Member

@bluwy I think we should merge this one, could you also review it?

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

I still think using builtinModules from module is the more robust way (if there isn't any gotchas), but otherwise this technique looks fine too.

@patak-dev
Copy link
Member

Is this something you could show with a suggestion to @ygj6 ?

@patak-dev patak-dev changed the title fix(isBuiltIn): consider the case of deep import fix: consider deep imports in isBuiltIn Oct 21, 2021
@patak-dev patak-dev merged commit 269a1b6 into vitejs:main Oct 21, 2021
sapphi-red added a commit to sapphi-red/vite that referenced this pull request Aug 3, 2022
@sapphi-red sapphi-red mentioned this pull request Aug 3, 2022
9 tasks
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

5 participants