Typescript: module esnext + target es3 output incompatible Object.defineProperty

Created on 19 Sep 2019  ·  3Comments  ·  Source: microsoft/TypeScript


TypeScript Version: 3.5
Webpck Version: 3.x
ts-loader Version: 3.x
Browser: IE8

Code

  • compilerOptions.module: "esnext"
  • compilerOptions.target: "es3"
  • dynamic import chunks
function load(type: string): Promise<any> {
  return import(
    `./types/${type}`
  )
}

Expected behavior:

output compatible code

Actual behavior:

webpackJsonp(["chunk-file"],[
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony default export */ __webpack_exports__["default"] = ({
    type: 'basic'
});


/***/ })
]);

Related Issues:

https://github.com/microsoft/TypeScript/issues/14495

when the target is es3,module import call transform should output compatible defineProperty

External

Most helpful comment

I think this is a webpack problem. Typescript don't generate outputs with webpackJsonp, __webpack_exports__ or __webpack_require__ references.

I cannot reproduce using only typescript:
tsconfig.json:

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es3",
    "lib": ["es2015"]
  },
  "include": [
    "./**/*.ts"
  ]
}

file (the same as yours):

function load(type: string): Promise<any> {
    return import(
        `./types/${type}`
    )
}

typescript 3.5.3, 3.6.3 and 3.7.0-dev.20190919 outs the same:

function load(type) {
    return import("./types/" + type);
}

All 3 comments

Why do you think it is a TypeScript issue? It looks like it is something webpack is injecting.

Why do you think it is a TypeScript issue? It looks like it is something webpack is injecting.

see https://github.com/microsoft/TypeScript/issues/14495 and https://github.com/microsoft/TypeScript/blob/master/src/compiler/transformers/module/module.ts#L620

I think this is a webpack problem. Typescript don't generate outputs with webpackJsonp, __webpack_exports__ or __webpack_require__ references.

I cannot reproduce using only typescript:
tsconfig.json:

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es3",
    "lib": ["es2015"]
  },
  "include": [
    "./**/*.ts"
  ]
}

file (the same as yours):

function load(type: string): Promise<any> {
    return import(
        `./types/${type}`
    )
}

typescript 3.5.3, 3.6.3 and 3.7.0-dev.20190919 outs the same:

function load(type) {
    return import("./types/" + type);
}
Was this page helpful?
0 / 5 - 0 ratings