[英]"Module parse failed: Unexpected token" when added the class field
Below TypeScript code (it means that static class fields and other TypeScript features are available) successfully has been built with Webpack:下面的 TypeScript 代码(这意味着静态类字段和其他 TypeScript 功能可用)已经使用 Webpack 成功构建:
export default class ConfigRepresentative {
constructor() {
console.log('ok');
}
}
Fails (same if to remove private
and static
):失败(与删除
private
和static
):
export default class ConfigRepresentative {
private static ownInstanceHasBeenCreated: boolean = false;
constructor() {
console.log('ok');
}
}
Error:错误:
ERROR in ./TypeScriptSource/index.ts 7:10
Module parse failed: Unexpected token (7:10)
You may need an appropriate loader to handle this file type.
| export default class ConfigRepresentative {
|
> private static ownInstanceHasBeenCreated: boolean = false;
|
| constructor() {
module.exports = {
entry: './TypeScriptSource/index.ts',
output: {
filename: 'index.js',
path: __dirname,
libraryTarget: 'umd'
},
target: 'node',
mode: 'production',
watch: true,
module: {
rules: [
{
test: /\.ts?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.ts', '.js']
}
};
For saving your time on reproducing this problem, I attached the souse files.为了节省您重现此问题的时间,我附上了 souse 文件。 error.zip
错误.zip
It is because the project was inside node_modules
folder.这是因为该项目位于
node_modules
文件夹中。 The setting exclude: /node_modules/
cancels the rule {test: /\\.ts?$/, use: 'ts-loader'}
, but without class property code is pure JavaScript.设置
exclude: /node_modules/
取消规则{test: /\\.ts?$/, use: 'ts-loader'}
,但没有类属性的代码是纯 JavaScript。
(I know that it is bad practice - to develop something inside node_modules
, however I don't know the other solution for developing dependencies. In this case, single ConfigRepresentative
is useless if it does not used by other project). (我知道这是不好的做法 - 在
node_modules
内部开发一些东西,但是我不知道开发依赖项的其他解决方案。在这种情况下,如果其他项目不使用单个ConfigRepresentative
是没用的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.