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

Exposing bundles #53

Closed
jednano opened this issue Nov 19, 2014 · 7 comments
Closed

Exposing bundles #53

jednano opened this issue Nov 19, 2014 · 7 comments

Comments

@jednano
Copy link

jednano commented Nov 19, 2014

I've been struggling the whole day over getting karma to work with browserify. I'm using broccoli-browserify to create a single js file that exposes a frontend package, like so:

var frontend = browserify('frontend/lib/src', {
    require: [
        ['./api.js', { expose: 'frontend' }]
    ],
    outputFile: '/api.js'
});

This is working quite well on the development server. I can require('frontend') just fine and everything works as expected. The karma configuration, however, has me stumped. I've tried the golden path, based on the readme:

config.set({
  frameworks: ['browserify', 'jasmine'],
  files: [
    'frontend/lib/src/api.js',
    // more files that require('frontend')
  ],
  preprocessors: {
    'frontend/lib/src/api.js': ['browserify']
  },
  browserify: {
    prebundle: function(bundle) {
      bundle.external('frontend');
    }
  }
  // ...
});

I've also tried returning the bundle, in case that had any bearing:

browserify: {
  prebundle: function(bundle) {
    return bundle.external('frontend');
  }
}

And I've tried to mimic what I was doing with broccoli in this way:

browserify: {
  prebundle: function(bundle) {
    return bundle.require('./frontend/lib/src/api.js', { expose: 'frontend' });
  }
}

Of course, I wouldn't be here if any of these worked. The error is always the same:

Error: Cannot find module 'frontend'

I'm hoping someone can show me the error in my ways and that there is, actually, a solution to this problem.

I'd be happy to submit a PR with a readme or doc update to address this very scenario, once it's solved.

@bendrucker
Copy link
Collaborator

Can you create a repo that reproduces this? Without seeing where you actually require this bundle from your tests it's hard to see what's up.

@jednano
Copy link
Author

jednano commented Nov 19, 2014

Absolutely. See https://github.com/jedmao/karma-browserify-test

In this repo, api.js and foo.js are commonjs files and bar.js is not, but it nonetheless requires the api, which should be exported by browserify. Let me know if you have any further questions.

@jednano
Copy link
Author

jednano commented Nov 19, 2014

Don't be fooled by bar.js because it has a require statement in it. The point is that require should be provided by browserify in the browser, if the api module is exposed.

@nikku
Copy link
Owner

nikku commented Nov 19, 2014

Looks like you ran into #52. For the moment you should not include source files in your preprocessors section.

@nikku
Copy link
Owner

nikku commented Nov 19, 2014

Other than that I am not exactly sure if this repository reproduces the problems you are experiencing.

The api file should be prebundled by browserify, right? In that case you should actually perform the pre-bundling before running the tests via karma. During the tests you should add the prebundled file in the files section and not in the preprocessors section. This should correctly reproduce your development setup and allow you to perform the correct tests.

Please try to update the setup and check if you can get it working this way.

@nikku nikku assigned nikku and unassigned nikku Nov 19, 2014
@jednano
Copy link
Author

jednano commented Nov 19, 2014

@nikku, thanks. That's actually the missing link that I needed. I think I need to look more into watchify to solve this scenario.

@nikku
Copy link
Owner

nikku commented Nov 19, 2014

Feel free to reopen this issue if prebundling + including the bundle does not solve the issue for you.

@nikku nikku closed this as completed Nov 19, 2014
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