squint-cljs / vite-plugin-squint

A plugin to compile .cljs source code with squint using vite
1 stars 3 forks source link

How does one get .js files to be used in other projects? #18

Closed borkdude closed 4 months ago

borkdude commented 4 months ago

In the case of https://github.com/nextjournal/clojure-mode, which is a library, I use squint (+ watch) to compile .cljs files into .mjs files. These .mjs files can then be directly used from other projects, like described here:

https://github.com/nextjournal/clojure-mode?tab=readme-ov-file#use-it-from-npm

How does one do that with this plugin, would every downstream user of a project which relies on this plugin, need this plugin?

martinklepsch commented 4 months ago

How does one do that with this plugin, would every downstream user of a project which relies on this plugin, need this plugin?

Vite is definitely more oriented towards application building / bundling but I think Rollup (used internally by Vite) could be used for compiling into a distributable library bundle.

On packaging libs

There's a Rollup config example here for bundling a library.

The plugin code in this repo changed a bit to accommodate for hot-reloading so the Rollup plugin will be a bit different but effectively you can use a Rollup plugin similar to the following:

{
  name: "squint_compile",
  transform: function (src, id) {
    if (/\.cljs$/.test(id)) {
      var jsx = compileString(src);
      var js = esbuild.transformSync(jsx, { loader: "jsx" }).code;
      // TODO handle warnings from esbuild transform
      return { code: js, map: null };
    }
  }
};

We could probably package up a squint plugin for plain Rollup (likely just those few lines above) and document how to package libraries with it.

borkdude commented 4 months ago

I don't think I will use this plugin for library purposes. Note that for clojure-mode I do need the .mjs files and I'm not using another tool like rollup, I publish the .mjs files straight to npm and they can be consumed from other JS projects without any other tool.