I think it would be more surprising to get the name as a string instead of
the angular module from e.g. `require('angular-route')` (Even though
`require('angular')` would return the `angular` global object).
var angular = require('angular');
var ngRoute = require('angular-route');
angular.module('app', [
ngRoute.name
]);
The most surprising thing would still be that I'd be thinking "I only
required angular-route and referenced one string, I didn't tell it to
register any modules." (This would be even bigger surprise if angular-route
only exported a string). In CommonJS I expect that I'd have to do something
like this:
var angular = require('angular');
var ngRoute = require('angular-route');
angular.use(ngRoute);
angular.module('app', [
ngRoute.name
]);
So angular-route would expose a function for registering itself. The
function would get the `angular` object as a parameter instead of relying
on globals. angular.use could be as simple as:
angular.use = function (fn) {
fn(angular);
};
Or it could be called angular.register or something, maybe even just do
`ngRoute(angular);` in the application code.
Anyway, that's what I would expect to see in CommonJS/nodeland, but I don't
know how feasible this would be in Angular.
The following would contradict with Angular's module system, but if I went
full-node, I'd want to do this: :D
angular.module('app')
.use(require('angular-route'))
.use(require('./myModule'))
.factory(...);
On Wednesday, January 14, 2015 at 5:30:11 AM UTC+2, Caitlin Potter wrote:
>
> LGTM, but we still need to establish the conventions that people will
> expect
>
> Is an npm module equivalent to an angular module?
>
> If so, `require('angular-animate').directive("myDirective", ...);` should
> register a new directive in the ngAnimate module. But, in this scenario,
> `require('angular')` doesn't give you what you'd expect, which would be the
> `angular` global object.
>
> There is also the case where one angular module exports multiple modules,
> like ngMock.
>
> One way to go would be `require('angular-mocks/e2e')` or
> `require('angular-mocks/animate')` for the e2e and animation mock modules,
> and `require('angular-mocks')` for the main unit test mocks.
>
> Or alternatively, exporting an object containing properties which are the
> modules to be mocked.
>
> I would love to hear feedback from people using angular + browserify to
> figure out the conventions that would be most useful there.
>
> On Tuesday, 13 January 2015 21:51:20 UTC-5, Ben Clinkinbeard wrote:
>>
>> Summary
>>
>> Change the publish script to support idiomatic CommonJS usage when
>> installed from npm.
>>
>> Motivation
>>
>> Now that the Angular modules are available on npm, supporting proper
>> CommonJS use via require() is the next logical step. This use case is
>> widely expected by developers using npm for dependency management.
>>
>>
>> Without this change, developers still have to use a script tag to include
>> Angular modules from within the node_modules folder. This is extremely
>> uncommon with npm packages, and does nothing to enable Browserify and
>> webpack support.
>>
>> Compatibility Risk
>>
>> Little to no risk as only the publish shell script needs to be changed.
>>
>> Ongoing Technical Constraints
>>
>> There should be virtually no ongoing maintenance needed since it's just a
>> publish script.
>>
>> Plans for Implementation
>>
>> Simple changes to the publish script will be made to remove the main
>> field from the package.json files and generate an index.js file for each
>> package so that it is used when requiring by name. The index.js file will
>> require() the standard built file and export a value(s) appropriate for the
>> module.
>>
>>
>> The core angular module will export the angular object.
>>
>>
>> The supporting modules will export their module name (ngAnimate, ngRoute,
>> etc.) with the exception of angular-mocks, which exports 3 values: ngMock,
>> ngMockE2E, ngAnimateMock
>>
>>
>> This would enable code akin to the following:
>>
>>
>> var angular = require('angular');
>>
>> angular.module('app', [
>>
>> require('angular-animate'),
>>
>> require('angular-route'),
>>
>> require('angular-mocks').ngMock
>>
>> ]);
>>
>> Prior Art
>> The overwhelming expectation, when a developer installs a package from
>> npm, is that they can then use require('package-name') to load it. This
>> change will bring Angular in line with the rest of the npm ecosystem.
>>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.