question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parcel ignores resources declared in JS template literals

See original GitHub issue

🐛 bug report

LitElement declares its ShadowDOM HTML in JavaScript template literals. When CSS files or other assets are declared inside the literal string Parcel doesn’t bundle them. CSS is especially relevant to allow CSS files to be injected into the ShadowDOM. Eg:

import { html } from 'lit-element';

export default (element) => html`
    <link rel="stylesheet" href="/css/global.css">
    <h1><slot></slot></h1>
`;

The JSX pattern for including resources does not work for template literals

🎛 Configuration (.babelrc, package.json, cli command)

parcel --port 8080 --https ./src/index.html --cache-dir ./build/cache --out-dir ./build/make

🤔 Expected Behavior

Assets declared in JS template literals are bundled as normal

😯 Current Behavior

The assets included in the template strings are ignored.

💁 Possible Solution

a) Parcel should parse JS template literals for resources and include them b) Allow specific resource path strings to be mapped and replaced during the build process c) Import a path variable that can be included in the template literal as a variable

🔦 Context

Build WebComponents using JavaScript template literals

💻 Code Sample

import { html } from 'lit-element';

export default (element) => html`
    <link rel="stylesheet" href="/css/global.css">
    <h1><slot></slot></h1>
`;

🌍 Your Environment

Software Version(s)
Parcel 1.12.3
Node 12.6
npm 6.9
Operating System Mac OS 10.13.6

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
mischniccommented, Oct 6, 2019

@nstansbury this seems to work: https://codesandbox.io/embed/parcel-issue-3553-1dobw

import { html } from "lit-html";
import img from "./img.png";

export const App = () => html`
  <div>
    <img src="${img}" />
  </div>
`;
0reactions
itamarzil123commented, Nov 5, 2020

One Year Later Here. I’m in a middle of vanilla JS project right now. I’m using template literals for rendering. (basically throwing template literals style html code into the innerHtml of DOM element. I have the same problem -> <img src="images/pic.png" /> will not work from within a template literal while it does work from within my index.html file. Finally I chose the solution above, by @mischnic, and so instead of an array containing my list of images, it is an array containing list of import img from ‘…/images’. WORKS FINE. any other solutions in 2020 ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can ES6 template literals be substituted at runtime (or reused)?
Yes you can do it by parsing your string with template as JS by Function (or eval ) ... of a 'string' )...
Read more >
API - ESBuild
Bundling with esbuild only works with statically-defined imports (i.e. when the import path is a string literal). Imports that are defined at run-time...
Read more >
Properly configuring server MIME types - MDN Web Docs
If the browser ignores the reported MIME type, web administrators and authors no longer have control over how their content is to be...
Read more >
Network security configuration - Android Developers
For example, trusting particular self-signed certificates or ... You can specify multiple certificate sources for a configuration in ...
Read more >
css-loader - npm
Then add the plugin to your webpack config. For example: file.js ... type url = | boolean | { filter: (url: string, resourcePath:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found