1.0.3 • Published 3 years ago

ntwc v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

NTWC - NodeJS TypeScript Webpack Compiler

Generate fully configured TypeScript project in matter of seconds for your new NodeJS application.

Installation

npm install -g ntwc

Features

  • Automatic configuration of TypeScript for specific node version
  • Automatic configuration of WebPack (Optional)
  • Automatic configuration of ESLint (Optional)
  • Automatic configuration of Prettier (Optional)
  • Automatic configuration for ES Modules after compile
  • Automatic generation of package.json with used dependencies only after build
  • Support for TSConfig Paths (Yes, after compilation also!)
  • Option to bundle your project with WebPack
  • Easy development with script watcher & script execution after success compile

Getting Started

1. Generating new project

  1. Commands & Options:
  • init, create, or generate - to generate new project
OptionShorthand OptionDescriptionDefaultValues
--target-tNodeJS version you are targeting14.0.0esnext or semver valeu
--module-mModule type of your scriptesmoduleesnext, module or commonjs
--name-nProject namen/a{string}
--webpack / --no-webpack-wpkEnable/Disable WebPackfalsen/a
--eslint / --no-eslint-eslEnable/Disable ESLinttruen/a
--prettier / --no-prettier-prtEnable/Disable Prettiertruen/a
  1. Example

The following command will generate project in current directory

ntwc create

Generating project with predefined target and module kind:

ntwc create -t 12.0.0 -m commonjs

2. Serving / Building project

2.1 Commands & Options:

  • serve, watch or dev - to build and run project in developer mode
  • build or compile - to build and run project in production mode

2.2 Example

Build Mode

To execute script after successfully file you have to set runAfterBuild: true for each script in .ntwcrc.json config.

{
  "entries": [
    {
      "script": "index",
      "argv": "",
      "runAfterBuild": true
    }
  ]
}
Watch Mode

Same method as Build Mode is used except during serve mode when reading from .ntwcrc.json compiler will check for runAfterDevBuild: true instead of runAfterBuild

{
  "entries": [
    {
      "script": "index",
      "argv": "",
      "runAfterDevBuild": true
    }
  ]
}

3. Adding New Entry Point

OptionShorthand OptionDescriptionDefaultValues
--entry-eName of the entry without extensionn/a<string>
--argv-aArgv for specific entryn/an/a or <string>
--bin-bName for your binary entryn/an/a or <string>

To add new entry point for your application you can use following command

ntwc add --entry "app"

or

ntwc add --entry "app" --argv "--myparam 123"

This will: 1. Create ./src/app.ts 2. Add entry to ./ntwcrc.json


4. Changing target

To change NodeJS target version you can use following command

ntwc change --target 12.0.0 --module commonjs

!IMPORTANT: This command will overwrite following config files: .ntwcrc.json

// Values to be overwriten
['target', 'module']

tsconfig.json

// Values to be overwriten
['compilerOptions.target', 'compilerOptions.module', 'compilerOptions.lib']

This will: 1. Reconfigure your tsconfig.json (IDE reload might be needed) 3. Reconfigure your .ntwcrc.json


.ntwcrc.json

{
  "target": "esnext" or "14.0.0",
  "module": "module", // esnext, module or commonjs
  "structure": {
    "bundle": "./bundle", // directory where bundled script will go
    "distribution": "./dist", // directory where compiled scripts will go
    "source": "./src", // source directory
    "resources": ["./public", "./assets"] // custom directories
  },
  "builder": {
    "bundle": false, // bundle code after production compile using webpack
    "updateBeforeCompile": false, // Update Packages before building project for production
    "cleanBeforeCompile": true // Remove /dist/ contents before each compile
  },
  "npm": {
    "publish": true, // After build, prepare project for npm publishing.
    "private": false // Append private flag in package.json
  },
  // List of scripts that will be used for compiler to determinate whether script should be executed or not.
  // Also this list will be used if using webpack to point entry points.
  "entries": [
    {
      "script": "index", // point to script name without extension ./src/{script}.ts
      "argv": "", // Pass arguments to current script. process.argv.
      "runAfterDevBuild": true, // Execute current script after successfull build during serve mode
      "runAfterBuild": false, // Execute current script after successfully build for production mode
      "binaryName": "" // If else then empty string it will be put into package.json/bin/${binaryName}. Used only when npm.publish is true.
    }
  ]
}

Disabled Config Properties

Some of the original config values will be always overwriten and have no effect if you set them in original config file. Like:


tsconfig.json

PropertyDescription
compilerOptions.moduleResolutionAlways set to node
include & filesThose settings can be modified for IDE purposes and has no effect during compilation. Compiler will ignore include and generate files based on .ntwcrc.json entries. This way we wont have to compile unused scripts.

package.json

PropertyDescription
mainAuto generated after build
typeAuto generated after build
engines.nodeAuto generated after build (unset value if target is 'esnext')

webpack.config.js

PropertyDescription
modeAlways set to production
targetBased on .ntwcrc.rc target
entryBased on .ntwcrc.rc entries
output.pathAlways set to ./bundle
output.libraryBased on .ntwcrc.rc module
output.libraryTargetUnused because its deprecated
node.globalAlways set to false
node.__filenameAlways set to false
node.__dirnameAlways set to false

Q&A

1. Why do we need entries in .ntwcrc.json? Unlike on Web Server, in NodeJS we are always executing only one script per call. This property will allow compiler to know which script should be compiled, bundled and/or executed without compiling or touching any unused/unmodified script from the source directory.

2. Will my code run faster if i bundle it? NO, bundling your code wont make any performance differences to your application. Its just there as an option. Who knows... maybe you wanna be that evil coworker that don't want his code to be edited by others 😈


Special Thanks

  • Linus Unnebäck for the typescript configuration research when targeting specific versions of NodeJS