web-infra-dev / rspack

The fast Rust-based web bundler with webpack-compatible API 🦀️
https://rspack.dev
MIT License
8.62k stars 506 forks source link

从Webpack迁移过来,编译完成后打开浏览器报 Ucaught ReferenceError: require is not defined #2781

Closed mamba-1024 closed 1 year ago

mamba-1024 commented 1 year ago

System Info

System: OS: macOS 13.0 CPU: (8) arm64 Apple M1 Memory: 1.87 GB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.19.1 - ~/.nvm/versions/node/v16.19.1/bin/node Yarn: 1.22.18 - /opt/homebrew/bin/yarn npm: 8.19.3 - ~/.nvm/versions/node/v16.19.1/bin/npm Browsers: Chrome: 112.0.5615.49 Edge: 112.0.1722.34 Safari: 16.1 npmPackages: @rspack/cli: latest => 0.1.7 @rspack/plugin-minify: ^0.1.7 => 0.1.7

Details

rspack.config.js 配置项如下:

const path = require('path')

module.exports = {
  devServer: {
        hot: true,
        compress: true,
    },
  mode: 'development',
  context: __dirname,
  entry: {
    main: './src/app.jsx',
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/',
  },
  externals: {
    './cptable': 'var cptable',
    "'../' + 'xlsx'": '../xlsx'
  },
  externalsPresets: {
    node: true,
  },
  builtins: {
    html: [{ template: './index.html' }],
    react: {
      development: true,
      refresh: true,
    },
    copy: {
      patterns: [
        {
          from: path.resolve(__dirname, './src/common/editor/ckeditor'),
          to: 'ckeditor',
        },
        { from: path.resolve(__dirname, './src/static'), to: 'static' },
      ],
    },
    pluginImport: [
      {
        libraryName: 'antd',
        style: 'css',
      },
    ],
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        type: 'jsx',
      },
      {
        test: /\.ts$/,
        type: 'tsx',
      },
      {
        test: /\.css$/i,
        type: 'css',
      },
      {
        // css-modules
        test: /\.scss$/i,
        type: 'css/module',
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'sass-loader',
            options: {
              prependData: '@import "styles/_theme.scss";',
            },
          },
        ],
      },
      { test: /.less$/, use: [{ loader: 'less-loader' }], type: 'css' },
      {
        test: /\.(svg|png|jpe?g|gif)$/i,
        type: 'asset',
      },
      {
        test: /\.(woff|woff2|eot|ttf)$/,
        type: 'asset/resource',
      },
    ],
  },
  resolve: {
    alias: {
      src: path.resolve(__dirname, './src'),
      components: path.resolve(__dirname, './src/components'),
      common: path.resolve(__dirname, './src/common'),
      styles: path.resolve(__dirname, './styles'),
      utils: path.resolve(__dirname, './src/utils'),
      static: path.resolve(__dirname, './src/static'),
    },
    modules: ['node_modules'],
  },
}

Reproduce link

No response

Reproduce Steps

  1. rspack serve image

  2. 浏览器打开 image

IWANABETHATGUY commented 1 year ago

Would you mind providing the minimum reproduce demo or repo?

mamba-1024 commented 1 year ago

Would you mind providing the minimum reproduce demo or repo?

demo 仓库地址:

https://github.com/mamba-1024/Rspack-demo

直接clone代码,按照README.md步骤即可

lippzhang commented 1 year ago

@terminus/bricks 这个包是找不到,是你们内部的包吧 ? @mamba-1024

mamba-1024 commented 1 year ago

你们内部的包吧

是的。 我已经把它移除掉。请重新pull一下代码。 @lippzhang

lippzhang commented 1 year ago

https://www.rspack.dev/zh/config/externals.html#node 你的这个配置有问题哦 @mamba-1024

hyf0 commented 1 year ago

我创建了一个最小例子在 https://github.com/hyf0/issue-rspack-2781

events 这个库被 external 掉了,因此在执行到相关语句的时候会使用 node 的 require 去外部导入对应的包,但是 web 环境下不存在 require 这个函数,因此产生了报错。

你可以在最小例子里看到 Rspack 和 Webpack 的输出是一样的,因此这不是一个 Rspack 的 bug。

Rspack

https://github.com/hyf0/issue-rspack-2781/blob/cabe65280d8cb416494cb484790cdeb20f1b2e83/rspack-dist/main.js#L3-L4

Webpack

https://github.com/hyf0/issue-rspack-2781/blob/cabe65280d8cb416494cb484790cdeb20f1b2e83/webpack-dist/main.js#L5-L13

mamba-1024 commented 1 year ago

https://www.rspack.dev/zh/config/externals.html#node 你的这个配置有问题哦 @mamba-1024

@lippzhang 那该如何配置? 在我的demo中可以让代码运行的配置发一下?谢谢