parcel-bundler / parcel

The zero configuration build tool for the web. πŸ“¦πŸš€
https://parceljs.org
MIT License
43.4k stars 2.26k forks source link

bundle-text: returns file hash (or something like it) instead of contents for HMR updates. #8481

Open benmosher opened 2 years ago

benmosher commented 2 years ago

πŸ› bug report

When using bundle-text:, hot reloading results in the text being a hash instead of the file content after the file is modified.

πŸŽ› Configuration (.babelrc, package.json, cli command)

{
  "extends": [
    "@parcel/config-default",
    "../icons/.parcelrc" // for sprites
  ],
  "compressors": {
    "*.{html,css,js,svg,map}": [
      "...",
      "@parcel/compressor-gzip"
    ]
  }
}

icons/.parcelrc: (shouldn't matter but just in case?)

{
  "transformers": {
    "sprite:*.svg": ["parcel-transformer-svg-sprite"],
    "sprite:*.js": ["..."],
  },
  "packagers": {
    "*.html": "parcel-packager-svg-sprite"
  }
}

πŸ€” Expected Behavior

import contents from "bundle-text:./file.xml"

contents is a string containing the file content on initial render, and after HMR updates.

😯 Current Behavior

contents is the file contents at initial page load, but is presented as a hash string of the contents if the file is modified. Refreshing the page results in contents containing the file contents inline again.

πŸ’ Possible Solution

Could be crosstalk between react-fast-refresh and HMR, perhaps?

Creating my own module.hot?.accept() didn't seem to make any difference.

πŸ”¦ Context

I have a handful of XML files (not RSS/Atom!) that I am loading and parsing. Eventually these will be requested from the backend but for the moment I'm prototyping with them locally.

I was hoping to use import * as files from 'bundle-text:./*.xml' which, again, works great on the initial page load but as I'm tweaking the XML I'd like to get hot reloads.

I am working around this with e.g.:

const BEN_TEST = fs.readFileSync(__dirname + '/ben-test.xml', 'utf8')
const HELLO_WORLD = fs.readFileSync(__dirname + '/hello-world.xml', 'utf8')

These do update via HMR correctly, as expected, when modified.

πŸ’» Code Sample

🌍 Your Environment

Software Version(s)
Parcel 2.7.0
Node v18.7.0
npm/Yarn Yarn 3.1.1
Operating System macOS Monterey 12.5.1 (21G83)
cairomassimo commented 1 year ago

Same issue here. Seems to occur with data-url: as well. After HMR, the entire URL is replaced with a hash string (not a valid URL anymore).

cairomassimo commented 1 year ago

Made a couple of tests:

{
  "extends": "@parcel/config-default",
  "transformers": {
    "*.md": [
      "@parcel/transformer-inline-string"
    ]
  }
}

and

import content from "./file.md";

export function Component() {
    return <>{content}</>;
}