Add support to transpile modules inside node_modules
Now some of us ships NPM packages (specially components) written in ES2015 without transpiling them.
That's a pretty good thing specially if they are gonna used in a project like Next.js or CRA (which does transpiling). They offer benefits like:
- No need to transpile before shipping to NPM
- Get the benefit of Webpack 2's tree-shaking
But we can't do this now we exclude everything inside node_modules from babel transpiling.
So, here's the proposed solution.
We have an entry in next.config.js to include modules which needs to go through babel. See:
module.exports = {
transpileModules: [
"my-component",
"redux/src"
]
}
- Why is this not doable with
webpack()extension? transpileModulessounds better- Will we accept regexps?
Why is this not doable with webpack() extension?
I hope you mean custom webpack config. That's doable. But this sits in a existing loader. Getting that is somewhat harder.
transpileModulessounds better
Awesome. I'll update.
Will we accept regexps? Yep. sure.
Hey,
As I'm starting my own website I'm trying new techs like Lerna/Next/Styled... and would be happy to provide early feedback on this.
I've opened a duplicate issue where I tried to import/transpile a CRA-based module in my NextJs module but didn't know how to make the transpilation happen (note that I'd like to keep my module runnable as a standalone)
I've also noticed that Babel, also based on Lerna, is transpiling each modules before exposing them, but it looks to me better to do like @arunoda suggest and let the client app do the transpiling.
I'd like to have a single babel config for my client and share that config with all my decoupled modules. That's probably not so easy if I want to keep ability to run my modules as standalone, outside of Next runner
My current test project is here: https://github.com/slorber/playground/ I'll try to upgrade it as soon as there's a fork/PR. @arunoda are you working on it?
@slorber currently we are focusing on 2.0 release and we are fine tuning stuff and finding bugs as possible as we can.
I haven't started work on this but we can do this just after 2.0.
Ok so I'll make a fork. I'm already running against 2.0.0 beta because I'm not building a critical website and I don't think webpack 1.13 resolve jsnext:main/module field.
I'm not a bundler expert but I think I'd rather use "module" field of package.json no? "main" seems for already transpiled code as far as I know. But as the webpack config allows to include/exclude transpilation I'm not sure it's relevant. Any recommendation on which of the 3 fields I'd rather use?
@slorber I think webpack only supports main just like NPM. You can use that.
We can check for the filepath in the exclude function in our next.conf.js
Hmm according to what I've seen in practice against Next 2.x I've seen module works (but fails later at runtime because not transpiled) while jsnext:main did not work (as far as I remember). But it's supposed to be supported.
Anyway, jsnext:main or module does not seem to be the solution to this problem so for company-internal modules just enabling transpilation is probably enough
The community has not agreed on one approach right? For example, I was able to use react-youtube the other out of the box with no problems. I'm assuming a big number of modules transpile before publish?
Ref: https://github.com/rauchg/blog/blob/master/components/post/youtube.js
Yes that makes sense to always transpile before publish because you don't know who/how the module will be consumed and you don't want to force the client to setup appropriate babel settings for your lib. That's what Rollup suggest: to publish the module transpiled in different ways so that bundler can decide which to use.
But for company internal packages, the transpilation settings might be the same across several project (like a babel preset) and it makes sense to me to let the client bundler to transpile all the company dependencies
Very much agree @slorber - this would be very handy for internal modules if you're breaking your project up and isolating things as much as possible.
And @rauchg / @arunoda supporting RegExp's would be really nice, so you could have one entry that catches all the company internal modules, using say the NPM org namespace:
// next.config.js
module.exports = {
transpileModules: [
/^\@my-npm-org\/.*/
]
}
Beautiful suggestion @philcockfield
Hey maybe it could be worth offering some presets. It looks to me most tools (Lerna/npm link...) rely on symlinks so why not something as simple as:
module.exports = {
transpileModules: ["symlinks"]
}
The more I use next.js in earnest, and build out a rich library of modules around it, the more this feature becomes important. It's becoming a real PITA replicating the babel compilation step in my internal modules.
🚀🤖
I'm working on this today :)
@philcockfield give this a try: https://github.com/zeit/next.js/pull/749
thanks @arunoda
So as commented on your PR if this does not support symlinks the feature will be a bit limited because it won't work with npm link or Lerna, but only for npm modules that are not transpiled (right? I don't see any other usecase unless you commit modules inside /node_modules)
Why not supporting symlinks? is it harder to support?
Also I wanted to test your branch on my app, but I'm not sure what's the best way to do that. Is there any known procedure so we can easily test a branch and it's not too painful for the tester? I've tried some stuff like:
- npm install https://github.com/arunoda/next.js.git#add-706 : fails because the next /bin folder is empty on github repo
- git clone of the fork inside /node_modules: not much success (but it might be because of my specific Lerna settings)
What's the best way to test a fork currently?
If you're looking at doing this with next.config.js: module.exports = { webpack: (config, then config.module.rules has a few things, looks like you need to change one of these rules, or add one?:
{ loader: 'babel-loader',
include: '/Users/me/gh/guide/node_modules/next/dist/pages',
options:
{ babelrc: false,
cacheDirectory: true,
sourceMaps: 'both',
plugins: [Object] } },
{ test: /\.js(\?[^?]*)?$/,
loader: 'babel-loader',
include:
[ '/Users/me/gh/guide',
'/Users/me/gh/guide/node_modules/next/dist/pages' ],
exclude: [Function: exclude],
query:
{ babelrc: true,
cacheDirectory: true,
sourceMaps: 'both',
presets: [] } } ]
Looking forward to the simpler syntax suggested.
Sorry for my ignorance I cant see what the resolution of this issue is? We would love to be importing es6 into our codebase, we need the tree-shaking .
Is there a PR on this?
@andrewmclagan This issue is still open and has a related PR that probably won't satisfy all (like LernaJS users)
What's the status of this? Are there any other ways to make next's webpack to transpile files imported from node_modules ?