-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to serve static files? #1080
Comments
To get the url you can do something like this:
This will return the new hashed unique url of the file instead of the original filename. |
Because Parcel - Could not serve static files parcel-bundler/parcel#1080 - Could not use native reload on HMR parcel-bundler/parcel#289
Because Parcel - Could not serve static files parcel-bundler/parcel#1080 - Could not use native reload on HMR parcel-bundler/parcel#289
The option of bundling a static file is not a real replacement for the option of serving a static file in many cases. For example, an application may have several large datasets in separate files, and should only load them one at a time when needed. |
Please correct me if I'm wrong, but it seems like one easy way to achieve that is to run Parcel like this: npx parcel index.html data/**/* That way all the files in the edit: it only works if There is also this: https://www.npmjs.com/package/parcel-plugin-static-files-copy |
@tad-lispy I couldn't get it to run with the combined command, but the plugin you referenced works wonderfully. Thanks! |
@tad-lispy json files are compiled to js files this way... |
It just occurred to us that with Parcel 2 alpha 3 and this parcelrc {
"extends": "@parcel/config-default",
"transformers": {
"url:*": ["@parcel/transformer-raw"]
}
} you can import files which will then be copied to the dist folder: import file from "url:./static.json"
fetch(file)... |
according to @tad-lispy solution, I found this almost working: folder structure:
package.son {
"scripts": {
"build": "parcel build src/index.html",
"postbuild": "parcel build 'public/**/*'",
"gh": "npm run build -- --public-url='/my-app-repo-name/'"
}
} usage npm run gh
tree dist -a
dist
├── app.f84e1103.js
├── app.f84e1103.js.map
├── bg.svg
├── favicon.9c9f1c97.ico
├── index.html
├── main.dc25f873.css
├── main.dc25f873.css.map
└── menu.svg but unfoturnately unknown raw files wont be processed as well as directory structure will be broken... so I have simply implemented this easily just by using ncp ( {
"scripts": {
"build": "parcel build src/index.html",
"postbuild": "ncp ./public ./dist --filter '**/*.*'",
"gh": "npm run build -- --public-url='/my-app-repo-name/'"
}
} now everything is working as expected npm run gh
tree dist -a
dist
├── .nojekyll
├── app.f84e1103.js
├── app.f84e1103.js.map
├── favicon.9c9f1c97.ico
├── images
│ ├── bg.svg
│ └── menu.svg
├── index.html
├── main.dc25f873.css
└── main.dc25f873.css.map Hope this helps. Regards, |
For the current version of parcel 2 I'm using (^2.0.0-alpha.3.2), the url:path pattern is default behaviour. So simply use the snippet below to get the url of a static file that you don't want any transpiler to modify: import csvFileUrl from 'url:./file.csv'
// Use the url to fetch the file asynchronously
fetch(csvFileUrl)
.then(response => ...) You no longer need to modify/create any On a side-note, it also seems like "transforms" has been renamed to "transformers" in |
What I did is use a postbuild script to copy over the static assets: "scripts": {
"postbuild": "cp -r public/* dist/", |
Have you seen parcel-bundler/website#655 (or the linked issue)? |
Hi @bergkvist |
@nardove Are you sure you're using Parcel 2? |
I just did npm -v parcel-bundler and got 6.14.5 🤔 weird |
But if you've installed |
Correct I made the installation global and look and the package.json file and is indeed v1.12.4, I'll delete this version and install V2 Thank you! |
It does seem this has been fixed by the |
How to serve static files like json , csv, tsv etc.
🤔 Expected Behavior
axios.get('data/data.json',).then((data) => console.log(data))
😯 Current Behavior
http request failed!!!
The text was updated successfully, but these errors were encountered: