Exploring the Power of Angular 5 with TypeScript and ES2015 Syntax

I have been working on an angular 5 application where I needed to incorporate the dmn-js library. Unfortunately, this library does not come with typings available. To tackle this issue, I followed the guidelines provided in the Angular-CLI wiki regarding how to include third-party libraries without typings under the section titled "If the library doesn't have typings available at @types/, you can still use it by manually adding typings for it:"

After following these steps, my code has now been updated as follows:

src/typings.d.ts

/* SystemJS module definition */
declare var module: NodeModule;
declare module 'dmn-js';
interface NodeModule {
  id: string;
}

src/app/app.component.ts

import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import * as DmnJS from 'dmn-js';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'DMN';

  constructor(private http: HttpClient){    
  }

  ngOnInit(): void {
    var viewer = new DmnJS ({ container: 'body' });
    var dmnXML; //DMN 1.1 xml
    viewer.importXML(dmnXML, this.handleError);
  }

   handleError(err: any) {
    if (err) {
      console.warn('Ups, error: ', err);
    }else {
      console.log('rendered');
    }
  }

  load(): void {
    const url = '/assets/dish-decision.dmn';
    this.http.get(url, {
      headers: {observe: 'response'}, responseType: 'text'
    }).subscribe(
      (x: any) => {
        console.log('Fetched XML, now importing: ', x);
        //this.modeler.importXML(x, this.handleError);
      },
      this.handleError
    );
  }

  save(): void {
    //this.modeler.saveXML((err: any, xml: any) => console.log('Result of saving XML: ', err, xml));
  }

}

However, when I attempt to compile the code, I encounter the following error message. Despite having followed all the necessary steps, I am uncertain about what needs to be done to address this issue.

