1.0.9 • Published 2 years ago

typescript-lib2 v1.0.9

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

initialize this library

How To Create a Node.js Module

init the folder

# direct initialize the library with default options
# this will just create the file "package.json"
$ npm init -y

add codes

Add codes in the new file "index.js", here is the content:

class Color {
  constructor(name, code) {
    this.name = name;
    this.code = code;
  }
}

const allColors = [
  new Color('brightred', '#E74C3C'),
  new Color('soothingpurple', '#9B59B6'),
  new Color('skyblue', '#5DADE2'),
  new Color('leafygreen', '#48C9B0'),
  new Color('sunkissedyellow', '#F4D03F'),
  new Color('groovygray', '#D7DBDD'),
];

exports.getRandomColor = () => {
  return allColors[Math.floor(Math.random() * allColors.length)];
}

exports.allColors = allColors;

This will export a list "allColors" and a function "getRandomColor()"

test

You can test lib with the Node.js REPL (Read-Evaluate-Print-Loop)

# this will enter REPL mode
$ node

> colors = require('./index');
> colors.getRandomColor();

# leave interactive mode
> .exit

test with application

Create the application "typescript-app2" in the same way.

# same with lib init, no extra things to setup
$ npm init -y

# install "typescript-lib2" and save the dependency
$ npm install --save ../typescript-lib2

Then, add a new file "index.js" with the content.

const colors = require('typescript-lib2');

const chosenColor = colors.getRandomColor();
console.log(`You should use ${chosenColor.name} on your website. It's HTML code is ${chosenColor.code}`);

Run application script "index.js"

$ node index.js

a global link for applications

This lib can add to global link (inside the folder "/usr/local/lib/node_modules")

# enter lib root, and run this command to put this lib to a global link
# /usr/local/lib/node_modules/typescript-lib2 -> ../../../../Users/fred/Fred/workspace/nodejs/typescript-lib2
$ sudo npm link

# then use this command in "typescript-app2" folder
# this will create a folder link in "node_modules" folder without dependency in package.json
$ sudo npm link typescript-lib2 

# unlink
$ npm unlink typescript-lib2

publish lib to npm

Publishing scoped public packages

$ npm publish --access public

Support typescript

Writing a Node.js module in TypeScript

What is a tsconfig.json/document

How To Run TypeScript Scripts with ts-node

Creating .d.ts Files from .js files

npm vs npx

https://stackoverflow.com/a/52018825/1896825

NPM - Manages packages but doesn't make life easy executing any.

NPX - A tool for executing Node packages.

ts REPL

not fully support for this feature. to enter the ts REPL, use this command

$ npx ts-node

ts script

# ts compiler
$ npm install typescript --save-dev

# function package, skip this
$ npm install emojione @types/emojione --save

Then, add "tsconfig.json". Or use this to generate "tsconfig.json" with all available options.

$ ./node_modules/.bin/tsc --init

Compile ts file "enmoji.ts" and "enmoji.test.ts"

# *.d.ts and *.js file will be generated on dist folder.
$ npm run build

Support Rollup

Rollup Tutorial

How to Setup a TypeScript project using Rollup.js

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago