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

Generate JavaScriptKit .js runtime with SwiftPM build tool #191

Open
MaxDesiatov opened this issue May 4, 2022 · 6 comments
Open

Generate JavaScriptKit .js runtime with SwiftPM build tool #191

MaxDesiatov opened this issue May 4, 2022 · 6 comments
Labels
enhancement New feature or request

Comments

@MaxDesiatov
Copy link
Contributor

I'm proposing to declare Runtime/lib directory as SwiftPM resource as described in SE-0271. With a few adjustments to carton, this will allow us to separate carton entrypoint files from JSKit runtime files, and simplify our release process as well. The end result would be that we can simply tag a new JSKit release without a need to update carton entrypoints and tag a new carton release each time.

The complication is that we need to keep .js files in Runtime/lib fresh and in sync with the .ts source files. I'm currently investigating whether SwiftPM build tools proposal can help us generate .js files at SwiftPM build time. While rollup.js requires Node.js to be installed to work, I'm looking at using swc.rs as a build tool, as it provides self-contained binaries for all most popular platforms. Bundling and minification with SWC is still marked as unstable, but with ES6 modules supported in all recent browsers, I wonder if we can keep JSKit runtime supplied in separate files, which could make it slightly easier to debug as well.

Bundling and minification of resulting .js files can then be left to the user, making it more flexible. As a future direction, rollup.js config could be supplied with carton, which it then could use when it runs carton bundle and detects that Node.js and rollup.js are installed and available on user's machine.

@MaxDesiatov MaxDesiatov added the enhancement New feature or request label May 4, 2022
@j-f1
Copy link
Member

j-f1 commented May 4, 2022

Another option is esbuild, which has a path to downloading directly from npm without having Node installed. Something to be aware of is that neither swc nor esbuild support type checking, so there would have to be a GitHub Action to make sure things stay type-safe.

Avoiding compilation entirely is also very possible — we could switch to using plain .js files with JSDoc-based type annotations. One small caveat with this is that we would no longer be able to use enums as they are not a native JS feature (but there are plenty of alternate ways this could be handled).

@MaxDesiatov
Copy link
Contributor Author

MaxDesiatov commented May 4, 2022

Ah, I forgot about esbuild. Could try that as well and see what's easier to use and better documented. I like TypeScript quite a lot, so would prefer to keep using it. As for type checking, you're right that GHA job would catch issues, and probably it's reasonable to expect that developers of JSKit have a proper TS compiler/type checker installed for the development process.

@MaxDesiatov
Copy link
Contributor Author

MaxDesiatov commented May 4, 2022

Glad to see that esbuild supports bundling and minification. Maybe we could utilize that in carton bundle when detecting that esbuild is available in SwiftPM dependency tree as a build tool. I also find it somewhat better documented than SWC.

@kateinoigakukun
Copy link
Member

kateinoigakukun commented May 5, 2022

Another option is to allow users to specify JSKit npm package version in package.json at the top of user code, then bundle it by carton bundle.

$ carton init
$ tree -L 2
.
├── Package.swift
├── README.md
├── Sources
│   └── MyApp
├── Tests
│   ├── LinuxMain.swift
│   └── MyAppTests
├── node_modules
│   └── javascript-kit-swift
├── package-lock.json
└── package.json

$ cat package.json
{
  "name": "MyApp",
  "private": true,
  "dependencies": {
    "javascript-kit-swift": "^0.14.0"
  }
}

This option doesn't solve the potential version mismatch problem between the runtime npm package and SwiftPM package, but we can reuse many parts of the existing JS ecosystem, and no need to sync ts artifacts.

@MaxDesiatov
Copy link
Contributor Author

MaxDesiatov commented May 5, 2022

I think JSKit version mismatch and npm/JS ecosystem interaction are two separate topics. Both deserve to be solved, but in my opinion the version mismatch and release engineering issues are more pressing.

For now I'm inclined to try the SwiftPM resources approach first in my own branches and see how that works before submitting any PRs here.

I'm trying to get WebGL/WebGPU stuff fixed in WebAPIKit, but that's blocked by BigInt changes, and I'm tired of rebuilding carton entrypoints every time anything changes in JSKit runtime. Especially when I only want to try latest commits in JSKit main branch.

@MaxDesiatov MaxDesiatov changed the title Supplying JavaScriptKit .js runtime as SwiftPM resources Generate JavaScriptKit .js runtime with SwiftPM build tool May 12, 2022
@MaxDesiatov
Copy link
Contributor Author

Keeping this open until Swift 5.7 and SwiftWasm 5.7 are available with swiftlang/swift-package-manager#4306 included in those releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants