Comments (14)
I don't really think this should be the responsibility of the bundler... Can you just enable CORS and hit localhost:3001 directly?
Alternatively, you could build your own small server with express and use parcel as a middleware.
let Bundler = require('parcel-bundler');
let express = require('express');
let bundler = new Bundler('entry.html');
let app = express();
// define proxy routes here
app.use(bundler.middleware());
from parcel.
const proxy = require('http-proxy-middleware')
const Bundler = require('parcel-bundler')
const express = require('express')
const bundler = new Bundler('src/index.html', {
cache: false
})
const app = express()
app.use(
'/api',
proxy({
target: 'http://localhost:3000'
})
)
app.use(bundler.middleware())
app.listen(Number(process.env.PORT || 1234))
from parcel.
Hey @webular , you can probably use browser-sync and http-proxy-middleware to achieve your end goal and it's relatively easy to set it up. E.g.
import assign from 'lodash/assign';
import Bundler from 'parcel-bundler';
import serve from 'browser-sync';
import proxy from 'http-proxy-middleware';
import compress from 'compression';
const bundler = new Bundler('index.html');
serve({
port: process.env.PORT || 3000,
open: false,
server: { baseDir: 'dist', https: true },
middleware: [
proxy(`${envConfig.endpoint}/api`, assign({}, proxyConfig)),
proxy(`${envConfig.endpoint}/auth`, assign({}, proxyConfig)),
compress(),
bundler.middleware()
],
});
from parcel.
An complete example with express
const proxy = require('http-proxy-middleware')
const Bundler = require('parcel-bundler')
const express = require('express')
let bundler = new Bundler('src/index.html')
let app = express()
app.use(
'/api',
proxy({
target: 'http://localhost:3000'
})
)
app.use(bundler.middleware())
app.listen(Number(process.env.PORT || 1234))
from parcel.
It would be a very useful feature for spa.
from parcel.
@devongovett I always use create-react-app proxy This allows me to use relative urls like /api/endpoint
and /graphql
. The same urls work then after deployment to production server.
from parcel.
@tb Oh - thats exactly what I want. Thx for the tip!
from parcel.
It's not the responsibility of the bundler, but it would be a very useful feature for the dev server. If your server is not in NodeJS (and you don't want to enable CORS), then without this you have to use some kind of post-processing to replace "locaholhost:PORT" with something else when deploying to production. It's much nicer to be able to use relative links and have the dev server have a config to proxy to the appropriate backend endpoint.
from parcel.
For the dev-server, I think this would be useful for non-spa sites as well (php applications, etc). Where you proxy the app (myphpsite.test
), but watch the assets and/or templates directory for changes. Templates directory changes would trigger full reloads, while asset changes trigger HMR (in a perfect world)
from parcel.
@albinotonnina oh~good example, but how can i use parcel no cache in this example ? like this : node example.js --no-cache
or set some parameters in 'app.use(bundler.middleware())'
from parcel.
For those still needing more help, the socketio example chat app altered to bundle the client-side files:
larrybolt/chat-example@aaeba77
from parcel.
Hi @NullVoxPopuli,
Don't know really.
To understand: http://${process.env.API_HOST}
prints http://localhost:9091 ?
from parcel.
@albinotonnina nice example~ but i want to set --open parameters in new Bundle('src/index.html')
? like this:
const bundler = new Bundler(proxypath, {
open: true,
})
But this has no effect,how to do it? Thx~
from parcel.
For some reason Parcel's dev server automatically responds with the header Access-Control-Allow-Origin : *
. Is this normal behaviour? If so surely it solves this problem? I've tested tonight and I get no CORS warnings.
from parcel.
Related Issues (20)
- Parcel encountered error HOT 3
- There is typescript error in production build.
- parcel start works but not parcel build HOT 2
- Parcel build fails consistently with 10:SIGBUS and SIGSEGV
- Parcel fetching configs from where it's installed instead of where the process is running
- watch causes web extension background script to reload when tab with active content script is closed
- Importing D3 is causing TheLayout to produce invalid unicode escape sequence on D3.random HOT 4
- Parcel dev server randomly crashes when trying to unlink a file in the dist directory. HOT 1
- This experimental syntax requires enabling the parser plugin: 'optionalChaining' HOT 2
- Object spread operator causes build error HOT 1
- No import() polyfill available for context 'node'
- Parcel not minifying by default even when it says it does HOT 2
- Images are not created in the dist folder HOT 7
- Non-ASCII characters in identifiers result in invalid escape sequences
- parcel/svgo optimiser not working consistently for all packages
- `@parcel/packager-js: Got unexpected null` with CSS module composes
- Support for Typescript Proxies
- "@parcel/packager-css: Unsupported layer combination in @import"
- @parcel/rust package should only install binaries for the host platform HOT 2
- "process is not defined", when using Typescript (MRE included) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parcel.