Skip to content

Commit

Permalink
Turbopack: allow shadowing the require global in ESM (vercel#70453)
Browse files Browse the repository at this point in the history
Closes PACK-3275

Among other things, a regression from
vercel#70255, but it could also happen
with `__dirname`.

To prevent `SyntaxError: Identifier 'require' has already been declared`
  • Loading branch information
mischnic authored and abhi12299 committed Sep 29, 2024
1 parent 6c4187c commit 894b711
Show file tree
Hide file tree
Showing 153 changed files with 1,207 additions and 885 deletions.
4 changes: 4 additions & 0 deletions turbopack/crates/turbopack-ecmascript/src/chunk/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ impl EcmascriptChunkItemContent {
if this.options.async_module.is_some() {
code += "__turbopack_async_module__(async (__turbopack_handle_async_dependencies__, \
__turbopack_async_result__) => { try {\n";
} else {
code += "{\n";
}

code.push_source(&this.inner_code, this.source_map);
Expand All @@ -152,6 +154,8 @@ impl EcmascriptChunkItemContent {
}}, {});",
opts.has_top_level_await
)?;
} else {
code += "}";
}

code += "})";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import './module.js'

const require = () => 'result'
const __dirname = 'something'
const __filename = 'something/else'

it('should allow declaring CJS globals in ESM', () => {
expect(require()).toBe('result')
expect(__dirname).toBe('something')
expect(__filename).toBe('something/else')
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 894b711

Please sign in to comment.