Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import followed by a comments should be moved to the top of the generated file #3774

Open
goswinr opened this issue Mar 1, 2024 · 5 comments

Comments

@goswinr
Copy link
Contributor

goswinr commented Mar 1, 2024

I did not know that 'emitJsStatement' will extract imports:
image
but if it has a comment, it won't:
image
see REPL
where is this implemented? I only found this

let emitJsStatement<'T> (args: obj) (jsCode: string) : 'T = nativeOnly

@MangelMaxime
Copy link
Member

Hello,

Fable always try to put import statement at the top of the file.

The line that you liked is the exposed API, you will need to look into Fable.Transforms to find where that API is handled by Fable compilation/transformation.

For example, this API is probably handled here:

| Naming.StartsWith "emitJs" rest, [ args; macro ] ->
match macro with
| RequireStringConstOrTemplate com ctx r template ->
let args = destructureTupleArgs [ args ]
let isStatement = rest = "Statement"
emitTemplate r t args isStatement template |> Some

But I am not sure yet where Fable does the re-ordering.

Could you please explain what is your request with this issue? Because I don't understand your request with the current title and description.

Is the request that import statement with a trailing comment should also be moved up too?

@goswinr
Copy link
Contributor Author

goswinr commented Mar 4, 2024

Yes, sorry, @MangelMaxime my question was not clear.
The real question is how to emit

import JSZip from "jszip";

not

import {JSZip} from "jszip";

I tried all attributes form the docs :

REPL [<ImportMember("..")>]

REPL [<ImportMember(".." , "...")>] ( this should compile but doesn't ! Are the docs wrong? )

REPL [<Import(".." , "...")>]

REPL [<ImportDefault("..")>]

Is including it in a emitJsStatement (without a comment) the only way ?

@MangelMaxime
Copy link
Member

There is indeed a typo in the documentation regarding ImportMember it only takes 1 argument.

/// Takes the member name from the value it decorates
type ImportMemberAttribute(from: string) =
inherit Attribute()

But it would still not generate what you want as it would generate import {JSZip} from "jszip".


In JavaScript import JSZip from 'jszip'; is importing the default export from jszip.

To proves that you can try to run:

import JSZip from 'jszip';
import { default as bar } from 'jszip';

console.log(JSZip);
console.log(bar);

and you will see the same output for both console.log:

[Function: JSZip] {
  support: {
	...
  },
  defaults: {
	...
  },
  version: '3.10.1',
  loadAsync: [Function (anonymous)],
  external: { Promise: [Function: Promise] }
}
[Function: JSZip] {
  support: {
	...
  },
  defaults: {
	...
  },
  version: '3.10.1',
  loadAsync: [Function (anonymous)],
  external: { Promise: [Function: Promise] }
}

So using:

[<ImportDefault("jszip")>]
type JSZip() = class end

//or 

let jsZip1 = import "default" "jszip"
let jsZip2 = importDefault "jszip"

are equivalent. You don't need JSZip to respect the casing this is just an alias.

@goswinr
Copy link
Contributor Author

goswinr commented Mar 5, 2024

Thanks, I found that only

open Fable.Core.JsInterop
do 
    import "default as JSZip" "jszip"

yields

import JSZip from "jszip";

REPL

@goswinr
Copy link
Contributor Author

goswinr commented Mar 5, 2024

I still leave this issue open because I think import statement with a trailing comment should not behave differently.
(see initial issue)

@MangelMaxime MangelMaxime changed the title Import magic in emitJsStatement ! Import followed by a comments should be moved to the top of the generated file Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants