@modern-js/server 1.4.7-canary.2 → 1.4.9-beta.peer.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/.eslintrc.js +8 -0
  2. package/CHANGELOG.md +41 -0
  3. package/dist/js/modern/dev-tools/babel/register.js +1 -0
  4. package/dist/js/modern/dev-tools/dev-server-plugin.js +2 -5
  5. package/dist/js/modern/dev-tools/watcher/index.js +1 -2
  6. package/dist/js/modern/server/dev-server.js +49 -2
  7. package/dist/js/node/dev-tools/babel/register.js +1 -0
  8. package/dist/js/node/dev-tools/dev-server-plugin.js +2 -5
  9. package/dist/js/node/dev-tools/watcher/index.js +1 -3
  10. package/dist/js/node/server/dev-server.js +49 -2
  11. package/dist/types/dev-tools/watcher/index.d.ts +2 -2
  12. package/dist/types/server/dev-server.d.ts +2 -0
  13. package/jest.config.js +0 -1
  14. package/package.json +23 -25
  15. package/tests/.eslintrc.js +0 -6
  16. package/tests/dev.test.ts +0 -130
  17. package/tests/fixtures/mock/exist/config/mock/index.ts +0 -11
  18. package/tests/fixtures/mock/module-error/config/mock/index.ts +0 -1
  19. package/tests/fixtures/mock/type-error/config/mock/index.ts +0 -3
  20. package/tests/fixtures/mock/zero/config/mock/index.ts +0 -1
  21. package/tests/fixtures/pure/config/mock/index.ts +0 -11
  22. package/tests/fixtures/pure/modern.config.js +0 -5
  23. package/tests/fixtures/pure/package.json +0 -21
  24. package/tests/fixtures/pure/src/App.css +0 -119
  25. package/tests/fixtures/pure/src/App.tsx +0 -43
  26. package/tests/fixtures/pure/tsconfig.json +0 -12
  27. package/tests/fixtures/watch/a.ts +0 -3
  28. package/tests/fixtures/watch/index.ts +0 -5
  29. package/tests/fixtures/watch/stats.txt +0 -1
  30. package/tests/helper.ts +0 -3
  31. package/tests/https.test.ts +0 -17
  32. package/tests/launch-editor.test.ts +0 -58
  33. package/tests/mock.test.ts +0 -150
  34. package/tests/server.test.ts +0 -238
  35. package/tests/tsconfig.json +0 -12
  36. package/tests/watcher.test.ts +0 -228
@@ -1,228 +0,0 @@
1
- import path from 'path';
2
- import { fs } from '@modern-js/utils';
3
- import Watcher, { getWatchedFiles } from '../src/dev-tools/watcher';
4
- import { StatsCache } from '../src/dev-tools/watcher/stats-cache';
5
-
6
- jest.useRealTimers();
7
-
8
- describe('watcher', () => {
9
- jest.setTimeout(25000);
10
- const pwd = path.join(__dirname, './fixtures/watch');
11
- const serverDir = path.normalize(path.join(pwd, './tmp-server'));
12
-
13
- beforeAll(() => {
14
- if (fs.existsSync(serverDir)) {
15
- fs.removeSync(serverDir);
16
- }
17
- fs.mkdirSync(serverDir);
18
- });
19
-
20
- afterAll(() => {
21
- fs.removeSync(serverDir);
22
- });
23
-
24
- const writeFiles = (content: string, filepath: string) => {
25
- fs.writeFileSync(path.normalize(filepath), content, 'utf8');
26
- };
27
-
28
- test('should emit add', done => {
29
- const watcher = new Watcher();
30
- const callback = jest.fn();
31
-
32
- const watchDir = path.join(serverDir, 'add');
33
- fs.mkdirSync(watchDir);
34
-
35
- watcher.listen(
36
- [`${watchDir}/**/*`],
37
- {
38
- ignoreInitial: true,
39
- ignored: /api\/typings\/.*/,
40
- },
41
- async () => {
42
- try {
43
- callback();
44
- expect(callback).toHaveBeenCalledTimes(1);
45
- await watcher.close();
46
- } catch (e) {
47
- console.error(e);
48
- }
49
- done();
50
- },
51
- );
52
-
53
- setTimeout(() => writeFiles('test', path.join(watchDir, 'index.js')), 100);
54
- });
55
-
56
- test('should emit unlink', done => {
57
- const watcher = new Watcher();
58
-
59
- const callback = jest.fn();
60
- const watchDir = path.join(serverDir, 'unlink');
61
- fs.mkdirSync(watchDir);
62
-
63
- const filepath = path.join(watchDir, 'index.js');
64
- writeFiles('unlink', filepath);
65
-
66
- watcher.listen(
67
- [`${watchDir}/**/*`],
68
- {
69
- ignoreInitial: true,
70
- ignored: /api\/typings\/.*/,
71
- },
72
- async () => {
73
- callback();
74
- expect(callback).toHaveBeenCalledTimes(1);
75
- await watcher.close();
76
- done();
77
- },
78
- );
79
-
80
- setTimeout(() => {
81
- fs.removeSync(filepath);
82
- }, 100);
83
- });
84
-
85
- test('should emit change', done => {
86
- const watcher = new Watcher();
87
-
88
- const callback = jest.fn();
89
- const watchDir = path.join(serverDir, 'change');
90
- fs.mkdirSync(watchDir);
91
-
92
- const filepath = path.join(watchDir, 'index.js');
93
- writeFiles('start', filepath);
94
-
95
- watcher.listen(
96
- [`${watchDir}/**/*`],
97
- {
98
- ignoreInitial: true,
99
- ignored: /api\/typings\/.*/,
100
- },
101
- async () => {
102
- callback();
103
- expect(callback).toHaveBeenCalledTimes(1);
104
- await watcher.close();
105
- done();
106
- },
107
- );
108
-
109
- setTimeout(() => writeFiles('end', filepath), 100);
110
- });
111
-
112
- test('should not emit change when typings file changed', done => {
113
- const watcher = new Watcher();
114
- const apiDir = path.normalize(path.join(pwd, './api'));
115
-
116
- const callback = jest.fn();
117
-
118
- if (fs.pathExistsSync(apiDir)) {
119
- fs.removeSync(apiDir);
120
- }
121
-
122
- const clear = () => {
123
- fs.removeSync(apiDir);
124
- };
125
-
126
- fs.mkdirSync(path.normalize(path.join(apiDir, 'typings')), {
127
- recursive: true,
128
- });
129
-
130
- watcher.listen(
131
- [`${apiDir}/**/*`],
132
- {
133
- ignoreInitial: true,
134
- ignored: /api\/typings\/.*/,
135
- },
136
- callback,
137
- );
138
-
139
- setTimeout(async () => {
140
- expect(callback).toHaveBeenCalledTimes(0);
141
- await watcher.close();
142
- clear();
143
- done();
144
- }, 1000);
145
- });
146
- });
147
-
148
- describe('test watcher', () => {
149
- let watcher: any;
150
- const baseDir = path.join(__dirname, 'fixtures');
151
- const watchDir = path.join(baseDir, 'watch/**');
152
- const filepath = path.join(baseDir, 'watch', 'index.ts');
153
- const filepatha = path.join(baseDir, 'watch', 'a.ts');
154
- const txt = path.join(baseDir, 'watch', 'stats.txt');
155
-
156
- afterEach(() => {
157
- if (watcher) {
158
- watcher.close();
159
- }
160
- fs.writeFileSync(txt, '1');
161
- });
162
-
163
- it('should create watcher instance correctly', resolve => {
164
- watcher = new Watcher();
165
- expect(watcher.dependencyTree).toBeNull();
166
- watcher.createDepTree();
167
- expect(watcher.dependencyTree).not.toBeNull();
168
-
169
- expect(watcher.watcher).toBeUndefined();
170
- watcher.listen([watchDir], {}, () => {
171
- // empty
172
- });
173
-
174
- expect(watcher.watcher).toBeDefined();
175
- require(filepath);
176
- expect(watcher.dependencyTree.getNode(filepath)).toBeUndefined();
177
- watcher.updateDepTree();
178
- expect(watcher.dependencyTree.getNode(filepath)).toBeDefined();
179
- watcher.cleanDepCache(filepath);
180
- expect(watcher.dependencyTree.getNode(filepath)).toBeDefined();
181
-
182
- jest.resetModules();
183
- watcher.updateDepTree();
184
- expect(watcher.dependencyTree.getNode(filepath)).toBeUndefined();
185
-
186
- setTimeout(() => {
187
- const fl = getWatchedFiles(watcher.watcher);
188
- expect(fl.includes(filepatha)).toBeTruthy();
189
- expect(fl.includes(filepath)).toBeTruthy();
190
- expect(fl.includes(txt)).toBeTruthy();
191
- resolve();
192
- }, 1000);
193
- });
194
-
195
- it('should stats cache instance work correctly', () => {
196
- const statsCache = new StatsCache();
197
-
198
- // should not exist false before add
199
- expect(statsCache.has(txt)).toBeFalsy();
200
-
201
- // should exist true after add
202
- statsCache.add([txt]);
203
- expect(statsCache.has(txt)).toBeTruthy();
204
-
205
- // should diff correctly
206
- fs.writeFileSync(txt, 'foo');
207
- expect(statsCache.isDiff(txt)).toBeTruthy();
208
-
209
- // should not diff if not refresh
210
- fs.writeFileSync(txt, '1');
211
- expect(statsCache.isDiff(txt)).toBeFalsy();
212
-
213
- // should diff after refresh
214
- fs.writeFileSync(txt, 'foo');
215
- statsCache.refresh(txt);
216
- fs.writeFileSync(txt, '1');
217
- expect(statsCache.isDiff(txt)).toBeTruthy();
218
-
219
- // should diff when content change
220
- statsCache.refresh(txt);
221
- fs.writeFileSync(txt, '2');
222
- expect(statsCache.isDiff(txt)).toBeTruthy();
223
-
224
- // should not exist after del
225
- statsCache.del(txt);
226
- expect(statsCache.has(txt)).toBeFalsy();
227
- });
228
- });