Trying to include socket.io js file/link that is served by the server, how would i write it in the file and let parcel just include the script tag and not try to bundle the file ?
script(src="/socket.io/socket.io.js")
**Using PUG
_Originally posted by @antonedvard in https://github.com/parcel-bundler/parcel/issues/144#issuecomment-445451197_
Host it on a cdn or some kind of server and use the full url.
Parcel ignores paths that are urls
is it not possible to add somekind of flag to make parcel ignore that line? i dont think its a good idea to host my socket connection on a cdn :/
Can i skip it somehow with a plugin or a middleware ? tried bundler.on('bundleStart') but that obviously just gave me an array with the input.
Would be nice to be able to do
bundler.on('file', (file, skip) => {
if(file.name === 'socket.io.js')聽{
skip()
}
});
Or something similar; Would do it but my skills aren't there yet, have been scratching my head on this for 3 days now, really halting my working process :/
What you are proposing is currently impossible. As urls are being rewritten in the htmlasset which can't be acessed from outside.
Perhaps you can use an environment variable in pug with the url of the server, this way parcel knows it's a hosted asset by a server and should ignore it...
what is the order of the bundler? does it parse the pug file to html and then fetches the assets from the compiled html file or does it fetch the assets before it is compiled to html ?
sounds like that should work if the pug isn't compiled and then the assets fetched from the compiled html file...
@antonedvard not sure what you mean, but most deps are extracted by html, after compiling pug.
well you got it right :D then i guess the "string/path" would be considered a path and therefore fetched in to the bundler even if i used an env-var, but i'll try it out, give me couple of mins ;D
do i need to set the variable as an environment variable? isn't it enough to set it in the input file?
didn't work either... it fetches the dep after the file is compiled which gives the bundler the same thing as setting it in the file.. :/
is there no way to get passed this? parcel is such an amazing tool, don't want to go back to the all too complicated webpack 馃槩
Got any code snippet of what you tried?
It should work, I've done it multiple times in various projects.
well i just kept the same thing
script(src=process.env.SOCK)
Except i put the string in the .env
SOCK=/socket.io/socket.io.js
Yeah that won't work.
I meant something like this:
.env.development
SERVER_URL=http://localhost:1234/
.env.production
SERVER_URL=https://someserver.com/
index.pug
script(src=process.env.SERVER_URL + '/socket.io/socket.io.js')
ah i see, so i basicly have to have the server url in... which won't work i think, i'll try it, worst case scenario i could just remove the url part from the compiled html before adding to server, the only issue would be to do it after every compile while working on it...
Hope "ignore" option is an upcoming feature in parcel, perhaps the only thing i see missing, else i could almost call it a perfect tool! 馃槃 could perhaps write a random thing and then create another watcher on the compile folder that finds that scrambled url text and removes it, leaving only the wanted part... ;D hehe! hack upon hack upon hack... hackception... 馃檮
Hope that this will be a future feature in parcel, really should be.
meanwhile i built a tool that monitors the dist folder and modifies all html that include a certain string to the correct socket.io script tag.
Closing as duplicate of #1186
We have been using Parcel for less than a year. One of the biggest and pretty much only struggle we have had with parcel led me to this ticket. It also led me to https://github.com/parcel-bundler/parcel/issues/3137 and https://github.com/parcel-bundler/parcel/issues/144 and https://github.com/parcel-bundler/parcel/issues/1379 and possibly even others.
Our use case is that we have index.html and app.js, straightforward. Within a given release/version those files are static across environments (dev, uat, prod) as well as across clients (we are saas). On top of those two files there is an additional file, env.js, which contains important differences at both a client and an environment level.
index.html looks like this:
<div id="root">loading</div>
<script src="./env.js"></script> <!-- this file wants to be ignored -->
<script src="./app.js"></script>
In each particular deployment, env.js WILL be there, in the right place. But it's not _parcel_'s job to put it there, it's the job of the deployment code.
I can't use a fully qualified URL here because that would require a priori knowledge of where this build will be deployed. I tried two different parcel plugins that claimed to fix this very issue, by either putting a special comment structure into index.html or otherwise indicating ignored files in package.json. Neither of these plugins worked for me, and/or I don't know how to use them or plugins in general. I thought about fixing this with a hack in the deployment, finding and symlinking over the generated env.hash.js file. I was considering writing my own plugin and trying to understand the innards of Parcel, but this seemed a waste when Parcel 2 is really where attention is and should be.
This morning, I stumbled on a stupidly simply workaround that works perfectly for my needs and I thought I'd share the aha moment. I might go so far as to not call it a workaround but a solution: I told parcel that env.js was an entry point file!
Conceptually, this makes sense. If it's an entry file that means the URL in the output directory can't be renamed since others depend on it. Parcel DOES take my copy of env.js during the build step and re-write its contents to include the parcel.require
scaffolding, but it leaves index.html with src="env.js"
, and now my deployment-time volume mapping onto env.js works like a charm.
Thanks to the parcel team! Even with this particular frustration, this tool is amazing and very appreciated.
@scottlaplante Could you please post the full example i.e. how commands look like? Thank you in advance
@vicmosin you can specify more than one entry files, for example:
$ parcel build app.js env.js --out-dir public --public-url=./
Unfortunately this only solves the problem when the other file already exists, however it does not solve the problem of generated files or files, which are accessed by using a proxy to route requests...
I have the same issue. In a cordova / phonegap app. The cordova tools insert a cordova.js file which index.html includes. parcel chokes if you pass it index.html since the file doesn't exist (however it will when deployed).
Anyone using cordova solve this issue?
By now, I decided to move away from Parcel even for simple static websites, because the only solution I found was to write an actual script, which cleans up after Parcel and does all the things Parcel won't do. If that's what I have to do, I can just use WebPack.
@GimpMaster If you want to stick to Parcel, I would write a script, which starts Parcel using its API and after Parcel is finished injects the required script tag into the generated html file (for example by searching for the </head>
tag and replacing it with <script src="cordova.js"></script></head>
). It's not nice, but it gets the job done.
@minecrawler - I was able to use this parcel plugin: parcel-plugin-html-externals
and then define in my package.json external files to have parcel ignore. For example:
"externals": {
"cordova.js": false
}
This solved it for me. It would be great if parcel did this by default. I think I saw somebody even had a merge request for parcel2.
If you do use that plugin it seems newer versions of parcel require that you also npm install parcel-bundler
Most helpful comment
We have been using Parcel for less than a year. One of the biggest and pretty much only struggle we have had with parcel led me to this ticket. It also led me to https://github.com/parcel-bundler/parcel/issues/3137 and https://github.com/parcel-bundler/parcel/issues/144 and https://github.com/parcel-bundler/parcel/issues/1379 and possibly even others.
Our use case is that we have index.html and app.js, straightforward. Within a given release/version those files are static across environments (dev, uat, prod) as well as across clients (we are saas). On top of those two files there is an additional file, env.js, which contains important differences at both a client and an environment level.
index.html looks like this:
In each particular deployment, env.js WILL be there, in the right place. But it's not _parcel_'s job to put it there, it's the job of the deployment code.
I can't use a fully qualified URL here because that would require a priori knowledge of where this build will be deployed. I tried two different parcel plugins that claimed to fix this very issue, by either putting a special comment structure into index.html or otherwise indicating ignored files in package.json. Neither of these plugins worked for me, and/or I don't know how to use them or plugins in general. I thought about fixing this with a hack in the deployment, finding and symlinking over the generated env.hash.js file. I was considering writing my own plugin and trying to understand the innards of Parcel, but this seemed a waste when Parcel 2 is really where attention is and should be.
This morning, I stumbled on a stupidly simply workaround that works perfectly for my needs and I thought I'd share the aha moment. I might go so far as to not call it a workaround but a solution: I told parcel that env.js was an entry point file!
Conceptually, this makes sense. If it's an entry file that means the URL in the output directory can't be renamed since others depend on it. Parcel DOES take my copy of env.js during the build step and re-write its contents to include the
parcel.require
scaffolding, but it leaves index.html withsrc="env.js"
, and now my deployment-time volume mapping onto env.js works like a charm.Thanks to the parcel team! Even with this particular frustration, this tool is amazing and very appreciated.