typescript-node-scripts v2.0.0-beta.3
typescript-node-scripts
Create Node.js applications based on TypeScript with zero-configuration.
Inspired by create-react-app
and Dan Abramov's The Melting Pot of JavaScript.
- Supports testing, building, and development in watch mode
- Supports custom TypeScript path mappings, aka
compilerOptions.path
Quick Start Guide
npx typescript-node-scripts create <appName>
cd <appName>
yarn start
Requirements
- node
>=8.0.0
process.platform !== 'win32'
Commands
yarn start
or npm run start
Starts the development server with watch mode and incremental builds. This command generates a bundle which is located at build/bundle.js
.
Options
Argument | Description |
---|---|
--no-collapse | Expands all the collapsed errors |
--no-auto-start | Disables auto starting and stopping of the application. By default, it executes build/bundle.js . |
--monorepo | Enable support for monorepos |
yarn build
or npm run build
Builds a production ready bundle. This minifies all sources into one simple, distributable dist/bundle.prod.js
alongside dist/bundle.prod.js.map
which is ideal for Docker builds.
This command does not bundle any dependencies which is require
-d or import
-ed from node_modules
!
Options
Argument | Description |
---|---|
--no-collapse | Expands all the collapsed errors |
--bypass-ci-warnings | Bypass CI warnings being treated as errors. |
--monorepo | Enable support for monorepo |
yarn test
or npm run test
Runs the Jest test runners in watch mode by default. You can add in Jest options as usual.
Example commands
yarn test --coverage
yarn test --watchAll
Tests
Jest is the main test runner that this package supports.
Test files
Everything that matches the globs below will be passed to Jest as a test file:
src/**/__tests__/**/*.(j|t)s?(x)
src/**/?(*.)(spec|test|t).(j|t)s?(x)
Setting up the test framework
You can use setupTests.ts
in your project root to set up the testing framework before each test.
Overriding Jest configuration
You can override the Jest configuration in your package.json
with the key jest
.
The following options can be overriden:
collectCoverageFrom
coverageReporters
coverageThreshold
Custom Module Paths
TNS supports custom module path mappings. A default custom path mapping is provided in tsconfig.json
{
"paths": {
"~/*": ["src/*"]
}
}
This configuration allows you to import any module which resolves absolutely to src
.
// src/lib/a/b/Module.ts
// instead of
import Module from '../../../Module'
// you can do
import Module from '~/Module'
Source Maps
Source maps are enabled by default and postfixed with .map
in the build
and dist
folders. These files provide accurate source mappings which are helpful when debugging errors and looking at stack traces during runtime.
In order to tell Node to use these source maps, you would need to install source-map-support
.
# if you're using yarn
yarn add source-map-support
# if you're using npm
npm i -S source-map-support
Then, in src/index.ts
, add:
import 'source-map-support/register'
Monorepo Support
To use typescript-node-scripts
with a monorepo like lerna + yarn workspaces, you need to add --monorepo
to the start and build scripts.
Then, you can use lerna run <start|build> --stream
in your root package.json
.
Debugging
Visual Studio Code
Webpack Build
TNS incrementally outputs a single bundle in build/bundle.js
(via webpack) when running yarn build
.
To debug the bundle, add the following into the configurations
array of your launch.json
file:
{
"type": "node",
"request": "launch",
"name": "Debug Webpack Build (via Node)",
"protocol": "inspector",
"program": "${workspaceFolder}/build/bundle.js"
}
You can now set breakpoints and run the task in order to debug your build.
TypeScript Source via ts-node
This method is NOT recommended due to ts-node being quite slow at running and compiling your source code.
First off, you would need to install ts-node
and tsconfig-paths
:
yarn add ts-node tsconfig-paths --dev
Then add the following into your configuration:
{
"type": "node",
"request": "launch",
"name": "Debug Source (ts-node)",
"protocol": "inspector",
"runtimeArgs": ["-r", "ts-node/register", "-r", "tsconfig-paths/register"],
"args": ["${workspaceFolder}/src/index.ts"],
"env": {
"TS_NODE_PROJECT": "./tsconfig.json",
"TS_NODE_FILES": "true"
}
}
Next, add the files
entry into your tsconfig.json
:
{
"files": ["src"]
}
You can now use breakpoints and run the build under debug mode.
Webpack
Overriding Webpack Configuration
This feature is marked as experimental and should not be used in production, unless you know exactly what you're doing
To override and merge your custom webpack configuration, create a file called webpack.config.override.js
in your root folder.
This file is then used to be merged into the base configuration that TNS provides, for example:
module.exports = {
mode: 'production' // always force production mode
}
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago