From 800268435a8465660f8569f7ba30a94c2b1f75a1 Mon Sep 17 00:00:00 2001 From: Fathy Boundjadj Date: Tue, 20 Mar 2018 03:14:40 +0100 Subject: [PATCH] Set TypeScript option esModuleInterop to true by default (#971) --- package.json | 2 +- src/assets/TypeScriptAsset.js | 6 +++++- .../typescript-interop/commonjs-module.js | 1 + test/integration/typescript-interop/index.ts | 3 +++ test/typescript.js | 20 +++++++++++++++++++ yarn.lock | 2 +- 6 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/integration/typescript-interop/commonjs-module.js create mode 100644 test/integration/typescript-interop/index.ts diff --git a/package.json b/package.json index d79747721be..6c96f7c7c5c 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,7 @@ "sinon": "^4.2.2", "sourcemap-validator": "^1.0.6", "stylus": "^0.54.5", - "typescript": "^2.6.2" + "typescript": "^2.7.0" }, "scripts": { "test": "cross-env NODE_ENV=test mocha", diff --git a/src/assets/TypeScriptAsset.js b/src/assets/TypeScriptAsset.js index e4b3b2a740f..ef41d84d522 100644 --- a/src/assets/TypeScriptAsset.js +++ b/src/assets/TypeScriptAsset.js @@ -8,7 +8,11 @@ class TypeScriptAsset extends JSAsset { let transpilerOptions = { compilerOptions: { module: typescript.ModuleKind.CommonJS, - jsx: typescript.JsxEmit.Preserve + jsx: typescript.JsxEmit.Preserve, + + // it brings the generated output from TypeScript closer to that generated by Babel + // see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html + esModuleInterop: true }, fileName: this.relativeName }; diff --git a/test/integration/typescript-interop/commonjs-module.js b/test/integration/typescript-interop/commonjs-module.js new file mode 100644 index 00000000000..21503057ff6 --- /dev/null +++ b/test/integration/typescript-interop/commonjs-module.js @@ -0,0 +1 @@ +module.exports = () => 'test passed' diff --git a/test/integration/typescript-interop/index.ts b/test/integration/typescript-interop/index.ts new file mode 100644 index 00000000000..91be0f05072 --- /dev/null +++ b/test/integration/typescript-interop/index.ts @@ -0,0 +1,3 @@ +import testModule from './commonjs-module' + +export const test = testModule diff --git a/test/typescript.js b/test/typescript.js index a0cb3e63695..56463519fd9 100644 --- a/test/typescript.js +++ b/test/typescript.js @@ -106,4 +106,24 @@ describe('typescript', function() { let file = fs.readFileSync(__dirname + '/dist/index.js', 'utf8'); assert(file.includes('React.createElement("div"')); }); + + it('should use esModuleInterop by default', async function() { + let b = await bundle( + __dirname + '/integration/typescript-interop/index.ts' + ); + + assertBundleTree(b, { + name: 'index.js', + assets: ['index.ts', 'commonjs-module.js'], + childBundles: [ + { + type: 'map' + } + ] + }); + + let output = run(b); + assert.equal(typeof output.test, 'function'); + assert.equal(output.test(), 'test passed'); + }); }); diff --git a/yarn.lock b/yarn.lock index 8f6ce18f06d..6257320bda4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6087,7 +6087,7 @@ typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" -typescript@^2.6.2: +typescript@^2.7.0: version "2.7.2" resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.7.2.tgz#2d615a1ef4aee4f574425cdff7026edf81919836"