From 2b972e68d514737e03bd01d800387169f6b5e9a9 Mon Sep 17 00:00:00 2001 From: Dan Dascalescu Date: Sat, 8 Feb 2020 20:02:08 +1300 Subject: [PATCH] Initial actual commit --- Lib.ts | 5 +++++ README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 25 +++++++++++++++++++++++++ package.json | 27 +++++++++++++++++++++++++++ run.ts | 5 +++++ tsconfig.json | 9 +++++++++ 6 files changed, 117 insertions(+) create mode 100644 Lib.ts create mode 100644 README.md create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 run.ts create mode 100644 tsconfig.json diff --git a/Lib.ts b/Lib.ts new file mode 100644 index 0000000..71d829c --- /dev/null +++ b/Lib.ts @@ -0,0 +1,5 @@ +export class Lib { + run() { + console.log('Success!'); + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..7849644 --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# Modern TypeScript project template + +Minimalistic example of configuring TypeScript and Node to: +* emit modern ES modules code +* import modules that use Node built-ins +* import modules that don't have named exports (e.g. [`apollo-server`](https://github.com/apollographql/apollo-server/issues/1356#issuecomment-565277759), [`node-influx`](https://github.com/node-influx/node-influx/issues/298)) +* import your own modules without specifying an extension +* run the resulting JavaScript code + +# Emit ES modules code + +In `tsconfig.json`, set this in `compilerOptions`: + +```json + "target": "esnext", + "module": "esnext", // Output `import`/`export` ES modules +``` + +# Import modules that use Node built-ins (`http`, `url` etc.) + +* run `npm install --save-dev @types/node` +* in `tsconfig.json` under `compilerOptions`, set + * `"moduleResolution": "node"`, so `tsc` can find modules [when targeting ES6+](https://github.com/Microsoft/TypeScript/issues/8189) + * `"types": ["node"]` to avoid errors related to Node built-in modules + +# Import modules that don't have named exports + +Set `"allowSyntheticDefaultImports": true` in `tsconfig.json`. In our code, +instead of `import { InfluxDB } from 'influx` we have to write: + +```js +import Influx from 'influx'; +const influx = new Influx.InfluxDB(); +``` + +# Import your own modules without specifying an extension + +Run Node with the `node --experimental-specifier-resolution=node` parameter: + + node --experimental-specifier-resolution=node run.js + +Otherwise, [node mandates that you specify the extension](https://nodejs.org/api/esm.html#esm_mandatory_file_extensions) in the `import` statement. + +# Run the resulting JavaScript code + +Add `"type": "module"` to `package.json`, because [TypeScript can't generate files with the .mjs extension](https://github.com/microsoft/TypeScript/issues/18442#issuecomment-581738714). \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..f0a385d --- /dev/null +++ b/package-lock.json @@ -0,0 +1,25 @@ +{ + "name": "tsmodules", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@types/node": { + "version": "13.7.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.7.0.tgz", + "integrity": "sha512-GnZbirvmqZUzMgkFn70c74OQpTTUcCzlhQliTzYjQMqg+hVKcDnxdL19Ne3UdYzdMA/+W3eb646FWn/ZaT1NfQ==", + "dev": true + }, + "influx": { + "version": "5.5.1", + "resolved": "https://registry.npmjs.org/influx/-/influx-5.5.1.tgz", + "integrity": "sha512-06kWkvKZzdk7ONBi+MydTTVcpziIfzIKcTUqXpK0Uc0KoSVye0umQYDwDPJvMJtBjiS6L2eadXI7YsnNvOxbQw==" + }, + "typescript": { + "version": "3.7.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.5.tgz", + "integrity": "sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw==", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f274e49 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "tsmodules", + "version": "1.0.0", + "description": "TypeScript project template to import modules without extension, and modules with default exports that use node built-ins", + "keywords": [ + "typescript", + "modules", + "es modules", + "esnext", + "influxdb", + "apollo-server" + ], + "license": "MIT", + "author": "dandv", + "type": "module", + "main": "run.js", + "scripts": { + "start": "node --experimental-specifier-resolution=node run.js" + }, + "dependencies": { + "influx": "^5.5.1" + }, + "devDependencies": { + "@types/node": "^13.7.0", + "typescript": "^3.7.5" + } +} diff --git a/run.ts b/run.ts new file mode 100644 index 0000000..1db46bb --- /dev/null +++ b/run.ts @@ -0,0 +1,5 @@ +import { Lib } from './Lib'; +import Influx from 'influx'; +const influx = new Influx.InfluxDB(); +const l = new Lib(); +l.run(); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f6b21c8 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "target": "esnext", + "module": "esnext", // Output `import`/`export` ES modules + "moduleResolution": "node", // Find modules in node_modules - https://github.com/Microsoft/TypeScript/issues/8189#issuecomment-212079251 + "allowSyntheticDefaultImports": true, // Allow default imports from modules with no default export. This does not affect code emit, just typechecking. + "types": ["node"] // Influx uses built-in node modules like http or url + } +}