Last active
August 1, 2023 20:47
-
-
Save ivanitskiy/76aa8a4a5218f259afacf1f0b538786a to your computer and use it in GitHub Desktop.
NJS + NPM + TS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"extends": [ | |
"eslint:recommended", | |
"plugin:@typescript-eslint/recommended", | |
"plugin:prettier/recommended" | |
], | |
"ignorePatterns": ["**/node_modules/", "/dist/", "/lib/"], | |
"parserOptions": { | |
"sourceType": "module" | |
}, | |
"rules": { | |
"@typescript-eslint/no-unused-vars": [ | |
"warn", | |
{ | |
"argsIgnorePattern": "^_", | |
"varsIgnorePattern": "^_", | |
"caughtErrorsIgnorePattern": "^_" | |
} | |
] | |
}, | |
"root": true | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# style settings, see https://prettier.io/docs/en/options.html | |
semi: false | |
singleQuote: true | |
trailingComma: es5 | |
tabWidth: 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// @ts-check | |
/** @type {babel.ConfigFunction} */ | |
// eslint-disable-next-line no-undef | |
module.exports = (api) => ({ | |
presets: [ | |
// Transpile modern JavaScript into code compatible with njs. | |
// This is used only for building the dist bundle with Rollup. | |
...(api.env('njs') ? ['babel-preset-njs'] : []), | |
// Parse TypeScript syntax and transform it to JavaScript (i.e. it strips | |
// type annotations, but does not perform type checking). | |
[ | |
'@babel/preset-typescript', | |
{ | |
allowDeclareFields: true, | |
}, | |
], | |
], | |
plugins: [ | |
...(!api.caller((c) => c && c.supportsStaticESM) | |
? [ | |
// Transform ES modules to CommonJS if needed needed for Mocha tests). | |
// Mocha, babel-node, babel/register etc. don't understand ES module | |
// syntax, so we have to transform it to CommonJS. | |
// This is not used with Rollup. | |
'@babel/plugin-transform-modules-commonjs', | |
] | |
: []), | |
...(api.env('mocha') | |
? [ | |
// Transform power-assert. This is used only for Mocha tests. | |
'babel-plugin-empower-assert', | |
'babel-plugin-espower', | |
] | |
: []), | |
], | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# syntax=docker/dockerfile:1 | |
FROM node:18 AS builder | |
WORKDIR /app | |
COPY package.json package-lock.json ./ | |
RUN --mount=type=cache,target=/app/.npm \ | |
npm set cache /app/.npm && \ | |
npm ci | |
COPY . . | |
RUN npm run build | |
FROM nginx:1.24.0 | |
COPY --from=builder /app/dist/acme.js /usr/lib/nginx/njs_modules/mymodule.js | |
# COPY ./examples/nginx.conf /etc/nginx/nginx.conf | |
# install the latest njs > 0.8.0 | |
RUN --mount=type=cache,target=/var/cache/apt <<EOF | |
set -eux | |
export DEBIAN_FRONTEND=noninteractive | |
apt -qq update | |
apt install -qq --yes --no-install-recommends --no-install-suggests \ | |
curl gnupg2 ca-certificates lsb-release debian-archive-keyring | |
update-ca-certificates | |
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \ | |
| tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null | |
gpg --dry-run --quiet --no-keyring --import --import-options import-show \ | |
/usr/share/keyrings/nginx-archive-keyring.gpg | |
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \ | |
http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \ | |
| tee /etc/apt/sources.list.d/nginx.list | |
apt update -qq | |
apt install -qq --yes --no-install-recommends --no-install-suggests \ | |
nginx-module-njs | |
apt remove --purge --auto-remove --yes | |
rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx.list | |
EOF |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "NAME", | |
"version": "1.0.0", | |
"description": "## How do I use this template?", | |
"main": "dist/main.js", | |
"files": [ | |
"dist", | |
"src" | |
], | |
"keywords": [], | |
"author": "", | |
"engines": { | |
"node": ">= 14.15" | |
}, | |
"scripts": { | |
"build": "rollup -c --environment NODE_ENV:dev", | |
"clean": "rimraf dist/* lib/* node_modules/.cache/*", | |
"lint": "run-p lint:*", | |
"lint:eslint": "npx eslint .", | |
"start": "run-p watch start-nginx", | |
"start-nginx": "start-nginx --version 1.25.x --port 8090 --watch dist/ integration-tests/nginx.conf", | |
"test": "run-p test:*", | |
"test:unit": "mocha --config ./unit-tests/.mocharc.js", | |
"test:integration": "rollup -c && mocha --config ./integration-tests/.mocharc.js", | |
"watch": "rollup -c --watch --no-watch.clearScreen", | |
"prettier": "prettier --check --write src/" | |
}, | |
"dependencies": { | |
"asn1js": "^3.0.5", | |
}, | |
"devDependencies": { | |
"@babel/core": "^7.21.8", | |
"@babel/plugin-transform-modules-commonjs": "^7.21.5", | |
"@babel/preset-typescript": "^7.21.5", | |
"@babel/register": "^7.21.0", | |
"@rollup/plugin-babel": "^5.3.1", | |
"@rollup/plugin-commonjs": "^18.1.0", | |
"@rollup/plugin-json": "^6.0.0", | |
"@rollup/plugin-node-resolve": "^11.2.1", | |
"@types/babel__core": "^7.20.0", | |
"@types/mocha": "^8.2.3", | |
"@types/rollup-plugin-add-git-msg": "^1.1.1", | |
"@typescript-eslint/eslint-plugin": "^4.33.0", | |
"@typescript-eslint/parser": "^4.33.0", | |
"babel-plugin-empower-assert": "^2.0.0", | |
"babel-plugin-espower": "^3.0.1", | |
"babel-preset-njs": "^0.2.1", | |
"babel-register-ts": "^7.0.0", | |
"eslint": "^7.32.0", | |
"eslint-config-prettier": "^8.8.0", | |
"eslint-plugin-prettier": "^4.2.1", | |
"got": "^11.8.6", | |
"mocha": "^10.2.0", | |
"mocha-suite-hooks": "^0.1.0", | |
"nginx-testing": "^0.4.0", | |
"njs-types": "^0.8.0", | |
"npm-run-all": "^4.1.5", | |
"power-assert": "^1.6.1", | |
"prettier": "^2.8.8", | |
"rimraf": "^3.0.2", | |
"rollup": "^2.79.1", | |
"rollup-plugin-add-git-msg": "^1.1.0", | |
"typescript": "~4.2.4" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-check | |
import addGitMsg from 'rollup-plugin-add-git-msg' | |
import babel from '@rollup/plugin-babel' | |
import commonjs from '@rollup/plugin-commonjs' | |
import resolve from '@rollup/plugin-node-resolve' | |
import json from '@rollup/plugin-json' | |
import pkg from './package.json' | |
// List of njs built-in modules. | |
const njsExternals = ['crypto', 'fs', 'querystring'] | |
// eslint-disable-next-line no-undef | |
const isEnvProd = process.env.NODE_ENV === 'production' | |
/** | |
* Plugin to fix syntax of the default export to be compatible with njs. | |
* (https://github.com/rollup/rollup/pull/4182#issuecomment-1002241017) | |
* | |
* @return {import('rollup').OutputPlugin} | |
*/ | |
const fixExportDefault = () => ({ | |
name: 'fix-export-default', | |
renderChunk: (code) => ({ | |
code: code.replace(/\bexport { (\S+) as default };/, 'export default $1;'), | |
map: null, | |
}), | |
}) | |
/** | |
* @type {import('rollup').RollupOptions} | |
*/ | |
const options = { | |
input: 'src/index.ts', | |
external: njsExternals, | |
plugins: [ | |
// Transpile TypeScript sources to JS. | |
babel({ | |
babelHelpers: 'bundled', | |
envName: 'njs', | |
extensions: ['.ts', '.mjs', '.js'], | |
}), | |
// Resolve node modules. | |
resolve({ | |
extensions: ['.mjs', '.js', '.json', '.ts'], | |
}), | |
json(), | |
// Convert CommonJS modules to ES6 modules. | |
commonjs(), | |
// Fix syntax of the default export. | |
fixExportDefault(), | |
// Plugins to use in production mode only. | |
...(isEnvProd | |
? [ | |
// Add git tag, commit SHA, build date and copyright at top of the file. | |
addGitMsg(), | |
] | |
: []), | |
], | |
output: { | |
file: pkg.main, | |
format: 'es', | |
}, | |
} | |
export default options |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// now you can use the NPM dep | |
// import { *} from name | |
async function hello(r: NginxHTTPRequest): Promise<void> { | |
return r.return(200, "Hello world") | |
} | |
export default { | |
hello, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"compilerOptions": { | |
/* Visit https://aka.ms/tsconfig.json to read more about this file */ | |
/* Basic Options */ | |
"incremental": true, /* Enable incremental compilation */ | |
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ | |
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ | |
// "lib": [] /* Specify library files to be included in the compilation. */ | |
"allowJs": true, /* Allow javascript files to be compiled. */ | |
// "checkJs": true, /* Report errors in .js files. */ | |
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ | |
// "declaration": true, /* Generates corresponding '.d.ts' file. */ | |
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ | |
// "sourceMap": true, /* Generates corresponding '.map' file. */ | |
// "outFile": "./", /* Concatenate and emit output to single file. */ | |
// "outDir": "./", /* Redirect output structure to the directory. */ | |
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ | |
// "composite": true, /* Enable project compilation */ | |
"tsBuildInfoFile": "./node_modules/.cache/tsc/root.tsbuildinfo", /* Specify file to store incremental compilation information */ | |
// "removeComments": true, /* Do not emit comments to output. */ | |
"noEmit": true, /* Do not emit outputs. */ | |
// "importHelpers": true, /* Import emit helpers from 'tslib'. */ | |
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ | |
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ | |
/* Strict Type-Checking Options */ | |
"strict": true, /* Enable all strict type-checking options. */ | |
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ | |
"strictNullChecks": true, /* Enable strict null checks. */ | |
"strictFunctionTypes": true, /* Enable strict checking of function types. */ | |
"strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ | |
"strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ | |
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ | |
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ | |
/* Additional Checks */ | |
"noUnusedLocals": true, /* Report errors on unused locals. */ | |
"noUnusedParameters": true, /* Report errors on unused parameters. */ | |
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ | |
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ | |
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ | |
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */ | |
/* Module Resolution Options */ | |
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ | |
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ | |
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ | |
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ | |
// "typeRoots": [], /* List of folders to include type definitions from. */ | |
// "types": [], /* Type declaration files to be included in compilation. */ | |
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ | |
// "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ | |
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ | |
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ | |
"resolveJsonModule": true, /* Include modules imported with '.json' extension. */ | |
/* Source Map Options */ | |
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ | |
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ | |
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ | |
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ | |
/* Experimental Options */ | |
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ | |
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ | |
/* Advanced Options */ | |
"skipLibCheck": true, /* Skip type checking of declaration files. */ | |
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */ | |
}, | |
"include": [ | |
"./integration-tests", | |
"./unit-tests", | |
"./*.js", | |
], | |
"references": [ | |
{ | |
"path": "./src" | |
}, | |
], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment