Skip to content

Commit

Permalink
Initial actual commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dandv committed Feb 8, 2020
1 parent 76f34a3 commit 2b972e6
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class Lib {
run() {
console.log('Success!');
}
}
46 changes: 46 additions & 0 deletions README.md
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).
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions package.json
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"
}
}
5 changes: 5 additions & 0 deletions run.ts
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();
9 changes: 9 additions & 0 deletions tsconfig.json
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
}
}

0 comments on commit 2b972e6

Please sign in to comment.