-
Notifications
You must be signed in to change notification settings - Fork 301
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
Best practise for native files in .fsproj #3657
Comments
Hello, Fable is only using files that are included in the In general, in your fsproj you should have line like: <ItemGroup>
<Content Include="*.fsproj; *.fs; *.fsi" PackagePath="fable\" />
</ItemGroup> You need to change it to: <ItemGroup>
<Content Include="*.fsproj; *.fs; *.fsi; *.js" Exclude="**\*.fs.js" PackagePath="fable\" />
</ItemGroup> Depending on how you build test your project you could have some Fable generate files |
Ahh that solution was so obvious! I am very sorry, could have thought about this myself. |
No worries, this is not that much explains in the documentation either. I will add a note for it |
Ah i actually just noticed that this change only affects nuget packaging. Which was already doing fine with the embedded ressource. We currently publish an npm package from the result of |
In this case, it depends on how your code/package is structured. For example, in Nacara I have a And what I did is in the {
"name": "nacara-layout-standard",
"version": "1.8.0",
"description": "",
"exports": {
".": "./dist/Export.js",
"./*": "./*"
},
"type": "module",
"files": [
"dist/**/*.*",
"scss",
"scripts",
"js"
]
} Which results in the following package structure: In theory Fable, imports are computed relatively to the destination of the file so you don't have anything special to do in that case. If you don't have having several folders in your package, you can also put your native javascript files inside of [<Import("hello", "./util.js")>]
let hello : unit -> unit = nativeOnly
[<Import("hello", "${outDir}/util.js")>]
let hello2 : unit -> unit = nativeOnly generates import { hello } from "../src/util.js";
import { hello as hello_1 } from "./util.js"; See how the second import is not relative to the destination of the compile file. |
@Freymaurer in the context of a pure Elmish example, I had to add the following lines to copy native files to my output directory: <!-- TargetPath are relative to bin/Debug/net8.0/ -->
<Content Include="src/index.html" CopyToOutputDirectory="PreserveNewest" TargetPath="../../../output/src/index.html" />
<Content Include="src/index.js" CopyToOutputDirectory="PreserveNewest" TargetPath="../../../output/src/index.js" /> @MangelMaxime |
@laurentpayot Is only used when package your package by running When creating an application via Vite, you should not need to copy things with MSBuild. Vite should do it for you when you point it to the entry file or your project. When creating a JavaScript application with Fable what is important to understand is this paragraph from the Fable doc: In your case, you want to compile the file using Fable and setup Vite to use them as the entry point. Then when you build using Vite, vite will create the bundle and everything for you in its output folder. Documentation on how to setup Fable and Vite are available here;
You can also have a look at this template which has Vite, Fable and React setup together. Using Elmish instead of pure React, should just be a matter of dependencies and touching the F# code. Not the project setup. If you still have problems, please open an issue so it can be discussed separately. |
Thanks for your answer @MangelMaxime. I should have mentioned that I use Fable with the I think opening a new issue would be redundant as I am literally looking for "Best practice for native files in a .fsproj", but simply when the output directory is not the same as the source directory (my example repo is just some preliminary research before a potential non-trivial Elm PWA rewrite to F#). |
There is not silver solution to the best practise unfortunately. Its depends on what people values the most. For some people having, generated files near the source is good because they can just configure their IDE to hide them or nest them inside of their parent file like VSCode allows you to do. For others, they prefer to have the output separated from the sources files and in this case they could follow .NET philosophy by using a sub-folder inside of the source directory. I often use the Another possibility, could be to move the To show one of the many possible structures:
|
Sadly this is not a solution for us, as i assume this will on-build copy the So currently for us it seems to be best to dismiss the js file and add some bindings, or use emit to call the js code in pure f# files. @MangelMaxime maybe it would be possible to have a option in fable to copy files on transpile? |
Thanks again @MangelMaxime for your detailed answer. I didn’t know you could do this in VSCode. |
@Freymaurer I’m not 100% sure of what you mean but did you try the Copy Task? It even has a |
@Freymaurer Can't you do like Nacara and have a
The difficulty I see with adding copy files support to Fable is that there are a lot of ways that could be done:
Also, when you write So if you move the native JavaScript file out, you need to know about the less known
@laurentpayot Fable only use MSBuild to restore the project and get the dependencies list. So in theory, MSBuild tasks are not going to be executed unless you invoke both This is reason why the following does nothing in Fable. <EmbeddedResource Include="Validation\JsonValidation.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource> I believe their are several solutions that can already be used today to work around this "limitation".
|
Description
We have a fable-library for which we wrote some native js code. We included it as
EmbeddedResource
in .fsproj, so it works on the .NET side with nuget packaging. But we noticed, that Fable does not copy it to the output directory. Should we refactor it to rmv the native file and do everything with fable bindings or is there a best practise on how to copy it?Repro code
Here is the relevant code from .fsproj:
native js file is referenced via:
Related information
4.8.1
The text was updated successfully, but these errors were encountered: