Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript without module loader?

Tags:

I am stuck with an old software that uses aspx pages. I have some controls (ascx) that can work almost as a small Single Page Applications (for example a control that lets you register to a Newsletter,or a control that lets the user customize the news feed). (i know... its not really a SPA... )

I started using VUE.js for this small micro-projects. I managed to make it all work with plain JavaScript.

Now I wanted to use typescript instead of JavaScript.

The issue I have: when I create my typescripts and reference some modules like the VUE or my own, it always writes in the generated js file some "require " commands (transpiled to ES5) or some Import commands if transpiled to ES6.

As far as I read, these commands are supposed to be used by some module loaders on the server. But I don't have a Node.Js server nor a Javascript building infrastructure. Is there a way to tell typescript just to ignore those commands? I would then reference manually the needed JS-genereated files with the modules inseide y ascx-control.

like image 986
CodeHacker Avatar asked Oct 17 '16 10:10

CodeHacker


People also ask

Can I use TypeScript without Webpack?

Saying that means you can run TypeScript without any external trans-compiler . Even you don't need typescript cli. Using Deno we can run typescript and same time Deno provides tools like a bundle. We can compile typescript and convert it to JavaScript.

What is module loader in TypeScript?

At runtime the module loader is responsible for locating and executing all dependencies of a module before executing it. Well-known module loaders used in JavaScript are Node. js's loader for CommonJS modules and the RequireJS loader for AMD modules in Web applications.

Does TypeScript use CommonJS?

Configuring Typescript — Compiling into Modules. The item of interest for now is module: 'commonjs'. TypeScript supports the following 4 Modules: commonjs, amd, system and umd.


1 Answers

In your tsconfig.json the module property should be none

{     "compilerOptions": {         "module": "none"     } } 

Now there are no more amd or system code. And now you can't use any more import and export statements.

Without this, TypeScript does not know the order of your scripts, and you can have Js undefined errors.

To fix this, just add a triple slash directive instead of import statement

Now TypeScript compile all the scripts in the right order.
Also use outFile instead of out because is deprecated.

like image 196
iamandrewluca Avatar answered Dec 30 '22 02:12

iamandrewluca