Skip to content

identifier require is not transformed #5498

@xc2

Description

@xc2

System Info


  System:
    OS: macOS 14.3
    CPU: (12) arm64 Apple M2 Max
    Memory: 79.67 MB / 32.00 GB
    Shell: 3.7.0 - /opt/homebrew/bin/fish
  Binaries:
    Node: 20.10.0 - ~/.managed/n/bin/node
    npm: 10.2.3 - ~/.managed/n/bin/npm
    pnpm: 8.14.3 - ~/.managed/n/bin/pnpm
    bun: 1.0.22 - /opt/homebrew/bin/bun
  Browsers:
    Chrome: 120.0.6099.234
    Safari: 17.3
  npmPackages:
    @rspack/cli: nightly => 0.5.2-canary-d22f049-20240128005446 
    @rspack/core: nightly => 0.5.2-canary-d22f049-20240128005446 

Details

original thread: https://discord.com/channels/977448667919286283/1200727907698491444

image

related code:

/*!
    localForage -- Offline Storage, Improved
    Version 1.10.0
    https://localforage.github.io/localForage
    (c) 2013-2017 Mozilla, Apache License 2.0
*/
(function(f) {
        if (typeof exports === "object" && typeof module !== "undefined") {
            module.exports = f()
        } else if (typeof define === "function" && define.amd) {
            define([], f)
        } else {
            var g;
            if (typeof window !== "undefined") {
                g = window
            } else if (typeof global !== "undefined") {
                g = global
            } else if (typeof self !== "undefined") {
                g = self
            } else {
                g = this
            }
            g.localforage = f()
        }
    }
)(function() {
    var define, module, exports;
    return (function e(t, n, r) {
            function s(o, u) {
                if (!n[o]) {
                    if (!t[o]) {
                        var a = typeof require == "function" && require;
                        if (!u && a)
                            return a(o, !0);
                        if (i)
                            return i(o, !0);
                        var f = new Error("Cannot find module '" + o + "'");
                        throw (f.code = "MODULE_NOT_FOUND",
                            f)
                    }
                    var l = n[o] = {
                        exports: {}
                    };
                    t[o][0].call(l.exports, function(e) {
                        var n = t[o][1][e];
                        return s(n ? n : e)
                    }, l, l.exports, e, t, n, r)
                }
                return n[o].exports
            }
// rspack's js output throws next line
            var i = typeof require == "function" && require;
            for (var o = 0; o < r.length; o++)
                s(r[o]);
            return s
        }
    )({
        1: [function(_dereq_, module, exports) {(
            function (global){

Reproduce link

https://github.com/xc2/rspack-repro-require/tree/b7e5fe3dd7ef1d251b7af205d73635b880dda59c

Reproduce Steps

Source

// identifier case 1
const r = typeof require === 'function' && require;
console.log(r('./foo'))

if (typeof require === 'function') {
    // call expression is ok
    console.log(require('./foo'))
}

if (typeof require === 'function') {
    // identifier case 2
    const r = require;
    console.log(r('./foo'))
}

Webpack

const r = undefined;
console.log(__webpack_require__(/*! ./foo */ "./src/foo.js"))

if (true) {
    console.log(__webpack_require__(/*! ./foo */ "./src/foo.js"))
}


if (true) {
    const r = undefined;
    console.log(__webpack_require__(/*! ./foo */ "./src/foo.js"))
}

Rspack

const r =  true && require;
console.log(r('./foo'));
if (true) console.log(__webpack_require__(/*! ./foo */"./src/foo.js"));
if (true) {
    const r = require;
    console.log(r('./foo'));
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingurgentFix build-breaking bugs affecting most users, should be released ASAP (priority)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions