-
-
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
babel transpile node_module dependencies #1655
Comments
This seems like a relevant place to mention that the local modules ( It should also be noted that in order to effectively use the local modules pattern, the |
Right now a project I'm currently working on is using along the lines of: // .browserslistrc.packages
node 10.11
// package.json
{
"scripts": {
"postinstall": "npm-run-all -p \"postinstall:*\"",
"postinstall:p-retry": "cpy --rename=.browserslistrc .browserslistrc.packages node_modules/p-retry",
"postinstall:query-string": "cpy --rename=.browserslistrc .browserslistrc.packages node_modules/query-string"
}
} It's super hacky, but it more or less works. |
Babel 7 allows custom transpilations of dependencies in node_modules with project-wide
Can we somehow emulate this on Parcel 1.x ? Will be such scenarios supported in Parcel 2, where a package consumer can decide how he transpiles a package? E.g. a use case would be a common module with es-next language syntax which can be consumed by browser (consider transpile to browser target, use babel plugins like I know, there is |
Parcel 2 will use the provided babel config without modification (and will most likely also support For future reference, from the other issue:
|
Are there other requirements for that to work? Every IE support issue in this tracker is being closed and marked as duplicate of this one. But I cannot get IE 11 to work. Even adding a What am I missing? const fs = require('fs');
const package = JSON.parse(
fs.readFileSync('package.json', { encoding: 'utf8' })
);
const modules = Object.keys(package.dependencies);
const browserslistrc = '.browserslistrc';
modules.forEach(name => {
fs.copyFileSync('.browserslistrc', `node_modules/${name}/${browserslistrc}`);
console.log(`copied ${browserslistrc} to node_modules/${name}`);
}); If I first run that script, and after it |
|
@floyd-may , I've been defining
Does the fact that I'm using both |
@smeijer Your Part of the issue is that Parcel seems to strictly require you to use npm packages that follow the general convention that bundled/compiled versions of a package are published as ES5 code for maximal compatibility. The downside is that when (for whatever reason) you need to use a package that does not follow this convention, there doesn't seem to be an easy way to make Parcel transpile the module. The hack I figured out when I used to use Parcel was to copy whatever ES6 file(s) were causing grief and manually transpile them. Then I just used the transpiled versions and ignored the original module. The disadvantage of this approach is that it makes receiving updates hard, but chances are the modules which don't bother transpiling down to ES5 probably aren't updated frequently anyways. The long-term solution I found was to switch to Webpack 4 for new projects. It incrementally rebuilds much faster (at least for me) and it provides much greater flexibility over how Babel and other processes run. |
I have a feeling people in this thread are talking of two different things as if they were the one and same...
But the problem discussed in this thread is that Parcel (v 1.*) doesn't transpile external node modules even if you put a browsers list in your project. The root cause of this is that Parcel assume NPM packages are ES5 by default and thus no need to transpile them. One then needs to find some way to "convince" Parcel somehow that the NPM package is actually in a very modern form of JavaScript and needs to be transpiled to be browser-compatible. One hacky way of doing this is putting a So, while the root |
@arntj thanks for the clarification! |
Exactly! I don't want to assume that all node_modules are already transpiled. Not every developer follows the same browser support so assuming node_modules are safe for every project feels irresponsible. |
So if I'm understanding correctly; I have been doing this wrong? #1655 (comment) I've been copying So, So correct? |
I used parcel and svelte for several months, // postinstall.js
const fs = require('fs')
const nodeVersion = 'node 10.11'
// Patch to node_modules/*
const patch = (staticPath) => {
let folderNames = fs.readdirSync(staticPath)
for(let folderName of folderNames){
let stats = fs.statSync(staticPath + '/' + folderName)
if(! stats.isDirectory()) continue
try{
let packageFilePath = `${staticPath}/${folderName}/package.json`
let browserListFilePath = `${staticPath}/${folderName}/.browserslistrc`
let packageFileData = JSON.parse(fs.readFileSync(packageFilePath))
delete packageFileData['browserslist']
fs.writeFileSync(browserListFilePath, nodeVersion)
fs.writeFileSync(packageFilePath, JSON.stringify(packageFileData, null, 2))
// console.log(`Fixed browserlist in ${packageFilePath}`)
// Patch to node_modules/*/node_modules/*
let nestedModulePath = `${staticPath}/${folderName}/node_modules`
if(fs.existsSync(nestedModulePath)) patch(nestedModulePath)
}catch(e) {}
}
}
patch('./node_modules')
console.log(`All browserlist has been updated.`) Enter the above code into the project main folder to 'postinstall.js' {
"scripts": {
"postinstall": "rm -rf ./.cache && node ./postinstall.ts"
}
} Now, if you do CAUTIONPostinstall does not appear to run automatically on manual module installation commands such as |
as npm hooks mentioned, a not so elegant way to run Postinstall automatically is add node_modules/.hooks/postinstall :
remember to run |
😔 |
When you place a However, when a (The script above by hmmhmmhm is actually removing the existing |
This comment has been minimized.
This comment has been minimized.
The only thing that prevents the Babel transformer from running on node_modules is this check (and a few other parcel/packages/transformers/babel/src/config.js Lines 29 to 32 in 49ac217
Transformers themselves run on every asset cc @wbinnssmith |
@mischnic why can't there be another I don't really see the point of not transpiling node_modules. Transpilation should not make anything worse or code not run, it can only make code run that otherwise wouldn't. So if the target says a language feature needs to be transpiled, then why not transpile everything? And I don't care if my code for old browsers builds in 3s or 5s. |
Hi guys, tested on parcel 2.0.0-nightly.577+be12a3cd Edit: My code actually works now. I really don't understand the reasoning behind not transpiling everything by default. |
Thanks for this @danieltroger. One of the libraries I depend on ( The only thing which worked for me was indeed changing Hopefully this will be configurable in the future, because it seems there's not really a supported way of achieving this at the moment. — I also don't think most users would mind the time difference for transpiling For now, on my CI step, before building: sed -i 's/!filePath.includes(NODE_MODULES)/true/' node_modules/\@parcel/core/lib/summarizeRequest.js |
Hey @zanona, thanks for your comment, I'm glad I could help :)
I actually wanted to do this and got more help from the fabolous @mischnic here: #5807 (reply in thread) I found a way to not have to transpile my polyfill tho and am currently very busy. Otherwise I would have attempted to add a flag, because while I don't see anyone adding it - they are stuck discussing - I don't think a PR would be rejected if it's just a non-invasive flag. Feel free to attempt to figure it out :D |
Parcel is sucked in this case. |
`node_modules` can't be transpiled, see parcel-bundler#1655 and parcel-bundler#6306 (comment)
Has anyone had any luck applying this workaround in Yarn 3? There is no longer a Are there any plans to fix this in parcel? I also don't see a point in not transpiling node_modules. I wonder if it would make sense to add a new target option like
|
@tyler-ham yeah, I have it working nicely with yarn 3. Did you see this? #6821 (comment)
When using my patch you'll have to set an environment variable on what you want to transpile, i.e.
It definitely works (I'm using yarn 3.1.0) so you must have configured something not-optimally.
Actually I am wiser now and it is indeed important that not everything in node_modules is transpiled. The reason is that it breaks core-js. And that's, I think, because of symbols. So basically |
We were actually thinking of transpiling node_modules by default, also because swc is now way faster than Babel. But that could indeed be a problem |
@danieltroger thank you for your help! I did see that patch, but I couldn't get it to work even with just It is very helpful to know that you have it working in yarn 3 with a patch like that. So I believe you are right: I must have something else not configured well. I will try stripping things down to figure out what is wrong with my config. I did manage to patch summarizeRequest.js manually by making |
@tyler-ham glad to help! Can you confirm that you added the |
@danieltroger Yes. After cleaning up from all the things I had been trying and making a fresh attempt at applying the patch, it started working. I must have had a typo somewhere. Here are a few notes on how I made use of the patch in case anyone else finds it useful: I added a diff --git a/lib/summarizeRequest.js b/lib/summarizeRequest.js
index d13efe8fcaf441d5488b60c0e666deff568389d9..ee36a4f7b685c403a15ded8d49a073d30ab616fd 100644
--- a/lib/summarizeRequest.js
+++ b/lib/summarizeRequest.js
@@ -56,6 +56,16 @@ async function summarizeRequest(fs, req) {
}
function isFilePathSource(fs, filePath) {
+ if (process.env.FORCE_TRANSPILE) {
+ for (const thing of process.env.FORCE_TRANSPILE.split(",")) {
+ if (filePath.includes(thing)) {
+ if (process.env.FORCE_TRANSPILE_VERBOSE) {
+ console.log("FORCE TRANSPILING", filePath);
+ }
+ return true;
+ }
+ }
+ }
return !filePath.includes(NODE_MODULES);
}
I also wanted to be able to define #!/usr/bin/env bash
#
# parcel.sh
# Execute from the project root, like `./build-scripts/parcel.sh`
set -eo pipefail
# Source .env and .env.local files, if they exist, to look for relevant
# environment variables that may be set there.
if [ -f .env ]; then
source .env
fi
if [ -f .env.local ]; then
source .env.local
fi
export FORCE_TRANSPILE="$FORCE_TRANSPILE"
export FORCE_TRANSPILE_VERBOSE="$FORCE_TRANSPILE_VERBOSE"
# Execute yarn parcel with any parameters passed in.
yarn run parcel "$@" Then in my "scripts": {
"start": "./build-scripts/parcel.sh",
"build": "./build-scripts/parcel.sh build"
} And finally, in FORCE_TRANSPILE=package-1,package-2,package-3
FORCE_TRANSPILE_VERBOSE= Toggle verbose mode on: FORCE_TRANSPILE_VERBOSE=1 |
Guys, i know that it's hacky, but i got arround it with double transpilation -
After some time ended with usage of |
❔ Question
I need a way to babel transpile some dependencies in
node_modules
, and there doesn't seem to be a way to do so with parcel.I've read these relevant issues: #948 #13 , and I'm aware of #1101 which solves the case for local repo symlink.
I'm using
file:
dependencies to reference local repos in my project and I need to run babel for those, but no existing solution covers this case.What would the problems be if we simply specify the target
node_modules
that need to be transpiled with babel? I'm curious why this wasn't considered as an alternative solution to #1101Having the repos flag themselves as "source" only solves part of the problem, and being able to manually specify packages from the root project seems a lot more intuitive and flexible. I agree that the community needs to collectively come up with a cleaner, more thought-out solution for managing code compatibility for released packages/modules, but until then I believe it is up to the package consumers to make sure that every code they import and release into production is transpiled and made compatible with their desired browser targets. Parcel is a tool for package consumers, therefore I feel it should support such use case.
🌍 Your Environment
The text was updated successfully, but these errors were encountered: