TypeScript - Building with Webpack

We walk through the main steps required when using ag-Grid, TypeScript and Webpack below, but for more information about either TypeScript or Webpack please refer to those sites for more in depth information around these tools.

Initialise Project

mkdir ag-grid-ts-webpack cd ag-grid-ts-webpack npm init --yes

Install Dependencies

npm i --save @ag-grid-community/all-modules # or, if using Enterprise features # npm i --save @ag-grid-enterprise/all-modules npm i --save-dev typescript ts-loader webpack webpack-dev-server webpack-cli npm i --save-dev sass-loader node-sass style-loader css-loader html-webpack-plugin

Create Application

Our application will be a very simple one, consisting of a single class that will render a simple grid:

import {Grid, GridOptions, ModuleRegistry} from "@ag-grid-community/all-modules"; import {ClientSideRowModelModule} from "@ag-grid-community/client-side-row-model"; // or, if using Enterprise features // import {Grid, GridOptions, ModuleRegistry} from "@ag-grid-enterprise/all-modules"; // import {ClientSideRowModelModule} from "@ag-grid-enterprise/all-modules"; ModuleRegistry.register(ClientSideRowModelModule); import './styles.scss'; class SimpleGrid { private gridOptions: GridOptions = <GridOptions>{}; constructor() { this.gridOptions = { columnDefs: this.createColumnDefs(), rowData: this.createRowData() }; let eGridDiv:HTMLElement = <HTMLElement>document.querySelector('#myGrid'); new Grid(eGridDiv, this.gridOptions); } // specify the columns private createColumnDefs() { return [ {headerName: "Make", field: "make"}, {headerName: "Model", field: "model"}, {headerName: "Price", field: "price"} ]; } // specify the data private createRowData() { return [ {make: "Toyota", model: "Celica", price: 35000}, {make: "Ford", model: "Mondeo", price: 32000}, {make: "Porsche", model: "Boxter", price: 72000} ]; } } new SimpleGrid(); <!DOCTYPE html> <html> <head> </head> <body> <div id="myGrid" style="height: 200px;width: 600px" class="ag-theme-alpine"></div> </body> </html>

Webpack Configuration

const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/SimpleGrid.ts', module: { rules: [ { test: /\.tsx?$/, use: 'ts-loader', exclude: /node_modules/, }, { test: /\.(sa|sc|c)ss$/, use: ["style-loader", "css-loader", "sass-loader"] } ], }, resolve: { extensions: ['.tsx', '.ts', '.js'], }, output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, plugins: [ new HtmlWebpackPlugin({ template: 'src/index.html' }) ] };

entry

This serves as our entry point for our application.

resolve

As our imports done specify what file extension to use, we need to specify what file types we want to match on - in this case we're looking at TypeScript and JavaScript files, but you could also add CSS & HTML files too.

module.rules

Loaders tell Webpack how & what to do with certain types of file - we have specified a few here to deal with Typescript, HTML, CSS and Images:

  • ts-loader: transpile Typescript to ES5
  • html
  • scss: process and bundle imported SCSS

plugins

  • HtmlWebpackPlugin: takes our supplied template index.html and inserts the generates JS file for us

Typescript Configuration

We don't need to specify any Typescript configuration here, but ts-loader does expect a tsconfig.json so we need to create an empty file in the root of the project named tsconfig.json.

With all this in place, we can now add the following npm scripts to our package.json:

"scripts": { "start": "webpack-dev-server --inline --progress --port 8080", "build": "webpack --progress --profile --bail" },

Now we can either run npm start to run the development setup, or npm run build for the production build. In the case of the production build the generated files will be under the dist/ folder.

If we now run our application with the above code we will see this:

Datagrid

Example Code

The code for this example can be found on GitHub.