Skip to content

Commit c4b2cb6

Browse files
author
Yuri Cherepanov
committed
#395 - require is not defined
Add support for @angular-builders/custom-webpack Setup environment target based on env params Remove direct code changeing scripts postinstall/postinstall-web
1 parent 93d5a8c commit c4b2cb6

File tree

6 files changed

+71
-44
lines changed

6 files changed

+71
-44
lines changed

angular.json

+32-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"projectType": "application",
1010
"architect": {
1111
"build": {
12-
"builder": "@angular-devkit/build-angular:browser",
12+
"builder": "@angular-builders/custom-webpack:browser",
1313
"options": {
1414
"outputPath": "dist",
1515
"index": "src/index.html",
@@ -27,7 +27,10 @@
2727
"styles": [
2828
"src/styles.scss"
2929
],
30-
"scripts": []
30+
"scripts": [],
31+
"customWebpackConfig": {
32+
"path": "./angular.webpack.js"
33+
}
3134
},
3235
"configurations": {
3336
"dev": {
@@ -47,6 +50,23 @@
4750
}
4851
]
4952
},
53+
"dev-web": {
54+
"optimization": false,
55+
"outputHashing": "all",
56+
"sourceMap": true,
57+
"extractCss": true,
58+
"namedChunks": false,
59+
"aot": false,
60+
"extractLicenses": true,
61+
"vendorChunk": false,
62+
"buildOptimizer": false,
63+
"fileReplacements": [
64+
{
65+
"replace": "src/environments/environment.ts",
66+
"with": "src/environments/environment.web.ts"
67+
}
68+
]
69+
},
5070
"production": {
5171
"optimization": true,
5272
"outputHashing": "all",
@@ -67,14 +87,17 @@
6787
}
6888
},
6989
"serve": {
70-
"builder": "@angular-devkit/build-angular:dev-server",
90+
"builder": "@angular-builders/custom-webpack:dev-server",
7191
"options": {
7292
"browserTarget": "angular-electron:build"
7393
},
7494
"configurations": {
7595
"dev": {
7696
"browserTarget": "angular-electron:build:dev"
7797
},
98+
"dev-web": {
99+
"browserTarget": "angular-electron:build:dev-web"
100+
},
78101
"production": {
79102
"browserTarget": "angular-electron:build:production"
80103
}
@@ -87,7 +110,7 @@
87110
}
88111
},
89112
"test": {
90-
"builder": "@angular-devkit/build-angular:karma",
113+
"builder": "@angular-builders/custom-webpack:karma",
91114
"options": {
92115
"main": "src/test.ts",
93116
"polyfills": "src/polyfills-test.ts",
@@ -104,7 +127,11 @@
104127
"src/favicon.icns",
105128
"src/favicon.256x256.png",
106129
"src/favicon.512x512.png"
107-
]
130+
],
131+
"customWebpackConfig": {
132+
"path": "./angular.webpack.js",
133+
"target": "web"
134+
}
108135
}
109136
},
110137
"lint": {

angular.webpack.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Custom angular webpack configuration
3+
*/
4+
5+
module.exports = (config, options) => {
6+
config.target = 'electron-renderer';
7+
if (options.customWebpackConfig.target) {
8+
config.target = options.customWebpackConfig.target;
9+
} else if (options.fileReplacements) {
10+
for(let fileReplacement of options.fileReplacements) {
11+
if (fileReplacement.replace !== 'src/environments/environment.ts') {
12+
continue;
13+
}
14+
15+
let fileReplacementParts = fileReplacement['with'].split('.');
16+
if (['dev', 'prod', 'test', 'electron-renderer'].indexOf(fileReplacementParts[1]) < 0) {
17+
config.target = fileReplacementParts[1];
18+
}
19+
break;
20+
}
21+
}
22+
23+
return config;
24+
}

package.json

+6-7
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,27 @@
1717
"main": "main.js",
1818
"private": true,
1919
"scripts": {
20-
"postinstall": "npm run postinstall:electron && electron-builder install-app-deps",
21-
"postinstall:web": "node postinstall-web",
22-
"postinstall:electron": "node postinstall",
20+
"postinstall": "electron-builder install-app-deps",
2321
"ng": "ng",
24-
"start": "npm run postinstall:electron && npm-run-all -p ng:serve electron:serve",
25-
"build": "npm run postinstall:electron && npm run electron:serve-tsc && ng build",
22+
"start": "npm-run-all -p ng:serve electron:serve",
23+
"build": "npm run electron:serve-tsc && ng build",
2624
"build:dev": "npm run build -- -c dev",
2725
"build:prod": "npm run build -- -c production",
2826
"ng:serve": "ng serve",
29-
"ng:serve:web": "npm run postinstall:web && ng serve -o",
27+
"ng:serve:web": "ng serve -c dev-web -o",
3028
"electron:serve-tsc": "tsc -p tsconfig-serve.json",
3129
"electron:serve": "wait-on http-get://localhost:4200/ && npm run electron:serve-tsc && electron . --serve",
3230
"electron:local": "npm run build:prod && electron .",
3331
"electron:linux": "npm run build:prod && electron-builder build --linux",
3432
"electron:windows": "npm run build:prod && electron-builder build --windows",
3533
"electron:mac": "npm run build:prod && electron-builder build --mac",
36-
"test": "npm run postinstall:web && ng test",
34+
"test": "ng test",
3735
"e2e": "npm run build:prod && mocha --timeout 300000 --require ts-node/register e2e/**/*.spec.ts",
3836
"version": "conventional-changelog -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
3937
"lint": "ng lint"
4038
},
4139
"devDependencies": {
40+
"@angular-builders/custom-webpack": "^8.2.0",
4241
"@angular-devkit/build-angular": "0.803.6",
4342
"@angular/cli": "8.3.6",
4443
"@angular/common": "8.2.12",

postinstall-web.js

-16
This file was deleted.

postinstall.js

-16
This file was deleted.

src/environments/environment.web.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// The file contents for the current environment will overwrite these during build.
2+
// The build system defaults to the dev environment which uses `index.ts`, but if you do
3+
// `ng build --env=prod` then `index.prod.ts` will be used instead.
4+
// The list of which env maps to which file can be found in `.angular-cli.json`.
5+
6+
export const AppConfig = {
7+
production: false,
8+
environment: 'DEV'
9+
};

0 commit comments

Comments
 (0)