davidbwaters / legacy-bundle-snowpack-plugin

Outputs a transpiled legacy bundle for differential serving with nomodule.
MIT License
7 stars 2 forks source link

Plugin should error if it can't find `filePath` #1

Open NfNitLoop opened 3 years ago

NfNitLoop commented 3 years ago

Hi! I came across your plugin on the snowpack plugin list. It sounds like just what I need -- I need to convert code destined to run in a web worker into a module-free version, since Firefox and Safari don't support modules in web workers yet.

Unfortunately, the plugin seems to do nothing. I've got a file like:

// Legacy
// Converted to a no-module version for use in the browser:
// See: https://www.npmjs.com/package/legacy-bundle-snowpack-plugin

@import worker.js
@import * as nacl from 'tweetnacl'

And a config that says:

{
   plugins: [
        [ 
            // https://www.npmjs.com/package/legacy-bundle-snowpack-plugin
            'legacy-bundle-snowpack-plugin',
            {
                filePath: "ts/naclWorker/worker-nomodule.jss"
            },
        ],
        // ...
    ]
    // ...
}

But I don't see any output from legacy-bundle-snowpack-plugin when I run snowpack build, and the version of the file in my build/ directory is unchanged.

(Note the intentional .jss file misspelling. I get the same behavior w/ the correct or incorrect file path.)

NfNitLoop commented 3 years ago

🤦‍♂️ Ah! It was the file path. Looks like the plugin silently does nothing if the filePath doesn't exist. (That should probably fail a bit more loudly!)

It doesn't seem to like the file format from the docs, though. I get the following error:

[legacy-bundle] SyntaxError: C:\Users\Cody\code\feoblog\web-client\build\ts\naclWorker\worker-nomodule.js: Unexpected keyword 'import' (5:1)

  3 | // See: https://www.npmjs.com/package/legacy-bundle-snowpack-plugin
  4 |
> 5 | @import worker.js
    |  ^
  6 | @import * as nacl from 'tweetnacl'
npm ERR! code ELIFECYCLE
npm ERR! errno 1

[Update:] Aha, I missed that the file path references the target directory. It worked! This is handy!

The file path issue is still a pain point for anybody new, though. Seems worth keeping this ticket around for that.

davidbwaters commented 3 years ago

Hi, I'll take a look and I could definitely add in better error handling.

This was really more of a proof of concept plugin I made to demonstrate how snowpack could be used to generate production builds. Snowpacks changed a lot since I first made it but it's pretty straightforward and should still work. If you didn't figure out the issue, I can take a closer look.