I am trying to build a very basic custom npm package which will be imported in a Typescript React app (created with CRA).
React app ALPHA:
import {MyText} from 'custom-package';
function App() {
return (
<div>
{MyText()}
</div>
);
}
Now, node_modules/custom-package includes package.json and index.ts:
index.ts
export const myText=():string=>'Hello world';
The above code gives me the following error: You may need an additional loader to handle the result...
1. Why React app is unable to parse the imported Typescript code? Why do I need to transpile it to JS?
My thought was Webpack of ALPHA app would take over that code. Isn't it Webpack that takes over in case of 'export' and 'import'? For example, the following works:
index.js
export const myText=()=>'Hello world';
2. It is Webpack that compiles 'export' in the above example. Otherwise the browser would not recognise it unless we define <script type='module'>. Am I right? If yes, why Webpack does not compile index.ts as well?
I hope you got my point. So,
3. Is it possible to import directly Typescript or even JSX code and how?