From 1f347fc537d3de353b3bd01b32ed3c1e9f423dee Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Fri, 16 Oct 2020 18:09:35 +0200 Subject: [PATCH 1/4] Add a self-hosted example --- .gitignore | 2 +- cjs/index.js | 39 ------- package.json | 17 +-- self-hosted-namespace/cjs/index.js | 7 ++ {esm => self-hosted-namespace/esm}/index.mjs | 32 +----- self-hosted-namespace/index.d.ts | 9 ++ self-hosted-namespace/index.js | 16 +++ .../tsconfig.json | 0 self-hosted-namespace/typed-cjs/index.js | 7 ++ .../typed-cjs}/tsconfig.json | 2 +- self-hosted-namespace/typescript/.gitignore | 1 + .../es-module-interop-false/index.ts | 31 ++++++ .../es-module-interop-false/tsconfig.json | 0 .../es-module-interop-true/index.ts | 31 ++++++ .../es-module-interop-true/tsconfig.json | 0 self-hosted-triplet/cjs/index.js | 7 ++ self-hosted-triplet/esm/index.mjs | 25 +++++ self-hosted-triplet/index.d.ts | 6 ++ self-hosted-triplet/index.js | 16 +++ self-hosted-triplet/tsconfig.json | 20 ++++ self-hosted-triplet/typed-cjs/index.js | 7 ++ self-hosted-triplet/typed-cjs/tsconfig.json | 11 ++ self-hosted-triplet/typescript/.gitignore | 1 + .../es-module-interop-false/index.ts | 31 ++++++ .../es-module-interop-false/tsconfig.json | 10 ++ .../es-module-interop-true/index.ts | 31 ++++++ .../es-module-interop-true/tsconfig.json | 10 ++ typed-cjs/index.js | 39 ------- .../es-module-interop-false/build/index.js | 78 -------------- typescript/es-module-interop-false/index.ts | 102 ------------------ .../es-module-interop-true/build/index.js | 100 ----------------- typescript/es-module-interop-true/index.ts | 102 ------------------ 32 files changed, 295 insertions(+), 495 deletions(-) delete mode 100644 cjs/index.js create mode 100644 self-hosted-namespace/cjs/index.js rename {esm => self-hosted-namespace/esm}/index.mjs (50%) create mode 100644 self-hosted-namespace/index.d.ts create mode 100644 self-hosted-namespace/index.js rename tsconfig.json => self-hosted-namespace/tsconfig.json (100%) create mode 100644 self-hosted-namespace/typed-cjs/index.js rename {typed-cjs => self-hosted-namespace/typed-cjs}/tsconfig.json (89%) create mode 100644 self-hosted-namespace/typescript/.gitignore create mode 100644 self-hosted-namespace/typescript/es-module-interop-false/index.ts rename {typescript => self-hosted-namespace/typescript}/es-module-interop-false/tsconfig.json (100%) create mode 100644 self-hosted-namespace/typescript/es-module-interop-true/index.ts rename {typescript => self-hosted-namespace/typescript}/es-module-interop-true/tsconfig.json (100%) create mode 100644 self-hosted-triplet/cjs/index.js create mode 100644 self-hosted-triplet/esm/index.mjs create mode 100644 self-hosted-triplet/index.d.ts create mode 100644 self-hosted-triplet/index.js create mode 100644 self-hosted-triplet/tsconfig.json create mode 100644 self-hosted-triplet/typed-cjs/index.js create mode 100644 self-hosted-triplet/typed-cjs/tsconfig.json create mode 100644 self-hosted-triplet/typescript/.gitignore create mode 100644 self-hosted-triplet/typescript/es-module-interop-false/index.ts create mode 100644 self-hosted-triplet/typescript/es-module-interop-false/tsconfig.json create mode 100644 self-hosted-triplet/typescript/es-module-interop-true/index.ts create mode 100644 self-hosted-triplet/typescript/es-module-interop-true/tsconfig.json delete mode 100644 typed-cjs/index.js delete mode 100644 typescript/es-module-interop-false/build/index.js delete mode 100644 typescript/es-module-interop-false/index.ts delete mode 100644 typescript/es-module-interop-true/build/index.js delete mode 100644 typescript/es-module-interop-true/index.ts diff --git a/.gitignore b/.gitignore index 53c804d..5d81abd 100644 --- a/.gitignore +++ b/.gitignore @@ -102,4 +102,4 @@ dist # TernJS port file .tern-port -package-lock.json \ No newline at end of file +package-lock.json diff --git a/cjs/index.js b/cjs/index.js deleted file mode 100644 index 29130b9..0000000 --- a/cjs/index.js +++ /dev/null @@ -1,39 +0,0 @@ -const Fastify = require("fastify"); -const { fastify } = require("fastify"); -const { default: fastifyDefault } = require("fastify"); - -const fastify1 = Fastify(); - -fastify1 - .listen(3000) - .then(() => { - console.log(`[CJS] module.exports is running.`); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -const fastify2 = fastify(); - -fastify2 - .listen(3001) - .then(() => { - console.log(`[CJS] named require is running.`); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -const fastify3 = fastifyDefault(); - -fastify2 - .listen(3002) - .then(() => { - console.log(`[CJS] default property require is running.`); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); diff --git a/package.json b/package.json index ba4bf2a..dc0d693 100644 --- a/package.json +++ b/package.json @@ -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/build", + "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/build", + "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/build", + "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/build", + "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", diff --git a/self-hosted-namespace/cjs/index.js b/self-hosted-namespace/cjs/index.js new file mode 100644 index 0000000..2a18c4c --- /dev/null +++ b/self-hosted-namespace/cjs/index.js @@ -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!'); diff --git a/esm/index.mjs b/self-hosted-namespace/esm/index.mjs similarity index 50% rename from esm/index.mjs rename to self-hosted-namespace/esm/index.mjs index 0b1ff53..7af7459 100644 --- a/esm/index.mjs +++ b/self-hosted-namespace/esm/index.mjs @@ -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] { @@ -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); diff --git a/self-hosted-namespace/index.d.ts b/self-hosted-namespace/index.d.ts new file mode 100644 index 0000000..36c2189 --- /dev/null +++ b/self-hosted-namespace/index.d.ts @@ -0,0 +1,9 @@ +export = helloWorld; + +declare namespace helloWorld2 { + type HelloWorldResponse = 'Hello World'; +} + +declare function helloWorld(): helloWorld2.HelloWorldResponse; +declare type helloWorld3 = { helloWorld: true }; + diff --git a/self-hosted-namespace/index.js b/self-hosted-namespace/index.js new file mode 100644 index 0000000..9e08c1e --- /dev/null +++ b/self-hosted-namespace/index.js @@ -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 \ No newline at end of file diff --git a/tsconfig.json b/self-hosted-namespace/tsconfig.json similarity index 100% rename from tsconfig.json rename to self-hosted-namespace/tsconfig.json diff --git a/self-hosted-namespace/typed-cjs/index.js b/self-hosted-namespace/typed-cjs/index.js new file mode 100644 index 0000000..2a18c4c --- /dev/null +++ b/self-hosted-namespace/typed-cjs/index.js @@ -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!'); diff --git a/typed-cjs/tsconfig.json b/self-hosted-namespace/typed-cjs/tsconfig.json similarity index 89% rename from typed-cjs/tsconfig.json rename to self-hosted-namespace/typed-cjs/tsconfig.json index d23292b..de98bd8 100644 --- a/typed-cjs/tsconfig.json +++ b/self-hosted-namespace/typed-cjs/tsconfig.json @@ -6,6 +6,6 @@ "noEmit": true, }, "include": [ - "./index.js" + "index.js", ] } \ No newline at end of file diff --git a/self-hosted-namespace/typescript/.gitignore b/self-hosted-namespace/typescript/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/self-hosted-namespace/typescript/.gitignore @@ -0,0 +1 @@ +build diff --git a/self-hosted-namespace/typescript/es-module-interop-false/index.ts b/self-hosted-namespace/typescript/es-module-interop-false/index.ts new file mode 100644 index 0000000..b1dc1f2 --- /dev/null +++ b/self-hosted-namespace/typescript/es-module-interop-false/index.ts @@ -0,0 +1,31 @@ +import helloWorld from "../../"; +import { helloWorld as namedHelloWorld } from "../../"; +import * as HelloWorldNamespace from "../../"; +import helloWorldRequire = require("../../") + +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!'); + +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); + }); \ No newline at end of file diff --git a/typescript/es-module-interop-false/tsconfig.json b/self-hosted-namespace/typescript/es-module-interop-false/tsconfig.json similarity index 100% rename from typescript/es-module-interop-false/tsconfig.json rename to self-hosted-namespace/typescript/es-module-interop-false/tsconfig.json diff --git a/self-hosted-namespace/typescript/es-module-interop-true/index.ts b/self-hosted-namespace/typescript/es-module-interop-true/index.ts new file mode 100644 index 0000000..b1dc1f2 --- /dev/null +++ b/self-hosted-namespace/typescript/es-module-interop-true/index.ts @@ -0,0 +1,31 @@ +import helloWorld from "../../"; +import { helloWorld as namedHelloWorld } from "../../"; +import * as HelloWorldNamespace from "../../"; +import helloWorldRequire = require("../../") + +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!'); + +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); + }); \ No newline at end of file diff --git a/typescript/es-module-interop-true/tsconfig.json b/self-hosted-namespace/typescript/es-module-interop-true/tsconfig.json similarity index 100% rename from typescript/es-module-interop-true/tsconfig.json rename to self-hosted-namespace/typescript/es-module-interop-true/tsconfig.json diff --git a/self-hosted-triplet/cjs/index.js b/self-hosted-triplet/cjs/index.js new file mode 100644 index 0000000..2a18c4c --- /dev/null +++ b/self-hosted-triplet/cjs/index.js @@ -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!'); diff --git a/self-hosted-triplet/esm/index.mjs b/self-hosted-triplet/esm/index.mjs new file mode 100644 index 0000000..7af7459 --- /dev/null +++ b/self-hosted-triplet/esm/index.mjs @@ -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: [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); + }); diff --git a/self-hosted-triplet/index.d.ts b/self-hosted-triplet/index.d.ts new file mode 100644 index 0000000..f7e6ecf --- /dev/null +++ b/self-hosted-triplet/index.d.ts @@ -0,0 +1,6 @@ +export type HelloWorldResponse = 'Hello World'; + +declare function helloWorld(): HelloWorldResponse; + +export default helloWorld; +export { helloWorld } \ No newline at end of file diff --git a/self-hosted-triplet/index.js b/self-hosted-triplet/index.js new file mode 100644 index 0000000..9e08c1e --- /dev/null +++ b/self-hosted-triplet/index.js @@ -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 \ No newline at end of file diff --git a/self-hosted-triplet/tsconfig.json b/self-hosted-triplet/tsconfig.json new file mode 100644 index 0000000..94664a4 --- /dev/null +++ b/self-hosted-triplet/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "target": "es2017", + "module": "commonjs", + "moduleResolution": "node", + "strict": true, + "skipLibCheck": false, + "pretty": true, + "lib": [ + "esnext" + ], + "typeRoots": [ + "./node_modules/@types", + "./typescript/typings" + ] + }, + "exclude": [ + "node_modules" + ] +} \ No newline at end of file diff --git a/self-hosted-triplet/typed-cjs/index.js b/self-hosted-triplet/typed-cjs/index.js new file mode 100644 index 0000000..2a18c4c --- /dev/null +++ b/self-hosted-triplet/typed-cjs/index.js @@ -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!'); diff --git a/self-hosted-triplet/typed-cjs/tsconfig.json b/self-hosted-triplet/typed-cjs/tsconfig.json new file mode 100644 index 0000000..de98bd8 --- /dev/null +++ b/self-hosted-triplet/typed-cjs/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "allowJs": true, + "checkJs": true, + "noEmit": true, + }, + "include": [ + "index.js", + ] +} \ No newline at end of file diff --git a/self-hosted-triplet/typescript/.gitignore b/self-hosted-triplet/typescript/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/self-hosted-triplet/typescript/.gitignore @@ -0,0 +1 @@ +build diff --git a/self-hosted-triplet/typescript/es-module-interop-false/index.ts b/self-hosted-triplet/typescript/es-module-interop-false/index.ts new file mode 100644 index 0000000..b1dc1f2 --- /dev/null +++ b/self-hosted-triplet/typescript/es-module-interop-false/index.ts @@ -0,0 +1,31 @@ +import helloWorld from "../../"; +import { helloWorld as namedHelloWorld } from "../../"; +import * as HelloWorldNamespace from "../../"; +import helloWorldRequire = require("../../") + +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!'); + +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); + }); \ No newline at end of file diff --git a/self-hosted-triplet/typescript/es-module-interop-false/tsconfig.json b/self-hosted-triplet/typescript/es-module-interop-false/tsconfig.json new file mode 100644 index 0000000..310328e --- /dev/null +++ b/self-hosted-triplet/typescript/es-module-interop-false/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "./build", + "esModuleInterop": false + }, + "include": [ + "./index.ts" + ] +} \ No newline at end of file diff --git a/self-hosted-triplet/typescript/es-module-interop-true/index.ts b/self-hosted-triplet/typescript/es-module-interop-true/index.ts new file mode 100644 index 0000000..b1dc1f2 --- /dev/null +++ b/self-hosted-triplet/typescript/es-module-interop-true/index.ts @@ -0,0 +1,31 @@ +import helloWorld from "../../"; +import { helloWorld as namedHelloWorld } from "../../"; +import * as HelloWorldNamespace from "../../"; +import helloWorldRequire = require("../../") + +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!'); + +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); + }); \ No newline at end of file diff --git a/self-hosted-triplet/typescript/es-module-interop-true/tsconfig.json b/self-hosted-triplet/typescript/es-module-interop-true/tsconfig.json new file mode 100644 index 0000000..4e5ed44 --- /dev/null +++ b/self-hosted-triplet/typescript/es-module-interop-true/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "./build", + "esModuleInterop": true + }, + "include": [ + "./index.ts" + ] +} \ No newline at end of file diff --git a/typed-cjs/index.js b/typed-cjs/index.js deleted file mode 100644 index 29130b9..0000000 --- a/typed-cjs/index.js +++ /dev/null @@ -1,39 +0,0 @@ -const Fastify = require("fastify"); -const { fastify } = require("fastify"); -const { default: fastifyDefault } = require("fastify"); - -const fastify1 = Fastify(); - -fastify1 - .listen(3000) - .then(() => { - console.log(`[CJS] module.exports is running.`); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -const fastify2 = fastify(); - -fastify2 - .listen(3001) - .then(() => { - console.log(`[CJS] named require is running.`); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -const fastify3 = fastifyDefault(); - -fastify2 - .listen(3002) - .then(() => { - console.log(`[CJS] default property require is running.`); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); diff --git a/typescript/es-module-interop-false/build/index.js b/typescript/es-module-interop-false/build/index.js deleted file mode 100644 index c4afb0f..0000000 --- a/typescript/es-module-interop-false/build/index.js +++ /dev/null @@ -1,78 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const fastify_1 = require("fastify"); -const fastify_2 = require("fastify"); -const FastifyNamespace = require("fastify"); -const tsconfig = require("../tsconfig.json"); -const esModuleInterop = tsconfig.compilerOptions.esModuleInterop; -const fastify1 = fastify_1.default(); -fastify1 - .listen(3020) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import Fastify from 'fastify';\` import is running. (namespace \`.default\` prop)`); -}) - .catch((err) => { - console.log(err); - process.exit(1); -}); -const fastify2 = fastify_2.fastify(); -fastify2 - .listen(3021) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import { fastify } from "fastify";\` is running. (fastify named import)`); -}) - .catch((err) => { - console.log(err); - process.exit(1); -}); -// since we used star import FastifyNamespace is never callable because of ESM spec -// however we can still use its "named" exports -const fastify3 = FastifyNamespace.default(); -fastify3 - .listen(3022) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import * as FastifyNamespace from "fastify";\` star import namespace is running (using \`.default\` prop).`); -}) - .catch((err) => { - console.log(err); - process.exit(1); -}); -// this case is the same as `import { fastify } from "fastify";` but I want to make this clear -const fastify4 = FastifyNamespace.fastify(); -fastify4 - .listen(3023) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import * as FastifyNamespace from "fastify";\` star import namespace named import is running (using \`.fastify\` prop).`); -}) - .catch((err) => { - console.log(err); - process.exit(1); -}); -// dynamic namespace import is not callable: that is, it is the same as star import -// however we can still use its "named" exports -Promise.resolve().then(() => require("fastify")).then(({ default: defaultFastify, fastify }) => { - const fastify5 = defaultFastify(); - const fastify6 = fastify(); - fastify5 - .listen(3024) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import("fastify");\` dynamic namespace import is running (using \`.default\` prop).`); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - fastify6 - .listen(3025) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import("fastify");\` dynamic namespace import is running (using \`.fastify\` prop).`); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); -}) - .catch((err) => { - console.log(err); - process.exit(1); -}); diff --git a/typescript/es-module-interop-false/index.ts b/typescript/es-module-interop-false/index.ts deleted file mode 100644 index ff4dcd5..0000000 --- a/typescript/es-module-interop-false/index.ts +++ /dev/null @@ -1,102 +0,0 @@ -import Fastify from "fastify"; -import { fastify } from "fastify"; -import * as FastifyNamespace from "fastify"; - -const tsconfig = require("../tsconfig.json"); - -const esModuleInterop = tsconfig.compilerOptions.esModuleInterop; - -const fastify1 = Fastify(); - -fastify1 - .listen(3020) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import Fastify from 'fastify';\` import is running. (namespace \`.default\` prop)` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -const fastify2 = fastify(); - -fastify2 - .listen(3021) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import { fastify } from "fastify";\` is running. (fastify named import)` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -// since we used star import FastifyNamespace is never callable because of ESM spec -// however we can still use its "named" exports -const fastify3 = FastifyNamespace.default(); - -fastify3 - .listen(3022) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import * as FastifyNamespace from "fastify";\` star import namespace is running (using \`.default\` prop).` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -// this case is the same as `import { fastify } from "fastify";` but I want to make this clear -const fastify4 = FastifyNamespace.fastify(); - -fastify4 - .listen(3023) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import * as FastifyNamespace from "fastify";\` star import namespace named import is running (using \`.fastify\` prop).` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -// dynamic namespace import is not callable: that is, it is the same as star import -// however we can still use its "named" exports -import("fastify") - .then(({ default: defaultFastify, fastify }) => { - const fastify5 = defaultFastify(); - const fastify6 = fastify(); - - fastify5 - .listen(3024) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import("fastify");\` dynamic namespace import is running (using \`.default\` prop).` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - - fastify6 - .listen(3025) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import("fastify");\` dynamic namespace import is running (using \`.fastify\` prop).` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); diff --git a/typescript/es-module-interop-true/build/index.js b/typescript/es-module-interop-true/build/index.js deleted file mode 100644 index d275298..0000000 --- a/typescript/es-module-interop-true/build/index.js +++ /dev/null @@ -1,100 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const fastify_1 = __importDefault(require("fastify")); -const fastify_2 = require("fastify"); -const FastifyNamespace = __importStar(require("fastify")); -const tsconfig = require("../tsconfig.json"); -const esModuleInterop = tsconfig.compilerOptions.esModuleInterop; -const fastify1 = fastify_1.default(); -fastify1 - .listen(3030) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import Fastify from 'fastify';\` import is running. (namespace \`.default\` prop)`); -}) - .catch((err) => { - console.log(err); - process.exit(1); -}); -const fastify2 = fastify_2.fastify(); -fastify2 - .listen(3031) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import { fastify } from "fastify";\` is running. (fastify named import)`); -}) - .catch((err) => { - console.log(err); - process.exit(1); -}); -// since we used star import FastifyNamespace is never callable because of ESM spec -// however we can still use its "named" exports -const fastify3 = FastifyNamespace.default(); -fastify3 - .listen(3032) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import * as FastifyNamespace from "fastify";\` star import namespace is running (using \`.default\` prop).`); -}) - .catch((err) => { - console.log(err); - process.exit(1); -}); -// this case is the same as `import { fastify } from "fastify";` but I want to make this clear -const fastify4 = FastifyNamespace.fastify(); -fastify4 - .listen(3033) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import * as FastifyNamespace from "fastify";\` star import namespace named import is running (using \`.fastify\` prop).`); -}) - .catch((err) => { - console.log(err); - process.exit(1); -}); -// dynamic namespace import is not callable: that is, it is the same as star import -// however we can still use its "named" exports -Promise.resolve().then(() => __importStar(require("fastify"))).then(({ default: defaultFastify, fastify }) => { - const fastify5 = defaultFastify(); - const fastify6 = fastify(); - fastify5 - .listen(3034) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import("fastify");\` dynamic namespace import is running (using \`.default\` prop).`); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - fastify6 - .listen(3035) - .then(() => { - console.log(`[TS - esModuleInterop: ${esModuleInterop}] \`import("fastify");\` dynamic namespace import is running (using \`.fastify\` prop).`); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); -}) - .catch((err) => { - console.log(err); - process.exit(1); -}); diff --git a/typescript/es-module-interop-true/index.ts b/typescript/es-module-interop-true/index.ts deleted file mode 100644 index e22e3f9..0000000 --- a/typescript/es-module-interop-true/index.ts +++ /dev/null @@ -1,102 +0,0 @@ -import Fastify from "fastify"; -import { fastify } from "fastify"; -import * as FastifyNamespace from "fastify"; - -const tsconfig = require("../tsconfig.json"); - -const esModuleInterop = tsconfig.compilerOptions.esModuleInterop; - -const fastify1 = Fastify(); - -fastify1 - .listen(3030) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import Fastify from 'fastify';\` import is running. (namespace \`.default\` prop)` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -const fastify2 = fastify(); - -fastify2 - .listen(3031) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import { fastify } from "fastify";\` is running. (fastify named import)` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -// since we used star import FastifyNamespace is never callable because of ESM spec -// however we can still use its "named" exports -const fastify3 = FastifyNamespace.default(); - -fastify3 - .listen(3032) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import * as FastifyNamespace from "fastify";\` star import namespace is running (using \`.default\` prop).` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -// this case is the same as `import { fastify } from "fastify";` but I want to make this clear -const fastify4 = FastifyNamespace.fastify(); - -fastify4 - .listen(3033) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import * as FastifyNamespace from "fastify";\` star import namespace named import is running (using \`.fastify\` prop).` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - -// dynamic namespace import is not callable: that is, it is the same as star import -// however we can still use its "named" exports -import("fastify") - .then(({ default: defaultFastify, fastify }) => { - const fastify5 = defaultFastify(); - const fastify6 = fastify(); - - fastify5 - .listen(3034) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import("fastify");\` dynamic namespace import is running (using \`.default\` prop).` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - - fastify6 - .listen(3035) - .then(() => { - console.log( - `[TS - esModuleInterop: ${esModuleInterop}] \`import("fastify");\` dynamic namespace import is running (using \`.fastify\` prop).` - ); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); From e5c5346aa456351178fea93cd41fbcd809694051 Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Fri, 16 Oct 2020 18:35:47 +0200 Subject: [PATCH 2/4] Highlight all of the non-supported variants --- package.json | 8 ++-- self-hosted-namespace/cjs/index.js | 10 +++-- self-hosted-namespace/tsconfig.json | 3 +- self-hosted-namespace/typed-cjs/index.js | 10 +++-- self-hosted-namespace/typescript/.gitignore | 2 +- .../es-module-interop-false/index.ts | 39 ++++++++++--------- .../es-module-interop-false/tsconfig.json | 2 +- .../es-module-interop-true/index.ts | 30 +++++++------- .../es-module-interop-true/tsconfig.json | 2 +- self-hosted-triplet/tsconfig.json | 3 +- self-hosted-triplet/typed-cjs/index.js | 6 ++- self-hosted-triplet/typescript/.gitignore | 2 +- .../es-module-interop-false/index.ts | 25 +++++++----- .../es-module-interop-false/tsconfig.json | 2 +- .../es-module-interop-true/index.ts | 26 +++++++------ .../es-module-interop-true/tsconfig.json | 2 +- 16 files changed, 96 insertions(+), 76 deletions(-) diff --git a/package.json b/package.json index dc0d693..e581c63 100644 --- a/package.json +++ b/package.json @@ -6,13 +6,13 @@ "scripts": { "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/build", - "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/build", + "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/build", - "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/build", + "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:**" }, diff --git a/self-hosted-namespace/cjs/index.js b/self-hosted-namespace/cjs/index.js index 2a18c4c..66c3082 100644 --- a/self-hosted-namespace/cjs/index.js +++ b/self-hosted-namespace/cjs/index.js @@ -1,7 +1,9 @@ const helloWorld = require("../"); -const { helloWorld: namedHelloWorld } = require("../"); -const { default: defaultHelloWorld } = require("../"); +// Non-supported +// 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!'); +// Non-supported +// if (namedHelloWorld() !== 'Hello World') throw new Error('Oh no!'); +// if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!'); diff --git a/self-hosted-namespace/tsconfig.json b/self-hosted-namespace/tsconfig.json index 94664a4..401bc44 100644 --- a/self-hosted-namespace/tsconfig.json +++ b/self-hosted-namespace/tsconfig.json @@ -10,8 +10,7 @@ "esnext" ], "typeRoots": [ - "./node_modules/@types", - "./typescript/typings" + "../node_modules/@types", ] }, "exclude": [ diff --git a/self-hosted-namespace/typed-cjs/index.js b/self-hosted-namespace/typed-cjs/index.js index 2a18c4c..66c3082 100644 --- a/self-hosted-namespace/typed-cjs/index.js +++ b/self-hosted-namespace/typed-cjs/index.js @@ -1,7 +1,9 @@ const helloWorld = require("../"); -const { helloWorld: namedHelloWorld } = require("../"); -const { default: defaultHelloWorld } = require("../"); +// Non-supported +// 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!'); +// Non-supported +// if (namedHelloWorld() !== 'Hello World') throw new Error('Oh no!'); +// if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!'); diff --git a/self-hosted-namespace/typescript/.gitignore b/self-hosted-namespace/typescript/.gitignore index 378eac2..012a3cd 100644 --- a/self-hosted-namespace/typescript/.gitignore +++ b/self-hosted-namespace/typescript/.gitignore @@ -1 +1 @@ -build +index.js diff --git a/self-hosted-namespace/typescript/es-module-interop-false/index.ts b/self-hosted-namespace/typescript/es-module-interop-false/index.ts index b1dc1f2..398bae7 100644 --- a/self-hosted-namespace/typescript/es-module-interop-false/index.ts +++ b/self-hosted-namespace/typescript/es-module-interop-false/index.ts @@ -1,25 +1,28 @@ -import helloWorld from "../../"; -import { helloWorld as namedHelloWorld } from "../../"; -import * as HelloWorldNamespace from "../../"; +// Not supported +// import helloWorld from "../../"; +// import { helloWorld as namedHelloWorld } from "../../"; +// import * as HelloWorldNamespace from "../../"; import helloWorldRequire = require("../../") -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!'); +// 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!'); -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); - }); +// 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) => { diff --git a/self-hosted-namespace/typescript/es-module-interop-false/tsconfig.json b/self-hosted-namespace/typescript/es-module-interop-false/tsconfig.json index 310328e..efd4ebf 100644 --- a/self-hosted-namespace/typescript/es-module-interop-false/tsconfig.json +++ b/self-hosted-namespace/typescript/es-module-interop-false/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "outDir": "./build", + "outDir": "./", "esModuleInterop": false }, "include": [ diff --git a/self-hosted-namespace/typescript/es-module-interop-true/index.ts b/self-hosted-namespace/typescript/es-module-interop-true/index.ts index b1dc1f2..9d25b5b 100644 --- a/self-hosted-namespace/typescript/es-module-interop-true/index.ts +++ b/self-hosted-namespace/typescript/es-module-interop-true/index.ts @@ -1,17 +1,20 @@ import helloWorld from "../../"; -import { helloWorld as namedHelloWorld } from "../../"; -import * as HelloWorldNamespace from "../../"; +// Not supported +// import { helloWorld as namedHelloWorld } from "../../"; +// import * as HelloWorldNamespace from "../../"; import helloWorldRequire = require("../../") 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!'); +// 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, - helloWorld + // Not supported + // helloWorld }) => { if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!'); if (helloWorld() !== 'Hello World') throw new Error('Oh no!'); @@ -21,11 +24,12 @@ import("../../") process.exit(1); }); -import("../../") - .then((helloWorld) => { - if (helloWorld() !== 'Hello World') throw new Error('Oh no!'); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); \ No newline at end of file +// Not supported +// import("../../") +// .then((helloWorld) => { +// if (helloWorld() !== 'Hello World') throw new Error('Oh no!'); +// }) +// .catch((err) => { +// console.log(err); +// process.exit(1); +// }); \ No newline at end of file diff --git a/self-hosted-namespace/typescript/es-module-interop-true/tsconfig.json b/self-hosted-namespace/typescript/es-module-interop-true/tsconfig.json index 4e5ed44..00e7be2 100644 --- a/self-hosted-namespace/typescript/es-module-interop-true/tsconfig.json +++ b/self-hosted-namespace/typescript/es-module-interop-true/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "outDir": "./build", + "outDir": "./", "esModuleInterop": true }, "include": [ diff --git a/self-hosted-triplet/tsconfig.json b/self-hosted-triplet/tsconfig.json index 94664a4..401bc44 100644 --- a/self-hosted-triplet/tsconfig.json +++ b/self-hosted-triplet/tsconfig.json @@ -10,8 +10,7 @@ "esnext" ], "typeRoots": [ - "./node_modules/@types", - "./typescript/typings" + "../node_modules/@types", ] }, "exclude": [ diff --git a/self-hosted-triplet/typed-cjs/index.js b/self-hosted-triplet/typed-cjs/index.js index 2a18c4c..5a1d625 100644 --- a/self-hosted-triplet/typed-cjs/index.js +++ b/self-hosted-triplet/typed-cjs/index.js @@ -1,7 +1,9 @@ -const helloWorld = require("../"); +// Non-supported +// const helloWorld = require("../"); const { helloWorld: namedHelloWorld } = require("../"); const { default: defaultHelloWorld } = require("../"); -if (helloWorld() !== 'Hello World') throw new Error('Oh no!'); +// 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!'); diff --git a/self-hosted-triplet/typescript/.gitignore b/self-hosted-triplet/typescript/.gitignore index 378eac2..012a3cd 100644 --- a/self-hosted-triplet/typescript/.gitignore +++ b/self-hosted-triplet/typescript/.gitignore @@ -1 +1 @@ -build +index.js diff --git a/self-hosted-triplet/typescript/es-module-interop-false/index.ts b/self-hosted-triplet/typescript/es-module-interop-false/index.ts index b1dc1f2..086c7f2 100644 --- a/self-hosted-triplet/typescript/es-module-interop-false/index.ts +++ b/self-hosted-triplet/typescript/es-module-interop-false/index.ts @@ -1,12 +1,16 @@ +// Not supported import helloWorld from "../../"; import { helloWorld as namedHelloWorld } from "../../"; import * as HelloWorldNamespace from "../../"; -import helloWorldRequire = require("../../") +// Not supported +// 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 +// if (helloWorldRequire() !== 'Hello World') throw new Error('Oh no!'); import("../../") .then(({ @@ -21,11 +25,12 @@ import("../../") process.exit(1); }); -import("../../") - .then((helloWorld) => { - if (helloWorld() !== 'Hello World') throw new Error('Oh no!'); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); \ No newline at end of file +// Not supported +// import("../../") +// .then((helloWorld) => { +// if (helloWorld() !== 'Hello World') throw new Error('Oh no!'); +// }) +// .catch((err) => { +// console.log(err); +// process.exit(1); +// }); \ No newline at end of file diff --git a/self-hosted-triplet/typescript/es-module-interop-false/tsconfig.json b/self-hosted-triplet/typescript/es-module-interop-false/tsconfig.json index 310328e..efd4ebf 100644 --- a/self-hosted-triplet/typescript/es-module-interop-false/tsconfig.json +++ b/self-hosted-triplet/typescript/es-module-interop-false/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "outDir": "./build", + "outDir": "./", "esModuleInterop": false }, "include": [ diff --git a/self-hosted-triplet/typescript/es-module-interop-true/index.ts b/self-hosted-triplet/typescript/es-module-interop-true/index.ts index b1dc1f2..c50c4f9 100644 --- a/self-hosted-triplet/typescript/es-module-interop-true/index.ts +++ b/self-hosted-triplet/typescript/es-module-interop-true/index.ts @@ -1,17 +1,20 @@ import helloWorld from "../../"; import { helloWorld as namedHelloWorld } from "../../"; import * as HelloWorldNamespace from "../../"; -import helloWorldRequire = require("../../") +// Not supported +// import helloWorldRequire = require("../../") 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 +// if (helloWorldRequire() !== 'Hello World') throw new Error('Oh no!'); import("../../") .then(({ default: defaultHelloWorld, - helloWorld + // Not supported + // helloWorld }) => { if (defaultHelloWorld() !== 'Hello World') throw new Error('Oh no!'); if (helloWorld() !== 'Hello World') throw new Error('Oh no!'); @@ -21,11 +24,12 @@ import("../../") process.exit(1); }); -import("../../") - .then((helloWorld) => { - if (helloWorld() !== 'Hello World') throw new Error('Oh no!'); - }) - .catch((err) => { - console.log(err); - process.exit(1); - }); \ No newline at end of file +// Not supported +// import("../../") +// .then((helloWorld) => { +// if (helloWorld() !== 'Hello World') throw new Error('Oh no!'); +// }) +// .catch((err) => { +// console.log(err); +// process.exit(1); +// }); \ No newline at end of file diff --git a/self-hosted-triplet/typescript/es-module-interop-true/tsconfig.json b/self-hosted-triplet/typescript/es-module-interop-true/tsconfig.json index 4e5ed44..00e7be2 100644 --- a/self-hosted-triplet/typescript/es-module-interop-true/tsconfig.json +++ b/self-hosted-triplet/typescript/es-module-interop-true/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "outDir": "./build", + "outDir": "./", "esModuleInterop": true }, "include": [ From fc87be0ffd1523c97647b01dfeccc057eae4ec7d Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Fri, 16 Oct 2020 18:39:42 +0200 Subject: [PATCH 3/4] Fix namespace types --- self-hosted-namespace/index.d.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/self-hosted-namespace/index.d.ts b/self-hosted-namespace/index.d.ts index 36c2189..b1f1a2e 100644 --- a/self-hosted-namespace/index.d.ts +++ b/self-hosted-namespace/index.d.ts @@ -1,9 +1,7 @@ export = helloWorld; -declare namespace helloWorld2 { +declare namespace helloWorld { type HelloWorldResponse = 'Hello World'; } -declare function helloWorld(): helloWorld2.HelloWorldResponse; -declare type helloWorld3 = { helloWorld: true }; - +declare function helloWorld(): helloWorld.HelloWorldResponse; From ff500818487b8576f5e474bd1ed9dcc67f789f90 Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Fri, 16 Oct 2020 18:40:37 +0200 Subject: [PATCH 4/4] Show that one can refer exported types --- self-hosted-namespace/typed-cjs/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/self-hosted-namespace/typed-cjs/index.js b/self-hosted-namespace/typed-cjs/index.js index 66c3082..1e418b8 100644 --- a/self-hosted-namespace/typed-cjs/index.js +++ b/self-hosted-namespace/typed-cjs/index.js @@ -3,7 +3,10 @@ const helloWorld = require("../"); // const { helloWorld: namedHelloWorld } = require("../"); // const { default: defaultHelloWorld } = require("../"); -if (helloWorld() !== 'Hello World') throw new Error('Oh no!'); +/** @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!');