Support run package wrote in typescript


#1

Typescript is good for maintaining big code base project, so it’s good to use it in a package with complex logic.
Is it possible to support run packages wrote in typescript?
I mean, people only commit the typescript code to package without compiled js files, just like using coffeescript.


#2

You can write a build script to transpile your TS into JS on install time. Atom packages install pretty much just like Node packages, so scripts mentioned in package.json will be run as normal.


#3

I generated a package using the package generator, added this config, and test.ts in ./lib, but when I reload atom, it didn’t generate the js file. Is any thing wrong? @DamnedScholar

{
  "name": "test-ts",
  "main": "./dist/test",
  "scripts": {
      "build": "tsc -p ./lib"
  },
  "version": "0.0.0",
  "description": "A short description of your package",
  "keywords": [],
  "repository": "https://github.com/atom/test-ts",
  "license": "MIT",
  "engines": {
    "atom": ">=1.0.0 <2.0.0"
  },
  "dependencies": {
    "typescript": "^2.6.2"
  }
}

#4

You need to go into the package folder in your command line and run npm install in order to run the build scripts. When a user downloads a package for the first time, that will be automatic, but when you’re developing a package, you need to install dependencies yourself.


#5

I have ran the npm install, but the build is not triggered automatically. Do you mean I need manually run npm run build after npm install?
So if I refactor the existing packages to ts, will it run the build scripts when user upgrade the packages?


#6

According to the NPM documentation, no build keyword is supported under scripts. You want install.


#7

OK, got the point. Just add a install to scripts in package.json, and the value of it is the compile command.

{
  "name": "test-ts",
  "main": "./dist/test",
  "scripts": {
    "install": "tsc"
  },
  "version": "0.3.0",
  "description": "A short description of your package",
  "keywords": [],
  "repository": "https://github.com/yubaoquan/test-ts",
  "license": "MIT",
  "engines": {
    "atom": ">=1.0.0 <2.0.0"
  },
  "dependencies": {
    "typescript": "^2.6.2"
  }
}

But is there any short comings of this approach? I found the package atom-typescript doesn’t use the compile-after-install way, instead, they put the compiled files into git repo as well.


#8

You can do that automatically with prepublish, prepare, or prepublishOnly, if you want to. It doesn’t make a difference to the person who installs your package.