Skip to content

Commit

Permalink
Merge pull request #3 from voxpelli/self-hosted
Browse files Browse the repository at this point in the history
Self host types to enable experimentation
  • Loading branch information
fox1t authored Oct 19, 2020
2 parents 9577f4f + ff50081 commit c4ff2f0
Show file tree
Hide file tree
Showing 32 changed files with 320 additions and 499 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ dist

# TernJS port file
.tern-port
package-lock.json
package-lock.json
39 changes: 0 additions & 39 deletions cjs/index.js

This file was deleted.

17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
"description": "",
"main": "index.js",
"scripts": {
"run:cjs": "node ./cjs",
"run:esm": "node ./esm/index.mjs",
"run:ts-interop-false": "tsc -p ./typescript/es-module-interop-false/tsconfig.json && node ./typescript/es-module-interop-false/build",
"run:ts-interop-true": "tsc -p ./typescript/es-module-interop-true/tsconfig.json && node ./typescript/es-module-interop-true/build",
"run:typed-cjs": "tsc -p ./typed-cjs/tsconfig.json && node ./cjs",
"all": "run-p run:*"
"run:namespace:cjs": "node ./self-hosted-namespace/cjs",
"run:namespace:esm": "node ./self-hosted-namespace/esm/index.mjs",
"run:namespace:ts-interop-false": "tsc -p ./self-hosted-namespace/typescript/es-module-interop-false/tsconfig.json && node ./self-hosted-namespace/typescript/es-module-interop-false",
"run:namespace:ts-interop-true": "tsc -p ./self-hosted-namespace/typescript/es-module-interop-true/tsconfig.json && node ./self-hosted-namespace/typescript/es-module-interop-true",
"run:namespace:typed-cjs": "tsc -p ./self-hosted-namespace/typed-cjs/tsconfig.json && node ./self-hosted-namespace/cjs",
"run:triplet:cjs": "node ./self-hosted-triplet/cjs",
"run:triplet:esm": "node ./self-hosted-triplet/esm/index.mjs",
"run:triplet:ts-interop-false": "tsc -p ./self-hosted-triplet/typescript/es-module-interop-false/tsconfig.json && node ./self-hosted-triplet/typescript/es-module-interop-false",
"run:triplet:ts-interop-true": "tsc -p ./self-hosted-triplet/typescript/es-module-interop-true/tsconfig.json && node ./self-hosted-triplet/typescript/es-module-interop-true",
"run:triplet:typed-cjs": "tsc -p ./self-hosted-triplet/typed-cjs/tsconfig.json && node ./self-hosted-triplet/cjs",
"all": "run-p --continue-on-error --print-label --silent --aggregate-output run:**"
},
"repository": {
"type": "git",
Expand Down
9 changes: 9 additions & 0 deletions self-hosted-namespace/cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const helloWorld = require("../");
// Non-supported
// const { helloWorld: namedHelloWorld } = require("../");
// const { default: defaultHelloWorld } = require("../");

if (helloWorld() !== 'Hello World') throw new Error('Oh no!');
// Non-supported
// if (namedHelloWorld() !== 'Hello World') throw new Error('Oh no!');
// if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!');
32 changes: 5 additions & 27 deletions esm/index.mjs → self-hosted-namespace/esm/index.mjs
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import Fastify from "fastify";
import helloWorld from "../index.js";

// we currently dont' support named exports in ESM context
// import { fastify } from "fastify";
// ^^^^^^^
// SyntaxError: The requested module 'fastify' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.

const fastify1 = Fastify();

fastify1
.listen(3010)
.then(() => {
console.log(`[ESM] namespace import is running.`);
})
.catch((err) => {
console.log(err);
process.exit(1);
});
if (helloWorld() !== 'Hello World') throw new Error('Oh no!');

// dynamic namespace import is not callable because because this is its shape:
// [Module] {
Expand All @@ -25,21 +15,9 @@ fastify1
// }
// }
// however we can still use its default export
import("fastify")
.then(({ default: defaultFastify }) => {
const fastify5 = defaultFastify();

fastify5
.listen(3011)
.then(() => {
console.log(
`[ESM] \`import("fastify");\` dynamic namespace import is running (using \`.default\` prop).`
);
})
.catch((err) => {
console.log(err);
process.exit(1);
});
import("../index.js")
.then(({ default: defaultHelloWorld }) => {
if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!');
})
.catch((err) => {
console.log(err);
Expand Down
7 changes: 7 additions & 0 deletions self-hosted-namespace/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export = helloWorld;

declare namespace helloWorld {
type HelloWorldResponse = 'Hello World';
}

declare function helloWorld(): helloWorld.HelloWorldResponse;
16 changes: 16 additions & 0 deletions self-hosted-namespace/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const helloWorld = () => 'Hello World';

/**
* These export configurations enable JS and TS developers
* to consumer fastify in whatever way best suits their needs.
* Some examples of supported import syntax includes:
* - `const helloWorld = require('./triplet')`
* - `const { helloWorld } = require('./triplet')`
* - `import * as HelloWorld from './triplet'`
* - `import { helloWorld, TSC_definition } from './triplet'`
* - `import helloWorld from './triplet'`
* - `import helloWorld, { TSC_definition } from './triplet'`
*/
module.exports = helloWorld
module.exports.helloWorld = helloWorld
module.exports.default = helloWorld
3 changes: 1 addition & 2 deletions tsconfig.json → self-hosted-namespace/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"esnext"
],
"typeRoots": [
"./node_modules/@types",
"./typescript/typings"
"../node_modules/@types",
]
},
"exclude": [
Expand Down
12 changes: 12 additions & 0 deletions self-hosted-namespace/typed-cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const helloWorld = require("../");
// Non-supported
// const { helloWorld: namedHelloWorld } = require("../");
// const { default: defaultHelloWorld } = require("../");

/** @type {import('../').HelloWorldResponse} */
const desiredResult = 'Hello World';

if (helloWorld() !== desiredResult) throw new Error('Oh no!');
// Non-supported
// if (namedHelloWorld() !== 'Hello World') throw new Error('Oh no!');
// if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!');
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"noEmit": true,
},
"include": [
"./index.js"
"index.js",
]
}
1 change: 1 addition & 0 deletions self-hosted-namespace/typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
index.js
34 changes: 34 additions & 0 deletions self-hosted-namespace/typescript/es-module-interop-false/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Not supported
// import helloWorld from "../../";
// import { helloWorld as namedHelloWorld } from "../../";
// import * as HelloWorldNamespace from "../../";
import helloWorldRequire = require("../../")

// Not supported
// if (helloWorld() !== 'Hello World') throw new Error('Oh no!');
// if (namedHelloWorld() !== 'Hello World') throw new Error('Oh no!');
// if (HelloWorldNamespace.default() !== 'Hello World') throw new Error('Oh no!');
if (helloWorldRequire() !== 'Hello World') throw new Error('Oh no!');

// Not supported
// import("../../")
// .then(({
// default: defaultHelloWorld,
// helloWorld
// }) => {
// if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!');
// if (helloWorld() !== 'Hello World') throw new Error('Oh no!');
// })
// .catch((err) => {
// console.log(err);
// process.exit(1);
// });

import("../../")
.then((helloWorld) => {
if (helloWorld() !== 'Hello World') throw new Error('Oh no!');
})
.catch((err) => {
console.log(err);
process.exit(1);
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./build",
"outDir": "./",
"esModuleInterop": false
},
"include": [
Expand Down
35 changes: 35 additions & 0 deletions self-hosted-namespace/typescript/es-module-interop-true/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import helloWorld from "../../";
// Not supported
// import { helloWorld as namedHelloWorld } from "../../";
// import * as HelloWorldNamespace from "../../";
import helloWorldRequire = require("../../")

if (helloWorld() !== 'Hello World') throw new Error('Oh no!');
// Not supported
// if (namedHelloWorld() !== 'Hello World') throw new Error('Oh no!');
// if (HelloWorldNamespace.default() !== 'Hello World') throw new Error('Oh no!');
if (helloWorldRequire() !== 'Hello World') throw new Error('Oh no!');

import("../../")
.then(({
default: defaultHelloWorld,
// Not supported
// helloWorld
}) => {
if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!');
if (helloWorld() !== 'Hello World') throw new Error('Oh no!');
})
.catch((err) => {
console.log(err);
process.exit(1);
});

// Not supported
// import("../../")
// .then((helloWorld) => {
// if (helloWorld() !== 'Hello World') throw new Error('Oh no!');
// })
// .catch((err) => {
// console.log(err);
// process.exit(1);
// });
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./build",
"outDir": "./",
"esModuleInterop": true
},
"include": [
Expand Down
7 changes: 7 additions & 0 deletions self-hosted-triplet/cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const helloWorld = require("../");
const { helloWorld: namedHelloWorld } = require("../");
const { default: defaultHelloWorld } = require("../");

if (helloWorld() !== 'Hello World') throw new Error('Oh no!');
if (namedHelloWorld() !== 'Hello World') throw new Error('Oh no!');
if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!');
25 changes: 25 additions & 0 deletions self-hosted-triplet/esm/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import helloWorld from "../index.js";

// we currently dont' support named exports in ESM context
// import { fastify } from "fastify";
// ^^^^^^^
// SyntaxError: The requested module 'fastify' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.

if (helloWorld() !== 'Hello World') throw new Error('Oh no!');

// dynamic namespace import is not callable because because this is its shape:
// [Module] {
// default: <ref *1> [Function: fastify] {
// fastify: [Circular *1],
// default: [Circular *1]
// }
// }
// however we can still use its default export
import("../index.js")
.then(({ default: defaultHelloWorld }) => {
if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!');
})
.catch((err) => {
console.log(err);
process.exit(1);
});
6 changes: 6 additions & 0 deletions self-hosted-triplet/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type HelloWorldResponse = 'Hello World';

declare function helloWorld(): HelloWorldResponse;

export default helloWorld;
export { helloWorld }
16 changes: 16 additions & 0 deletions self-hosted-triplet/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const helloWorld = () => 'Hello World';

/**
* These export configurations enable JS and TS developers
* to consumer fastify in whatever way best suits their needs.
* Some examples of supported import syntax includes:
* - `const helloWorld = require('./triplet')`
* - `const { helloWorld } = require('./triplet')`
* - `import * as HelloWorld from './triplet'`
* - `import { helloWorld, TSC_definition } from './triplet'`
* - `import helloWorld from './triplet'`
* - `import helloWorld, { TSC_definition } from './triplet'`
*/
module.exports = helloWorld
module.exports.helloWorld = helloWorld
module.exports.default = helloWorld
19 changes: 19 additions & 0 deletions self-hosted-triplet/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"strict": true,
"skipLibCheck": false,
"pretty": true,
"lib": [
"esnext"
],
"typeRoots": [
"../node_modules/@types",
]
},
"exclude": [
"node_modules"
]
}
9 changes: 9 additions & 0 deletions self-hosted-triplet/typed-cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Non-supported
// const helloWorld = require("../");
const { helloWorld: namedHelloWorld } = require("../");
const { default: defaultHelloWorld } = require("../");

// Non-supported
// if (helloWorld() !== 'Hello World') throw new Error('Oh no!');
if (namedHelloWorld() !== 'Hello World') throw new Error('Oh no!');
if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!');
11 changes: 11 additions & 0 deletions self-hosted-triplet/typed-cjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
},
"include": [
"index.js",
]
}
1 change: 1 addition & 0 deletions self-hosted-triplet/typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
index.js
Loading

0 comments on commit c4ff2f0

Please sign in to comment.