Employ gulp to compile typescript files

While following the angular 2 quick start guide, I noticed that they use a typescript compiler and include a tsconfig.json file. I wanted to find a way to incorporate gulp into this process, and it seems like there are methods available to make it work. However, I'm a little confused on how to properly implement gulp with angular 2.

It appears that gulp-typescript and gulp-tslint are two plugins that can help achieve this, along with the involvement of the tsconfig.json file. Though I am still unsure about the specific role of the tsconfig.json file in this setup.

Can someone provide an example of how to implement gulp in a way that accomplishes the goals mentioned above? My understanding is that all development .ts files should be located within the src folder, with the resulting javascript files being outputted into the build folder. (Assuming that both folders have been set up according to the guidelines outlined in the angular 2 quick start guide)

Answer №1

My gulp setup is organized within the gulpfile.js/ folder. Inside this folder, you'll find index.js, config.js, and a tasks/ folder. The tasks/typescript.js file handles the compilation of TypeScript (with 15 other tasks in the tasks folder). This structure allows me to work with manageable chunks instead of one large gulpfile.js that does everything...

gulpfile.js/index.js

var gulp = require('gulp');
var config = require('./config.js');
var plugins = require('gulp-load-plugins')();
    plugins.brsync = require('browser-sync').create();
    plugins.builder = require('systemjs-builder');

function run(name) {
  return require('./tasks/' + name)(gulp, plugins, config);
}
// ...other tasks, in alphabetical order! (:
gulp.task('typescript',     run('typescript'));

gulpfile.js/config.js

var distDir           = 'dist';
var staticDir         = isGAE() ? '/static' : '';

module.exports = {
  SRC: {
    TYPESCRIPT:       'src/scripts/**/*.ts',
  },
  DST: {
    MAPS:                                   './maps',
    SCRIPTS:          distDir + staticDir + '/scripts',
  },
};

gulpfile.js/tasks/typescript.js

module.exports = function (gulp, plugins, CONFIG) {
  return function typescript() {

    var tsProject = plugins.typescript.createProject('tsconfig.json');
    var tsReporter = plugins.typescript.reporter.fullReporter();

    var stream = gulp
      .src(CONFIG.SRC.TYPESCRIPT, { since: gulp.lastRun('typescript') })
      .pipe(plugins.sourcemaps.init())

      .pipe(plugins.typescript(tsProject, undefined, tsReporter))

      .pipe(plugins.sourcemaps.write(CONFIG.DST.MAPS,
                                     {sourceMappingURLPrefix: '/scripts'}))
      .pipe(gulp.dest(CONFIG.DST.SCRIPTS))
      .on('error', plugins.util.log);

    return stream;
  };
};

gulpfile.js/tasks/watch.js

module.exports = function (gulp, plugins, CONFIG) {
  return function watch() {
    plugins.brsync.init(CONFIG.BRSYNC);

    gulp.watch(CONFIG.SRC.TEST,           () => queue_tasks(['karma']));
    gulp.watch(CONFIG.SRC.TYPESCRIPT,     () => queue_tasks(['typescript'], brsync_reload));
  };
};

I encountered an issue with gulp watch: When watching files and saving multiple at once, it would trigger tasks multiple times. To address this, I implemented a queue_tasks() function. Check the link for more details...

It's worth noting that I'm leveraging Gulp 4:

gulp.src(CONFIG.SRC.TYPESCRIPT, { since: gulp.lastRun('typescript') })

I've included the since option in src() to cache files and only process changed ones. Although I haven't thoroughly tested it with TypeScript files yet (it works elsewhere), so feel free to remove it if any issues arise...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to toggle an element's visibility using the select tag?

Let me break down the issue for you. In my HTML code, I have a select element that looks like this: <select id="seasons" multiple="multiple"> <option value="s01">Season 1</option> <option value="s02">Season 2</option> ...

Ensuring consistent geometry size regardless of renderer size is crucial - Plane resizing based on screen dimensions

https://i.sstatic.net/CwUxD.gif Here is a codepen showcasing the issue along with a GIF illustrating the confusion. Codepen: https://codepen.io/carelesscourage/pen/bGMWjNg My expectation I desire the plane to maintain the same size regardless of screen ...

I am looking to showcase the JSON output on a ReactJS interface

Here is the output of my JSON data I am using the Map function in ReactJS to display elements from a JSON array. I have attempted the code below and encountered an error stating "Cannot read property '_currentElement' of null." Can someone pleas ...

Transferring Object Information from AJAX to PHP

