scala-js / vite-plugin-scalajs

Vite plugin for integration of Scala.js
Apache License 2.0
51 stars 10 forks source link

production build fails with @crxjs/vite-plugin #11

Open xko opened 1 year ago

xko commented 1 year ago

I was trying to make a POC chrome extension using @crxjs/vite-plugin. npm run dev worked great (live-reloaded, catched changes in scala code etc.) , but npm run build failed:

[commonjs--resolver] buildStart must be called before resolveId
error during build:
Error: buildStart must be called before resolveId
    at Object.resolveId (file:///home/xko/dev/schrome/node_modules/@scala-js/vite-plugin-scalajs/dist/index.js:52:23)
    at file:///home/xko/dev/schrome/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:24656:40
    at async PluginDriver.hookFirstAndGetPlugin (file:///home/xko/dev/schrome/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:24556:28)
    at async resolveId (file:///home/xko/dev/schrome/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:23400:26)
    at async ModuleLoader.resolveId (file:///home/xko/dev/schrome/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:23664:15)
    at async Object.resolveId (file:///home/xko/dev/schrome/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:7990:10)
    at async PluginDriver.hookFirstAndGetPlugin (file:///home/xko/dev/schrome/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:24556:28)
    at async resolveId (file:///home/xko/dev/schrome/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:23400:26)
    at async ModuleLoader.resolveId (file:///home/xko/dev/schrome/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:23664:15)
    at async file:///home/xko/dev/schrome/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:23952:50

To reproduce:

sjrd commented 1 year ago

Maybe crxjs/vite-plugin does not respect the Vite specification? The Vite documentation clearly says that buildStart will be called once when the build starts. vite-plugin-scalajs needs this method to be called to read options and resolve sbt projects. It cannot process resolveId before that has been done.

xko commented 1 year ago

before I go there to create an issue, I'd like to clarify couple of things (sorry if I'd sound stupid, I'm a backend developer and this is my 1st journey JS and frontend world :)):

xko commented 1 year ago

@sjrd , after some random attempts, I was able to make it work by doing this in scala-js plugin:

        buildStart :{
            sequential: true,
            order: 'pre',
            async handler(options) {
                if (isDev === undefined)
                    throw new Error("configResolved must be called before buildStart");
                const task = isDev ? "fastLinkJSOutput" : "fullLinkJSOutput";
                const projectTask = projectID ? `${projectID}/${task}` : task;
                scalaJSOutputDir = await printSbtTask(projectTask, cwd);
            }
        },

I don't fully understand what I have changed, but if it makes sense to you, I'll create a PR