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

[Windows] Path parts in strings replaced with undefined #161

Open
jamesplease opened this issue Jun 1, 2022 · 7 comments
Open

[Windows] Path parts in strings replaced with undefined #161

jamesplease opened this issue Jun 1, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@jamesplease
Copy link

jamesplease commented Jun 1, 2022

OS: Windows 11
webpack-asset-relocator-loader version: 1.7.2
Node version: 16.15.0

Configuration

 {
    test: /\.(m?js|node)$/,
    parser: { amd: false },
    use: {
      loader: '@vercel/webpack-asset-relocator-loader',
      options: {
        outputAssetBase: 'native_modules',
      },
    },
  },

What is the behavior that I'm seeing

Any string containing paths to the project has that pieceof the string replaced with undefined.

What I would expect to see

Path parts not replaced with undefined.

Example

In this example, the project is located at C:\Users\example-user\web\example-project, and a file exists at .\src\some-path\some-file.txt

var path = 'C:\\Users\\example-user\\web\\example-project\\src\\some-path\\some-file.txt';
console.log(path);
// => undefinedsome-file.txt

More Information

This is an Electron Forge application. The problem has been narrowed down to this plugin (removing the plugin fixes the issue)

@jamesplease jamesplease changed the title Returns undefined on Windows Replaces path parts with undefined on Windows Jun 1, 2022
@jamesplease jamesplease changed the title Replaces path parts with undefined on Windows [Windows] Path parts in strings replaced with undefined Jun 1, 2022
@styfle styfle added the bug Something isn't working label Jun 2, 2022
@TuringMachinegun
Copy link

TuringMachinegun commented Jun 9, 2022

I am observing the same problem on Windows 11, using Electron Forge. Same code works fine on Linux. I also tried with an older node version (14.19.2), and an older version of this repo (1.6.0), but no luck.

On my side, it seems like __webpack_require__.ab is left undefined in the generated code for requiring the native module:

/***/ "./src/<a subproject>/build/Release/<subproject_module>.node":
/*!***************************************************************************!*\
  !*** ./src/<a subproject>/build/Release/<subproject_module>.node ***!
  \***************************************************************************/
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

module.exports = require(__webpack_require__.ab + "<subproject_module>.node")

/***/ }),

If I manually substitute __webpack_require__.ab with the native_modules folder (in which the relocator correctly relocated the native module), it works again.

I'd be grateful for any workaround or suggestions, as the manual approach doesn't really map out when packaging for production, as webpack can't then apply its path substitution, and it seems like the relative path import trick I use in development doesn't work in the packaged file.

I'm also available to help tracing down this bug.

More info:

@jamesplease
Copy link
Author

This comment solved the problem for me:

electron/forge#575 (comment)

tl;dr: forge’s config can’t handle every type of native module. Some need to be marked as external.

If you try to get the code in that issue working it may work for you as well.

@TuringMachinegun
Copy link

Thanks @jamesplease, that did the trick. It only works in development mode though (for me), but using https://github.com/timfish/forge-externals-plugin solved that and now it also packages correctly.

@jamesplease
Copy link
Author

I also had to add the externals plugin for builds. Glad you got it working!

@sibelius
Copy link

is there a workaround for this ?

@alfredomariamilano
Copy link

This is still an issue that should be easily fixable. I'm trying to debug where it all goes wrong.

@alfredomariamilano
Copy link

alfredomariamilano commented Apr 10, 2024

This is still an issue that should be easily fixable. I'm trying to debug where it all goes wrong.

I still cannot pinpoint exactly where the plugin is failing, but if I import a native node module in the webpack entry, instead of importing from another file, then the __webpack_require__.ab snippet is created.

EDIT: caching the webpack build results in the same error, which means it's a problem with checking if the file requesting the .node file is also the entry of webpack, since when caching, the entry would probably be different, from the cache.

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

5 participants