question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using ts-loader to emitDeclerationOnly

See original GitHub issue

currently i am using bable-loader to generate the javascript from typescript. but i want to generate typescript declarations as well.

to solve this originally i simply added tsc --emitDeclarationOnly at the end of our build script. this approach works for building but not for watch

hence i wanted to still use the ts-loader just of emitDeclarationsOnly. however i kept getting the following error

lerna ERR! yarn run build stdout:
$ cross-env NODE_ENV=production webpack --config ./webpack.config.js --mode production
assets by status 122 KiB [cached] 117 assets
./src/index.ts 39 bytes [built]

ERROR in ./src/index.ts
Module build failed (from ./node_modules/ts-loader/index.js):
Error: TypeScript emitted no output for G:\SOMEPATH\src\index.ts.
    at makeSourceMapAndFinish (G:\SOMEPATH\node_modules\ts-loader\dist\index.js:53:18)
    at successLoader (G:\SOMEPATH\node_modules\ts-loader\dist\index.js:40:5)
    at Object.loader (G:\SOMEPATH\node_modules\ts-loader\dist\index.js:23:5)

webpack 5.11.1 compiled with 1 error in 8620 ms
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

my tsconfig is

{
  "compilerOptions": {
    "alwaysStrict": true,
    "declaration": true,
    "declarationMap": true,
    "esModuleInterop": true,
    "emitDeclarationOnly": true, // <------ magic
    "lib": ["ESNext", "DOM"],
    "module": "esnext",
    "moduleResolution": "node", 
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true, 
    "useDefineForClassFields": true,
    "outDir": "./dist/",
    "sourceMap": true,
    "strict": true,
    "strictFunctionTypes": true, 
    "strictNullChecks": true, 
    "strictPropertyInitialization": true, 
    "target": "es2015",
    "types": []
  },
  "exclude": ["node_modules"],
  "files": [
    "./src/index.ts",
    "./src/index.d.ts",
  ]
}

webpack

/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');

const isProduction = process.env.NODE_ENV === 'production';

module.exports = {
  cache: {
    type: 'filesystem',
  },
  devtool: isProduction ? 'source-map' : 'source-map',
  entry: './src/index.ts',
  module: {
    rules: [
      {
        enforce: 'pre',
        exclude: /node_modules/,
        test: /\.(ts|tsx)$/,
        use: [
          {
            loader: require.resolve('eslint-loader'),
            options: {
              eslintPath: require.resolve('eslint'),
            },
          },
        ],
      },
      {
        exclude: /node_modules/,
        use: ['ts-loader'],
      },
      {
        exclude: /node_modules/,
        test: /\.tsx?$/,
        use: ['babel-loader'],
      },
    ],
  },
  output: {
    filename: 'index.js',
    globalObject: 'this',
    library: 'data',
    libraryTarget: 'umd',
    path: path.resolve(__dirname, 'dist'),
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
};

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
appzukacommented, Apr 8, 2021

@melloc01 , are you seeing the same error as @ZNackasha reported? (Error: TypeScript emitted no output for G:\SOMEPATH\src\index.ts.) The error is coming from webpack. It is reporting that ts-loader emitted no output, which is not surprising as @ZNackasha told it not to produce any output.

This seems a strange use case as it runs ts-loader but then tells it not to produce any Javascript output. I’m also not sure that the webpack config given above is valid. Perhaps you could explain what you expect ts-loader to do in this case and provide a minimal repository demonstrating the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

using ts-loader to run emitDeclarationOnly - Stack Overflow
ts -loader emits an error when no output is generated. This occurs when using Typescript's emitDeclarationOnluy * flag.
Read more >
ts-loader - npm
The simple solution is to disable it by using the transpileOnly: true option, but doing so leaves you without type checking and will...
Read more >
Documentation - Using Babel with TypeScript
This technique is a hybrid approach, using Babel's preset-typescript to generate your JS files, and then using TypeScript to do type checking and...
Read more >
TypeScript With Babel: A Beautiful Marriage - I Am Turns
Tweak your Webpack config to feed *.ts into TypeScript, and then feed the result into Babel. But which TypeScript loader do you use?...
Read more >
TypeStrong/ts-loader - module - Gitter
Hi folks. I am working with a package that merely has an index.d.ts at its root (next to package.json) with "types": "./index.d.ts" in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found