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
this is what my .posthtmlrc file looks like:
{
"plugins": {
"posthtml-modules": {
"root": "./src"
},
"posthtml-obfuscate": {}
}
}
I'd expect the posthtml transformer to work without an error. This example (code provided below) works fine with parcel v1.
馃毃 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)
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
.posthtmlrc
{
"plugins": {
"posthtml-obfuscate": {}
}
}
index.html
<p>this is a test</p>
command:
npx parcel build index.html
| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 2.0.0-beta.1
| Node | v10.19.0
| npm/Yarn | 6.14.2
| Operating System | macOS 10.15.5
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
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.