简体   繁体   English

为什么 webpack 无法正确编译 ts 文件?

[英]Why webpack does not compile the ts file properly?

I have a simple function on ./src/ts/bundle.ts But it generates an error, I don't why?我在 ./src/ts/bundle.ts 上有一个简单的./src/ts/bundle.ts但它会产生错误,我不知道为什么?

ERROR in ./src/ts/bundle.ts 6:22
Module parse failed: Unexpected token (6:22)
You may need an appropriate loader to handle this file type, currently, no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|  */
|
> function sayHI ( text : string ){
|     console.log ( text );
| }

function sayHI ( text : string ){
    console.log ( text );
}

package.json package.json

{
  "name": "document",
  "version": "1.0.0",
  "description": "",
  "main": "sample.js",
  "scripts": {
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-loader": "^7.0.5",
    "typescript": "^3.9.5",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.11"
  }
}

This is the webpack.config.js这是 webpack.config.js

/**
 * 
 * @package Webpack
 * main configuration file for the webpack bundler
 */

const path = require("path");

module.exports = {

    watch: true,
    devtool: 'source-map',
    mode: 'development',

    entry: {
        bundle: path.resolve(__dirname, 'src/ts/bundle.ts'),
    },


    output: {
        path: path.resolve(__dirname, 'dist/js/'),
        filename: '[name].js'
    },

    module: {
        rules: [{
            test: /\.ts$|js/,
            use: 'ts-loader',
            include: [
                path.resolve(__dirname, 'src/js/')
            ]
        }]
    }
}

You have a typeo in your webpack.config.js , either remove it or fix it.你的 webpack.config.js 中有一个webpack.config.js ,要么删除它,要么修复它。 You've told it to only run on certain folders, so the file that you have specified which is located in /src/ts/ is ignored by ts-loader .您已告诉它仅在某些文件夹上运行,因此您指定的位于/src/ts/中的文件将被ts-loader忽略。

// ...rest of webpack.config.js
    include: [
        path.resolve(__dirname, 'src/js/')
    ]
// ...rest of webpack.config.js

to

// ...rest of webpack.config.js
    include: [
        path.resolve(__dirname, 'src/ts/')
    ]
// ...rest of webpack.config.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webpack 编译从 ts 文件导出为空 - Webpack compile export from ts file into nothing 为什么 webpack 警告不要编译不受信任的代码? - Why does webpack warn not to compile untrusted code? 为什么 webpack 在我的 ts 文件中不包括我的 function - Why is webpack not including my function in my ts file 为什么tsc编译输出一个在运行时抛出异常但ts-node正确运行ts文件的文件? - Why tsc compile output a file that throw exception on running but ts-node run the ts file correctly? 为什么 webpack 需要 4 秒来编译一个空的 react / redux 项目? - Why does webpack take 4 seconds to compile an empty react / redux project? Webpack教程的配置文件无法正确运行Babel loader - Config file for webpack tutorial does not properly run Babel loader Webpack 2:如何测试文件但不编译它? - Webpack 2: how to test for file but not compile it? webpack如何替换.ts文件中的字符串? - how webpack replace a string in .ts file? webpack - 在不捆绑的情况下转译 1 个 ts 文件(2 个条目) - webpack - transpile 1 ts file without bundling it (2 entries) 为什么webpack编译成功,但编译输出中不存在我的源代码? - Why does webpack compile successfully but my source code does not exist in the compiled output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM