由于最新的Angular已经弃用了'ng eject'命令,因此我开始使用webpack.config添加自定义文件来启动我的项目,该文件名为:
extra-webpack.config.js
我已经按照这个教程进行了操作: https://codeburst.io/customizing-angular-cli-6-build-an-alternative-to-ng-eject-a48304cd3b21
我已经配置好了一切,看起来一切正常。我的问题是,我想设置我的新文件。
extra-webpack.config.js
只在执行命令
"npm run start:dev"
时起作用,不想在执行
"npm run start:prod"
的时候使用。我尝试使用if语句来检查module.exports中是否将环境设置为非生产模式,但是我无法从environment.ts文件中获取环境信息。我已经尝试过:
import { environment } from './src/environments/environment';
有谁能帮我正确配置?非常感谢
这是我的extra-webpack.config.js文件:
'use strict';
const path = require('path'); const ForkTsCheckerWebpackPlugin =
require('fork-ts-checker-webpack-plugin');
module.exports = {
optimization: {
splitChunks: {
chunks: 'async',
minSize: 30000,
maxSize: 0,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
automaticNameDelimiter: '~',
name: true,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true
}
}
}
},
context: __dirname,
output: {
pathinfo: false
},
mode: 'development',
optimization: {
removeAvailableModules: false,
removeEmptyChunks: false,
splitChunks: false
},
module: {
rules: [
{
test: /\.ts?$/,
include: path.resolve(__dirname, 'src'),
use: [{
loader: 'ts-loader',
options: {
transpileOnly: true,
experimentalWatchApi: true,
},
}]
}
]
},
resolve: {
extensions: [ '.ts', '.js' ]
},
plugins: [
new ForkTsCheckerWebpackPlugin({
tslint: true
})
]
};
这是我的 angular.json 文件:
{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": {
"my-portal": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./extra-webpack.config.js"
},
"outputPath": "dist/my-portal",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/app/styles/style.scss"
],
"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,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-builders/dev-server:generic",
"options": {
"browserTarget": "my-portal:build"
},
"configurations": {
"production": {
"browserTarget": "my-portal:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "my-portal:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.scss"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"my-portal-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "my-portal:serve"
},
"configurations": {
"production": {
"devServerTarget": "my-portal:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
} }, "defaultProject": "my-portal" }
我还注意到,如果我删除了:
plugins: [
new ForkTsCheckerWebpackPlugin({
tslint: true
})
]
从webpack配置中,我可以通过
npm run start:prod
来运行它。
angular.json
结构是怎样的?你已经将什么作为了target
? - Agneyng serve
命令运行正常吗? - Raghav Garg