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

[Bug]: legalComments issue - not executable code if we retain comments #6366

Closed
Tsymalyi opened this issue Apr 24, 2024 · 3 comments
Closed
Assignees

Comments

@Tsymalyi
Copy link

Tsymalyi commented Apr 24, 2024

Version

System:
    OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-11300H @ 3.10GHz
    Memory: 1.92 GB / 15.31 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Browsers:
    Chrome: 121.0.6167.139
    Chromium: 124.0.6367.60
    Firefox: 124.0.2
  npmPackages:
    @rsbuild/core: ^0.6.6 => 0.6.6 
    @rsbuild/plugin-react: ^0.6.6 => 0.6.6

Details

In production mode we have not exucutable code for libraries with comments.
lib-lodash.js example without legalComments property

... /*! ./_stackHas */.nodeType&&t,i=u&&u.exports===o?e.Buffer:void ....

Error in browser:
errorLodash

It can be fixed by setting legalComments ='none'
In this case we have executable code with webpack wrapper
correct

rsbuild.config.js

const isProd = process.env.NODE_ENV === 'production';
const devDefaultFilename = {
  js: '[name].js',
  css: '[name].css',
  svg: '[name].[contenthash:8].svg',
  font: '[name].[contenthash:8][ext]',
  image: '[name].[contenthash:8][ext]',
  media: '[name].[contenthash:8][ext]',
};
// Production files names
const prodDefaultFilename = {
  js: '[name].js?[contenthash:8]',
  css: '[name].css?[contenthash:8]',
  svg: '[name].svg?[contenthash:8]',
  font: '[name].[ext]?[contenthash:8]',
  image: '[name].[ext]?[contenthash:8]',
  media: '[name].[ext]?[contenthash:8]',
};
export default defineConfig({
  dev: {
    hmr: true,
    cache: false,
    setupMiddlewares: isProd
      ? []
      : [
          (middleware) => {
            middleware.unshift(getSeverInstance());
          },
        ],
  },
  server: {
    port: process.env.PORT || 3000,
  },
  performance: {
    buildCache: false,
  },
  plugins: [pluginReact()],
  tools: {
    swc: {
      jsc: {
        experimental: {
          plugins: [['swc-plugin-jsx-control-statements', {}]],
        },
      },
    },
    rspack(config, { appendPlugins }) {
      // Only register the plugin when RSDOCTOR is true, as the plugin will increase the build time.
      if (process.env.RSDOCTOR === 'true') {
        appendPlugins(
          new RsdoctorRspackPlugin({
            // plugin options
          })
        );
      }
    },
  },
  html: {
    template: './public/index.html',
  },
  output: {
    sourceMap: {
      js: 'source-map',
    },
    cleanDistPath: true,
    charset: 'utf8',
    distPath: {
      root: 'build',
    },
    dataUriLimit: 0,
    filename: isProd ? prodDefaultFilename : devDefaultFilename,
  },
});

Reproduce link

Don't have

Reproduce Steps

  1. Add lodash to your project
    "lodash": "^4.17.21"
  2. Run rsbuild build
@chenjiahan
Copy link
Member

This seems like a bug of Rspack when using ?[contenthash:8] in the filename

@chenjiahan chenjiahan transferred this issue from web-infra-dev/rsbuild Apr 25, 2024
@chenjiahan
Copy link
Member

chenjiahan commented Apr 25, 2024

A minimal reproduction repo: https://github.com/chenjiahan/rsbuild-repro-invalid-legal-comments

Steps:

  1. pnpm i
  2. pnpm build

Open dist/static/js/index.js, there are three possible random results:

  1. empty file:

image

  1. invalid comments:

image

  1. correct comments:

image

@LingyuCoder LingyuCoder self-assigned this Apr 28, 2024
@LingyuCoder
Copy link
Contributor

LingyuCoder commented Apr 28, 2024

There are three part of this issue:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants