Skip to content

Commit 437c3b4

Browse files
committed
feat: support workspace configs
1 parent 567a7ed commit 437c3b4

File tree

12 files changed

+410
-62
lines changed

12 files changed

+410
-62
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"projects": {
3+
"testProject": {
4+
"architect": {
5+
"default": {
6+
"configurations": {
7+
"dev": {
8+
"fileReplacements": [
9+
{
10+
"replace": "src/something.ts",
11+
"with": "src/something.replaced.ts"
12+
}
13+
]
14+
}
15+
}
16+
},
17+
"ios": {
18+
"configurations": {
19+
"dev": {
20+
"fileReplacements": [
21+
{
22+
"replace": "src/something.ts",
23+
"with": "src/something.replaced.ios.ts"
24+
}
25+
]
26+
}
27+
}
28+
},
29+
"android": {
30+
"configurations": {
31+
"dev": {
32+
"fileReplacements": [
33+
{
34+
"replace": "src/something.ts",
35+
"with": "src/something.replaced.android.ts"
36+
}
37+
]
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"projects": {
3+
"testProject": {
4+
"architect": {
5+
"default": {
6+
"configurations": {
7+
"dev": {
8+
}
9+
}
10+
}
11+
}
12+
}
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"projects": {
3+
"testProject": {
4+
"architect": {
5+
"default": {
6+
"configurations": {
7+
"dev": {
8+
"fileReplacements": [
9+
{
10+
"replace": "src/something.ts",
11+
"with": "src/something.replaced.ts"
12+
}
13+
]
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}

packages/webpack5/__tests__/configuration/angular.spec.ts

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import Config from 'webpack-chain';
2-
import angular from '../../src/configuration/angular';
2+
import {
3+
default as angular,
4+
getFileReplacementsFromWorkspaceConfig,
5+
applyFileReplacements
6+
} from '../../src/configuration/angular';
37
import { init } from '../../src';
8+
import { additionalCopyRules } from '../../src/helpers/copyRules'
9+
10+
import { resolve } from 'path'
411

512
jest.mock(
613
'@ngtools/webpack',
714
() => {
8-
class AngularCompilerPlugin {}
15+
class AngularCompilerPlugin {
16+
}
917

1018
return {
1119
AngularCompilerPlugin,
@@ -25,4 +33,124 @@ describe('angular configuration', () => {
2533
expect(angular(new Config()).toString()).toMatchSnapshot();
2634
});
2735
}
36+
37+
describe('workspace configuration', () => {
38+
it('no config', () => {
39+
init({
40+
ios: true,
41+
configuration: 'dev',
42+
projectName: 'testProject'
43+
})
44+
const res = getFileReplacementsFromWorkspaceConfig(
45+
''
46+
)
47+
48+
expect(res).toBe(null)
49+
})
50+
51+
it('no project', () => {
52+
init({
53+
ios: true,
54+
configuration: 'dev',
55+
projectName: 'nonProject'
56+
})
57+
const res = getFileReplacementsFromWorkspaceConfig(
58+
resolve(__dirname, './__fixtures__/workspace-without-replacements.json')
59+
)
60+
61+
expect(res).toBe(null);
62+
})
63+
64+
it('no replacements', () => {
65+
init({
66+
ios: true,
67+
configuration: 'dev',
68+
projectName: 'testProject'
69+
})
70+
const res = getFileReplacementsFromWorkspaceConfig(
71+
resolve(__dirname, './__fixtures__/workspace-without-replacements.json')
72+
)
73+
74+
expect(res).toBeDefined();
75+
expect(res).toEqual({});
76+
})
77+
78+
it('default file replacements', () => {
79+
init({
80+
// irrelevant to this test case - ensures getPlatformName() returns a valid platform
81+
ios: true,
82+
83+
configuration: 'dev',
84+
projectName: 'testProject'
85+
})
86+
const res = getFileReplacementsFromWorkspaceConfig(
87+
resolve(__dirname, './__fixtures__/workspace.json')
88+
)
89+
const entries = Object.entries(res)
90+
91+
expect(res).toBeDefined();
92+
expect(entries.length).toBe(1)
93+
expect(entries[0]).toEqual([
94+
resolve(__dirname, './__fixtures__/src/something.ts'),
95+
resolve(__dirname, './__fixtures__/src/something.replaced.ts'),
96+
])
97+
})
98+
99+
it('ios file replacements', () => {
100+
init({
101+
ios: true,
102+
configuration: 'dev',
103+
projectName: 'testProject'
104+
})
105+
const res = getFileReplacementsFromWorkspaceConfig(
106+
resolve(__dirname, './__fixtures__/workspace-with-platform-replacements.json')
107+
)
108+
const entries = Object.entries(res)
109+
110+
expect(res).toBeDefined();
111+
expect(entries.length).toBe(1)
112+
expect(entries[0]).toEqual([
113+
resolve(__dirname, './__fixtures__/src/something.ts'),
114+
resolve(__dirname, './__fixtures__/src/something.replaced.ios.ts'),
115+
])
116+
})
117+
118+
it('android file replacements', () => {
119+
init({
120+
android: true,
121+
configuration: 'dev',
122+
projectName: 'testProject'
123+
})
124+
const res = getFileReplacementsFromWorkspaceConfig(
125+
resolve(__dirname, './__fixtures__/workspace-with-platform-replacements.json')
126+
)
127+
const entries = Object.entries(res)
128+
129+
expect(res).toBeDefined();
130+
expect(entries.length).toBe(1)
131+
expect(entries[0]).toEqual([
132+
resolve(__dirname, './__fixtures__/src/something.ts'),
133+
resolve(__dirname, './__fixtures__/src/something.replaced.android.ts'),
134+
])
135+
})
136+
137+
it('applies file replacements', () => {
138+
const config = new Config();
139+
applyFileReplacements(config, {
140+
// should apply as an alias
141+
'foo.ts': 'foo.replaced.ts',
142+
143+
// should apply as a file replacement using the copy plugin
144+
'foo.json': 'foo.replaced.json'
145+
})
146+
147+
expect(config.resolve.alias.get('foo.ts')).toBe('foo.replaced.ts')
148+
expect(additionalCopyRules.length).toBe(1)
149+
expect(additionalCopyRules[0]).toEqual({
150+
from: 'foo.replaced.json',
151+
to: 'foo.json',
152+
force: true,
153+
})
154+
})
155+
})
28156
});

packages/webpack5/__tests__/configuration/base.spec.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Config from 'webpack-chain';
2-
import { mockFile } from '../../scripts/jest.mockFiles';
2+
import fs from 'fs';
33
import base from '../../src/configuration/base';
44
import { init } from '../../src';
55

@@ -16,31 +16,64 @@ describe('base configuration', () => {
1616
}
1717

1818
it('supports dotenv', () => {
19-
mockFile('./.env', '');
19+
const fsSpy = jest.spyOn(fs, "existsSync")
20+
fsSpy.mockReturnValue(true)
21+
2022
init({
2123
ios: true,
2224
});
2325
const config = base(new Config());
2426

27+
expect(config.plugin('DotEnvPlugin')).toBeDefined();
2528
config.plugin('DotEnvPlugin').tap((args) => {
2629
expect(args[0].path).toEqual('__jest__/.env');
2730
return args;
2831
});
29-
expect(config.plugin('DotEnvPlugin')).toBeDefined();
32+
33+
fsSpy.mockRestore()
3034
});
3135

3236
it('supports env specific dotenv', () => {
33-
mockFile('./.env.prod', '');
37+
const fsSpy = jest.spyOn(fs, "existsSync")
38+
fsSpy.mockReturnValue(true)
39+
3440
init({
3541
ios: true,
3642
env: 'prod',
3743
});
3844
const config = base(new Config());
3945

46+
expect(fsSpy).toHaveBeenCalledWith('__jest__/.env.prod')
47+
expect(fsSpy).toHaveBeenCalledTimes(1)
48+
expect(config.plugin('DotEnvPlugin')).toBeDefined();
4049
config.plugin('DotEnvPlugin').tap((args) => {
4150
expect(args[0].path).toEqual('__jest__/.env.prod');
4251
return args;
4352
});
53+
fsSpy.mockRestore()
54+
});
55+
56+
it('falls back to default .env', () => {
57+
const fsSpy = jest.spyOn(fs, "existsSync")
58+
fsSpy
59+
.mockReturnValueOnce(false)
60+
.mockReturnValueOnce(true)
61+
62+
63+
init({
64+
ios: true,
65+
env: 'prod',
66+
});
67+
const config = base(new Config());
68+
69+
expect(fsSpy).toHaveBeenCalledWith('__jest__/.env.prod')
70+
expect(fsSpy).toHaveBeenCalledWith('__jest__/.env')
71+
expect(fsSpy).toHaveBeenCalledTimes(2)
4472
expect(config.plugin('DotEnvPlugin')).toBeDefined();
73+
config.plugin('DotEnvPlugin').tap((args) => {
74+
expect(args[0].path).toEqual('__jest__/.env');
75+
return args;
76+
});
77+
fsSpy.mockRestore()
4578
});
4679
});

packages/webpack5/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"@babel/core": "^7.13.10",
2020
"@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
21+
"@types/lodash.get": "^4.4.6",
2122
"@types/sax": "^1.2.1",
2223
"babel-loader": "^8.2.1",
2324
"chalk": "^4.1.0",
@@ -29,6 +30,7 @@
2930
"dotenv-webpack": "^7.0.1",
3031
"fork-ts-checker-webpack-plugin": "^6.1.1",
3132
"loader-utils": "^2.0.0",
33+
"lodash.get": "^4.4.2",
3234
"micromatch": "^4.0.2",
3335
"postcss": "^8.2.7",
3436
"postcss-import": "^14.0.0",
@@ -61,11 +63,9 @@
6163
"@types/webpack-virtual-modules": "^0.1.0",
6264
"jest": "^26.6.3",
6365
"jest-matcher-utils": "^26.6.2",
64-
"memfs": "^3.2.0",
6566
"nativescript-vue-template-compiler": "^2.8.2",
6667
"ts-jest": "^26.5.3",
67-
"typescript": "^4.2.3",
68-
"unionfs": "^4.4.0"
68+
"typescript": "^4.2.3"
6969
},
7070
"peerDependencies": {
7171
"nativescript-vue-template-compiler": "^2.8.1"

packages/webpack5/scripts/jest.mockFiles.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

packages/webpack5/scripts/jest.setup.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import './jest.mockFiles';
21
// we are mocking the cwd for the tests, since webpack needs absolute paths
32
// and we don't want them in tests
43
process.cwd = () => '__jest__';
@@ -65,3 +64,15 @@ jest.mock('path', () => {
6564
},
6665
};
6766
});
67+
68+
// a virtual mock for package.json
69+
jest.mock(
70+
'__jest__/package.json',
71+
() => ({
72+
main: 'src/app.js',
73+
devDependencies: {
74+
typescript: '*',
75+
},
76+
}),
77+
{ virtual: true }
78+
);

0 commit comments

Comments
 (0)