Code Monkey home page Code Monkey logo

Comments (1)

undecaf avatar undecaf commented on June 9, 2024 3

This issue results from the combination of a few peculiarities:

  1. The file zbar.wasm is loaded by a loader created by Emscripten. Even if created as an ES6 module, this loader references __dirname and uses require('fs') and require('path'). This is a known issue.

  2. For pre-rendering, Next.js evaluates React components also in a Node context .

  3. @undecaf/zbar-wasm requires the file zbar.wasm to be located in the same directory as the zbar-wasm JS code.

Solution:

  • Instead of importing, require(@undecaf/zbar-wasm) in Next.js React components:

    import type { NextPage } from 'next'
    // Avoid importing from '@undecaf/zbar-wasm'
    const { scanImageData } = require('@undecaf/zbar-wasm')
    
    const Home: NextPage = () => {
      const scan = (image: ImageData) => {
        return scanImageData(image)
      }
      return (
        <div>Hello!</div>
      )
    }
    
    export default Home

    This covers 1. and 2. in Node context.

  • Provide stubs for fs and path in browser context. This can be done by adding a custom Webpack config in next.config.js:

    /** @type {import('next').NextConfig} */
    const nextConfig = {
      reactStrictMode: true,
      swcMinify: true,
    
      webpack(config, { isServer }) {
        if (!isServer) {
          // Resolve node modules that are irrelevant for the client
          config.resolve.fallback = {
            ...config.resolve.fallback,
    
            fs: false,
            path: false,
          }
        }
    
        return config
      },
    }
    
    module.exports = nextConfig

    This covers 1. in browser context.

  • Have Webpack copy zbar.wasm to where the chunk containing the zbar-wasm JS code resides. This requires the copy-webpack-plugin:

    yarn add -D copy-webpack-plugin
    

    Then we need to extend next.config.js like so:

    const CopyPlugin = require('copy-webpack-plugin')
    
    /** @type {import('next').NextConfig} */
    const nextConfig = {
      reactStrictMode: true,
      swcMinify: true,
    
      webpack(config, { isServer }) {
        // Copy zbar.wasm to where the chunk containing zbar-wasm is located
        config.plugins.push(new CopyPlugin({
          patterns: [
            {
              // In this project, zbar-wasm becomes part of static/chunks/pages/_app.js
              from: 'node_modules/@undecaf/zbar-wasm/dist/zbar.wasm',
              to: 'static/chunks/pages/'
            },
          ],
        }))
    
        if (!isServer) {
          // Resolve node modules that are irrelevant for the client
          config.resolve.fallback = {
            ...config.resolve.fallback,
    
            fs: false,
            path: false,
          }
        }
    
        return config
      },
    }
    
    module.exports = nextConfig

    This covers 3.

I made a pull request to https://github.com/Loskir/zbar-wasm-nextjs that contains all these changes and fixes this issue.

from zbar-wasm.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.