我想在我的angular.json文件中有两个项目,都用相同的Webpack配置处理。尝试提供该应用程序时出现此错误:
../ projects / catalog / src / main.ts中的错误 模块构建失败(来自../node_modules/@ngtools/webpack/src/index.js): 错误:TypeScript编译中缺少/home/dev/myApp/projects/catalog/src/main.ts。请通过“文件”或“包含”属性确保它在您的tsconfig中。
我有一个运行良好的主应用程序(“ app”)。我想通过“ ng generate application”创建另一个。该应用程序称为“目录”,已在“项目”目录中创建(使用ng generate时为默认设置)。
这是我现在的angular.json文件:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./webpack.config.js"
},
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"tsConfig": "src/tsconfig.app.json",
"polyfills": "src/polyfills.ts",
"assets": [
"src/assets",
"src/favicon.ico"
],
"styles": [
"src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-builders/dev-server:generic",
"options": {
"browserTarget": "app:build"
},
"configurations": {
"production": {
"browserTarget": "app:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "app:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"karmaConfig": "./karma.conf.js",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"scripts": [],
"styles": [
"src/styles.css"
],
"assets": [
"src/assets",
"src/favicon.ico"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"app-e2e": {
"root": "e2e",
"sourceRoot": "e2e",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "./protractor.conf.js",
"devServerTarget": "app:serve"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"e2e/tsconfig.e2e.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"catalog": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./webpack.config.js"
},
"outputPath": "dist/catalog",
"index": "projects/catalog/src/catalog.html",
"main": "projects/catalog/src/main.ts",
"polyfills": "projects/catalog/src/polyfills.ts",
"tsConfig": "projects/catalog/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "projects/catalog/src/environments/environment.ts",
"with": "projects/catalog/src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-builders/dev-server:generic",
"options": {
"browserTarget": "catalog:build"
},
"configurations": {
"production": {
"browserTarget": "catalog:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "catalog:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "projects/catalog/src/test.ts",
"polyfills": "projects/catalog/src/polyfills.ts",
"tsConfig": "projects/catalog/tsconfig.spec.json",
"karmaConfig": "projects/catalog/karma.conf.js",
"styles": [
"src/styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"projects/catalog/tsconfig.app.json",
"projects/catalog/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"catalog-e2e": {
"root": "projects/catalog-e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "projects/catalog-e2e/protractor.conf.js",
"devServerTarget": "catalog:serve"
},
"configurations": {
"production": {
"devServerTarget": "catalog:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "projects/catalog-e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "app",
"schematics": {
"@schematics/angular:component": {
"prefix": "app",
"styleext": "css"
},
"@schematics/angular:directive": {
"prefix": "app"
}
}
}
以及我的Webpack配置的一部分:
module.exports = {
target: 'web',
context: path.resolve(__dirname, 'src'),
entry: {
app: './main.ts',
catalog: '../projects/catalog/src/main.ts',
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
sourceMapFilename: '[name].map.js'
},
resolve: {
extensions: ['.ts', '.js', '.tsx', '.json']
},
plugins: [
new ForkTsCheckerWebpackPlugin({tsconfig: '../tsconfig.json', workers: ForkTsCheckerWebpackPlugin.TWO_CPUS_FREE }),
// provide configuration global variable. Webpack REPLACE (works similar as preprocessor) each defined variable occurrence
new webpack.DefinePlugin({
__APP_CONFIG__: '\'' + prepareAppConfig(appConfig) + '\'' // quote json object (DefinePlugin requires primitives). On usage JSON string will be again parsed.
}),
new CopyWebpackPlugin([ { from: 'assets', to: 'assets' } ]), // copy assets
new HtmlWebpackPlugin({filename: 'app.html', chunks: ['vendor', 'app'], template: './index.html', chunksSortMode: chunksOrderFunction}),
new HtmlWebpackPlugin({filename: 'catalog.html', chunks: ['vendor', 'catalog'], template: '../projects/catalog/src/catalog.html', chunksSortMode: chunksOrderFunction})
]
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"module": "commonjs",
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"types": ["node", "jasmine"],
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}
tsconfig.app.json:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/app",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
向tsconfig文件中添加“文件”或“包含”根本没有帮助。
如何在使用“ ng serve”时摆脱错误?