-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Webpack without bundling for server-side compilation #5866
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
Comments
I think this can be useful also for publishing some libraries to npm as not bundled ES6 modules. Then the consumer can do much better tree shaking with own bundler. |
There is a plenty of posts on the internet saying "Ha-ha! webpack + node + etc. Works like a charm!". Unfortunately it doesn't. Can somebody please share some working dev configs that allow to develop a project using node + ts + express? Not bundling code, of course. Not necessarily webpack. Thank you! |
Hello everyone, may I ask is there any update for this issue? |
If I understand correctly this feature is available since webpack v5 alpha 3:
|
@frenzzy can you explain how given this input:
webpack should output the transpiled code while preserving directory structure:
if it is possible to give us an example repo that would be much appreciated.
|
just want to share with everyone my working solution: const glob = require('glob')
const path = require('path')
module.exports = {
entry: glob.sync('./src/**/*.js', { ignore: './src/**/*.test.js' }).reduce((acc, file) => {
acc[file.replace(/^\.\/src\/(.*?)\.js$/, (_, filename) => filename)] = file
return acc
}, {}),
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
}
} what this does is make an entrypoint for every file in the [edit] |
@andykais It will still bundle everything together. |
@etienne-martin see my comment at the bottom of my previous message. It does in fact create separate output files
|
I found a fork of |
Just to add to the list of reasons why this would be a useful thing to have:
|
adding to those reasons: |
I support this issue and have been supporting it for a while. This also relates to libraries, libraries not always need to be bundled (even if they have front end assets), they only need to be compiled (assets inlined, typescript, babel, emotion, etc). This also allows for a more granular use of the library when needed. |
I have a use case where I am bundling a lambda function for database migrations. Each database migration is a separate file. The migration tool scans a directory of files and performs these migrations. Since webpack bundles the whole thing as one file, the tool cannot find these files on disk. I wish it was possible to opt out from bundling everything and keep files separate. |
This feature is so much needed on server side development, currently having to resort to separate babel, rimraf libraries rather than a single webpack config to transpile server side code. |
Yes, it will be great feature, we will consider it after the stable webpack 5 release |
Solution for serverless-framework, can be also used for express, koa, etc.. just replace slsw.lib.entries to your entries. const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const CopyPlugin = require("copy-webpack-plugin");
const plugins = [
new CopyPlugin({
patterns: Object.keys(slsw.lib.entries).map(el => {
const srcFolder = slsw.lib.entries[el].replace('index.js', '');
return { from: srcFolder,
to: srcFolder,
force: true, }
})
}),
];
console.log('plugins', plugins[0]);
module.exports = {
entry: slsw.lib.entries,
target: 'node',
optimization: {
minimize: false,
runtimeChunk: true,
},
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
plugins,
devtool: false,
externals: [nodeExternals(), 'aws-sdk', 'mjml', 'handlebars'], // exclude Lambda Layers
output: {
libraryTarget: 'commonjs',
path: path.resolve(__dirname, '.webpack'),
filename: '[name].js',
},
}; |
@odykyi Firstly, thanks a lot for this. I tried it out, but sadly it didn't work for me. I have post my serverless.ts, tsconfig.json and webpack.config.js file below, kindly let me know what am I doing wrong. This is the error I get webpack.config.js
tsconfig.json
webpack.config.js
|
@alexander-akait, hi 👋 Stable webpack 5 was there for a while. Can we expect progress on this issue? |
Hello, it is on our roadmap, recently we implement support es modules output with small limitations, after fixing limitations we will implement this |
Nice to hear that, thank you. As you said earlier it will be a great feature :) |
God, I really need this feature! |
This is awesome news! Could you link to the implementation or discussion of this feature? |
Right now no, we still need do some improvement, in many places, in near future we will implement |
Has this been implemented yet? |
@alexander-akait Can you add this issue to webpack 5 or 6 project board, or set a milestone? |
Our use case for this would be nodejs/typescript based lambdas. I would like to bundle the dependencies in node_modules but keep our own source code as it is. Since the lambdas usually are only some hundreds of lines of code, dependencies usually make 99% of the final package. Reason for keeping everything under '/src/' as it is, is to be able to easily read and modify the code in AWS lambda console. This is especially valuable in dev/test environments. |
Any info on this? This feature is much needed ASAP. Option to bundle/not bundle/bundle some, is crucial. |
Is this still on the roadmap? |
Hey, how about using this plugin transpile-webpack-plugin? It collects all the files imported direcly or indirectly by the const TranspilePlugin = require('transpile-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: __dirname + '/dist',
},
plugins: [new TranspilePlugin(/* options */)],
}; Assuming the entry |
I am just learning webpack, so my terminology may be off.
MY QUESTION: |
out of date? |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
Webpack always bundles files and this is not a desirable behaviour when compiling server-side code. I know that it sounds like an oxymoron to ask for it not to bundle since webpack is a "bundler".
What is the expected behavior?
It would be great if webpack could accept an option to only compile files and then output them back out while preserving the directory structure, essentially skipping the bundling step.
If this is a feature request, what is motivation or use case for changing the behavior?
When writing a universal react application, the entire react app is shared between client and server. Moreover, may other files such as schemas, validation logic, utils, api clients, etc are also shared and need to be compiled separately for the browser and node.js. Currently, I have two webpack config files, one for client and one for server. This generates two bundled files.
While this setup works fine for now, it does negatively impact my development speed. Node.js code is really not meant to be bundled. For instance, after bundling, you can't use
fs.read
with a relative path anymore. The biggest issue, however, which has led me on a path to find an alternative is the terrible debugging experience. With recent versions of Node, we can use Chrome Dev Tools to debug our server application. Unfortunately, due to limitations of the dev tools, we don't have acccess to source maps when debugging the server code. Believe it or not, this is a huge nightmare when trying to debug server code and all you have is one massive webpack-generated file. There's an open issue about this problem on node.js' github repo but so far, there doesn't seem to be any fix.I can use something like gulp-babel to compile my server code but that won't handle assets like webpack does so it won't work for react ssr. Moreover, gulp doesn't "crawl" the import statements like webpack does so I have to completely separate my server and client code into separate folders (or use a silly naming convention like ".server.js") which again causes many issues when it comes to shared files. Probably more than 50% of my application is shared files so you can imagine what a nightmare that would be trying to separate client and server.
If webpack had an option to disable the bundling step and just output files after compilation, then this problem would be resolved.
The text was updated successfully, but these errors were encountered: