Code Monkey home page Code Monkey logo

node_proj_tpl01's Introduction

  1. Using Repo as Template
  2. Generating this Template
    1. Create new Container
    2. Instaling minimal packages
    3. NPM Helper function
    4. Adding User same as in hostos
    5. Installing Minimal ( typescript + webpack )
    6. Check generated file
    7. Generating tsconfig.json
      1. Webpack config file
    8. Dummy Test code
    9. Run webpack
      1. Run webpack for Production build
      2. Run webpack Debugging build
  3. Further links
  • Sample setup for Nodejs + Webpack + Typescript *

Using Repo as Template

## Checkout from git
mkdir -p /home/hemant/Desktop/Node_Projects/typescript_webpack_01
cd /home/hemant/Desktop/Node_Projects/typescript_webpack_01
git clone https://git.affinity.com/hemantkumar.patel/node_setup.git .
## Removing .git to avoid accidental push 
rm -f .git
## Installing node packages
npm install
npx webpack
ls dist/
> dist/bundle.js.map
> dist/bundle.js
> 

Generating this Template

Create new Container

docker run --name node4 --hostname node4 -v /home/hemant:/home/hemant:rw -it ubuntu:20.04 bash

BELLOW ALL COMMANDS NOW INSIDE CONTAINER

Instaling minimal packages

apt-get update
apt-get install nodejs npm vim -y

NPM Helper function

function  npmll(){
## Helper dunftion to list all available version of package
echo  "Latest top 5 versions ";
npm view $1 versions --json | tail -n 6 | awk -vpkg="${1}" -F '"'  '{print "npm i -D " pkg "@" $2 }'
echo  "Currenlty installed version ";
npm list $1;
  
echo -ne "\n\nRemoving eisting package\n"
echo  "npm uninstall ${1}";
echo  "";
}
npmll tsc

Adding User same as in hostos

useradd hemant
mkdir -p /home/hemant
chown hemant:hemant /home/hemant

##Init new Node project

npm init --y

Installing Minimal ( typescript + webpack )

npm install --save-dev \
typescript \
ts-loader \
webpack \
webpack-cli \
wrapper-webpack-plugin \
declaration-bundler-webpack-plugin \
copy-webpack-plugin \
clean-webpack-plugin \
@types/node \
@types/webpack \
@types/google-publisher-tag

Check generated file

vim package.json

Generating tsconfig.json

vim tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"jsx": "react",
"allowJs": true,
"moduleResolution": "node",
"sourceMap": true
}
}
Webpack config file
vim webpack.config.js
const  path = require('path');
const  WrapperPlugin = require('wrapper-webpack-plugin');
  
module.exports = {
entry:  './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use:  'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
plugins: [
// strict mode for the whole bundle
new  WrapperPlugin({
test: /\.js$/, // only wrap output of bundle files with '.js' extension
header:  '(function (W,D,N) { "use strict";W[N]=W[N]||{};if(W[N].hb23){return;}W[N].hb23="20221228";\n',
footer:  '\n})(window,document,"__afflib");'
})
],
mode:  "production",
output: {
chunkLoadingGlobal:  'AffChunk',
chunkLoading:  'jsonp',
filename:  'bundle.js',
path:  path.resolve(__dirname, 'dist'),
// libraryTarget: 'amd',
// library: "AffHb2023",
}
};

Dummy Test code

mkdir src
echo  "export function add( a:number , b:number ) : number { return a + b ; } " > src/lib.ts
echo -ne "import { add } from './lib'
console.log( add(1,3) )
" > src/index.ts

Run webpack

Run webpack for Production build

######## Also source map enaled in production, you can utilise it

npx webpack
cat dist/bundle.js
cat dist/bundle.js.map
Run webpack Debugging build
npx webpack --mode development watch
npx webpack watch

Further links

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.