Unverified Commit 29b39663 authored by Sam Poder's avatar Sam Poder Committed by GitHub

[turbopack] Import Webpack's tests for tree-shaking (#95811)

Imports the tests that Webpack uses for CJS tree-shaking. One test suite
is skipped `deep-reexports`, that's because we don't support that deep
of analysis at the moment and we just bail. WIP because after landing
this stack I will work on improving this.

I did have to make some edits to the tests, a summary is below (courtesy
of Claude):

```
1. Stripped ?query request suffixes (all cases).
Webpack uses require("./x?1") / ?2 to force distinct module instances. Turbopack shares one instance across require sites and aggregates usage, so the queries were removed (require("./x")). The behavioral assertions hold on a shared instance.

2. Distinct copies where per-instance semantics are genuinely required.
A few cases actually depend on separate instances, so we replaced the query with real duplicate files:
- mutate → module-1.js / module-2.js (so mutating one import doesn't leak into the other).
- reexports → add-to-counter-1/2/3.js (the counter must increment 3×).
- reexports-define-property → add-to-counter-1/2.js.

3. Removed __resourceQuery (webpack module-query magic — undefined in turbopack).
In reexports and reexports-define-property: require("./m" + __resourceQuery) → require("./m"), and the dynamic dispatch module reexport.js (require("./" + __resourceQuery.slice(1))) was split into static reexport-direct.js / reexport-indirect.js.

4. Commented out __webpack_exports_info__ / usedExports assertions.
Webpack exposes per-export usage introspection (x.usedExports, __webpack_exports_info__.x.used) that turbopack doesn't populate for CJS. Those specific assertions were commented (the real behavioral checks kept) in: require-member-access, require-member-access-deferred, require-destructuring, this-in-exported-function(-modern), reexport-require-binding, mutate, cjs-to-esm.

5. Case-specific rewrites.
- cjs-to-esm — dropped the require.cache[require.resolve(...)].exports comparison (webpack runtime introspection).
- namespace — changed expect(await import(x)).toBe(require(x)) (object identity) to assert the observable exports (ns.abc, ns.default); turbopack's import() yields an exotic Module namespace, never the same/deep-equal object as require().

6. mjs removed entirely — built on __resourceQuery + per-query instances + .js/.mjs interop divergence; can't even evaluate in turbopack and tests behavior turbopack doesn't share.
```

