tachibana-shin / vite-plugin-arraybuffer

Vite plugin for import file with ArrayBuffer or Uint8Array!
https://www.npmjs.com/package/vite-plugin-arraybuffer
14 stars 1 forks source link

version 0.0.4 fails with Remix #5

Closed kevlened closed 8 months ago

kevlened commented 8 months ago

Remix now uses Vite. When using this plugin in the Vite build, it fails with:

Error: [vite:load-fallback] Could not load /react/jsx-runtime (imported by app/routes/test/route.tsx): ENOENT: no such file or directory, open '/react/jsx-runtime'

This doesn't happen on version 0.0.2

codeautopilot[bot] commented 8 months ago

Potential solution

The solution involves ensuring that the vitePluginArraybuffer does not interfere with Vite's default module resolution, particularly for modules like /react/jsx-runtime. Since the error message indicates a problem with module resolution, we need to verify that the plugin is not mistakenly capturing or altering requests for the React JSX runtime module. We should also check for any updates to Vite's plugin API that could affect how plugins should handle module resolution, especially with the introduction of Vite in Remix.

What is causing this bug?

The bug is likely caused by a conflict between the vitePluginArraybuffer and Vite's module resolution mechanism. Since the error occurs when trying to load /react/jsx-runtime, it's possible that the plugin is either incorrectly handling the resolution of this module or that changes in Vite's API or Remix's integration with Vite have introduced new requirements for plugin compatibility that the vitePluginArraybuffer does not yet meet.

Code

To address the issue, we need to review and potentially update the resolveId and transform functions in src/main.ts. We should add checks to ensure that the plugin does not interfere with the resolution of modules that it is not intended to handle, such as /react/jsx-runtime.

// src/main.ts
export default function vitePluginArraybuffer() {
  return {
    name: 'vite-plugin-arraybuffer',
    resolveId(source, importer, options) {
      // Add a check to ensure that the plugin does not interfere with React JSX runtime resolution
      if (source === '/react/jsx-runtime') {
        return null; // This tells Vite to handle the resolution as it normally would
      }
      // Existing logic for handling '?arraybuffer' and '?uint8array' goes here...
    },
    transform(code, id) {
      // Existing logic for transforming matched files goes here...
    },
    // Additional plugin hooks or configuration may be added here if necessary
  };
}

How to replicate the bug

To replicate the bug, set up a Remix project that uses Vite as the build tool. Then, install the plugin version 0.0.4 and attempt to build the project. The error should occur during the build process, specifically when Vite tries to load the /react/jsx-runtime module.

Task

The task is to ensure that the vitePluginArraybuffer is compatible with the latest version of Vite used by Remix and does not interfere with the resolution of modules it is not designed to handle. This involves reviewing and updating the plugin's resolveId and transform functions to respect Vite's default module resolution process, especially for critical modules like the React JSX runtime.

Click here to create a Pull Request with the proposed solution

Files used for this task:

Changes on vite.config.ts The `vite.config.ts` file is quite minimal and doesn't contain much logic. It imports the `defineConfig` function from Vite and the `vitePluginArraybuffer` from the local `src/main.ts` file. It then exports the default configuration for Vite, which includes the `vitePluginArraybuffer` plugin in the plugins array. Given the issue described, the problem doesn't seem to be directly related to the configuration in this file, as it doesn't specify any custom resolve options or JSX-specific configurations. The error message indicates that there is an issue with resolving the `/react/jsx-runtime` import, which is likely related to how the `vitePluginArraybuffer` plugin is handling module resolution or transformation. Since this file doesn't contain any specific configuration related to module resolution or JSX handling, it's unlikely that the bug is directly caused by anything in `vite.config.ts`. The issue is more likely to be found in the `src/main.ts` file, where the `vitePluginArraybuffer` plugin is implemented. That's where the `resolveId` and `transform` functions would be defined, which are responsible for resolving module paths and transforming code during the build process. In summary, there's nothing in `vite.config.ts` that stands out as a cause for the reported bug. The investigation should focus on the `src/main.ts` file, where the logic for the plugin is defined.
Changes on src/main.ts Upon reviewing the `src/main.ts` file, which defines the `vitePluginArraybuffer` plugin for Vite, there are a couple of things to note: 1. The `resolveId` function is designed to handle module IDs that end with either `?arraybuffer` or `?uint8array`. It does not seem to have any logic related to resolving React's JSX runtime or any other modules. 2. The `transform` function is responsible for reading files that match the conditions checked in `resolveId` and converting them into JavaScript code that exports either an ArrayBuffer or a Uint8Array. 3. There is no explicit handling or resolution of the `/react/jsx-runtime` module, which is the cause of the error reported by the user. Given the error message `Error: [vite:load-fallback] Could not load /react/jsx-runtime (imported by app/routes/test/route.tsx): ENOENT: no such file or directory, open '/react/jsx-runtime'`, it seems that the issue is related to the resolution of the React JSX runtime module, which is not being handled by this plugin. Since the plugin is not designed to handle React JSX runtime resolution, the issue might not be directly related to the code in this file. However, if the plugin is somehow interfering with the normal resolution of modules in Vite, especially after the update to Vite that integrates with Remix, it might be necessary to ensure that the plugin is not causing any conflicts. To ensure compatibility with Vite's handling of React's JSX runtime, the following steps could be taken: - Verify that the plugin is not interfering with Vite's default module resolution, especially for modules like `/react/jsx-runtime`. - Ensure that the plugin is compatible with the latest version of Vite used by Remix, which might have different expectations for plugins. - Check if there are any updates or changes in Vite's plugin API that need to be reflected in this plugin. Since the plugin's current functionality does not seem to be related to the issue, it is possible that the problem lies elsewhere, such as in the Vite configuration or another plugin that is responsible for handling React-related modules. The `vite.config.ts` file or other parts of the build setup might need to be reviewed to find the root cause of the issue.
kevlened commented 8 months ago

The autopilot doesn't offer a good solution. Ideally, this plugin is inert if it's not transforming a file. Returning false isn't inert in resolveId.

Returning false signals that source should be treated as an external module and not included in the bundle source

codeautopilot[bot] commented 8 months ago

The autopilot doesn't offer a good solution. Ideally, this plugin is inert if it's not transforming a file. Returning false isn't inert in resolveId.

Returning false signals that source should be treated as an external module and not included in the bundle source

Thank you for pointing that out. You're correct that returning false from resolveId would treat the module as external, which is not the intended behavior. Instead, the plugin should return null or undefined to indicate that it doesn't want to handle the resolution of the module, allowing Vite to continue its normal resolution process.

Here's the updated resolveId function:

// src/main.ts
export default function vitePluginArraybuffer() {
  return {
    name: 'vite-plugin-arraybuffer',
    resolveId(source, importer, options) {
      // Check if the source ends with '?arraybuffer' or '?uint8array'
      if (source.endsWith('?arraybuffer') || source.endsWith('?uint8array')) {
        // Handle the resolution for these specific cases
        // Existing logic for handling '?arraybuffer' and '?uint8array' goes here...
      } else {
        // For all other cases, do not interfere with the resolution
        return null; // This tells Vite to handle the resolution as it normally would
      }
    },
    transform(code, id) {
      // Existing logic for transforming matched files goes here...
    },
    // Additional plugin hooks or configuration may be added here if necessary
  };
}

This change should ensure that the plugin is inert for any imports that do not match its specific use case, allowing Vite to resolve /react/jsx-runtime and other modules correctly.

tachibana-shin commented 8 months ago

Does version 0.0.5 solve your problem?

kevlened commented 8 months ago

It does! Thanks