Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack with typescript getting TypeScript emitted no output error

I've got this configuration from https://www.npmjs.com/package/ts-loader:

webpack.config.js:

var path = require('path');
var webpack = require('webpack');

module.exports = {
    mode: "development",
    devtool: "inline-source-map",
    entry: "./src/Api.ts",
    output: {
        filename: "bundle.js"
    },
    resolve: {
        // Add `.ts` and `.tsx` as a resolvable extension.
        extensions: [".ts", ".tsx", ".js"]
    },
    module: {
        rules: [
            // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
            { test: /\.tsx?$/, loader: "ts-loader" }
        ]
    }
};

./src/Api.ts:

export class Api {
...
}

But when I run webpack I get:

Error: TypeScript emitted no output for Api.ts
like image 412
Claudiu Creanga Avatar asked Mar 22 '19 16:03

Claudiu Creanga


People also ask

Does Webpack work with TypeScript?

Webpack allows TypeScript, Babel, and ESLint to work together, allowing us to develop a modern project. The ForkTsCheckerWebpackPlugin Webpack plugin allows code to be type-checked during the bundling process. Next up is a quiz to test our knowledge of this module.

Can Webpack config be Ts?

ts file directly as configuration for your project. Of course, because TypeScript is just a superset for Javascript, you can always use TypeScript to write your webpack. config.

What Webpack plugin compiles TypeScript code?

ts-loader uses tsc , the TypeScript compiler, and relies on your tsconfig. json configuration.

Does Webpack use TSC?

Like Babel, Webpack depends on TSC to transpile TypeScript to JavaScript but as TSC doesn't have a clue about Webpack, hence Webpack needs a loader to talk to TSC. This is where the ts-loader comes into play. Webpack compiles a TypeScript file using ts-loader package which asks TSC to do the compilation.


2 Answers

Check that you don't have noEmit set to true In your tsconfig.json file.

like image 133
felixmosh Avatar answered Oct 05 '22 07:10

felixmosh


First change webpack config entry like index.js to index.tsx. Second make sure rule added for tsx file like:

{   test: /\.(ts|js)x?$/,   exclude: /node_modules/,   use: {     loader: "babel-loader",     options: {       presets: [         "@babel/preset-env",         "@babel/preset-react",         "@babel/preset-typescript",       ],     },   }, }, 
like image 27
Soshiv Upreti Avatar answered Oct 05 '22 08:10

Soshiv Upreti