Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

@parcel/transformer-svg and @parcel/transformer-css clashing over CSS inline styles #7479

Closed
SirkoS opened this issue Dec 22, 2021 · 5 comments 路 Fixed by #7482
Closed

@parcel/transformer-svg and @parcel/transformer-css clashing over CSS inline styles #7479

SirkoS opened this issue Dec 22, 2021 · 5 comments 路 Fixed by #7482

Comments

@SirkoS
Copy link

SirkoS commented Dec 22, 2021

馃悰 bug report

Applying @parcel/transformer-svg on an HTML-file with inline styles breaks a subsequent @parcel/transformer-css call.

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

I tried to find an MVE on the issue and so far reduced to this

package.json

{
  "name": "parcel-test",
  "version": "0.1.0",
  "description": "",
  "scripts": {
    "build": "npx parcel build test.html --no-source-maps --public-url ./ --no-cache"
  },
  "keywords": [],
  "license": "MIT",
  "dependencies": {
    "parcel": "^2.0.1"
  },
  "devDependencies": {
    "@parcel/config-default": "^2.0.1",
    "@parcel/transformer-svg": "^2.0.1"
  }
}

.parcelrc

{
  "extends": "@parcel/config-default",
  "transformers": {
    "*.html": ["@parcel/transformer-svg", "..." ]
  },
}

test.html

<!doctype html>
<html>
<body>
  <h3>test</h3>
  <svg>
    <image href="./gfx/architecture/kicktipp.png" x="150" style="width: 300px" />
  </svg>
</body>
</html>

CLI command

npm run build

馃 Expected Behavior

Both the image as well as the inline CSS are processed and optimized.

馃槸 Current Behavior

Instead the above example result in the following error message - no output being generated.

> parcel-test@0.1.0 build
> npx parcel build test.html --no-source-maps --public-url ./ --no-cache

脳 Build failed.

@parcel/transformer-css: S:\tmp\parcel\test.html:1:1: Unknown word

  CssSyntaxError: S:\tmp\parcel\test.html:1:1: Unknown word
  at Input.error (S:\tmp\parcel\node_modules\.pnpm\postcss@8.4.5\node_modules\postcss\lib\input.js:148:16)
  at Parser.unknownWord (S:\tmp\parcel\node_modules\.pnpm\postcss@8.4.5\node_modules\postcss\lib\parser.js:522:22)
  at Parser.other (S:\tmp\parcel\node_modules\.pnpm\postcss@8.4.5\node_modules\postcss\lib\parser.js:149:12)
  at Parser.parse (S:\tmp\parcel\node_modules\.pnpm\postcss@8.4.5\node_modules\postcss\lib\parser.js:59:16)
  at Function.parse (S:\tmp\parcel\node_modules\.pnpm\postcss@8.4.5\node_modules\postcss\lib\parse.js:11:12)
  at Object.parse (S:\tmp\parcel\node_modules\.pnpm\@parcel+transformer-css@2.0.1\node_modules\@parcel\transformer-css\lib\CSSTransformer.js:116:35)
  at async Transformation.runTransformer (S:\tmp\parcel\node_modules\.pnpm\@parcel+core@2.0.1\node_modules\@parcel\core\lib\Transformation.js:601:17)
  at async Transformation.runPipeline (S:\tmp\parcel\node_modules\.pnpm\@parcel+core@2.0.1\node_modules\@parcel\core\lib\Transformation.js:366:36)
  at async Transformation.runPipelines (S:\tmp\parcel\node_modules\.pnpm\@parcel+core@2.0.1\node_modules\@parcel\core\lib\Transformation.js:244:40)
  at async Transformation.runPipelines (S:\tmp\parcel\node_modules\.pnpm\@parcel+core@2.0.1\node_modules\@parcel\core\lib\Transformation.js:287:34)

馃拋 Possible Solution

Not a solution, but an observation:

  • @parcel/transformer-svg seems to already move inline CSS to a separate asset in order to optimize. The specific occurrence is replaced with a key to later substitute again for the optimized CSS (I guess).
  • However, @parcel/transformer-css tries to do the same, but this time only retrieves the previous key, which then fails once it is passed to postcss for parsing.

馃敠 Context

I'm trying to package an HTML file with quite mixed content (actually derived from a reveal.js presentation).

If I remove @parcel/transformer-svg from the .parcelrc the inline-style works just fine, but the image is not processed.
Adding @parcel/transformer-svg and removing the inline style works just fine and the image is processed as expected.

But the combination of both (adding @parcel/transformer-svg and using inline style) breaks parcel it seems.

馃捇 Code Sample

All necessary code is attached above.

馃實 Your Environment

Software Version(s)
Parcel 2.0.1
Node 16.3.0
npm 7.5.1
pnpm 6.24.2
Operating System Windows 10
@folknor
Copy link

folknor commented Dec 23, 2021

It might be a naive question but can I ask why you're using transformer-svg on a HTML file? I have several projects with SVG embedded in the HTML and I've never done that. Parcel automatically installs optimizer-svgo for me, so I presume it's doing something to process the inline SVGs.

@SirkoS
Copy link
Author

SirkoS commented Dec 23, 2021

It might be a naive question but can I ask why you're using transformer-svg on a HTML file?

As I mentioned, without @parcel/transformer-svg the images referenced within the SVG are not recognized and thus are not part of the result in /dist.

Maybe I'm doing something else wrong, but this seems like a bug (somewhere) to me.

@folknor
Copy link

folknor commented Dec 23, 2021

Indeed, I did not read the "Context" part of your description - my mistake.

I can confirm that parcel nightly does not process image references in SVG elements, tested using

<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
	<image href="images/image.jpg?width=200&quality=40" height="200" width="200" />
</svg>

When served or built using parcel without transformer-svg, the resulting HTML is exactly the same as the input.

@mischnic
Copy link
Member

Running @parcel/transformer-svg on HTML is definitely not the intended setup

So <image> tags in HTML not being detected as dependencies is a feature request/bug. (And the workaround you tried apparently doesn't work)

@Shinyaigeek
Copy link
Contributor

Shinyaigeek commented Dec 25, 2021

https://github.com/parcel-bundler/parcel/blob/v2/packages/transformers/html/src/dependencies.js#L8-L28

In collecting HTML dependencies, it supports the xlink: href of the image tag, SVG 1.1 notation, so it seems reasonable to support the href of the image tag, which is the SVG2 notation.

Simply it is a tiny fix, only to add "image" to ATTRS.href in the code I'm quoting. I will send PR.

(By the way, the href of the image tag is supported when collecting the dependencies in svg)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants