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

Bad umd2esm plugin in build.js script #4

Closed
arthur-fontaine opened this issue Oct 11, 2023 · 1 comment
Closed

Bad umd2esm plugin in build.js script #4

arthur-fontaine opened this issue Oct 11, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@arthur-fontaine
Copy link

arthur-fontaine commented Oct 11, 2023

Description

build.onResolve({ filter: /^(vscode-.*|estree-walker|jsonc-parser)/ }, args => {
const pathUmdMay = require.resolve(args.path, { paths: [args.resolveDir] })
// Call twice the replace is to solve the problem of the path in Windows
const pathEsm = pathUmdMay.replace('/umd/', '/esm/').replace('\\umd\\', '\\esm\\')
return { path: pathEsm }
})

The code above will convert umd imports to esm by replacing "umd" with "esm" in the filepath.

However, the path generated no longer work for vscode-uri as of 3.0.8.
The path for umd import is "lib/umd/index.js", but the path for esm import is "lib/esm/index.mjs". As the script replaces "lib/umd/index.js" with "lib/esm/index.js", esbuild fails to resolve the esm import.

Reference to the commit that changes the export path for esm: microsoft/vscode-uri@53e4ca6#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R9

See the error my Github Action that builds my VSCode extension : https://github.com/bywhitebird/kazam/actions/runs/6483646531/job/17608453201#step:5:218

Steps to Reproduce

Use vscode-uri>=3.0.8.

Solutions

I managed to build my VSCode extension with this code:

build.onResolve({ filter: /^(vscode-.*|estree-walker|jsonc-parser)/ }, (args) => {
  const pathUmdMay = require.resolve(args.path, { paths: [args.resolveDir] })
  // Call twice the replace is to solve the problem of the path in Windows
  let pathEsm = pathUmdMay.replace('/umd/', '/esm/').replace('\\umd\\', '\\esm\\')

  // Try to resolve the path
  // If it fails, if the file extension is .js, try to replace it with .mjs
  try {
    require.resolve(pathEsm, { paths: [args.resolveDir] })
  }
  catch {
    if (pathEsm.endsWith('.js'))
      pathEsm = pathEsm.replace(/\.js$/, '.mjs')
  }

  return { path: pathEsm }
})

This code tries to resolve the new path. If it fails, it tries to resolve the import by replacing .js with .mjs.

It works, but I think it is still not robust enough. Another solution might be considered.

@johnsoncodehk johnsoncodehk added the bug Something isn't working label Oct 12, 2023
@johnsoncodehk
Copy link
Member

johnsoncodehk commented Oct 12, 2023

Thanks for the report! Thanks to @remcohaszing efforts at microsoft/vscode#192144, some vscode packages have fixed the module format, we fixed the build script in other repo, but forgot to fix the starter repo, will update it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants