After working on some code that utilizes plain browser Javascript APIs and can be executed within a browser HTML environment (served by IIS Server or Chrome Extensions), I am eager to contribute to the community by creating a library that is not currently available in the market. However, when examining existing solutions, I am overwhelmed by the complexities of building a project (WebPack/Browserify etc). It's worth mentioning that I have limited experience with NodeJS/NPM.
For instance, I have a TypeScript project with the main file AwesomeClass.ts
structured as follows:
import { Helper1 } from "./Helper1.js";
import { Helper2 } from "./Helper2.js";
export class AwesomeClass {
doSomething() {
new Helper1().doSomething();
new Helper2().doSomething();
}
}
When compiled using tsc
(I utilize VS Code as my IDE), I can neatly package this into a JavaScript module that can be run in a browser.
import { AwesomeClass } from "./AwesomeClass.js";
// Utilizing AwesomeClass functionality
Therefore, my query is centered around how to build and distribute the AwesomeClass
. Perhaps NPM might not be necessary, but what about utilizing a CDN? Ideally, I envision having the following outputs in a dist
folder where developers can either host the files themselves or use a CDN:
awesomeclass.js
: Catering to those who prefer usingAwesomeClass
without the necessity of modules (referred to as UMD?). Essentially, exposing theAwesomeClass
globally.awesomeclass.es6.js
: Tailored for individuals who wish to employ theimport
statement, such as
. This approach resonates with me and is the one I prefer.import { AwesomeClass } from "https://cdn.example.com/awesomeclass.es6.js";
- I would also need something like
awesomeclass.d.ts
to accommodate TypeScript users. This poses a unique challenge because I currently lack understanding regarding its implementation for the 2nd scenario. TypeScript struggles to interpret types from import statements in JavaScript, adding another layer of complexity.
In all scenarios, I am inclined towards having just one packed js
/ts
file if feasible. Nevertheless, it's not a deal-breaker if I'm unable to achieve this (i.e., users may need to download Helper1.js
and Helper2.js
separately).
Here is a glimpse of my current tsconfig.json
:
{
"compileOnSave": true,
"compilerOptions": {
"noImplicitAny": true,
"target": "ES2020",
"module": "ES2020",
"declaration": true
},
"exclude": [
"node_modules"
]
}