gpt4 book ai didi

angular - webpack 2 ts-loader 排除 node_modules 不工作

转载 作者:行者123 更新时间:2023-12-04 16:10:26 24 4
gpt4 key购买 nike

我尝试在 webpack 2 中使用 typescript 2.2.2babel 加载器并排除 node_modules,但是当尝试 webpack 命令时仍然从 node_modules 中的 angular 4.0.0 中得到错误。

webpack.congfig.js:

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

var PROD = true;

module.exports = {
entry: path.join(__dirname, 'ng2', 'src','index.js'),
output: {
path: path.join(__dirname, 'ng2', 'dist'),
filename: 'vendorNG2.js'
},
devtool: 'eval',
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
watch: true,
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: [/node_modules/],
},
{
test: /\.js$/,
exclude: [/node_modules/],
use: {

loader: "babel-loader?cacheDirectory=true",
options: {
presets: ['es2015']
}
}
},
]
},
plugins: PROD ? [
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false }
})
] : []
}

在 webpack --progress 命令之后:

...
ERROR in /home/user/Projects/project/front/node_modules/angular2/src/facade/lang.d.ts
(13,17): error TS2693: 'Map' only refers to a type, but is being used as a value here.

ERROR in /home/user/Projects/project/front/node_modules/angular2/src/facade/lang.d.ts
(14,17): error TS2693: 'Set' only refers to a type, but is being used as a value here.

ERROR in ./ng2/src/app/main.ts
(19,14): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.

您可以看到来自 node_modules 的错误。

tsconfig.json v1:

{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"allowJs": true
}
}

tsconfig.json v2:

{
"compilerOptions": {
"outDir": "./ng2/dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"type":[],
"allowJs": true
},
"exclude": [
"node_modules"
]
}

v2 错误:

ERROR in /home/user/Projects/project/front/admin/tsconfig.json
error TS5023: Unknown compiler option 'type'.
@ ./ng2/src/index.js 8:0-21

ERROR in ./ng2/src/app/main.ts
Module build failed: error while parsing tsconfig.json
@ ./ng2/src/index.js 8:0-21

tsconfig.js v3 add ("experimentalDecorators": true) 一些错误消失了:

{
"compilerOptions": {
"outDir": "./ng2/dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"experimentalDecorators": true,
"allowJs": true
},
"exclude": [
"node_modules"
]
}

v3 错误:

.../home/user/Projects/project/front/node_modules/angular2/src/facade/lang.d.ts 中的错误(13,17): 错误 TS2693: 'Map' 只引用了一个类型,但在这里被用作一个值。

/home/user/Projects/project/front/node_modules/angular2/src/facade/lang.d.ts 中的错误 (14,17): 错误 TS2693: 'Set' 仅指类型,但在此处用作值。

最佳答案

您是否已将 exclude node_modules 添加到您的 tsconfig 中?

{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"allowJs": true
},
"exclude": [
"node_modules"
]
}

此外,您没有指定您使用的是哪个版本的 Typescript 和 Angular。

编辑:您更新了问题以指定您使用的是 Angular 4,但更新后的错误显示 node_modules/angular2/文件夹似乎不正确。在继续之前,请尝试删除您的 node_modules 文件夹并再次运行 npm install

否则看起来您遇到了打字问题。你有安装任何@types吗?

这是我的 tsconfig.json 设置示例:

{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"removeComments": false,
"noImplicitAny": false,
"types": [ "jasmine", "lodash", "core-js" ],
"typeRoots": [
"../node_modules/@types"
]
},
"exclude": [
"node_modules",
"wwwroot",
"temp"
]
}

我的 node_modules 文件夹在父目录中,所以我在 typeRoots 中指定它。我还安装了以下类型:

"@types/core-js": "0.9.37",
"@types/jasmine": "2.5.43",
"@types/lodash": "4.14.55",

关于angular - webpack 2 ts-loader 排除 node_modules 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43097173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com