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

injecting "path" doesn't work for me #968

Closed
retorquere opened this issue Mar 13, 2021 · 8 comments
Closed

injecting "path" doesn't work for me #968

retorquere opened this issue Mar 13, 2021 · 8 comments

Comments

@retorquere
Copy link

I use --inject:./path-shim.js in my command line, with path-shim.js being

export let path = {
  join() { return OS.Path.join.apply(null, arguments) }
}

but I'm still getting

 > node_modules/.pnpm/kuromoji@0.1.2/node_modules/kuromoji/src/loader/DictionaryLoader.js:20:19: error: Could not resolve "path" (set platform to "node" when building for node)
    20 │ var path = require("path");
       ╵                    ~~~~~~
@retorquere
Copy link
Author

Oh I just now found #10. I'm in the same boat -- I use modules that require node modules, but my code is guaranteed to never hit those paths.

@retorquere
Copy link
Author

Strangely though, if I use this entry:

// entry.js
console.log(path.join('x', 'y'))

it does get shimmed in.

@zaydek
Copy link

zaydek commented Mar 13, 2021

@retorquere Can you show what your path-shim.js looks like? And also what your full build command looks like?

@retorquere
Copy link
Author

It's just

export let path = {
  join: function() { return OS.Path.join.apply(null, arguments) }
}

I rewrote the shim to more closely match the sample in the docs but it made no difference.

Full build command is

rm -rf build/dist && esbuild content/BetterBibTeX.ts content/ExportOptions.ts content/ItemPane.ts content/Preferences.ts content/ZoteroPane.ts content/ErrorReport.ts content/FirstRun.ts content/KeyManager.ts content/TestSupport.ts 'content/better-bibtex.ts' --inject:./path-shim.js --splitting --keep-names --bundle --outdir=build/dist --format=esm

@evanw
Copy link
Owner

evanw commented Mar 13, 2021

The inject feature is a way to shim unbound global variable references within a file. It has nothing to do with package path resolution. In this case the inject feature is not doing anything because var path = require('path') defines a local variable, so references to path in that file are not unbound global variable references.

If you want to substitute the path package path for something else, one way of doing this is to use an on-resolve plugin to redirect the path package path to some other path: https://esbuild.github.io/plugins/#resolve-callbacks.

@retorquere
Copy link
Author

Aha! So in order to shim path I just resolve to a file with

export function join() {
  return OS.Path.join.apply(null, arguments)
}

export function dirname(filename) {
  return OS.Path.dirname(filename)
}

export function resolve(path) {
  throw 'not implemented'
}

export function extname(filename) {
  return filename.includes('.') ? ('.' + filename.split('.').pop()) : ''
}

pretty sweet!

@retorquere
Copy link
Author

The documentation with the JS samples is stellar BTW.

@evanw
Copy link
Owner

evanw commented Mar 14, 2021

Good to hear. Ok I'm going to close this then since it sounds like this line of investigation has concluded.

@evanw evanw closed this as completed Mar 14, 2021
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

No branches or pull requests

3 participants