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

How to provide generated content as module ? #1436

Open
Cactusbone opened this issue Nov 18, 2015 · 7 comments
Open

How to provide generated content as module ? #1436

Cactusbone opened this issue Nov 18, 2015 · 7 comments

Comments

@Cactusbone
Copy link

I need to be able to provide generated content as dependencies to bundles

during build (using api) I know what the generated content will be, but there's no file containing it. it's only in memory. so i want to provide it to the bundle requiring it.

I can indeed do

bundle.exclude('a');
bundle.require(stream, {expose:'a',file:'a.js'});

but this means my bundle ends up with 'a' even when it's not required.
any tips ?

@Cactusbone
Copy link
Author

with custom resolve function :

var generatedModules = {
    'a': textContent,
};
browserify({
    fileCache: _.clone(generatedModules),
    resolve: function (id, parent, cb) {
        var filename = null;
        if (generatedModules[id]) {
            filename = id;
        }
        _.defer(_.partial(cb, null, filename));
    },
})

@jmm
Copy link
Collaborator

jmm commented Nov 23, 2015

@Cactusbone Perhaps what's discussed here will give you at least a temporary solution: #1440. As noted there though, the plugin is implemented using the undocumented ability to set module-deps .resolver, so fundamentally it's no better than doing that (with the usual cautions about using undocumented behavior), it just provides some conveniences on top of it.

@Cactusbone
Copy link
Author

@jmm what i wrote here is working for me, so i'm using it. but since it's using a PR, i'm leaving the bug opened.
And i've stumbled on #1440 before, but i had the file not found issue with it :/

@jmm
Copy link
Collaborator

jmm commented Nov 23, 2015

@Cactusbone Even if you combine it with the fileCache part from here?

@Cactusbone
Copy link
Author

@jmm Yeah, I'll try it again and report back :)

@Cactusbone
Copy link
Author

@jmm humm looks like you're right ! I must have found fileCache option long after trying .resolver

I like this solution better since I don't have to depend on a custom fork. even if i'm using undocumented code.

final code :

var generatedModules = {
    'a': textContent,
};

var bundle = browserify({
    fileCache: _.clone(generatedModules),
});

bundle.plugin(generateRequires, {generated: generatedModules});

function generateRequires(b, opts) {
    var generatedBundles = opts.generated || {};
    var deps = b.pipeline.get('deps');
    deps = deps.get(0);
    var defaultResolver = deps.resolver;
    deps.resolver = function (id, opts, cb) {
        if (generatedBundles[id]) {
            cb(null, id);
        } else {
            return defaultResolver(id, opts, cb);
        }
    };
}

@jmm
Copy link
Collaborator

jmm commented Nov 23, 2015

@Cactusbone Thanks for the feedback. Just be aware of the usual caution that it's subject to break at any time. You may also want to handle the reset event, e.g. if you want it to work with watchify.

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

No branches or pull requests

2 participants