When attempting to send data to a PHP file for database storage, I am not receiving any response. If a checkbox is checked, the [obj][idCheckbox] value is set to 1; otherwise, it is set to 0. This is the file responsible for sending the data: var i=0; v ...

Tips for sending the setState function to a different function and utilizing it to identify values in a material-ui select and manage the "value is undefined" issue

I am currently utilizing a Material UI select component that is populated with data from an array containing values and options. Within this array, there exists a nested object property named "setFilter". The setFilter property holds the value of setState ...

Refreshing user information on Ionic platform

Hello there, I am seeking assistance on updating user data in Angular and Ionic. I have managed to retrieve the user ID from local storage and create a method to store the new user data. However, I'm struggling with updating the user with this new inf ...

Verify whether a group of div elements overlaps a fixed div while scrolling

I have a layout with a list of posts and a logo positioned at the bottom of the page. The issue I am facing is that sometimes the title of the posts and the logo overlap, and I want to set the logo's opacity to 0.25 only when the text from .cat-date i ...

Is there a way to customize the ripple effect color on a Button?

As I'm exploring the API (v3.9.2), I have noticed a reference to TouchRippleProps for ButtonBase on https://material-ui.com/api/button-base/ The code for my button is as follows: <Button variant="text" size={"large"} ...

The beauty of crafting intricate forms with Angular's reactive nested

In my Angular project, I am exploring the concept of nesting multiple reactive forms within different components. For instance, I have a component called NameDescComponent that includes a form with two inputs - one for name and one for description, along ...

I am experiencing an issue with my service provider when it comes to displaying multiple navigator stacks

Currently, I am developing a provider to manage the user's state across different views. The primary function of this provider is to display either one stack navigator or another based on whether a certain variable is filled or empty. This setup allow ...

Refrain from using inline JavaScript code within your

Here is the basic structure of my code: <a href="javascript:void(0);" onClick="viewServices(1)">Services</a> <a href="javascript:void(0);" onClick="viewServices(43)">Services</a> <a href="javascript:void(0);" onClick="viewServic ...

What is the reason for allowing var to declare duplicates, while const and let restrict duplicate declarations?

What is the reason behind var allowing duplicate declaration while const and let do not? var allows for duplicate declarations: xx=1; xx=2; console.log(xx+xx);//4 var xx=1; var xx=2; console.log(xx+xx);//4 However, let and const do not allow for dupl ...

Utilize Babel transpiling specifically for necessary Node modules

My current setup in nextjs is configured to handle ES6 code for IE11. module.exports = { poweredByHeader: false, distDir: "ssr_build", webpack(config) { config.node = { fs: "empty", net: "empty", tls: "empty" } config.plugins = config.plugi ...

Having trouble with conditional statements in jQuery when handling ajax responses?

I am currently working with the following code: $.ajax({ url: 'upload.php', //Server script to process data type: 'POST', xhr: function() { // Custom XMLHttpRequest var myXhr = $.ajaxSettings.xhr(); if(myX ...

Moving towards the target is a Three.js object

I'm struggling with an issue where the x position of my object moves quicker than the z position, or vice versa. How can I make my object slow down for the x position if the z position requires more time to move? This is the code in my animation func ...

Tips for generating search engine optimized URLs with category/subcategories/article slug in an Angular application

Currently, I am utilizing Angular 8 Version to develop a news application. My objective is to showcase the link in the following format: www.domain.com/category/category/title and www.domain.com/category. Can you guide me on how to accomplish this task? T ...

React: Avoid unnecessary re-rendering of child components caused by a bloated tree structure

I am dealing with a tree/directory structured data containing approximately 14k nodes. The issue I am facing is that every time a node is expanded or minimized by clicking a button, causing it to be added to an 'expanded' Set in the Redux state, ...

Deleting the HTML element

Can someone assist me with my script issue? I am facing a problem: when I open my file on fullscreen (over 768px), and move my pointer around the logo div, nothing happens. However, if I resize my browser to below 768px, then back again above 768px, the ...

Encountered a style group error 'non-collision' while using Angular with HERE Maps JS API 3.1

Occasionally, I encounter an error when trying to load a HERE map with the satellite base layer: Tangram [error]: Error for style group 'non-collision' for tile 13/16/15542/12554/15 Cannot read property 'retain' of undefined: TypeE ...

Making a dropdown menu spin using Jquery and Javascript

I am in need of a unique solution for a dropdown menu that appears rotated 90 degrees anticlockwise. The goal is to have the dropdown select "button" text displayed vertically, with the options sliding out in a similarly rotated, sideways manner featuring ...