Asynchronous module definition (AMD) with Angular.dart? - dart

I am curious to know if its possible to have Asynchronous module definition with Angular.dart (or dart in general), if yes, then please share a sample code for such a single page application.

AMD is made largely irrelevant by the fact that Dart comes with a complete module system that is built into the language. That is the purpose of the import statement. Dart files are dynamically loaded in Dartium. If you are using the darttojs transpiler, it will create a single large file. The solution to this is to use use the DeferredLibrary class which will cause darttojs to create separate javascript files for each annotated import
See Seth Ladd's blog post for more details.

Related

Importing files under the src directory from a dart library

Effective dart says
"DON’T import libraries that are inside the src directory of another package."
reason being it breaks abstraction and could potentially break your app if the library was to change its underlying implementation.
They do not provide an alternative or a solution.
Im currently working on a dart package that my app depends on.
What would be the correct way to import the models or classes from it instead of importing it directly from package src folder?
As previously mentioned in the comments, you can define which classes you can expose from your package. You're correct that it's recommended to avoid exposing more API than intended - it's also mentioned in the docs. To do this, you can define the exposed classes:
export 'src/your_file.dart' show ExposedClass hide HiddenClass;

When to use part/part of versus import/export in Dart?

I do not completely understand the difference between part/part of and import/export when using libraries in Dart. For example:
one.dart:
library one;
part "two.dart";
Class One {
};
and
two.dart:
part of one;
import 'somefile.dart';
Class Two {
}
versus
library one;
import 'two.dart';
Class One {
}
and
library two;
import 'somefile.dart';
export 'somefile.dart';
Class Two {
}
Both scenarios seem to do the same thing. When is it advantageous to use part and part of rather than import? And are there scenarios where import will not work, but part and part of will?
update 2018/03
part and part of is used more and more for code generation scenarios recently (instead of deprecated transformers) and unlikely to go away anytime soon.
Packages like built_value, json_serializable, and many others depend on it.
Discouraged is only the patter where all files of a package are tied together to a single library by having one library file and all other files being part of that library.
original
In Dart, private members are accessible within the same library. With import you import a library and can access only its public members. With part/part of you can split one library into several files and private members are accessible for all code within these files.
see clarifications to below paragraph in above update
Using part / part of is discouraged and the Dart team is considering getting rid of it. I assume they will introduce something like "friend" (https://github.com/dart-lang/sdk/issues/22841), where two libraries can access each other's private members as an alternative before they discontinue part / part of (maybe in a future Dart version).
Let's suppose we have a Dart library called mylib, whose file is lib/mylib.dart.
library mylib;
// Definitions
That library can be included in the main.dart file as
import 'package:mypackage/mylib.dart';
When you create a new library and use other libraries you want to make available automatically when using your package, then you use export:
library mylib;
export 'otherlib.dart';
// Definitions
You can use the show keyword to import/export only some parts of a library (like a class or something).
You are using the part of directive wrong here. You can't use both library and part of, which is used to specify the contents that belong to a library. For example, you can split your library file in more than one file (the parts):
Suppose we have in the file mylib.dart:
library mylib;
part 'src/class1.part';
// More parts
And then we have in another file src/class1.part the part specified in mylib.dart
part of mylib;
class Class1 {
/* ... */
}
The Creating Library Packages article on the dartlang.org site recommends avoiding part / part of.
Note: You may have heard of the part directive, which allows you to
split a library into multiple Dart files. We recommend that you avoid
using part and create mini libraries instead.
The 'mini libraries' referred to are small library dart files in src which are imported into and exported from main libraries.
using part/part of makes many files be treated as if they were one file
import/export doesn't, so this may be useful when private fields need to be accessed from another files (classes created on other files)

Combining different dart files when executed by JS

I'm building up some Dart code that I would like to use in an app where it is essentially a library to the javascript. I'm wondering how I can specify which Dart files I'd like in the project to be part of the library. For example, theres Foo.dart and Bar.dart. How can I have the created product include both Foo.dart and Bar.dart in one file? I'm also concerned about tree shaking since none of the classes are instantiated in Dart.
There's also a Baz.dart, and I would like to have a different build for compiling Foo.dart and Baz.dart into a single file (though this is less important, as I can accomplish this would separate projects and some symlinking).
Thanks!
This use case (build a JavaScript library with Dart) isn't supported yet.
The reworked js-interop package is supposed to allow to do that but I don't know about it's current state.

Too large JS-file generated

I have this code:
// main.dart
import "package:angular/angular.dart";
main () => ngBootstrap();
I make dart2js --minify --out=main.dart.js main.dart
Then i have main.dart.js with size 2.6 MiB (2,744,320 bytes).
It is not normal. What i'm doing wrong?
Is angular.dart usable for production at this stage?
#media-slave24
Maybe this will be helpful for You:
https://code.google.com/p/dart/issues/detail?id=14686
It's reported on dart bug tracking system. Some people using mirrors got 760kb. So it's definitely a bug.
UPDATE (Jan '14): AngularDart 0.9.5 now includes a standard MirrorsUsed list. To finalize it and trigger Dart's tree-shaking optimizations, you need to add a MirrorsUsed to your program
listing all the classes you introduce.
override: '*' to finalize the MirrorsUsed.
Since helloworld has no new classes, say:
#MirrorsUsed(override: '*')
import 'dart:mirrors';
See Github for the complete helloworld program
The key is to include a #MirrorsUsed annotation in your Dart file. Pavel's link to the AngularDart tutorial is an excellent resource.
To actually answer your question: Yes, AngularDart can be used in production but be aware that it is in "beta" release right now. We expect many breaking API changes!
Take a look at angular.dart.tutorial Chapter 7 about deployment. It has a section on managing compiled code size:
https://github.com/angular/angular.dart.tutorial/tree/master/Chapter_07

Where should I put my dart files if they are the server side files of restful api providers?

I'm trying to use start to write some restful apis for clients.
The content is looking like:
import 'package:start/start.dart';
import 'myfile1.dart';
import 'myfile2.dart';
import 'myfile3.dart';
import 'myfile4.dart';
void main() {
start(public: 'web', port: 3000).then((Server app) {
app.get('/').listen((request) {
request.response
.send('Hello, dart');
});
app.get('/aaa').listen(...);
app.get('/bbb').listen(...);
app.get('/ccc').listen(...);
app.get('/ddd').listen(...);
});
}
Notice there are also some other dart files myfileN.dart in the same dir.
Where should I put them?
According to the dart project layout, there are several entry points:
bin
lib
web
example
test
But I don't know where should I put my dart files. Since this is not a library, it's just an server-side application, and is not a web app, not a test, not an example, and not a command-line script.
I think there should be a src to put my files, but there is not. How to solve this problem? Doesn't the standard layout doesn't design well?
You can create an src within lib and put your files there.
If the files are executable, you can put them inside bin, or within a directory inside bin.
See the package layout conventions for details.
Many applications use third-party software.
Third-party software often organized as pluggable functionality.
Pluggable functionality often implemented not as independent executables but as linked libraries.
Also when you wrote application then you often divide it on different functionality.
The best way in this case (in Dart) put different functionality in different libraries.
This simplify your life when you will be testing and maintain your application.
By convention libraries in Dart resides in 'lib' directory.
Also implementation of libraries by convention must be located in 'lib/src' directory.
There may also be other possible explanations for this need.
I just tried explain it to you as simple as possible.

Resources