-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class Lib { | ||
run() { | ||
console.log('Success!'); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Lib } from './Lib'; | ||
import Influx from 'influx'; | ||
const influx = new Influx.InfluxDB(); | ||
const l = new Lib(); | ||
l.run(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |