0

At the moment, our organization is developing a JavaScript library for use with one of our products. Currently, this is developed through the following steps:

  • use TypeScript and internal modules to create namespaced classes (one namespace for our internal classes, and one for the public API), using /// <reference> tags to determine dependencies
  • targeting ES5, generate compiled JavaScript (with the --out flag) from _references.ts (a two step process, for internal and public APIs), concatenate and minify everything into a single js file
  • automatically generate documentation using the generated .d.ts file for the public API

We've been looking at updating our codebase, and we're looking at the more recent updates to TypeScript's features, specifically, moving towards using a single tsconfig.json, and the use of ES6 modules. I admit I don't have the most comprehensive knowledge myself but I've encountered the following issues:

  • ES6 modules seem to be dependent on preserving a directory structure so this might mean we lose our namespaces. I'm not sure how we will later be able to access classes on an ES5 platform.
  • I've tried investigating the use of browserify to see how everything can be bundled up together; however, it requires an entry point for the program, which our library does not really have as it is generally just a series of class declarations with functionality to interact with our organization's products.

Am I missing some fundamental knowledge here? Is it feasible to use ES6 classes for what we're trying to accomplish, and if so, what would be a suggested workflow (in terms of tooling?) Or do we need to stay with namespaced classes for this kind of required output?

1 Answer 1

2

ES6 modules seem to be dependent on preserving a directory structure so this might mean we lose our namespaces

Not true in general, but this is how you would use them "within a project" when using typescript.

I'm not sure how we will later be able to access classes on an ES5 platform

Compile down to ES5 using a module pattern like --module commonjs. Using commonjs modules (aka NPM modules) is a well established pattern with major workflow tools out that that support it like webpack, jspm, browserify.

which our library does not really have as it is generally just a series of class declarations with functionality to interact with our organization's products.

Create an index.ts and export the stuff that is your Libraries public api from it. This file is then your entry point.

Sign up to request clarification or add additional context in comments.

2 Comments

I disagree with your answer to "ES6 modules seem to be dependent on preserving a directory structure". ES6 modules don't make any assumption about how the module identifier should be resolved. That all depends on the actual module loader. And currently, ES6 modules are typically transpiled to CommonJS, which indeed uses file paths to identify modules (but again, this has nothing to do with ES6 modules).
Thanks. I've updated that section to be typescript specific ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.