diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2020-11-02 22:33:43 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-02 22:33:43 +1100 |
commit | 272e9b82ebddaa33cbc01cc8b1efd8a36ec3c645 (patch) | |
tree | 33ec0f339eeaa302ca05543a7110019468ac4b07 | |
parent | fdcc78500cc1aff8c87d76abd1692e79977ac9cc (diff) |
fix(cli): inject helpers when transpiling via swc (#8221)
Fixes #8212
-rw-r--r-- | cli/ast.rs | 5 | ||||
-rw-r--r-- | cli/module_graph2.rs | 12 | ||||
-rw-r--r-- | cli/tests/integration_tests.rs | 5 | ||||
-rw-r--r-- | cli/tests/no_check_decorators.ts | 21 | ||||
-rw-r--r-- | cli/tests/no_check_decorators.ts.out | 3 | ||||
-rw-r--r-- | cli/tests/subdir/more_decorators.ts | 18 | ||||
-rw-r--r-- | cli/tests/ts_decorators_bundle.out | 2 | ||||
-rw-r--r-- | cli/tests/ts_decorators_bundle.ts | 3 |
8 files changed, 64 insertions, 5 deletions
diff --git a/cli/ast.rs b/cli/ast.rs index bc7265c15..fcce0cc92 100644 --- a/cli/ast.rs +++ b/cli/ast.rs @@ -298,6 +298,7 @@ impl ParsedModule { legacy: true, emit_metadata: options.emit_metadata }), + helpers::inject_helpers(), typescript::strip(), fixer(Some(&self.comments)), ); @@ -413,6 +414,7 @@ pub fn transpile_module( src: &str, media_type: &MediaType, emit_options: &EmitOptions, + globals: &Globals, cm: Rc<SourceMap>, ) -> Result<(Rc<SourceFile>, Module), AnyError> { // TODO(@kitsonk) DRY-up with ::parse() @@ -464,10 +466,11 @@ pub fn transpile_module( legacy: true, emit_metadata: emit_options.emit_metadata }), + helpers::inject_helpers(), typescript::strip(), fixer(Some(&comments)), ); - let module = swc_common::GLOBALS.set(&Globals::new(), || { + let module = swc_common::GLOBALS.set(globals, || { helpers::HELPERS.set(&helpers::Helpers::new(false), || { module.fold_with(&mut passes) }) diff --git a/cli/module_graph2.rs b/cli/module_graph2.rs index 6ac27906d..f795a7acb 100644 --- a/cli/module_graph2.rs +++ b/cli/module_graph2.rs @@ -120,20 +120,23 @@ impl Error for GraphError {} /// avoid a circular dependency with `ast`. struct BundleLoader<'a> { cm: Rc<swc_common::SourceMap>, - graph: &'a Graph2, emit_options: &'a ast::EmitOptions, + globals: &'a swc_common::Globals, + graph: &'a Graph2, } impl<'a> BundleLoader<'a> { pub fn new( graph: &'a Graph2, emit_options: &'a ast::EmitOptions, + globals: &'a swc_common::Globals, cm: Rc<swc_common::SourceMap>, ) -> Self { BundleLoader { cm, - graph, emit_options, + globals, + graph, } } } @@ -158,6 +161,7 @@ impl swc_bundler::Load for BundleLoader<'_> { &src, &media_type, self.emit_options, + self.globals, self.cm.clone(), ) } else { @@ -965,9 +969,9 @@ impl Graph2 { let cm = Rc::new(swc_common::SourceMap::new( swc_common::FilePathMapping::empty(), )); - let loader = BundleLoader::new(self, emit_options, cm.clone()); - let hook = Box::new(BundleHook); let globals = swc_common::Globals::new(); + let loader = BundleLoader::new(self, emit_options, &globals, cm.clone()); + let hook = Box::new(BundleHook); let bundler = swc_bundler::Bundler::new( &globals, cm.clone(), diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 64ef69ba1..2fd28a109 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -2570,6 +2570,11 @@ itest!(no_check { http_server: true, }); +itest!(no_check_decorators { + args: "run --quiet --reload --no-check no_check_decorators.ts", + output: "no_check_decorators.ts.out", +}); + itest!(lib_ref { args: "run --quiet --unstable --reload lib_ref.ts", output: "lib_ref.ts.out", diff --git a/cli/tests/no_check_decorators.ts b/cli/tests/no_check_decorators.ts new file mode 100644 index 000000000..d9a3747b9 --- /dev/null +++ b/cli/tests/no_check_decorators.ts @@ -0,0 +1,21 @@ +/* eslint-disable */ +function a() { + console.log("a(): evaluated"); + return ( + _target: any, + _propertyKey: string, + _descriptor: PropertyDescriptor, + ) => { + console.log("a(): called"); + }; +} + +class B { + @a() + method() { + console.log("method"); + } +} + +const b = new B(); +b.method(); diff --git a/cli/tests/no_check_decorators.ts.out b/cli/tests/no_check_decorators.ts.out new file mode 100644 index 000000000..015f7076e --- /dev/null +++ b/cli/tests/no_check_decorators.ts.out @@ -0,0 +1,3 @@ +a(): evaluated +a(): called +method diff --git a/cli/tests/subdir/more_decorators.ts b/cli/tests/subdir/more_decorators.ts new file mode 100644 index 000000000..a67ae3fd6 --- /dev/null +++ b/cli/tests/subdir/more_decorators.ts @@ -0,0 +1,18 @@ +/* eslint-disable */ +function a() { + console.log("a(): evaluated"); + return ( + _target: any, + _propertyKey: string, + _descriptor: PropertyDescriptor, + ) => { + console.log("a(): called"); + }; +} + +export class B { + @a() + method() { + console.log("method"); + } +} diff --git a/cli/tests/ts_decorators_bundle.out b/cli/tests/ts_decorators_bundle.out index 3a152aecc..a5b77b7bf 100644 --- a/cli/tests/ts_decorators_bundle.out +++ b/cli/tests/ts_decorators_bundle.out @@ -1,3 +1,5 @@ [WILDCARD] +function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { +[WILDCARD] new SomeClass().test(); [WILDCARD]
\ No newline at end of file diff --git a/cli/tests/ts_decorators_bundle.ts b/cli/tests/ts_decorators_bundle.ts index a8e2e952b..d67ea4d5f 100644 --- a/cli/tests/ts_decorators_bundle.ts +++ b/cli/tests/ts_decorators_bundle.ts @@ -1,5 +1,7 @@ /* eslint-disable */ +import { B } from "./subdir/more_decorators.ts"; + function Decorator() { return function ( target: Record<string, any>, @@ -20,3 +22,4 @@ class SomeClass { } new SomeClass().test(); +new B().method(); |