Skip to content
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

Get bundle as a string with programmatically usage with 0.6.x #23

Closed
topa opened this issue Sep 14, 2012 · 7 comments
Closed

Get bundle as a string with programmatically usage with 0.6.x #23

topa opened this issue Sep 14, 2012 · 7 comments

Comments

@topa
Copy link

topa commented Sep 14, 2012

We're using for alamid's client tests nof5.
nof5 is basically a server which is able to use different bundlers, e.g. webpack, to bundle and serve client tests. One advantage of nof5 is that there is no need to write any tmp-file for the tests. So you won't have any problems with permissions.
So nof5 expects from the bundlers to be able to return a bundle as a string.
This is how webpack is implemented as bundler for nof5: WebpackBundler.
I wanted to updated the dependecy to webpack to version 0.6.x, but webpack isn't return a string any more.

Is there any possibility to get the bundle as string?

@sokra
Copy link
Member

sokra commented Sep 14, 2012

yes. There is an option noWrite and you need to pass the option emitFile to get the source of a file.

Here is a working example: https://github.com/webpack/worker-loader/blob/master/index.js

It's a webpack loader which compiles to WebWorker.
It's called from the parent webpack and executes a embedded webpack. The embeddend webpack do not write out files, but the worker-loader grabs the emitted files and pass them to the parent webpack.

Good luck.

@sokra
Copy link
Member

sokra commented Sep 14, 2012

The API which returns the source in the callback was deprecated in webpack 0.5 and I removed it in 0.6. The reason for removing was that many loader would not work with the old API because they emit files. I. e. file-loader.

The webpack-dev-middleware also do webpack in memory: https://github.com/webpack/webpack-dev-middleware/blob/master/middleware.js#L15

@topa
Copy link
Author

topa commented Sep 14, 2012

Thank you for your fast response. I'm not sure if I got it, but are the following steps correct?

  • first: set noWrite to true
  • second: define with emitFile a function that collects the source of each file, like function collectSources(filename, content) { /* do some collecting stuff */ }
  • third: execute webpack(absolutePath, options, function(err, stats) { /* nothing really to do here */})
  • fourth: on "bundle"-event webpack has finished and I can return in any way the collected sources

If I try it like described I get the following error:

Error: options.output is required

@sokra
Copy link
Member

sokra commented Sep 14, 2012

Set output to the name of the bundled file, i. e. bundle.js.

You don't need the "bundle" event just use the callback in third.

webpack(absoluteFile, {
  output: "bundle.js",
  noWrite: true,
  emitFile: function(name, stringOrBuffer) {
    files[name] = stringOrBuffer;
  }
}, function(err, stats) {
  if(err) return handleError(err);
  processFiles(files);
})

@topa
Copy link
Author

topa commented Sep 16, 2012

Thank you again for your fast response and help. It works now. If you like to you can take a look my implementation here: WebpackBundler

@sokra
Copy link
Member

sokra commented Sep 16, 2012

@sokra sokra closed this as completed Sep 16, 2012
@AndersDJohnson
Copy link
Contributor

AndersDJohnson commented Jul 30, 2016

How to do this with webpack 1.x or later? emitFile and noWrite seem gone. Already changed absoluteFile arg to entry: absoluteFile in config options.

Only possible with memory file system, e.g. https://webpack.github.io/docs/node.js-api.html#compile-to-memory? Or custom read from filesystem after compile?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants