I am faced with a challenge of utilizing an enum from a library I created in a different project. The library is developed using Vue and typescript, bundled with rollup. On the other hand, the project is built with Symfony, with the front end also using Vue and typescript, built with Webpack Encore.
The library serves as a dependency for my project, so I attempted to import the enum in this manner:
import { MyEnum } from 'myLibrary/src/enum/MyEnum';
The structure of the enum is as follows:
// node_modules/myLibrary/src/enum/MyEnum.ts
export enum MyEnum {
One = 'one',
Two = 'two',
Three = 'three'
}
However, upon building, I encountered an error (using Symfony's Webpack Encore):
ERROR Failed to compile with 1 errors 4:37:05 PM
Error loading ./node_modules/myLibrary/src/enum/MyEnum.ts
FIX To process TypeScript files:
1. Add Encore.enableTypeScriptLoader() to your webpack.config.js file.
I have already included enableTypeScriptLoader()
in my webpack.config.js, but the issue persists.
Creating the same enum file within my project and importing it resolves the issue, but I wish to avoid duplicating code. Interestingly, I can import interfaces from the library without any issues.
I have tried some solutions that did not work:
export const enum
somewhat works, but I encounter the
error, and I need to redeclare the enum in a different object to utilize it in my template :/TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
export declare enum
still results in the
errorAdd Encore.enableTypeScriptLoader()
Any suggestions on how to resolve this issue?
EDIT
In addition to the previous error message, my browser displayed additional information:
Module parse failed: Unexpected token (4:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> export enum MyEnum {
| One = 'one',
| Two = 'two'
I also tested importing the same enum from the library into a new project created with Vue CLI, and encountered no errors. It seems like the issue lies with Webpack Encore.