ERROR in ./node_modules/dmn-js-drd/lib/Viewer.js                                                                                                                                            
Module parse failed: Unexpected token (175:4)                                                                                                                                               
You may need an appropriate loader to handle this file type.                                                                                                                                
|     additionalModules,                                                                                                                                                                    
|     canvas,                                                                                                                                                                               
|     ...additionalOptions                                                                                                                                                                  
|   } = options;                                                                                                                                                                            
|                                                                                                                                                                                           
ERROR in ./node_modules/dmn-js-shared/lib/base/Manager.js                                                                                                                                   
Module parse failed: Unexpected token (292:16)                                                                                                                                              
You may need an appropriate loader to handle this file type.                                                                                                                                
|   }                                                                                                                                                                                       
|                                                                                                                                                                                           
|   _viewsChanged = () => {                                                                                                                                                                 
|     this._emit('views.changed', {                                                                                                                                                         
|       views: this._views,                                                                                                                                                                 
ERROR in ./node_modules/dmn-js-decision-table/lib/Viewer.js                                                                                                                                 
Module parse failed: Unexpected token (75:6)                                                                                                                                                
You may need an appropriate loader to handle this file type.                                                                                                                                
|       modules,                                                                                                                                                                            
|       additionalModules,                                                                                                                                                                  
|       ...config                                                                                                                                                                           
|     } = options;                                                                                                                                                                          
|                                                                                                                                                                                           
ERROR in ./node_modules/dmn-js-literal-expression/lib/Viewer.js                                                                                                                             
Module parse failed: Unexpected token (77:6)                                                                                                                                                
You may need an appropriate loader to handle this file type.                                                                                                                                
|       modules,                                                                                                                                                                            
|       additionalModules,                                                                                                                                                                  
|       ...config                                                                                                                                                                           
|     } = options;                                                                                                                                                                          
|                                                                                                                                                                                           

webpack: Failed to compile.   

Answer №1

After following the instructions in the Angular-cli wiki to add a third party library, you may encounter some issues with plugins required by dmn-js for proper support, such as spread operators and internal transforms using Babel.

To resolve these issues and properly configure webpack for dmn-js, here is a solution that has been achieved after investing a considerable amount of time:

https://i.sstatic.net/dJCu6.png

In your.Component.ts

constructor(private http: HttpClient ){
    this.http.get('../assets/val.xml',{responseType: 'text'}).subscribe(x=>{
     var xml= x; // my DMN 1.1 xml
     //var myContainer = document.getElementsByClassName('canvas');
    var viewer = new Viewer({
      container: '.canvas'
    });

    viewer.importXML(xml, function(err) {
     console.log('*********************');
      if (err) {
        console.log('error rendering', err);
      } else {
        viewer
        .getActiveViewer()
        .get('canvas')
          .zoom('fit-viewport');
      }
    });
    });

In your.Component.html

<body >
<div class="canvas" style="width:100vh;height:100vh ;padding-left:100px"></div>
  </body>

In Webpack.config.js (use ng eject , if not exists) Add a rule in module -> rules

 { test: /\.js$/, 
        exclude: /node_modules\/(?!(dmn-js|dmn-js-drd|dmn-js-shared|dmn-js-decision-table|table-js|dmn-js-literal-expression|diagram-js)\/).*/,
        loader: 'babel-loader',query: {presets: ["react","es2015","stage-0"]} 

      }

In index.html add stylesheet links

  <link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="680c050645021b285c465b4658">[email protected]</a>/dist/assets/dmn-js-drd.css">
  <link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e581888bc88f96a5d1cbd6cbd5">[email protected]</a>/dist/assets/dmn-js-decision-table.css">
  <link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9ddd4d794d3caf98d978a9789">[email protected]</a>/dist/assets/dmn-js-literal-expression.css">
  <link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b2f26256621380b7f6578657b">[email protected]</a>/dist/assets/dmn-font/css/dmn.css">

You have already added Typings.d.ts !!

Install the dependencies:

npm i --save-dev babel-plugin-inferno babel-plugin-transform-object-rest-spread babel-plugin-transform-class-properties babel-plugin-transform-object-assign

We hope this solution resolves any issues you may have encountered!

Ref1: https://github.com/bpmn-io/dmn-js-examples/tree/master/bundling

Ref2: Error: Missing class properties transform

Ref3:https://github.com/webpack/webpack/issues/2902

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

Modify the content of a tooltip when hovering over the session times button

My website coded in ASP.Net is able to generate buttons which contain the following HTML code: <a id="1173766" val="248506" titletext="<b>Click to book online for ABC Cinemas</b><strong>$10 tickets </strong>: Preview Screening& ...

Is there a way to assign an id to a hyperlink when it is clicked in order to trigger specific CSS changes?

I am working on a navigation bar that includes links passing data through GET to display information from the database. The challenge is styling these links with the id 'selected' to show which one is currently active. <a href="./welcome.php? ...

An issue has occurred: (SystemJS) XHR error (404 Not Found) while trying to load the resource at http://localhost:3000/ng2-google

I attempted to implement the ng2-google-recaptcha component found at https://github.com/leewinder/ng2-google-recaptcha. I followed the instructions on the page, but encountered the following error: Error: (SystemJS) XHR error (404 Not Found) loading htt ...

What is the best way to retrieve the coordinates of highlighted text within input or textarea fields?

I am currently developing a Chrome application that involves injecting JavaScript code into a webpage. Whenever a user selects text, a popup appears next to the selected text. To achieve this, I have implemented the following code to retrieve the coordinat ...

I am having trouble getting my console.log function to work properly on my HTML page

As a beginner in JavaScript, I am facing an issue with my console.log not working at all. When I type a console.log message, nothing shows up on my HTML page. I have tried to debug it, but being a newbie, I lack the necessary knowledge. All I can do is see ...

Javascript syntax error: Unexpected ending of data while trying to parse JSON data at line 1, column 1

I operate a CS:GO betting platform, and I encountered an issue when attempting to access the page for withdrawing skins. After completing the reCAPTCHA verification process to confirm that I am not a robot, I received the following error: Javascript err ...

Transition your Sequelize migrations to TypeORM

I'm currently in the process of transitioning a Node.js application from vanilla JS to Nest.js. In our previous setup, we used Sequelize as our ORM, but now we've decided to switch to TypeORM for its improved type safety. While exploring the Type ...

Encountering Difficulties Incorporating Angular Material into Custom Angular Library

While attempting to create a custom library using Angular Material components, I added Angular Material to my Angular project and successfully tested the material components in my application. However, when I tried to use Angular Material within a library ...

The combination of Javascript and CSS allows for interactive and visually

I'm currently working on a project where I had to create a simulated weather page using only Javascript. However, I am struggling with the overall layout and have a few questions that I hope someone can help me with: Despite trying various methods ...

Material-inspired Design Device Compatible DIV slide with JS, JQuery, and CSS

My goal is to achieve something similar to this: Desired Live Website I am looking for a feature where clicking on the div will slide in content from the right, load an external page inside it, and close when prompted. The slider div should be device c ...

What is the best way to clear and fill a div without causing it to resize?

I am faced with a challenge involving four thumbnail divs labeled .jobs within a #job-wrap container. When a .job is clicked, I want the #job-wrap to fade out, clear its contents, load information from the selected .job, and then fade back in. However, whe ...

Preserving the plus sign in a URL-encoded HTTP POST request

I have been working on a login function that handles requests. private login(params: LoginParams): Promise<any> { const loginHeaders: HttpHeaders = new HttpHeaders() .set('Content-Type', 'application/x-www-form-urlencoded; ...

Hide the MaterialTopTabNavigator from view

Is there a way to hide the react native MaterialTopTabNavigator on the screen while still maintaining the swiping functionality between screens? I want the user to be able to swipe between screens but not see the tab navigator itself. const Tab = creat ...

Leverage Typescript to convert string literal types to uppercase

type x = 'first' | 'second' I am looking to create a type y that is similar to this: type y = 'FIRST' | 'SECOND' Here is what I attempted: type x = 'first' | 'second' type y = {[key in x]: key[& ...

Communication between components in Angular 7 using services

I am working on creating a wizard-like interface with multiple steps. Within this setup, I have two components that do not have a direct parent-child relationship: The footer component contains a submit button that triggers the transition to the next ste ...

Divide a JavaScript project into multiple packages using either webpack or npm

I am embarking on the development of an application that needs to be compatible with Windows (PC), Android, and iOS. To achieve this, I plan to use Electron for Windows and React Native for mobile platforms. Both applications will be built using React and ...

Utilizing Typescript: Ensuring an array includes only specified values from an enum through strict enforcement

In my Angular application, I have an HTTP service that returns the allowed accesses for a specific user. The response structure is as shown below:- { "accessId": 4209318492034, "folderPath": "data/sample_folder/", ...

unable to connect a value with the radio button

I am struggling to incorporate a radio group within a list of items. I am facing difficulty in setting the radio button as checked based on the value provided. Can anyone provide me with some guidance? Here is the HTML code snippet: <div *ngFor=" ...

Redirecting with response headers in Next.js

Objective: The Goal: Clicking a button on a page should send a request to the controller. The controller will then set a cookie, and upon receiving the response, redirect the page to another page, such as the about page. Directly Calling API route from th ...

Whenever I attempt to send a request to a localhost API, I encounter the error message: "java.net.ConnectException: Connection timed out"

Currently, I am in the process of developing an application using NativeScript. To aid in debugging, I have connected my Android phone to the computer via a USB cable. However, when attempting to make a post request from my phone after running the app, I ...