0

I'm trying to inline preconfguration data into a dynamically generated script bundled using browserify.

To do so I generate a "module" string like:

const moduleString = `module.exports = ${JSON.stringify(configurationData)};`;

Now I'd like to pass this to browserify.add. First option would be to write the string to a file, generate the bundle and delete the temp file. This works, but feels tedious.

So I thought of pushing the data to a read stream:

var dataModuleStream = new stream.Readable();
dataModuleStream.push(moduleString);
dataModuleStream.push(null);

and then pass this to the .add call:

b.add(dataModuleStream, {expose: 'config'});

This also works and inlines the correct data as a module named config into the bundle, yet when I try to require that module from my application script:

var config = require('config');

browserify fails with the error message:

Cannot find module 'config' from '/foo/bar/baz'

which won't happen when I use the temp file approach. When using the stream and I omit the require call the file will get built, including a module named config.

Will my approach even work? Am I doing something wrong?

3
  • Any resolution or progress on this issue @m90?
    – Shawn
    Mar 30, 2016 at 19:48
  • Unfortunately not, no. We are using a temp file approach now. I never got it to work.
    – m90
    Mar 30, 2016 at 19:55
  • I was able to use a stream by passing it browserify instead of browserify.add : gist.github.com/shawninder/24ff053c202aa50379007ab2603ca453 Hope this is useful
    – Shawn
    Mar 31, 2016 at 16:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.