由于最新的角已经不再推荐“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运行启动:dev“
,我不想在开始的时候用它
"npm运行启动:prod“
我尝试创建一个if语句,以检查是否在module.exports上将环境设置为生产,但无法从environment.ts文件中收集环境。我尝试过:从‘./src/environment/environment’导入{environment};
有人能帮我做正确的配置吗?非常感谢
下面是我的额外的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运行启动:prod
发布于 2019-03-11 18:57:54
您只需移动
"customWebpackConfig": {
"path": "./extra-webpack.config.js"
},在configurations:production下使其自定义为该production配置
"configurations": {
"production": {
"customWebpackConfig": {
"path": "./extra-webpack.config.js"
},
"fileReplacements": [],
...更多细节:https://github.com/meltedspark/angular-builders/issues/248#issuecomment-466650709
https://stackoverflow.com/questions/53768121
复制相似问题