During the development of this package we often find ourselves recreating all the package.json scripts and information. Thus we decided to create a template that can easily be setup.
In this post we will look at what tools we use and how we came to creating this kick-ass template!
๐ง Tools Used
Here is a quick list of tools used and a quick description of how it is used in the project
Tool
Description
Yarn
Handle package installation
Npm
Publish the package to npm registry
Typescript
Strongly typed JavaScript
Webpack
Bundle the typescript into a single JavaScript Library
Snowpack
Hot Module Reload for testing
Concurrently
Start multiple commands at the same time
๐๏ธ Managing Structure
Let's take a look now at how we manage folders and files in our current project
Webpack is used for bundling everything in a library that is exposed in the browser while ts-loader let's us handle all the typescript necessities.
For the webpack config we use the output:library property to specify the name of the exposed library.
webpack.config.js
constpath=require('path');module.exports={entry:'./src/index.ts',module:{rules:[{test:/\.tsx?$/,use:'ts-loader',exclude:/node_modules/,},],},resolve:{extensions:['.tsx','.ts','.js'],},output:{// Define the package namelibrary:'pixiumPackage',libraryTarget:'umd',globalObject:'this',filename:'bundle.js',path:path.resolve(__dirname,'dist'),},};
This means we can then access the library like this:
Snowpack is an awesome tool that we discovered recently at Pixium. It is a modern frontend build tool that is so fast!
In this project Snowpack has a very simple use. Rendering a test environment to visualize changes in real time. It supports Hot Module Replacement which means everytime webpack will recompile a new bundle after making some changes the test environment will reload
test: '/', means we will mount the test folder as the root of the web
dist: '/dist' means we will mount the dist folder to /dist. This means that we can then require scripts from dist for instance <script src="./dist/bundle.js"></script>
๐ป Exposing our Code
And now let's finish off with taking a look at the code and how we can expose our constants, functions, class.
To make code accessible through our library we simply have to use the export method. For instance:
/**
* Say hello world in the console
*/exportconstPixiumHello=()=>{console.log(`hello world`)}
We can then call this in the following way:
<script>pixiumPackage.PixiumHello()</script>
You can also easily interact with the DOM through classic functions:
// Set the inner textdocument.getElementById("pixiumBanner").innerText="Pixium Digital Pte Ltd";
๐ Conclusion
We now have a very simple typescript bundling in place with a HMR system in place for easy testing.
There are a few command shortcuts implemented to easily push patches, minors, majors to npm.
All this coupled with a CDN and you get an awesome blazing fast library for your frontend.
In this boilerplate we have a simple look at a very basic typescript package exported for the web.
We leverage the use of webpack to expose our package to the browser.
Installation
install the dev packages with yarn:
yarn
Developement
yarn start
This will start webpack to compile the library on changes.
This will also start snowpack which will update the index.html in test once the library from webpack has been compiled
Build
yarn build
Publish Package
First login to the npm cli if not already done
npm login
You will need to remove the following line in your package.json