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

Final scripts aliases fail due to trailing slash when required #2730

Closed
alxroyer opened this issue Dec 9, 2022 · 2 comments
Closed

Final scripts aliases fail due to trailing slash when required #2730

alxroyer opened this issue Dec 9, 2022 · 2 comments

Comments

@alxroyer
Copy link

alxroyer commented Dec 9, 2022

Problem

When alias is resolved with a final .js script (1), and a require() call adds a trailing slash after the package name (2), esbuild fails with the following error:

X [ERROR] Could not resolve "(...)\\node_modules\\util\\util.js/" (originally "util/")

    ../../node_modules/assert/assert.js:73:19:
      73 │ var util = require('util/');
         ╵                    ~~~~~~~

  The path "util/" was remapped to "(...)\\node_modules\\util\\util.js/" using the alias feature, which then couldn't be resolved. Keep in mind that import path aliases are resolved in the current working directory.

  You can mark the path "(...)\\node_modules\\util\\util.js/" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.

(1) As node-libs-react-native@1.2.1 does.
(2) As commonjs-assert@1.5.0 does.

Example

Reuse of node-libs-react-native's polyfills in esbuild configuration:

const nodeLibsReactNative = require("node-libs-react-native");
for (const _packageName in nodeLibsReactNative) {
    /** @const {string | null} */ const _packagePath = nodeLibsReactNative[_packageName];
    if (_packagePath) {
        esbuildOptions.alias[_packageName] = _packagePath;
    }
}

Environment: React Native, Android

Workaround

Based on the previous configuration example, I had to fix node-libs-react-native final script paths to package directory paths for a couple of modules:

for (const _packageName in nodeLibsReactNative) {
    (...)
}
const path = require("path");
esbuildOptions.alias["util"] = path.resolve(__dirname, "../../node_modules/util/");
@alxroyer alxroyer changed the title Aliasing fails due to trailing slash when required Final scripts aliases fail due to trailing slash when required Dec 9, 2022
@alxroyer
Copy link
Author

alxroyer commented Dec 9, 2022

Not sure whether this is a bug or just a misuse of the feature.
Maybe it is nonsense to set final script paths with it?

@evanw evanw closed this as completed in 7bf440d Dec 14, 2022
@alxroyer
Copy link
Author

Tested : fixed with esbuild@0.16.7.

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

1 participant