Let's say that, I have a main module:
angular.module('myApp', ['myApp.view1']);
And the other module
angular.module('myApp.view1', ['ngRoute'])
the second one is in another directory in the project.The first module cannot find it's dependency, only if I also add
<script src="view1/view1.js"></script> in the index.html
,but it quickly becomes pretty hard to manage by hand, if one has lots of javascript files.
What is the best way to manage dependencies between angular modules, so that they can recognize each other?
You can use a task runner like grunt or gulp and concatenate all the javascript files during the build step and include that one file in your index.html file. I use gulp and here is a sample gulp task that helps you concatenate all the JS files using the gulp-concat plugin.
gulpfile.js
var gulp = require("gulp");
var concat = require("gulp-concat");
//if all your source js files are inside the src directory
var srcJs = ["src/**/*.js"];
gulp.task("js", function() {
return gulp.src(srcJs)
.pipe(concat("app.js") // concat into 1 file called app.js
.pipe(gulp.dest("dist"); //save app.js in dist directory
});
So add this gulpfile.js in your project root folder and every time you make code changes, go to the project root folder in the command line and run the command "gulp js". This will run the js task and concatenate all your JS files and store it in a file called app.js in the dist directory. And in your index.html file you can always point to this one file dist/app.js.
They can only recognize each other, if they are added as script files. A best practice is to minify all of the javascript files within your directory structure into one file before publishing.
Related
I'm trying to dynamically set the file paths to the static files (js & css) in the index.html file of my create-react-app such that they can point to different sub-directories depending on what I set in a settings.json file.
Example:
If I set the base_url in my settings.json file like this:
{
"BASE_PATH_URL": "/subdirec1"
}
I expect the file path in my index.html file to be like this:
<script src="/subdirec1/static/vendors/js/core/jquery-3.2.1.min.js" type="text/javascript"></script>
I'd be grateful if anyone could help me out here. Thanks!
If you're using webpack, you can use webpack variables that you can set within the webpack config object, which themselves come from a .json/.js file.
This is the example you can use if you're using webpack!
WARNING: Don't use the command below before reading up on it, because it will make a big mess of files you might not understand yet!
Since you're using create-react-app, I think it uses webpack under the hood but you need to npm run eject it to have more complete access to its configuration!
I've made my personal jQuery microframework with useful utilities. It has a directory structure like this:
/jspocket
- jspocket.js
/scripts
- include.js
- navigation.js
- slider.js
- popups.js
...
Therefore it is imported into html like this:
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="js/jspocket/jspocket.js"></script>
In jspocket.js is this code for importing all the .js files from '/script' directory into an html file:
$.getScript(jspocket_dir + "/scripts/navigation.js");
$.getScript(jspocket_dir + "/scripts/popups.js");
$.getScript(jspocket_dir + "/scripts/slider.js");
$.getScript(jspocket_dir + "/scripts/include.js");
...
Now I would like to create a minified version of my framework so there will be only one file jspocket.min.js. But the problem is that the commands like:
$.getScript(jspocket_dir + "/scripts/navigation.js");
will not work, simply becouse scripts/navigation.js does not exist in minified version, it should be all in one file.
So the question is how could I minify the framework into one file (without manually copying all the code into one file)? Should I change the way scripts are imported? Does the new import/export features of JS solve it somehow? How is this problem solved in general? I'm using node.js and npm, so maybe there could be a good packages for this?
You need to use a build system to minify the files into one file but leave jspocket.js out of the process.
There are many build systems out there like GruntJs , Webpack or Gulp
This following is how to do it in Gulp
// the plugins
var gulp = require('gulp')
var uglify = require("gulp-uglify");
var concat = require('gulp-concat');
// task
gulp.task('minify-js', function () {
gulp.src([
./jspocket/scripts/navigation.js,
// the rest of your files in your order
])
.pipe(concat('jspocket.min.js'))
.pipe(uglify())
.pipe(gulp.dest('dist'));
});
then run gulp minify-js
I'm new in AngularJS and Gulp.
In one example, some libs are copied by Gulp from the node_modules folder in a js/lib/angular2 folder:
gulp.task('libs', function() {
return gulp.src([
'node_modules/angular2/bundles/angular2.dev.js'
... // other libs
]).pipe(gulp.dest(src + 'js/lib/angular2'));
});
then added in index.html via script tag
<script src="js/lib/angular2/angular2.dev.js"></script>
What if I would load them via CDN?
During development I can use local js files, copied by Gulp, but in production have I substitute them "by hand" with their corresponding CDN file (if any)? or there is a way to do it directly with Gulp?
EDIT
I found the plugin gulp-cdnizer
There is a plugin for gulp, gulp-processhtml, that can do this. It uses conditional comments that will remove/replace/add to your HTML files based on the gulp task.
https://www.npmjs.com/package/gulp-processhtml
In Ember JS project, we have package.json (for NPM managed) and bower.json (Bower managed) where we have all our dependencies/devDependencies (e.g. bootstrap, jquery, ember, etc)
Now these get downloaded from their respective registries and get downloaded locally into node_modules/bower_components folder.
Now my question is while these folders (node_modules/bower_components) contain a lot of code dependencies, when we do a build, I see some code in the "dist" folder.
I want to understand what actually goes into this dist ?
I see things like vendor.css, vendor.js, myappName.css, myappName.js, etc
So how do these get constructed and what code actually goes inside these ?
Is it also base on what we have in our package/bower json config files ?
Or is it based on what we have in ember-cli-build.js ?
What is put under /dist should be everything you need to publish your application. Components from bower_components are typically loaded via app.import() in ember-cli-build.js and stuff from node_modules by addons you've installed (which ember-cli picks up automatically).
Here is a quick rundown of the files.
index.html --> Generated by ember-cli upon project creation
* --> Everything from /public
assets/
appName.css --> All css from under /app
appName.js --> All js and compiled templates from /app
vendor.css --> Any css imported from bower_components/node_modules (via ember-cli-build.js)
vendor.js --> Any js imported from bower_components/node_modules (via ember-cli-build.js)
test-*.js --> Test loader/support for ember-cli if you've run "ember test"
Most files also come with sourcemaps as .map which you can exclude when publishing the site.
As you said, the dependencies you declare in your bower.json and package.json get downloaded to bower_components and node_modules
When you do you an ember build command what happens is that all the code you decide to import in your ember-cli-build.js will get dumped to the vendor.js / vendor.css file. All your application code (templates/routes/components/controllers/services) will be placed in my-app-name.js. All your application styles will go to the my-app-name.css file. All these files will be placed in the dist directory so that you can deploy it.
See this sample ember-cli-build.js file:
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
//CSS - Content of these files will go to "vendor.css"
app.import('vendor/css/bootstrap.css');
app.import('bower_components/datatables/media/css/jquery.dataTables.css');
app.import('bower_components/datatables/media/css/dataTables.bootstrap.css');
app.import('vendor/css/plugins/toastr/toastr.min.css');
// Javascript - Content of these files will go to "vendor.js"
app.import('vendor/js/bootstrap.js');
app.import('vendor/js/plugins/metisMenu/jquery.metisMenu.js');
app.import('vendor/js/plugins/toastr/toastr.min.js');
app.import('bower_components/datatables/media/js/jquery.dataTables.js');
return app.toTree();
};
The CSS imports will go to the vendor.css file and the JS imports will go to the vendor.js files.
The content of your my-app-name.css comes from the app/styles folder.
If you do ember build --environment production the ember build process will also fingertring your assets (append a hash at the end of the filename and generate an appropriate reference in the index.html file).
I've been trying to find a definitive answer to a problem I'm having using GULP to load the latest jquery CDN or any other Javascript CDN external sources.
What I've got so far is all our JS files being found in a folder, concatenated to a single file and placed in a new folder called min. Ideally I'd like to also link into the concat process the jquery CDN's and other external js files.
Does anyone know what is the best way to do this?
Here is the code I've got so far:
var gulp = require('gulp');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var notify = require('gulp-notify');
gulp.task('js', function () {
return gulp.src('js/**/*.js') //select all javascript files under js/ and any subdirectory
.pipe(concat('mynewfile.min.js')) //the name of the resulting file
.pipe(uglify())
.pipe(gulp.dest('min')) //the destination folder
.pipe(notify({ message: 'Finished minifying JavaScript'}));
});
gulp.task('watch', function() {
gulp.watch('js/**/*.js', ['js']);
});
gulp.task('default', ['js', 'watch']);
As far as I know, Gulp is a helper to manage your project locally, not by connecting to external sources.
A common approach would be to manage current library versions by a package manager like Bower – there is an integration bridge available (didn't test it though, I just update packages manually).