From b6c04944e43c02d564b79fd05cb0ed8084d86675 Mon Sep 17 00:00:00 2001 From: George Corney Date: Tue, 8 Mar 2022 13:37:11 +0000 Subject: [PATCH 1/5] Add support for `js.import(path, exportName)` For example: ```haxe @:js.import("three/examples/jsm/controls/OrbitControls.js") extern class OrbitControls { ``` does not work because it tries to import "three_examples_jsm_controls_orbitcontrols_OrbitControls", rather than just OrbitControls. Given the module could export under any name, we need a variant where we specify which import we want --- src/jsImport/Macro.hx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/jsImport/Macro.hx b/src/jsImport/Macro.hx index c61f56a..3f41433 100644 --- a/src/jsImport/Macro.hx +++ b/src/jsImport/Macro.hx @@ -37,8 +37,10 @@ class Macro { lines.push('import $id from "$v";'); case [{ params: [macro $v{(v:String)}] }]: lines.push('import { $id } from "$v";'); + case [{ params: [macro $v{(v:String)}, macro $v{(exportName:String)}] }]: + lines.push('import { $exportName } from "$v";'); case [{ pos: pos }]: - error('@$META requires a string parameter, optionally preceeded by an identifier', pos); + error('@$META requires a string parameter, optionally preceded by an identifier', pos); default: error('Duplicate @$META', meta[0].pos); } @@ -52,4 +54,4 @@ class Macro { }); } } -#end \ No newline at end of file +#end From 269581090a8e65a7223d544d4c824928cd22e400 Mon Sep 17 00:00:00 2001 From: George Corney Date: Tue, 8 Mar 2022 13:37:58 +0000 Subject: [PATCH 2/5] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5b0df8a..e4fff6e 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,5 @@ With `-lib jsImport` you can decorate any extern class with `@:js.import` like s - `@:js.import(@star './some/path.js') extern class Foo {}` becomes `import * as Foo from './some/path.js'` - `@:js.import(@default './some/path.js') extern class Foo {}` becomes `import Foo from './some/path.js'` -- `@:js.import('./some/path.js') extern class Foo {}` becomes `import { Foo } from './some/path.js'` \ No newline at end of file +- `@:js.import('./some/path.js') extern class Foo {}` becomes `import { Foo } from './some/path.js'` +- - `@:js.import('./some/path.js', 'SomeName') extern class Foo {}` becomes `import { SomeName } from './some/path.js'` From ce280d7e20400aea63f2d5dcecb5651890f70f02 Mon Sep 17 00:00:00 2001 From: George Corney Date: Tue, 8 Mar 2022 13:38:05 +0000 Subject: [PATCH 3/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e4fff6e..305dd7d 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,4 @@ With `-lib jsImport` you can decorate any extern class with `@:js.import` like s - `@:js.import(@star './some/path.js') extern class Foo {}` becomes `import * as Foo from './some/path.js'` - `@:js.import(@default './some/path.js') extern class Foo {}` becomes `import Foo from './some/path.js'` - `@:js.import('./some/path.js') extern class Foo {}` becomes `import { Foo } from './some/path.js'` -- - `@:js.import('./some/path.js', 'SomeName') extern class Foo {}` becomes `import { SomeName } from './some/path.js'` +- `@:js.import('./some/path.js', 'SomeName') extern class Foo {}` becomes `import { SomeName } from './some/path.js'` From 32dba97f725b8936d6c3238fe19653255b055ba4 Mon Sep 17 00:00:00 2001 From: George Corney Date: Thu, 10 Mar 2022 15:40:33 +0000 Subject: [PATCH 4/5] add as $id to correct haxe identifier --- src/jsImport/Macro.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jsImport/Macro.hx b/src/jsImport/Macro.hx index 3f41433..a02a426 100644 --- a/src/jsImport/Macro.hx +++ b/src/jsImport/Macro.hx @@ -30,7 +30,7 @@ class Macro { cl.meta.remove(':native'); cl.meta.add(':native', [macro $v{id}], (macro null).pos); - switch meta { + var native = switch meta { case [{ params: [macro @star $v{(v:String)}] }]: lines.push('import * as $id from "$v";'); case [{ params: [macro @default $v{(v:String)}] }]: @@ -38,7 +38,7 @@ class Macro { case [{ params: [macro $v{(v:String)}] }]: lines.push('import { $id } from "$v";'); case [{ params: [macro $v{(v:String)}, macro $v{(exportName:String)}] }]: - lines.push('import { $exportName } from "$v";'); + lines.push('import { $exportName as $id } from "$v";'); case [{ pos: pos }]: error('@$META requires a string parameter, optionally preceded by an identifier', pos); default: From c5699bed5f380905904600b5f3e04a6489b82bb0 Mon Sep 17 00:00:00 2001 From: George Corney Date: Thu, 10 Mar 2022 15:41:19 +0000 Subject: [PATCH 5/5] remove `var native =` --- src/jsImport/Macro.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jsImport/Macro.hx b/src/jsImport/Macro.hx index a02a426..3b37c0a 100644 --- a/src/jsImport/Macro.hx +++ b/src/jsImport/Macro.hx @@ -30,7 +30,7 @@ class Macro { cl.meta.remove(':native'); cl.meta.add(':native', [macro $v{id}], (macro null).pos); - var native = switch meta { + switch meta { case [{ params: [macro @star $v{(v:String)}] }]: lines.push('import * as $id from "$v";'); case [{ params: [macro @default $v{(v:String)}] }]: