NativeScript/nativescript-dev-webpack

Enum values are not inlined in TypeScript projects (compared to the Legacy Workflow)

DimitarTachev opened this issue · 1 comments

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 5.4.0
  • Cross-platform modules: 5.4.0
  • Android Runtime: 5.4.0
  • iOS Runtime: 5.4.0
  • Plugin: 0.24.0

To Reproduce

  1. Open the attached app (it contains a blank TypeScript app with an additional file with an enum which is declarated but not defined anywhere in the app).
  2. tns run ios (or android)
  3. file:///app/bundle.js:152:95: JS ERROR TypeError: undefined is not an object (evaluating 'enum__WEBPACK_IMPORTED_MODULE_1_["TestEnum"].value2')
  4. tns run ios --no-bundle
  5. The app will run without crashing and log the enum value.

Expected behavior
The enum value should be printed using the bundle workflow.

Sample project
blankTsWithEnum.zip

Additional context
The Legacy Workflow is using the nativescript-dev-typescript plugin which executes the TypeScript compiler (tsc) while the Bundle workflow is using the ts-loader which is configured in a transpileOnly mode (with an additional TypeCheck plugin spawning child processes for a better performance). The ts-loader is compiling the TypeScript file by file without validating the dependencies and it's not able to inline enum values from external files.

As a workaround, you could edit the webpack.config.js, set the transpileOnly option of the ts-loader to false and remove the additional ForkTsCheckerWebpackPlugin. In this way, the enums will be inlined in the Bundle workflow but the Webpack compilation performance could be decreased a lot depending on the app size and the HMR will not update your app properly as mentioned in the loader docs below:
https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#faster-builds
https://github.com/TypeStrong/ts-loader/blob/ea2fcf925ec158d0a536d1e766adfec6567f5fb4/README.md#hot-module-replacement