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.Is an npm module equivalent to an angular module?
`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.
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.
--
You received this message because you are subscribed to the Google Groups "angular-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
`require('angular-animate').directive("myDirective", ...);` should register a new directive in the ngAnimate module.That code would not work as the module itself is not returned. `require('angular-animate')` will return the module name as a string: "ngAnimate". That would be the case for everything but angular itself.
Is there any concrete reason why you consider exporting a string as the better alternative to exporting the actual Angular module?
If the `require('angular-route').name` format were enabled, people currently shimming the modules would essentially be able to remove the shimming infrastructure and leave their code untouched. That seems like a win to me.
I would expect var angular = require('angular') to work. And then module dependencies via var dependecies = [require('foo').name, require('bar').name].
// Inside a folder named mn-menu
var moduleName = 'mnMenu'
var controllerName = moduleName + 'Controller'
var dependencies = []
angular.module(moduleName, dependencies).directive(moduleName, function (){
return {
template: require('./template.html'),
controller: controllerName
}
})
.controller(controllerName, require('./controller'))it doesn't protect any core modules from being polluted (although to be fair, they could be polluted anyways).
convention for the ngMock / multiple modules exported
I also can't think of much precedent for CommonJS modules that silently do things behind the scenes, and subsequently just return a string.If we want to write our own angular modules with CommonJS, presumably we'd want to be able to write something like:```jsrequire('ngApp').controller('homeCtrl', ctrlFunc);```In my experience, Angular's module system is a key weakness. If we build compatibility with established module systems, large Angular will be able to immediately take advantage of the improved encapsulation and dependency-resolution that CJS promotes. Similarly, this gradually moves toward the module system that will be used in Angular 2.x.While CJS support is a great short-term goal, I believe that Angular 1.x needs to adopt (or, at least support) ES6 modules. ES6 is the future, the module specification has been finalized, and the module syntax can be efficiently transpiled into ES5 code that can be used today. The upgrade path to Angular 2 will not necessarily be smooth, but this is one area where we can certainly ease the transition.
That's how I use it right now.Angular becomes the boilerplate code and my controller/factories become normal functions with DI that are exported via CommonJS.
UMD is a Lie from one of the UMD authors. :)
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/i5by29Okh48/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
All we need to do is add it to the publish scripts, it’s trivialOn Jan 19, 2015, at 6:16 AM, Pete Bacon Darwin <pe...@bacondarwin.com> wrote:What is the process right now for creating a new module in the angular.js repository?On 19 January 2015 at 10:49, Caitlin Potter <caitpo...@gmail.com> wrote:That’s exactly the reason why it should be in the release process, it’s much easier if it’s all dealt with in one place
--
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 angular+u...@googlegroups.com.
Import {Grinder} from './grinder'; // Manually annotate the function coffeeMaker.annotations = [ new Inject(Grinder) ]; function coffeeMaker(grinder) { ... } export coffeeMaker;
var Grinder = require('./grinder'); // Manually annotate the function coffeeMaker.$inject = [Grinder]; // The below is necessary in my mind to make it easier to hook to the standard component definition coffeemaker.$type = 'factory'; function coffeeMaker(grinder) { ... } module.exports.coffeeMaker = coffeeMaker;
What do you guys think?
--
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/i5by29Okh48/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
On Jan 20, 2015, at 5:00 PM, Paul Everitt <paulwe...@gmail.com> wrote:
Just out of curiosity, what's the process on this discussion period? Is there an end date for when input is finished? I'd love for this to get into the 1.4 release cycle, or even 1.3.x if there's going to be another release.Are there points from Pete's two notes that need to be digested?
Igor seemed interested in the prospect of preserving the existence of a single “distribution” of Angular (hence my previous comments about UMD). IMO, even in spite of UMD’s drawbacks, this allows us to ship a single Angular release immediately, with no surprises for existing users.
Any breaking changes (such as publishing a CJS-only version of Angular on NPM) would need to go in 1.4.
At the end of the day, we want developers to be able to npm install angular, write require('angular'), and have it work without any surprises.
~Andrew
May I ask what problems you are solving with RequireJS and putting each file in its own Angular module? Even 170 source files could be bundled, minimized, compressed, and loaded up front, don't you think? And why so many modules? I'm just being curious.
Hi AdamMay I ask what problems you are solving with RequireJS and putting each file in its own Angular module? Even 170 source files could be bundled, minimized, compressed, and loaded up front, don't you think? And why so many modules? I'm just being curious.
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/i5by29Okh48/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.