Angular 2 build without CLI - javascript

I am new to angular js. I followed the docs in their site on how to build an angular 2 app, but the problem is all the .js files are still living in the same directory of the .ts file which will be very problematic if my app grows or have many .ts files. How can make sure after compilation it will be put in the dist directory without using angular cli? Because it tried angular cli, it is very slow. It will take a minute just to reflect a simple changes in my file.
How can i use like this directory structure:
dist
index.html
myapp-component.js
another.js
-- etc...
src
index.html
myapp.component.ts
another.ts
-- etc...
and/or if it is possible to minify or make does js file into 1 big .js file only.
Also to put angular js source files/dependencies in the dist/library folder not from node_modules.

Related

How to use minified third party Javascript files using JSPM

My application is using JSPM and SystemJS for module loading and is using angular.
My config.js file has angular map like:
"angular": "github:angular/bower-angular#1.5.8"
So when I do import angular from 'angular', I am getting the angular.js file from the Path specified in config.js file. That's good.
Now the requirement is I want to use minified third party javascript files (angular.min.js) in the app. But there is no minified files in jspm registry
So the initial loading time of my application is high because of so many large files e.g. angular.js, browser.js etc. that takes too much time to load.
I know, I can do a jspm bundle to minify all dependency files recursively which includes vendors' files also. But my questions are:
1 - Is it possible to use vendor's minified file (angular.min.js) directly with JSPM? It is good to use vendor's minified file rather than minifying them ourshelves, Isn't it?
2 - If above one is not possible, then how can I bundle only my application specific files and still able to use (bundle separately) minified angular.js file?
What is the recommended approach here?
In your config.js file you can map any file with a name and have it imported by SystemJs in the browser
So you could potentially do something like
"angular": "jspm_packages/...../angular.min" (The path to your file)
However the recommended approach would be to bundle and minify all your vendor files and as you mentioned, bundle your application specific files separately
You can do this with something like Gulp or Grunt to generate the 2 files
There are lots of examples online of how to do this but here is one to get you started
https://blog.dmbcllc.com/using-gulp-to-bundle-minify-and-cache-bust/
Once you have generated both files you add them to your html page via a script tag
<script src="Your File Path"></script>
Option 2 is the preferred approach and I would recommend spending the time to get setup as you only have to do it one time to get your head around it

Using hashed bundles in a .Net project

So the .Net project uses a .csproj file to keep track of files that are included in project which eventually make it's way on prod. Having a webpack configuration that bundles the JS and CSS and also moves all assets into a /media folder, all with a hash how it's possible to "include" those files in the project in an automatic way. Maybe by reading an asset-manifest.json file or some other way?
Working with MVC applications for example you can configure webpack to output the bundle in your scripts/dist folder and your mvc app can referrence this one.
Other option if you need advanced scenario is using gulp to copy files / clean / whatever.

PDF Viewer for Ionic 2: cannot import or require

I am trying to build a PDF viewing component for an Ionic 2 app. I have lots of experience of Angular 2, but not of Ionic.
The component will be built using pdf.js and have created the pdf.js asset to be included in my project as described on the github page. However, trying to require this fails — require is not defined —, so I copied the script to the asset folder and tried importing it. It seems the file is not being copied to the build so that fails too.
Anyone have any pointers for requiring or importing a non-weboack non-SystemJS script into an Ionic 2 app?
Create a js folder (or something like this) in the www folder; include the files needed and reference them from there. This folder is not emptied on build.
EDIT
The root of the www folder does not change and you can reference it as someting like ... 'js/need-this.js'.
There should have some references in your index.html file to the build folder and maybe the assets folder (icon); this concept of using a js folder works the same. Only build and assets will change.
I feel that I should point out that you shouldn't copy anything into www since this is autogenerated and any changes will be overwritten. You should copy into assets instead.

Adding a config file to my JavaScript project

I have a JavaScript project that includes both frontend and backend codes (NodeJS). This is my production folder structure:
/prod
/server
sourceCode1.js
sourceCode2.js
...
sourceCodeN.js
index.js
/client
bundle.js
ReadMe.md
license.txt
When user hit my /server/index.js, I call express.static(__dirname + '/../client') to serve user js files in /client folder.
I have plenty of frontend js files under /client folder originally, but they are minified and combined into one bundle.js when they are moved to prod as you can see above. I want to add one single config file in JSON format that contains configuration for both my backend and frontend code. But I'm not sure where/how to place it. I think no matter where I put it, my code in /server/index.js can access it with no problem. But for /client/bundle.js, accessing the config file will require another request to the server which seems poor design to me...
Can anyone suggest a way that solves the problem better?
P.S.
I use gulp to minify and combine my frontend js files into bundle.js, I can put the config file in /client folder and use gulp to bundle it together with other js files as well. But that means every time I change the config file, I need to gulp everything again which doesn't make any sense.
PPS.
I agree pulp will work just fine for myself. Another reason I didn't want to use pulp to bundle the config file is that this is an open source project, I'm hoping when someone else uses it and he only wants to change one tiny setting in the config file, he doesn't have to go through the gulp step..
I can put the config file in /client folder and use gulp to bundle it together with other js files as well. But that means every time I change the config file, I need to gulp everything again which doesn't make any sense.
I think it does make sense. Other people do too.
If you're worried about speed, I recommend having two Gulp tasks: Dev and Build.
Dev should watch the source for changes and update the configuration.
Build should do slow things like bundling and minification as well as update the configuration.

Grunt uglify - replace original file

I have an angular application that uses require.js to load all scripts. The application is deployed to the web server using Hudson CI. When the deploy job runs, I want to:
Minify all JavaScript files but preserve the original file names so that the require.js config file works out of the box.
Have an option to exclude specific JavaScript files while running uglify.
The JS files are distributed all across the application, which has an ontology similar to the following:
app/
common/
controllers/
factories/
services/
assets/
js/
Is there a way to do this?
you need grunt-contrib-uglify
for the excluding files requirement:
http://gruntjs.com/configuring-tasks#files

Categories