- Webpack is a "module bundler". On its own it does not change the source of a program (note: there are some caveats to this, e.g. minification), only join all the different bits of a large codebase together for easier and more efficient delivery over the Internet (depending on the use case; you may be bundling server-side code, in which case the benefits are mostly around being able to consolidate build tooling).
- A webpack loader is used to process files during bundling. It is specific to webpack (you would not use
babel-loaderwithout webpack, except maybe in cases of interop with other build tools, but even then it would not be used on its own). - In a webpack configuration one specifies a mapping of file extensions to webpack loaders. For example, a common case is to process
.tsfiles usingts-loader. This way, webpack will pass off files with the.tsextension to the TypeScript compiler, and use the output of this compilation in the bundle, instead of the source program. - Babel is a compiler; it takes an ESNext program and produces an equivalent ES3+ compatible program.
babel-loaderdoes whatts-loaderdoes for TypeScript; passes off files to the Babel compiler, and returns the result to be used in the bundle in-place of the original source program.
Isn't transpiling and bundling is a separate process?
Yes. That is why we have "webpack the bundler", and "Babel the compiler/transpiler", and babel-loader to connect the two together. Without babel-loader webpack would not be able to process files through Babel.
Hope that helps.
Answer from sdgluck on Stack Overflow
» npm install babel-loader
why babel-loader is part of webpack instead babel itself?
Do you guys compile with ts-loader or babel-loader?
Are Babel and Webpack used often TOGETHER or is it 'one or the other'?
Is babel needed with webpack anymore?
I'm bundling with webpack and normally I use ts-loader for ts files and babel for js
{
test: /\.jsx?$/,
use: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}But I've noticed a lot of tutorial use babel-loader for both and just adding @babel/preset-typescript to .babelrc
{
test: /\(.jsx?|.tsx?)$/,
use: 'babel-loader',
exclude: /node_modules/
},
//.babelrc
plugins: ["@babel/preset-env", "@babel/preset-typescript"]Do y'all go for approach #1, #2 or something else entirely?
» npm install @kyfe/babel-loader
I know what Babel does but I see tutorial videos on Webpack and it seems that it is Webpack which backdates the code to pre-ES5 and it miniaturises it as well. It was unclear whether that is something which you need Babel for so Webpack can do it automatically or if Webpack has that feature built-in independent from Babel and it's a matter of picking one tool or the other to ensure your code works on such-and-such an edition of IE.
Again, I am aware of what each tool accomplishes. But not how they both work together, if they work together, and which tool has what particular duty when they are being used. It's all very abstract. How do professional developers use Babel and Webpack, if they decide to?