Parcel: [v2] posthtml-transform throws an unknown error when using posthtml-obfuscate

Created on 21 Jun 2020  路  7Comments  路  Source: parcel-bundler/parcel

馃悰 bug report

When I use posthtml-obfuscate to obfuscate email addresses with v2, I get this mysterious error:

@parcel/transformer-posthtml: Unexpected token u in JSON at position 0

馃帥 Configuration (.babelrc, package.json, cli command)

this is what my .posthtmlrc file looks like:

{
  "plugins": {
    "posthtml-modules": {
      "root": "./src"
    },
    "posthtml-obfuscate": {}
  }
}

馃 Expected Behavior

I'd expect the posthtml transformer to work without an error. This example (code provided below) works fine with parcel v1.

馃槸 Current Behavior

馃毃 Build failed.
@parcel/transformer-posthtml: Unexpected token u in JSON at position 0
SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.transform (node_modules/@parcel/transformer-posthtml/lib/PostHTMLTransformer.js:119:21)

馃拋 Possible Solution

Digging into PostHTMLTransformer.js, the error happens here:

    asset.setAST({
      type: 'posthtml',
      version: '0.4.1',
      program: JSON.parse(JSON.stringify(res.tree)) // posthtml adds functions to the AST that are not serializable

    });

My guess is that res.tree is undefined, but I'm unsure what would cause that

馃捇 Code Sample

.posthtmlrc

{
  "plugins": {
    "posthtml-obfuscate": {}
  }
}

index.html

<p>this is a test</p>

command:
npx parcel build index.html

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 2.0.0-beta.1
| Node | v10.19.0
| npm/Yarn | 6.14.2
| Operating System | macOS 10.15.5

Good First Issue Bug Help Wanted

All 7 comments

You can see we actually use the clone module in config.js:

I think this is the actual problem:

My guess is that res.tree is undefined, but I'm unsure what would cause that

JSON.parse(JSON.stringify(undefined) throws that error

@mischnic you're right, I just assumed posthtml would throw instead of returning undefined at first. But it does seem like the JSON.parse(JSON.stringify(...)) was intentional to remove functions for serialisation and that JSON.stringify(undefined) throw that exact error...

@maxweisel can you create a small test-case we can use to reproduce this?

I'm wondering if it's undefined why posthtml didn't throw an error and just returned undefined... Seems kind of strange

Sure thing. Here you go!

Max

parcel2-posthtml-obfuscate.zip

I've figured out why it happens, it's still pretty weird that posthtml didn't throw an error but apparently we did not instantiate plugins in the loadPlugin function if the config object is empty. Will create a PR with a test case and fix shortly

So you can workaround this using the following posthtml config:

{
  "plugins": {
    "posthtml-modules": {
      "root": "./src"
    },
    "posthtml-obfuscate": {
      "includeMailto": false
    }
  }
}

Wonderful! Thanks for the incredibly fast responses.

It seems like this issue is happening again in parcel ^2.0.0-beta.1. It should probably be re-opened.

@john01dav I was having this issue as well using 2.0.0-beta.1. I switched to using parcel@nightly, and all works as expected now. Try that if you can.

Was this page helpful?
0 / 5 - 0 ratings