electron - File path ignoring variable value during resolution with webpack - Stack Overflow

admin2025-04-10  1

I have a strange problem that have not been able to solve after hours of troubleshooting.

The path to a file created from a variable is resolving to the wrong path/file.

The code below is the code that sets the path

let libDir = ''
let libName = ''
  
switch (os.platform()) {
  case 'darwin':         
    libDir = 'osx-x64'
    libName = 'librtiddsconnector.dylib'
    break

  case 'linux':
    libDir = 'linux-x64'
    libName = 'librtiddsconnector.so'
    break

  // Windows returns win32 even on 64-bit platforms
  case 'win32':
    libDir = 'win-x64'
    libName = 'rtiddsconnector.dll'
    isWindows = true
    break

  default:
    throw new Error(os.platform() + ' not yet supported')
}

this.library = path.join(__dirname, '/rticonnextdds-connector/lib/', libDir, '/', libName)

On Windows everything works correctly.

On Linux, webpack sets this.library to rtiddsconnector.dll even though libName is librtiddsconnector.so and libDir is linux-x64

The resulting bundled code on both Linux and Windows

this.library = __webpack_require__.ab + "rtiddsconnector.dll"

The webpack configuration

export const mainConfig: Configuration = {
 entry: './src/index.ts',
  module: {
    rules: [
      // Add support for native node modules
      {
        // We're specifying native_modules in the test because the asset relocator loader generates a
        // "fake" .node file which is really a cjs file.
        test: /native_modules[/\\].+\.node$/,
        use: 'node-loader',
      },
      {
        test: /[/\\]node_modules[/\\].+\.(m?js|node)$/,
        parser: { amd: false },
        use: {
          loader: '@vercel/webpack-asset-relocator-loader',
          options: {
            outputAssetBase: 'native_modules',
          },
        },
      },
      {
        test: /\.tsx?$/,
        exclude: /(node_modules|\.webpack)/,
        use: {
          loader: 'ts-loader',
          options: {
            transpileOnly: true,
          },
        },
      },
      {
        test: /\.xml/,
        type: "asset/resource",
      },
    ]
  },
  plugins: [
    new ForkTsCheckerWebpackPlugin({
      logger: 'webpack-infrastructure',
    }),
  ],
  resolve: {
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json'],
  },
};

The project is the electron fe webpack-typescript template with the RTI connector dependency RTI Connector for Javascript.

Is there a problem with the webpack configuration?

I am using webpack 5 and electronfe 7.7.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744257491a238386.html

最新回复(0)