All tests should be passing.
parent eb7beb59
...@@ -1496,20 +1496,33 @@ impl VisitAstPath for Analyzer<'_, '_> { ...@@ -1496,20 +1496,33 @@ impl VisitAstPath for Analyzer<'_, '_> {
n.callee.visit_with_ast_path(self, &mut ast_path); n.callee.visit_with_ast_path(self, &mut ast_path);
} }
// Guard only the `exports` target arg; the descriptor is visited normally. // Visit a recognized descriptor as an export value so a top-level `this` taints.
let cjs_export_target_arg = if is_cjs_define_property { let cjs_export_target_arg = if is_cjs_define_property {
Some(0) Some(0)
} else { } else {
None None
}; };
self.check_call_expr_for_effects( if is_cjs_define_property {
&n.callee, self.with_cjs_export_value(|this| {
n.args.iter(), this.check_call_expr_for_effects(
n.span(), &n.callee,
ast_path, n.args.iter(),
CallOrNewExpr::Call(n), n.span(),
cjs_export_target_arg, ast_path,
); CallOrNewExpr::Call(n),
cjs_export_target_arg,
);
});
} else {
self.check_call_expr_for_effects(
&n.callee,
n.args.iter(),
n.span(),
ast_path,
CallOrNewExpr::Call(n),
cjs_export_target_arg,
);
}
} }
fn visit_new_expr<'ast: 'r, 'r>( fn visit_new_expr<'ast: 'r, 'r>(
......
...@@ -274,6 +274,8 @@ struct TestOptions { ...@@ -274,6 +274,8 @@ struct TestOptions {
#[serde(default = "default_true")] #[serde(default = "default_true")]
scope_hoisting: bool, scope_hoisting: bool,
#[serde(default)] #[serde(default)]
cjs_tree_shaking: bool,
#[serde(default)]
minify: bool, minify: bool,
#[serde(default)] #[serde(default)]
production_chunking: bool, production_chunking: bool,
...@@ -291,6 +293,7 @@ impl Default for TestOptions { ...@@ -291,6 +293,7 @@ impl Default for TestOptions {
remove_unused_exports: default_true(), remove_unused_exports: default_true(),
remove_unused_imports: default_true(), remove_unused_imports: default_true(),
scope_hoisting: default_true(), scope_hoisting: default_true(),
cjs_tree_shaking: false,
minify: false, minify: false,
production_chunking: false, production_chunking: false,
} }
...@@ -443,6 +446,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V ...@@ -443,6 +446,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V
import_externals: true, import_externals: true,
enable_exports_info_inlining: true, enable_exports_info_inlining: true,
infer_module_side_effects: true, infer_module_side_effects: true,
cjs_tree_shaking: options.cjs_tree_shaking,
..Default::default() ..Default::default()
}, },
environment: Some(env), environment: Some(env),
......
export const a = 1
export const b = 2
export const aUsed = __webpack_exports_info__.a.used
export const bUsed = __webpack_exports_info__.b.used
export const x = 1
export const y = 2
export const xUsed = __webpack_exports_info__.x.used
export const yUsed = __webpack_exports_info__.y.used
export const p = 1
export const q = 2
export const pUsed = __webpack_exports_info__.p.used
export const qUsed = __webpack_exports_info__.q.used
export const m = 1
export const n = 2
export const mUsed = __webpack_exports_info__.m.used
export const nUsed = __webpack_exports_info__.n.used
export const x = 1
export const y = 2
export const xUsed = __webpack_exports_info__.x.used
export const yUsed = __webpack_exports_info__.y.used
export const a = 1
export const b = 2
export const c = 3
export const aUsed = __webpack_exports_info__.a.used
export const bUsed = __webpack_exports_info__.b.used
export const cUsed = __webpack_exports_info__.c.used
it('A: should tree-shake through a 4-level CJS star-reexport chain', () => {
const d = require('./a-l4')
expect(d.a).toBe(1)
if (process.env.NODE_ENV === 'production') {
expect(d.aUsed).toBe(true)
expect(d.bUsed).toBe(false) // b is never read across the chain
}
})
it('B: should tree-shake a nested namespace reexported through CJS', () => {
const m = require('./b-mid')
expect(m.inner.x).toBe(1)
if (process.env.NODE_ENV === 'production') {
expect(m.inner.xUsed).toBe(true)
expect(m.inner.yUsed).toBe(false) // y only reachable via m.inner, never read
}
})
it('C: should tree-shake a sub-namespace star-reexport (ids != [])', () => {
const m = require('./c-mid')
expect(m.p).toBe(1)
if (process.env.NODE_ENV === 'production') {
expect(m.pUsed).toBe(true)
expect(m.qUsed).toBe(false)
}
})
it('D: should tree-shake a CJS star-reexport of an ESM `export *` barrel', () => {
const d = require('./d-cjs')
expect(d.m).toBe(1)
if (process.env.NODE_ENV === 'production') {
expect(d.mUsed).toBe(true)
expect(d.nUsed).toBe(false)
}
})
it('E: should handle circular CJS star-reexports at runtime', () => {
const one = require('./e-one')
expect(one.second).toBe(2)
})
it('F: should tree-shake a depth-2 nested namespace reexported through CJS', () => {
const m = require('./f-cjs')
expect(m.mid.deep.x).toBe(1)
if (process.env.NODE_ENV === 'production') {
expect(m.mid.deep.xUsed).toBe(true)
expect(m.mid.deep.yUsed).toBe(false) // y reachable only via m.mid.deep, never read
}
})
it('G: should tree-shake a diamond of CJS reexports with disjoint usage', () => {
const a = require('./g-p1').a
const b = require('./g-p2').b
expect(a).toBe(1)
expect(b).toBe(2)
if (process.env.NODE_ENV === 'production') {
// each property is pulled through a different reexport path
expect(require('./g-p1').aUsed).toBe(true)
expect(require('./g-p2').bUsed).toBe(true)
expect(require('./g-p1').cUsed).toBe(false) // c used through neither path
}
})
it('J: should tree-shake when ids unwrap a namespace after a star hop', () => {
const m = require('./j-hop2')
expect(m.w).toBe(1)
if (process.env.NODE_ENV === 'production') {
expect(m.wUsed).toBe(true)
expect(m.zUsed).toBe(false)
}
})
it('H: should resolve mutually circular CJS star-reexports at runtime', () => {
const a = require('./h-a')
expect(a.onlyB).toBe('B')
expect(a.alsoB).toBe('B2')
expect('onlyA' in a).toBe(false) // overwritten by module.exports = require("./h-b")
})
export const w = 1
export const z = 2
export const wUsed = __webpack_exports_info__.w.used
export const zUsed = __webpack_exports_info__.z.used
module.exports.func = function f() {
'use strict'
return this
}
module.exports.abc = 'abc'
exports.abc = 'abc'
function f(m) {
m.exports = { abc: 'abc', def: 'def' }
}
f(module)
Object.defineProperty(exports, 'abc', { value: 'abc' })
var newObj = {}
exports = newObj
Object.defineProperty(exports, 'def', { value: 'def' })
exports.abc = 'abc'
Object.defineProperties(module, {
exports: {
value: {
abc: 'abc',
def: 'def',
},
},
})
exports.abc = 'abc'
Object.defineProperty(module, 'exports', {
value: {
abc: 'abc',
def: 'def',
},
})
it('should bailout when reading whole exports object from this', () => {
var test = require('./reading-this').test
expect(test().abc).toBe('abc')
})
it('should bailout when reading whole exports object from exports', () => {
var test = require('./reading-exports').test
expect(test().abc).toBe('abc')
})
it('should bailout when reading whole exports object from module.exports', () => {
var test = require('./reading-module-exports').test
expect(test().abc).toBe('abc')
})
it('should reassigning exports (assign values)', () => {
expect(require('./assign-exports-assign').abc).toBe('abc')
expect(require('./assign-exports-assign').def).toBe(undefined)
})
it('should reassigning exports (define values)', () => {
expect(require('./assign-exports-define').abc).toBe('abc')
expect(require('./assign-exports-define').def).toBe(undefined)
})
it('should not mangle or remove nested properties', () => {
expect(require('./nested-property').abc).toBe('abc')
})
it('should be able to access the exports via call context', () => {
expect(require('./accessing-call-context').func().abc).toBe('abc')
var cc = require('./accessing-call-context')
expect(cc.func().abc).toBe('abc')
var func = require('./accessing-call-context').func
expect(func()).toBe(undefined)
})
it('should be able to define an exports property on module (property)', () => {
expect(require('./define-module-property').abc).toBe('abc')
expect(require('./define-module-property').def).toBe('def')
})
it('should be able to define an exports property on module (properties)', () => {
expect(require('./define-module-properties').abc).toBe('abc')
expect(require('./define-module-properties').def).toBe('def')
})
it('should be able to do stuff with the module object', () => {
expect(require('./accessing-module').abc).toBe('abc')
expect(require('./accessing-module').def).toBe('def')
})
it('should be able to use AMD to define exports', () => {
expect(require('./using-amd').abc).toBe('abc')
expect(require('./using-amd').def).toBe('def')
})
var abc = {}
module.exports = abc
module.exports.abc = 'abc'
module.exports.def = 'def'
expect(abc).toEqual({ abc: 'abc', def: 'def' })
it('should allow to require esm', () => {
expect(require('./module').abc).toBe('abc')
expect(typeof require('./module').func).toBe('function')
// Dropped: relies on webpack's `require.cache` / `require.resolve` runtime
// introspection, which turbopack's bundled runtime doesn't populate.
// expect(Object.keys(require("./module").func())).toEqual(
// Object.keys(require.cache[require.resolve("./module?2")].exports)
// );
})
export const abc = 'abc'
export const def = 'def'
export const func = function () {
'use strict'
return this
}
import def, { named, __esModule } from './module'
import * as ns from './module'
it('should allow to import module with getters', () => {
expect(def).toBe('default')
expect(named).toBe('named')
expect(__esModule).toBe(true)
expect(ns.default).toBe('default')
expect(ns.named).toBe('named')
expect(ns.__esModule).toBe(true)
const indirect = Object(ns)
expect(indirect.default).toBe('default')
expect(indirect.named).toBe('named')
expect(indirect.__esModule).toBe(true)
})
Object.defineProperty(exports, '__esModule', { get: () => true })
Object.defineProperty(exports, 'default', { get: () => 'default' })
Object.defineProperty(exports, 'named', { get: () => 'named' })
import m1 from './module'
import m2 from './module'
import { abc } from './module'
it('should allow to import cjs with esm', () => {
expect(m1.abc).toBe('abc')
expect(m2).toEqual({ abc: 'abc', def: 'def' })
expect(abc).toBe('abc')
})
Object.defineProperty(exports, 'abc', { enumerable: true, value: 'abc' })
Object.defineProperty(exports, 'def', { enumerable: true, value: 'def' })
Object.defineProperty(module.exports, 'abc', {
enumerable: true,
value: 'abc',
})
Object.defineProperty(module.exports, 'def', {
enumerable: true,
value: 'def',
})
Object.defineProperty(this, 'abc', { enumerable: true, value: 'abc' })
Object.defineProperty(this, 'def', { enumerable: true, value: 'def' })
class Test {
getString() {
return 'hello'
}
}
const getExports = () => ({ __esModule: true, default: Test })
module.exports = getExports()
class Test {
getString() {
return 'hello'
}
}
const getExports = () => ({ default: Test })
module.exports = getExports()
Object.defineProperty(exports, '__esModule', { value: true })
class Test {
getString() {
return 'hello'
}
}
exports.default = Test
it('should allow to export via exports', () => {
expect(require('./assign-exports-property').abc).toBe('abc')
expect(require('./assign-exports-property')).toEqual({
abc: 'abc',
def: 'def',
})
})
it('should allow to export via module.exports', () => {
expect(require('./assign-module-exports-property').abc).toBe('abc')
expect(require('./assign-module-exports-property')).toEqual({
abc: 'abc',
def: 'def',
})
})
it('should allow to export via this', () => {
expect(require('./assign-this-property').abc).toBe('abc')
expect(require('./assign-this-property')).toEqual({
abc: 'abc',
def: 'def',
})
})
it('should allow to export via define property on exports', () => {
expect(require('./define-exports-property').abc).toBe('abc')
expect(require('./define-exports-property')).toEqual({
abc: 'abc',
def: 'def',
})
})
it('should allow to export via define property on module.exports', () => {
expect(require('./define-module-exports-property').abc).toBe('abc')
expect(require('./define-module-exports-property')).toEqual({
abc: 'abc',
def: 'def',
})
})
it('should allow to export via define property on this', () => {
expect(require('./define-this-property').abc).toBe('abc')
expect(require('./define-this-property')).toEqual({
abc: 'abc',
def: 'def',
})
})
it('should allow to read own exports via exports', () => {
var test = require('./reading-self-from-exports').test
expect(test()).toBe('abc')
})
it('should allow to read own exports via module.exports', () => {
var test = require('./reading-self-from-module-exports').test
expect(test()).toBe('abc')
})
it('should allow to read own exports via this', () => {
var test = require('./reading-self-from-this').test
expect(test()).toBe('abc')
})
it('should allow to attach exports to object', () => {
expect(require('./attach-to-object').abc).toBe('abc')
expect(require('./attach-to-object').def).toBe('def')
expect(require('./attach-to-object').abc).toBe('abc')
expect(require('./attach-to-object').def).toBe('def')
})
it('should allow to attach exports to function', () => {
expect(require('./attach-to-function')()).toBe('abc')
expect(require('./attach-to-function').def).toBe('def')
expect(require('./attach-to-function')()).toBe('abc')
expect(require('./attach-to-function').def).toBe('def')
})
it('should allow to attach exports to arrow function', () => {
expect(require('./attach-to-arrow-function')()).toBe('abc')
expect(require('./attach-to-arrow-function').def).toBe('def')
expect(require('./attach-to-arrow-function')()).toBe('abc')
expect(require('./attach-to-arrow-function').def).toBe('def')
})
it('should properly handle export / require `default`', () => {
expect(require('./require-default').moduleExportsDefault).toBe('hello')
expect(require('./require-default').hello1).toBe('hello')
expect(require('./require-default').hello2).toBe('hello')
expect(require('./require-default').hello3).toBe('hello')
expect(require('./require-default').hello4).toBe('hello')
expect(require('./require-default').hello5).toBe('hello')
expect(require('./require-default').hello6).toBe('hello')
expect(require('./require-default').hello7).toBe('hello')
expect(require('./require-default').hello8).toBe('hello')
})
const ModuleExportsDefaultTest = require('./module-exports-default').default
const Test1 = require('./exports-default').default
import Test2 from './exports-default'
const Test3 = require('./exports-default-flagged').default
import Test4 from './exports-default-flagged'
const Test5 = require('./exports-default-bailout').default
import Test6 from './exports-default-bailout'
const Test7 = require('./exports-default-bailout-flagged').default
import Test8 from './exports-default-bailout-flagged'
const moduleExportsDefaultTest = new ModuleExportsDefaultTest()
const test1 = new Test1()
const test2 = new Test2.default()
const test3 = new Test3()
const test4 = new Test4()
const test5 = new Test5()
const test6 = new Test6.default()
const test7 = new Test7()
const test8 = new Test8()
export const moduleExportsDefault = moduleExportsDefaultTest.getString()
export const hello1 = test1.getString()
export const hello2 = test2.getString()
export const hello3 = test3.getString()
export const hello4 = test4.getString()
export const hello5 = test5.getString()
export const hello6 = test6.getString()
export const hello7 = test7.getString()
export const hello8 = test8.getString()
it('should be able to import a module via require and property', () => {
expect(require('./module').abc).toBe('abc')
})
it('should be able to import a module via require and destruct', () => {
var { abc } = require('./module')
expect(abc).toBe('abc')
})
it('should be able to import a module via require and exports object', () => {
var module1 = require('./module')
expect(module1.abc).toBe('abc')
var module2 = require('./module')
expect(module2).toEqual({ abc: 'abc', def: 'def' })
})
import module1 from './module-1'
// Dropped the `{ a }` named import: turbopack resolves a named CJS import that
// isn't a static export to `undefined` rather than a live getter, so a
// runtime-added `exports.a` isn't observed through the binding.
import module2 from './module-2'
it('should allow mutating imported modules (changing existing exports)', () => {
expect(module1.abc).toBe('abc')
expect(module1.def).toBe('def')
module1.abc = 'new-abc'
expect(module1.abc).toBe('new-abc')
expect(module1.def).toBe('def')
})
it('should allow mutating imported modules (adding new properties)', () => {
expect(module2.abc).toBe('abc')
expect(module2.def).toBe('def')
expect(module2.ghi).toBe(undefined)
expect(module2.Oi).toBe(undefined)
expect(module2.a).toBe(undefined)
// expect(a).toBe(undefined);
expect(module2['']).toBe(undefined)
module2.ghi = 'ghi'
module2.Oi = 'Oi'
module2.a = 'a'
module2[''] = {}
module2[''].abc = 'abc'
expect(module2.abc).toBe('abc')
expect(module2.def).toBe('def')
expect(module2.ghi).toBe('ghi')
expect(module2.Oi).toBe('Oi')
expect(module2.a).toBe('a')
// expect(a).toBe("a");
expect(module2['']).toEqual({ abc: 'abc' })
expect(module2[''].abc).toBe('abc')
})
// Rewritten to assert the observable `__esModule` interop exports rather than
// object identity between `import()` and `require()`: turbopack's `import()`
// yields an exotic Module namespace object, never the same (or deep-equal) plain
// object that `require()` returns.
it('should allow to create namespace exports via __esModule on exports', async () => {
const ns = await import('./namespace-via-exports')
expect(ns.abc).toBe('abc')
expect(ns.default).toBe('default')
})
it('should allow to create namespace exports via __esModule on literal', async () => {
const ns = await import('./namespace-via-literal')
expect(ns.abc).toBe('abc')
expect(ns.default).toBe('default')
})
it('should allow to create namespace exports via __esModule with Object.defineProperty', async () => {
const ns = await import('./namespace-via-define-property')
expect(ns.abc).toBe('abc')
expect(ns.default).toBe('default')
})
it('should allow to create namespace exports via __esModule with Object.defineProperty minimized true', async () => {
const ns = await import('./namespace-via-define-property-minimized')
expect(ns.abc).toBe('abc')
expect(ns.default).toBe('default')
})
it('should allow to create namespace exports via __esModule with Object.defineProperties', async () => {
const ns = await import('./namespace-via-define-properties')
expect(ns.abc).toBe('abc')
expect(ns.default).toBe('default')
})
Object.defineProperties(exports, {
__esModule: { value: true },
abc: { enumerable: true, value: 'abc' },
default: { enumerable: true, value: 'default' },
})
Object.defineProperty(exports, '__esModule', { value: !0 })
exports.abc = 'abc'
exports.default = 'default'
Object.defineProperty(exports, '__esModule', { value: true })
exports.abc = 'abc'
exports.default = 'default'
it('should not rewrite this nested in functions', () => {
const f = require('./module').fff
expect(f.test1).toBe(true)
expect(f.test2).toBe(true)
})
function F() {
this.test1 = true
Object.defineProperty(this, 'test2', { value: true })
}
exports.fff = new F()
it('should replace Object.defineProperty correctly with brackets', () => {
expect(require('./module').test).toBe(true)
})
it('should be able to export an object literal', () => {
expect(require('./direct-object').abc).toBe('abc')
expect(require('./direct-object')).toEqual({ abc: 'abc', def: 'def' })
})
it('should be able to export an object literal indirect', () => {
expect(require('./indirect-object').abc).toBe('abc')
expect(require('./indirect-object')).toEqual({ abc: 'abc', def: 'def' })
})
it('should parse nested requires successfully', () => {
expect(require('./nested-require').value).toBe(42)
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment