diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/a.js new file mode 100644 index 00000000000..5e60fdbb15c --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/a.js @@ -0,0 +1,3 @@ +const v = require("./b.js"); + +output = v; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/b.js new file mode 100644 index 00000000000..1e38fdcafe2 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/b.js @@ -0,0 +1,7 @@ +const value = require("./c.js"); + +const obj = { + value, +}; + +exports = module.exports = obj.value; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/c.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/c.js new file mode 100644 index 00000000000..3481de8569a --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/c.js @@ -0,0 +1 @@ +module.exports = 1234; diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index 0cc240d9570..cfe6d356432 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -3705,8 +3705,6 @@ describe('scope hoisting', function () { ), ); - // console.log(await outputFS.readFile(b.getBundles()[0].filePath, 'utf8')); - let output = await run(b); assert.equal(output, 1); }); @@ -3771,6 +3769,18 @@ describe('scope hoisting', function () { assert.deepEqual(output, {foo: 2}); }); + it('should support referencing a require in object literal shorthands when wrapped', async function () { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/commonjs/wrap-module-obj-literal-require/a.js', + ), + ); + + let output = await run(b); + assert.strictEqual(output, 1234); + }); + it('should support typeof require when wrapped', async function () { // https://github.com/parcel-bundler/parcel/issues/5883 let b = await bundle( diff --git a/packages/transformers/js/core/src/hoist.rs b/packages/transformers/js/core/src/hoist.rs index a7d7ed65578..d29c6e1bf7f 100644 --- a/packages/transformers/js/core/src/hoist.rs +++ b/packages/transformers/js/core/src/hoist.rs @@ -914,10 +914,6 @@ impl<'a> Fold for Hoist<'a> { } fn fold_prop(&mut self, node: Prop) -> Prop { - if self.collect.should_wrap { - return node.fold_children_with(self); - } - match node { Prop::Shorthand(ident) => Prop::KeyValue(KeyValueProp { key: PropName::Ident(Ident::new(ident.sym.clone(), DUMMY_SP)), diff --git a/packages/transformers/js/core/src/lib.rs b/packages/transformers/js/core/src/lib.rs index 6e4b31979f8..945d5069041 100644 --- a/packages/transformers/js/core/src/lib.rs +++ b/packages/transformers/js/core/src/lib.rs @@ -28,11 +28,10 @@ use std::str::FromStr; use path_slash::PathExt; use serde::{Deserialize, Serialize}; -use swc_atoms::JsWord; use swc_common::comments::SingleThreadedComments; use swc_common::errors::{DiagnosticBuilder, Emitter, Handler}; use swc_common::{chain, sync::Lrc, FileName, Globals, Mark, SourceMap}; -use swc_ecmascript::ast::{Ident, Module}; +use swc_ecmascript::ast::Module; use swc_ecmascript::codegen::text_writer::JsWriter; use swc_ecmascript::parser::lexer::Lexer; use swc_ecmascript::parser::{EsConfig, PResult, Parser, StringInput, Syntax, TsConfig}; @@ -44,7 +43,7 @@ use swc_ecmascript::transforms::{ optimization::simplify::dead_branch_remover, optimization::simplify::expr_simplifier, pass::Optional, proposals::decorators, react, typescript, }; -use swc_ecmascript::visit::{Fold, FoldWith, VisitWith}; +use swc_ecmascript::visit::{FoldWith, VisitWith}; use decl_collector::*; use dependency_collector::*;