Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Webpacks 'require' and 'require.ensure' in TypesScript

I am trying to integrate TypeScript into Webpack and am looking for some @types that will help with all the Webpack specific stuff.

I was using declare function require(string): any; to use fe.: require('some.scss') but quickly got annoyed with adding it directly or over the /// <reference path="... syntax and was hoping that there is a better way to do this.

The ideal thing would be just npm i @types/webpack but that package only seems to be for the Webpack config file which I think is totally useless...

to be more specific, I am trying to get this code to work in TypeScript:

require.ensure([
    require('./first')
], function(require) {
    require('./second')
}, 'second');
like image 979
BrendanMullins Avatar asked Jan 12 '17 13:01

BrendanMullins


People also ask

Can we use require in TypeScript?

TypeScript offers support for creating and using modules with a unified syntax that is similar to the ES Module syntax, while allowing the developer to output code that targets different module loaders, like Node. js (CommonJS), require.

Does TypeScript use CommonJS?

Yes, you can use it in a same manner that you would use it in Javascript. Typescript is superset of Javascript, all things possible in Javascript are also possible in Typescript.

How do you send a HTTP request in TypeScript?

The http requests in TypeScript can be placed by making use of a function called fetch() function. The URL of the website whose contents must be fetched or to which the contents must be posted should be passed as a parameter to the fetch() function.


1 Answers

These are available in @types/webpack-env

npm install --save-dev @types/webpack-env
like image 66
alechill Avatar answered Nov 09 '22 19:11

alechill