akejolin / snowpack-plugin-content-hash

A small snowpack plugin which will add a content hash to all your imports paths on build command.
MIT License
8 stars 4 forks source link

Doesn't work #4

Open jcorbin opened 3 years ago

jcorbin commented 3 years ago

All of my sources fail with an esprima error similar to:

Error parsing ecma script with file:  /Users/jcorbin/borkshop-js/packages/jspit/build/ui.js Error: Line 9: Unexpected token .
    at ErrorHandler.constructError (/Users/jcorbin/borkshop-js/node_modules/esprima/dist/esprima.js:5012:22)
    at ErrorHandler.createError (/Users/jcorbin/borkshop-js/node_modules/esprima/dist/esprima.js:5028:27)
    at Parser.unexpectedTokenError (/Users/jcorbin/borkshop-js/node_modules/esprima/dist/esprima.js:1985:39)
    at Parser.throwUnexpectedToken (/Users/jcorbin/borkshop-js/node_modules/esprima/dist/esprima.js:1995:21)
    at Parser.parsePrimaryExpression (/Users/jcorbin/borkshop-js/node_modules/esprima/dist/esprima.js:2366:38)
    at Parser.inheritCoverGrammar (/Users/jcorbin/borkshop-js/node_modules/esprima/dist/esprima.js:2285:37)
    at Parser.parseLeftHandSideExpressionAllowCall (/Users/jcorbin/borkshop-js/node_modules/esprima/dist/esprima.js:2899:26)
    at Parser.inheritCoverGrammar (/Users/jcorbin/borkshop-js/node_modules/esprima/dist/esprima.js:2285:37)
    at Parser.parseUpdateExpression (/Users/jcorbin/borkshop-js/node_modules/esprima/dist/esprima.js:3004:26)
    at Parser.parseUnaryExpression (/Users/jcorbin/borkshop-js/node_modules/esprima/dist/esprima.js:3048:26) {

Trying to build a typescript package: https://github.com/borkshop/js/tree/main/packages/jspit

TylorS commented 3 years ago

I'm also experiencing this build error. Are there any helpful things we could do to provide support in sorting this out?

I don't know how helpful this might be, but my guess is things are failing on files that are types only.

akejolin commented 3 years ago

Hi, sorry for late answer.

I may have an update on this. I think the problem is that the package esprima (which is used for scanning imports) needs to run on already babel compiled code, see code . That is why I used the optimize hook. But I guess it does not seems to be that way all the time.

So one solution would be to make it possible to bring your local babel configuration to the esprima function call and pre compile the file before running esprima.

Something like this.

const result = babel.transformFileSync(filepath, babelOptions);
  try {
    const tree = esprima.parse(result.code, {
      sourceType: 'module',
      tolerant: true,
    })
}
TylorS commented 3 years ago

Hey @akejolin thanks for getting back either way 😄

Unfortunately I'm using TypeScript only, so I don't even have a babel configuration to import.

I actually really needed this kind of plugin for a side-project of mine, and I came to realize I really would prefer to have sourceMaps and support for CSS. So I hacked on my own similar snowpack plugin which may or may not be useful to you @jcorbin https://github.com/TylorS/snowpack-plugin-hash.

jbuckner commented 3 years ago

Thank you for that, @TylorS. It works great in my Snowpack / TypeScript project!