@atlaspack/core 2.12.1-canary.3354

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 (215) hide show
  1. package/LICENSE +201 -0
  2. package/index.d.ts +22 -0
  3. package/lib/AssetGraph.js +518 -0
  4. package/lib/Atlaspack.js +587 -0
  5. package/lib/AtlaspackConfig.js +298 -0
  6. package/lib/AtlaspackConfig.schema.js +125 -0
  7. package/lib/BundleGraph.js +1445 -0
  8. package/lib/CommittedAsset.js +133 -0
  9. package/lib/Dependency.js +88 -0
  10. package/lib/Environment.js +128 -0
  11. package/lib/InternalConfig.js +48 -0
  12. package/lib/PackagerRunner.js +519 -0
  13. package/lib/ReporterRunner.js +151 -0
  14. package/lib/RequestTracker.js +1089 -0
  15. package/lib/SymbolPropagation.js +625 -0
  16. package/lib/TargetDescriptor.schema.js +115 -0
  17. package/lib/Transformation.js +536 -0
  18. package/lib/UncommittedAsset.js +342 -0
  19. package/lib/Validation.js +215 -0
  20. package/lib/applyRuntimes.js +279 -0
  21. package/lib/assetUtils.js +189 -0
  22. package/lib/atlaspack-v3/AtlaspackV3.js +74 -0
  23. package/lib/atlaspack-v3/fs.js +30 -0
  24. package/lib/atlaspack-v3/index.js +19 -0
  25. package/lib/atlaspack-v3/jsCallable.js +15 -0
  26. package/lib/atlaspack-v3/plugins/Resolver.js +12 -0
  27. package/lib/atlaspack-v3/plugins/index.js +16 -0
  28. package/lib/atlaspack-v3/worker/index.js +3 -0
  29. package/lib/atlaspack-v3/worker/worker.js +30 -0
  30. package/lib/buildCache.js +18 -0
  31. package/lib/constants.js +21 -0
  32. package/lib/dumpGraphToGraphViz.js +206 -0
  33. package/lib/index.js +106 -0
  34. package/lib/loadAtlaspackPlugin.js +148 -0
  35. package/lib/loadDotEnv.js +54 -0
  36. package/lib/projectPath.js +86 -0
  37. package/lib/public/Asset.js +259 -0
  38. package/lib/public/Bundle.js +231 -0
  39. package/lib/public/BundleGraph.js +193 -0
  40. package/lib/public/BundleGroup.js +44 -0
  41. package/lib/public/Config.js +202 -0
  42. package/lib/public/Dependency.js +131 -0
  43. package/lib/public/Environment.js +244 -0
  44. package/lib/public/MutableBundleGraph.js +184 -0
  45. package/lib/public/PluginOptions.js +71 -0
  46. package/lib/public/Symbols.js +247 -0
  47. package/lib/public/Target.js +64 -0
  48. package/lib/registerCoreWithSerializer.js +45 -0
  49. package/lib/requests/AssetGraphRequest.js +427 -0
  50. package/lib/requests/AssetGraphRequestRust.js +187 -0
  51. package/lib/requests/AssetRequest.js +137 -0
  52. package/lib/requests/AtlaspackBuildRequest.js +78 -0
  53. package/lib/requests/AtlaspackConfigRequest.js +473 -0
  54. package/lib/requests/BundleGraphRequest.js +419 -0
  55. package/lib/requests/ConfigRequest.js +198 -0
  56. package/lib/requests/DevDepRequest.js +166 -0
  57. package/lib/requests/EntryRequest.js +295 -0
  58. package/lib/requests/PackageRequest.js +88 -0
  59. package/lib/requests/PathRequest.js +349 -0
  60. package/lib/requests/TargetRequest.js +1177 -0
  61. package/lib/requests/ValidationRequest.js +66 -0
  62. package/lib/requests/WriteBundleRequest.js +252 -0
  63. package/lib/requests/WriteBundlesRequest.js +153 -0
  64. package/lib/resolveOptions.js +234 -0
  65. package/lib/serializer.js +216 -0
  66. package/lib/serializerCore.browser.js +29 -0
  67. package/lib/serializerCore.js +16 -0
  68. package/lib/summarizeRequest.js +55 -0
  69. package/lib/types.js +35 -0
  70. package/lib/utils.js +160 -0
  71. package/lib/worker.js +170 -0
  72. package/package.json +59 -0
  73. package/src/AssetGraph.js +646 -0
  74. package/src/Atlaspack.js +632 -0
  75. package/src/AtlaspackConfig.js +490 -0
  76. package/src/AtlaspackConfig.schema.js +141 -0
  77. package/src/BundleGraph.js +2193 -0
  78. package/src/CommittedAsset.js +143 -0
  79. package/src/Dependency.js +142 -0
  80. package/src/Environment.js +151 -0
  81. package/src/InternalConfig.js +72 -0
  82. package/src/PackagerRunner.js +790 -0
  83. package/src/ReporterRunner.js +158 -0
  84. package/src/RequestTracker.js +1697 -0
  85. package/src/SymbolPropagation.js +794 -0
  86. package/src/TargetDescriptor.schema.js +136 -0
  87. package/src/Transformation.js +752 -0
  88. package/src/UncommittedAsset.js +426 -0
  89. package/src/Validation.js +223 -0
  90. package/src/applyRuntimes.js +341 -0
  91. package/src/assetUtils.js +254 -0
  92. package/src/atlaspack-v3/AtlaspackV3.js +73 -0
  93. package/src/atlaspack-v3/fs.js +36 -0
  94. package/src/atlaspack-v3/index.js +5 -0
  95. package/src/atlaspack-v3/jsCallable.js +13 -0
  96. package/src/atlaspack-v3/plugins/Resolver.js +9 -0
  97. package/src/atlaspack-v3/plugins/index.js +3 -0
  98. package/src/atlaspack-v3/worker/index.js +8 -0
  99. package/src/atlaspack-v3/worker/worker.js +14 -0
  100. package/src/buildCache.js +15 -0
  101. package/src/constants.js +22 -0
  102. package/src/dumpGraphToGraphViz.js +244 -0
  103. package/src/index.js +22 -0
  104. package/src/loadAtlaspackPlugin.js +239 -0
  105. package/src/loadDotEnv.js +56 -0
  106. package/src/projectPath.js +87 -0
  107. package/src/public/Asset.js +359 -0
  108. package/src/public/Bundle.js +337 -0
  109. package/src/public/BundleGraph.js +335 -0
  110. package/src/public/BundleGroup.js +55 -0
  111. package/src/public/Config.js +261 -0
  112. package/src/public/Dependency.js +176 -0
  113. package/src/public/Environment.js +316 -0
  114. package/src/public/MutableBundleGraph.js +320 -0
  115. package/src/public/PluginOptions.js +99 -0
  116. package/src/public/Symbols.js +315 -0
  117. package/src/public/Target.js +71 -0
  118. package/src/registerCoreWithSerializer.js +34 -0
  119. package/src/requests/AssetGraphRequest.js +577 -0
  120. package/src/requests/AssetGraphRequestRust.js +222 -0
  121. package/src/requests/AssetRequest.js +179 -0
  122. package/src/requests/AtlaspackBuildRequest.js +109 -0
  123. package/src/requests/AtlaspackConfigRequest.js +709 -0
  124. package/src/requests/BundleGraphRequest.js +547 -0
  125. package/src/requests/ConfigRequest.js +287 -0
  126. package/src/requests/DevDepRequest.js +246 -0
  127. package/src/requests/EntryRequest.js +385 -0
  128. package/src/requests/PackageRequest.js +105 -0
  129. package/src/requests/PathRequest.js +454 -0
  130. package/src/requests/TargetRequest.js +1632 -0
  131. package/src/requests/ValidationRequest.js +79 -0
  132. package/src/requests/WriteBundleRequest.js +362 -0
  133. package/src/requests/WriteBundlesRequest.js +209 -0
  134. package/src/resolveOptions.js +278 -0
  135. package/src/serializer.js +255 -0
  136. package/src/serializerCore.browser.js +8 -0
  137. package/src/serializerCore.js +5 -0
  138. package/src/summarizeRequest.js +47 -0
  139. package/src/types.js +581 -0
  140. package/src/utils.js +211 -0
  141. package/src/worker.js +189 -0
  142. package/test/AssetGraph.test.js +679 -0
  143. package/test/Atlaspack.test.js +142 -0
  144. package/test/AtlaspackConfig.test.js +405 -0
  145. package/test/AtlaspackConfigRequest.test.js +993 -0
  146. package/test/BundleGraph.test.js +121 -0
  147. package/test/EntryRequest.test.js +215 -0
  148. package/test/Environment.test.js +99 -0
  149. package/test/InternalAsset.test.js +76 -0
  150. package/test/PackagerRunner.test.js +27 -0
  151. package/test/PublicAsset.test.js +40 -0
  152. package/test/PublicBundle.test.js +68 -0
  153. package/test/PublicDependency.test.js +22 -0
  154. package/test/PublicEnvironment.test.js +27 -0
  155. package/test/PublicMutableBundleGraph.test.js +192 -0
  156. package/test/RequestTracker.test.js +448 -0
  157. package/test/SymbolPropagation.test.js +716 -0
  158. package/test/TargetRequest.test.js +1715 -0
  159. package/test/fixtures/application-targets/package.json +3 -0
  160. package/test/fixtures/atlaspack/index.js +1 -0
  161. package/test/fixtures/atlaspack/other.js +3 -0
  162. package/test/fixtures/atlaspack/package.json +1 -0
  163. package/test/fixtures/atlaspack/yarn.lock +0 -0
  164. package/test/fixtures/bundle.js +10 -0
  165. package/test/fixtures/common-targets/package.json +24 -0
  166. package/test/fixtures/common-targets-ignore/package.json +13 -0
  167. package/test/fixtures/config/.atlaspackrc +6 -0
  168. package/test/fixtures/config/subfolder/.atlaspackrc +6 -0
  169. package/test/fixtures/config-extends-not-found/.atlaspackrc +3 -0
  170. package/test/fixtures/config-extends-not-found/.atlaspackrc-json5 +3 -0
  171. package/test/fixtures/config-extends-not-found/.atlaspackrc-multiple +3 -0
  172. package/test/fixtures/config-extends-not-found/.atlaspackrc-node-modules +3 -0
  173. package/test/fixtures/config-malformed/.atlaspackrc +3 -0
  174. package/test/fixtures/config-node-pipeline/.atlaspackrc +6 -0
  175. package/test/fixtures/config-plugin-not-found/.atlaspackrc +6 -0
  176. package/test/fixtures/context/package.json +8 -0
  177. package/test/fixtures/custom-format-infer-ext/package.json +6 -0
  178. package/test/fixtures/custom-format-infer-type/package.json +7 -0
  179. package/test/fixtures/custom-format-mismatch/package.json +8 -0
  180. package/test/fixtures/custom-targets/package.json +20 -0
  181. package/test/fixtures/custom-targets-distdir/package.json +11 -0
  182. package/test/fixtures/duplicate-targets/package.json +4 -0
  183. package/test/fixtures/glob-like/[entry].js +0 -0
  184. package/test/fixtures/invalid-distpath/package.json +11 -0
  185. package/test/fixtures/invalid-engines/package.json +12 -0
  186. package/test/fixtures/invalid-source-missing/package.json +5 -0
  187. package/test/fixtures/invalid-source-not-file/package.json +5 -0
  188. package/test/fixtures/invalid-source-not-file/src/index.js +1 -0
  189. package/test/fixtures/invalid-target-source-missing/package.json +9 -0
  190. package/test/fixtures/invalid-target-source-not-file/package.json +9 -0
  191. package/test/fixtures/invalid-target-source-not-file/src/index.js +1 -0
  192. package/test/fixtures/invalid-targets/package.json +19 -0
  193. package/test/fixtures/library-custom-scopehoist/package.json +9 -0
  194. package/test/fixtures/library-scopehoist/package.json +8 -0
  195. package/test/fixtures/local-plugin-config-pkg/.atlaspackrc +3 -0
  196. package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/index.json +8 -0
  197. package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/local-plugin.js +7 -0
  198. package/test/fixtures/local-plugin-config-pkg/node_modules/atlaspack-config-local/package.json +7 -0
  199. package/test/fixtures/main-format-mismatch/package.json +8 -0
  200. package/test/fixtures/main-global/package.json +8 -0
  201. package/test/fixtures/main-mjs/package.json +8 -0
  202. package/test/fixtures/module-a.js +1 -0
  203. package/test/fixtures/module-b.js +1 -0
  204. package/test/fixtures/plugins/local-plugin.js +7 -0
  205. package/test/fixtures/plugins/node_modules/atlaspack-transformer-bad-engines/index.js +7 -0
  206. package/test/fixtures/plugins/node_modules/atlaspack-transformer-bad-engines/package.json +7 -0
  207. package/test/fixtures/plugins/node_modules/atlaspack-transformer-no-engines/index.js +7 -0
  208. package/test/fixtures/plugins/node_modules/atlaspack-transformer-no-engines/package.json +4 -0
  209. package/test/fixtures/targets-default-distdir-none/package.json +5 -0
  210. package/test/fixtures/targets-default-distdir-one/package.json +8 -0
  211. package/test/fixtures/targets-default-distdir-two/package.json +18 -0
  212. package/test/requests/ConfigRequest.test.js +254 -0
  213. package/test/serializer.test.js +302 -0
  214. package/test/test-utils.js +80 -0
  215. package/test/utils.test.js +43 -0
@@ -0,0 +1,254 @@
1
+ // @flow strict-local
2
+
3
+ import WorkerFarm from '@atlaspack/workers';
4
+ import path from 'path';
5
+ import assert from 'assert';
6
+ import sinon from 'sinon';
7
+ import {MemoryFS} from '@atlaspack/fs';
8
+ import {hashString} from '@atlaspack/rust';
9
+
10
+ import type {
11
+ ConfigRequest,
12
+ ConfigRequestResult,
13
+ } from '../../src/requests/ConfigRequest';
14
+ import type {RunAPI} from '../../src/RequestTracker';
15
+ import {runConfigRequest} from '../../src/requests/ConfigRequest';
16
+ import {toProjectPath} from '../../src/projectPath';
17
+
18
+ // $FlowFixMe unclear-type forgive me
19
+ const mockCast = (f: any): any => f;
20
+
21
+ describe('ConfigRequest tests', () => {
22
+ const projectRoot = 'project_root';
23
+ const farm = new WorkerFarm({
24
+ workerPath: require.resolve('../../src/worker.js'),
25
+ maxConcurrentWorkers: 1,
26
+ });
27
+ let fs = new MemoryFS(farm);
28
+ beforeEach(() => {
29
+ fs = new MemoryFS(farm);
30
+ });
31
+
32
+ const getMockRunApi = (
33
+ options: mixed = {projectRoot, inputFS: fs},
34
+ ): RunAPI<ConfigRequestResult> => {
35
+ const mockRunApi = {
36
+ storeResult: sinon.spy(),
37
+ canSkipSubrequest: sinon.spy(),
38
+ invalidateOnFileCreate: sinon.spy(),
39
+ getInvalidSubRequests: sinon.spy(),
40
+ getInvalidations: sinon.spy(),
41
+ getPreviousResult: sinon.spy(),
42
+ getRequestResult: sinon.spy(),
43
+ getSubRequests: sinon.spy(),
44
+ invalidateOnBuild: sinon.spy(),
45
+ invalidateOnConfigKeyChange: sinon.spy(),
46
+ invalidateOnEnvChange: sinon.spy(),
47
+ invalidateOnFileDelete: sinon.spy(),
48
+ invalidateOnFileUpdate: sinon.spy(),
49
+ invalidateOnOptionChange: sinon.spy(),
50
+ invalidateOnStartup: sinon.spy(),
51
+ runRequest: sinon.spy(request => {
52
+ return request.run({
53
+ api: mockRunApi,
54
+ options,
55
+ });
56
+ }),
57
+ };
58
+ return mockRunApi;
59
+ };
60
+
61
+ const baseRequest: ConfigRequest = {
62
+ id: 'config_request_test',
63
+ invalidateOnBuild: false,
64
+ invalidateOnConfigKeyChange: [],
65
+ invalidateOnFileCreate: [],
66
+ invalidateOnEnvChange: new Set(),
67
+ invalidateOnOptionChange: new Set(),
68
+ invalidateOnStartup: false,
69
+ invalidateOnFileChange: new Set(),
70
+ };
71
+
72
+ it('can execute a config request', async () => {
73
+ const mockRunApi = getMockRunApi();
74
+ await runConfigRequest(mockRunApi, {
75
+ ...baseRequest,
76
+ });
77
+ });
78
+
79
+ it('forwards "invalidateOnFileChange" calls to runAPI', async () => {
80
+ const mockRunApi = getMockRunApi();
81
+ await runConfigRequest(mockRunApi, {
82
+ ...baseRequest,
83
+ invalidateOnFileChange: new Set([
84
+ toProjectPath(projectRoot, 'path1'),
85
+ toProjectPath(projectRoot, 'path2'),
86
+ ]),
87
+ });
88
+
89
+ assert(
90
+ mockCast(mockRunApi.invalidateOnFileUpdate).called,
91
+ 'Invalidate was called',
92
+ );
93
+ assert(
94
+ mockCast(mockRunApi.invalidateOnFileUpdate).calledWith('path1'),
95
+ 'Invalidate was called with path1',
96
+ );
97
+ assert(
98
+ mockCast(mockRunApi.invalidateOnFileUpdate).calledWith('path2'),
99
+ 'Invalidate was called with path2',
100
+ );
101
+ assert(
102
+ mockCast(mockRunApi.invalidateOnFileDelete).calledWith('path1'),
103
+ 'Invalidate was called with path1',
104
+ );
105
+ assert(
106
+ mockCast(mockRunApi.invalidateOnFileDelete).calledWith('path2'),
107
+ 'Invalidate was called with path2',
108
+ );
109
+ });
110
+
111
+ it('forwards "invalidateOnFileCreate" calls to runAPI', async () => {
112
+ const mockRunApi = getMockRunApi();
113
+ await runConfigRequest(mockRunApi, {
114
+ ...baseRequest,
115
+ invalidateOnFileCreate: [
116
+ {filePath: toProjectPath(projectRoot, 'filePath')},
117
+ {glob: toProjectPath(projectRoot, 'glob')},
118
+ {
119
+ fileName: 'package.json',
120
+ aboveFilePath: toProjectPath(projectRoot, 'fileAbove'),
121
+ },
122
+ ],
123
+ });
124
+
125
+ assert(
126
+ mockCast(mockRunApi.invalidateOnFileCreate).called,
127
+ 'Invalidate was called',
128
+ );
129
+ assert(
130
+ mockCast(mockRunApi.invalidateOnFileCreate).calledWithMatch({
131
+ filePath: 'filePath',
132
+ }),
133
+ 'Invalidate was called for path',
134
+ );
135
+ assert(
136
+ mockCast(mockRunApi.invalidateOnFileCreate).calledWithMatch({
137
+ glob: 'glob',
138
+ }),
139
+ 'Invalidate was called for glob',
140
+ );
141
+ assert(
142
+ mockCast(mockRunApi.invalidateOnFileCreate).calledWithMatch({
143
+ fileName: 'package.json',
144
+ aboveFilePath: 'fileAbove',
145
+ }),
146
+ 'Invalidate was called for fileAbove',
147
+ );
148
+ });
149
+
150
+ it('forwards "invalidateOnEnvChange" calls to runAPI', async () => {
151
+ const mockRunApi = getMockRunApi();
152
+ await runConfigRequest(mockRunApi, {
153
+ ...baseRequest,
154
+ invalidateOnEnvChange: new Set(['env1', 'env2']),
155
+ });
156
+
157
+ assert(
158
+ mockCast(mockRunApi.invalidateOnEnvChange).called,
159
+ 'Invalidate was called',
160
+ );
161
+ assert(
162
+ mockCast(mockRunApi.invalidateOnEnvChange).calledWithMatch('env1'),
163
+ 'Invalidate was called for env1',
164
+ );
165
+ assert(
166
+ mockCast(mockRunApi.invalidateOnEnvChange).calledWithMatch('env2'),
167
+ 'Invalidate was called for env1',
168
+ );
169
+ });
170
+
171
+ it('forwards "invalidateOnOptionChange" calls to runAPI', async () => {
172
+ const mockRunApi = getMockRunApi();
173
+ await runConfigRequest(mockRunApi, {
174
+ ...baseRequest,
175
+ invalidateOnOptionChange: new Set(['option1', 'option2']),
176
+ });
177
+
178
+ assert(
179
+ mockCast(mockRunApi.invalidateOnOptionChange).called,
180
+ 'Invalidate was called',
181
+ );
182
+ assert(
183
+ mockCast(mockRunApi.invalidateOnOptionChange).calledWithMatch('option1'),
184
+ 'Invalidate was called for option1',
185
+ );
186
+ assert(
187
+ mockCast(mockRunApi.invalidateOnOptionChange).calledWithMatch('option2'),
188
+ 'Invalidate was called for option2',
189
+ );
190
+ });
191
+
192
+ it('forwards "invalidateOnStartup" calls to runAPI', async () => {
193
+ const mockRunApi = getMockRunApi();
194
+ await runConfigRequest(mockRunApi, {
195
+ ...baseRequest,
196
+ invalidateOnStartup: true,
197
+ });
198
+
199
+ assert(
200
+ mockCast(mockRunApi.invalidateOnStartup).called,
201
+ 'Invalidate was called',
202
+ );
203
+ });
204
+
205
+ it('forwards "invalidateOnBuild" calls to runAPI', async () => {
206
+ const mockRunApi = getMockRunApi();
207
+ await runConfigRequest(mockRunApi, {
208
+ ...baseRequest,
209
+ invalidateOnBuild: true,
210
+ });
211
+
212
+ assert(
213
+ mockCast(mockRunApi.invalidateOnBuild).called,
214
+ 'Invalidate was called',
215
+ );
216
+ });
217
+
218
+ it('forwards "invalidateOnConfigKeyChange" calls to runAPI', async () => {
219
+ await fs.mkdirp('/project_root');
220
+ await fs.writeFile(
221
+ '/project_root/config.json',
222
+ JSON.stringify({key1: 'value1'}),
223
+ );
224
+ sinon.spy(fs, 'readFile');
225
+ sinon.spy(fs, 'readFileSync');
226
+ const mockRunApi = getMockRunApi();
227
+ await runConfigRequest(mockRunApi, {
228
+ ...baseRequest,
229
+ invalidateOnConfigKeyChange: [
230
+ {
231
+ configKey: 'key1',
232
+ filePath: toProjectPath(
233
+ projectRoot,
234
+ path.join('project_root', 'config.json'),
235
+ ),
236
+ },
237
+ ],
238
+ });
239
+
240
+ const fsCall = mockCast(fs).readFile.getCall(0);
241
+ assert.deepEqual(
242
+ fsCall?.args,
243
+ [path.join('project_root', 'config.json'), 'utf8'],
244
+ 'readFile was called',
245
+ );
246
+
247
+ const call = mockCast(mockRunApi.invalidateOnConfigKeyChange).getCall(0);
248
+ assert.deepEqual(
249
+ call.args,
250
+ ['config.json', 'key1', hashString('"value1"')],
251
+ 'Invalidate was called for key1',
252
+ );
253
+ });
254
+ });
@@ -0,0 +1,302 @@
1
+ // @flow
2
+ import {
3
+ serialize,
4
+ deserialize,
5
+ registerSerializableClass,
6
+ unregisterSerializableClass,
7
+ } from '../src/serializer';
8
+ import assert from 'assert';
9
+ import sinon from 'sinon';
10
+
11
+ describe('serializer', () => {
12
+ it('should serialize a basic object', () => {
13
+ let serialized = serialize({foo: 2, bar: 3});
14
+ assert(Buffer.isBuffer(serialized));
15
+ let deserialized = deserialize(serialized);
16
+ assert.equal(typeof deserialized, 'object');
17
+ assert.deepEqual(deserialized, {foo: 2, bar: 3});
18
+ });
19
+
20
+ it('should serialize an object with multiple references', () => {
21
+ let a = {foo: 2};
22
+ let b = {bar: a, baz: a};
23
+ let res = deserialize(serialize(b));
24
+ assert.deepEqual(res, b);
25
+ assert.equal(res.bar, res.baz);
26
+ });
27
+
28
+ it('should serialize a cyclic object', () => {
29
+ let a = {foo: 2, bar: {}};
30
+ a.bar = a;
31
+ let res = deserialize(serialize(a));
32
+ assert.deepEqual(res, a);
33
+ assert.equal(res.bar, res);
34
+ assert.equal(a.bar, a);
35
+ });
36
+
37
+ it('should serialize a Map', () => {
38
+ let a = new Map([[2, 3]]);
39
+ let res = deserialize(serialize(a));
40
+ assert(res instanceof Map);
41
+ assert.equal(res.get(2), 3);
42
+ });
43
+
44
+ it('should serialize a Set', () => {
45
+ let a = new Set([2, 3]);
46
+ let res = deserialize(serialize(a));
47
+ assert(res instanceof Set);
48
+ assert(res.has(2));
49
+ assert(res.has(3));
50
+ });
51
+
52
+ it('should serialize a class', () => {
53
+ class Test {
54
+ x: number;
55
+ constructor(x: number) {
56
+ this.x = x;
57
+ }
58
+ }
59
+
60
+ registerSerializableClass('Test', Test);
61
+
62
+ let x = new Test(2);
63
+ let res = deserialize(serialize(x));
64
+ assert(res instanceof Test);
65
+ assert.equal(res.x, x.x);
66
+
67
+ unregisterSerializableClass('Test', Test);
68
+ });
69
+
70
+ it('should serialize a class with a custom serialize method', () => {
71
+ class Test {
72
+ x: number;
73
+ constructor(x: number) {
74
+ this.x = x;
75
+ }
76
+
77
+ serialize() {
78
+ return {
79
+ x: this.x,
80
+ serialized: true,
81
+ };
82
+ }
83
+ }
84
+
85
+ registerSerializableClass('Test', Test);
86
+
87
+ let x = new Test(2);
88
+ let res = deserialize(serialize(x));
89
+ assert(res instanceof Test);
90
+ assert.equal(res.x, x.x);
91
+ assert.equal(res.serialized, true);
92
+
93
+ unregisterSerializableClass('Test', Test);
94
+ });
95
+
96
+ it('should serialize a class with a custom deserialize method', () => {
97
+ class Test {
98
+ x: number;
99
+ constructor(x: number) {
100
+ this.x = x;
101
+ }
102
+
103
+ static deserialize(x: any) {
104
+ return {
105
+ deserialized: true,
106
+ value: x,
107
+ };
108
+ }
109
+ }
110
+
111
+ registerSerializableClass('Test', Test);
112
+
113
+ let x = new Test(2);
114
+ let res = deserialize(serialize(x));
115
+ assert(!(res instanceof Test));
116
+ assert.equal(res.value.x, x.x);
117
+ assert.equal(res.deserialized, true);
118
+
119
+ unregisterSerializableClass('Test', Test);
120
+ });
121
+
122
+ it('should serialize a class recursively', () => {
123
+ class Foo {
124
+ x: number;
125
+ constructor(x: number) {
126
+ this.x = x;
127
+ }
128
+ }
129
+
130
+ class Bar {
131
+ foo: Foo;
132
+ constructor(foo: Foo) {
133
+ this.foo = foo;
134
+ }
135
+ }
136
+
137
+ registerSerializableClass('Foo', Foo);
138
+ registerSerializableClass('Bar', Bar);
139
+
140
+ let x = new Bar(new Foo(2));
141
+ let res = deserialize(serialize(x));
142
+ assert(res instanceof Bar);
143
+ assert(res.foo instanceof Foo);
144
+ assert.equal(res.foo.x, 2);
145
+
146
+ unregisterSerializableClass('Foo', Foo);
147
+ unregisterSerializableClass('Bar', Bar);
148
+ });
149
+
150
+ it('should serialize a cyclic class', () => {
151
+ class Foo {
152
+ x: ?Foo;
153
+ constructor(x: ?Foo) {
154
+ this.x = x;
155
+ }
156
+ }
157
+
158
+ registerSerializableClass('Foo', Foo);
159
+
160
+ let x = new Foo();
161
+ x.x = x;
162
+
163
+ let res = deserialize(serialize(x));
164
+ assert(res instanceof Foo);
165
+ assert(res.x instanceof Foo);
166
+ assert.equal(res.x, res);
167
+
168
+ assert.equal(x.x, x);
169
+
170
+ unregisterSerializableClass('Foo', Foo);
171
+ });
172
+
173
+ it('should copy on write', () => {
174
+ class Foo {
175
+ x: number;
176
+ constructor(x: number) {
177
+ this.x = x;
178
+ }
179
+ }
180
+
181
+ registerSerializableClass('Foo', Foo);
182
+
183
+ let x = {y: {foo: new Foo(2)}};
184
+
185
+ let res = deserialize(serialize(x));
186
+ assert(res.y.foo instanceof Foo);
187
+ assert(x.y.foo instanceof Foo);
188
+
189
+ unregisterSerializableClass('Foo', Foo);
190
+ });
191
+
192
+ it('should serialize a cyclic class and copy on write', () => {
193
+ class Foo {
194
+ x: ?Foo;
195
+ constructor(x: ?Foo) {
196
+ this.x = x;
197
+ }
198
+ }
199
+
200
+ registerSerializableClass('Foo', Foo);
201
+
202
+ let x = new Foo();
203
+ x.x = x;
204
+ let y = {x: {y: x}};
205
+
206
+ let res = deserialize(serialize(y));
207
+ assert(res.x.y instanceof Foo);
208
+ assert(res.x.y.x instanceof Foo);
209
+ assert(y.x.y instanceof Foo);
210
+ assert(y.x.y.x instanceof Foo);
211
+ assert.equal(res.x.y.x, res.x.y);
212
+
213
+ assert.equal(x.x, x);
214
+
215
+ unregisterSerializableClass('Foo', Foo);
216
+ });
217
+
218
+ it('should serialize a class inside a Map', () => {
219
+ class Test {
220
+ x: number;
221
+ constructor(x: number) {
222
+ this.x = x;
223
+ }
224
+ }
225
+
226
+ registerSerializableClass('Test', Test);
227
+
228
+ let x = new Map([[2, new Test(2)]]);
229
+ let res = deserialize(serialize(x));
230
+ assert(res instanceof Map);
231
+ assert(res.get(2) instanceof Test);
232
+
233
+ unregisterSerializableClass('Test', Test);
234
+ });
235
+
236
+ it('should serialize a class inside a Set', () => {
237
+ class Test {
238
+ x: number;
239
+ constructor(x: number) {
240
+ this.x = x;
241
+ }
242
+ }
243
+
244
+ registerSerializableClass('Test', Test);
245
+
246
+ let x = new Set([new Test(2)]);
247
+ let res = deserialize(serialize(x));
248
+ assert(res instanceof Set);
249
+ assert(res.values().next().value instanceof Test);
250
+
251
+ unregisterSerializableClass('Test', Test);
252
+ });
253
+
254
+ describe('raw values', () => {
255
+ class Outer {
256
+ inner: Inner;
257
+
258
+ constructor(inner) {
259
+ this.inner = inner;
260
+ }
261
+
262
+ serialize() {
263
+ return {
264
+ $$raw: true,
265
+ inner: this.inner,
266
+ };
267
+ }
268
+ }
269
+
270
+ class Inner {
271
+ x: number;
272
+
273
+ constructor(x) {
274
+ this.x = x;
275
+ }
276
+
277
+ static deserialize = sinon.spy();
278
+ }
279
+
280
+ beforeEach(() => {
281
+ registerSerializableClass('Outer', Outer);
282
+ registerSerializableClass('Inner', Inner);
283
+ });
284
+
285
+ afterEach(() => {
286
+ unregisterSerializableClass('Outer', Outer);
287
+ unregisterSerializableClass('Inner', Inner);
288
+ });
289
+
290
+ it('should not recursively serialize raw values', () => {
291
+ let res = deserialize(serialize(new Outer(new Inner(42))));
292
+ assert(res instanceof Outer);
293
+ assert(!(res.inner instanceof Inner));
294
+ assert.equal(res.inner.x, 42);
295
+ });
296
+
297
+ it('should not recursively deserialize raw values', () => {
298
+ deserialize(serialize(new Outer(new Inner(42))));
299
+ assert(Inner.deserialize.notCalled);
300
+ });
301
+ });
302
+ });
@@ -0,0 +1,80 @@
1
+ // @flow strict-local
2
+
3
+ import type {Environment, AtlaspackOptions, Target} from '../src/types';
4
+
5
+ import {DEFAULT_FEATURE_FLAGS} from '@atlaspack/feature-flags';
6
+ import {FSCache} from '@atlaspack/cache';
7
+ import tempy from 'tempy';
8
+ import path from 'path';
9
+ import {inputFS, outputFS} from '@atlaspack/test-utils';
10
+ import {relativePath} from '@atlaspack/utils';
11
+ import {NodePackageManager} from '@atlaspack/package-manager';
12
+ import {createEnvironment} from '../src/Environment';
13
+ import {toProjectPath} from '../src/projectPath';
14
+
15
+ let cacheDir = tempy.directory();
16
+ export let cache: FSCache = new FSCache(outputFS, cacheDir);
17
+ cache.ensure();
18
+
19
+ export const DEFAULT_OPTIONS: AtlaspackOptions = {
20
+ cacheDir: path.join(__dirname, '.atlaspack-cache'),
21
+ atlaspackVersion: '',
22
+ watchDir: __dirname,
23
+ watchIgnore: undefined,
24
+ watchBackend: undefined,
25
+ entries: [],
26
+ logLevel: 'info',
27
+ targets: undefined,
28
+ projectRoot: __dirname,
29
+ shouldAutoInstall: false,
30
+ hmrOptions: undefined,
31
+ shouldContentHash: true,
32
+ shouldBuildLazily: false,
33
+ lazyIncludes: [],
34
+ lazyExcludes: [],
35
+ shouldBundleIncrementally: true,
36
+ serveOptions: false,
37
+ mode: 'development',
38
+ env: {},
39
+ shouldDisableCache: false,
40
+ shouldProfile: false,
41
+ shouldTrace: false,
42
+ inputFS,
43
+ outputFS,
44
+ cache,
45
+ shouldPatchConsole: false,
46
+ packageManager: new NodePackageManager(inputFS, '/'),
47
+ additionalReporters: [],
48
+ instanceId: 'test',
49
+ defaultTargetOptions: {
50
+ shouldScopeHoist: false,
51
+ shouldOptimize: false,
52
+ publicUrl: '/',
53
+ distDir: undefined,
54
+ sourceMaps: false,
55
+ },
56
+ featureFlags: {
57
+ ...DEFAULT_FEATURE_FLAGS,
58
+ },
59
+ };
60
+
61
+ export const DEFAULT_ENV: Environment = createEnvironment({
62
+ context: 'browser',
63
+ engines: {
64
+ browsers: ['> 1%'],
65
+ },
66
+ });
67
+
68
+ export const DEFAULT_TARGETS: Array<Target> = [
69
+ {
70
+ name: 'test',
71
+ distDir: toProjectPath('/', '/dist'),
72
+ distEntry: 'out.js',
73
+ env: DEFAULT_ENV,
74
+ publicUrl: '/',
75
+ },
76
+ ];
77
+
78
+ export function relative(f: string): string {
79
+ return relativePath(__dirname, f, false);
80
+ }
@@ -0,0 +1,43 @@
1
+ // @flow strict-local
2
+
3
+ import assert from 'assert';
4
+ import {getPublicId} from '../src/utils';
5
+
6
+ const id = '0123456789abcdef0123456789abcdef';
7
+ const fullPublicId = '296TIIbB3u904riPYGPJJ';
8
+
9
+ describe('getPublicId', () => {
10
+ it('only accepts 32-character hexadecimal strings', () => {
11
+ assert.throws(() => {
12
+ getPublicId('abc', () => false);
13
+ });
14
+
15
+ let notHexadecimal = 'abcdefghiklmnopqrstuvwxyz1234567';
16
+ assert.equal(notHexadecimal.length, 32);
17
+ assert.throws(() => {
18
+ getPublicId(notHexadecimal, () => false);
19
+ });
20
+ });
21
+
22
+ it('if no collisions, returns the first 5 base62 characters of value represented by the input', () => {
23
+ assert.equal(
24
+ getPublicId(id, () => false),
25
+ fullPublicId.slice(0, 5),
26
+ );
27
+ });
28
+
29
+ it('uses more characters if there is a collision', () => {
30
+ assert.equal(
31
+ getPublicId(id, publicId =>
32
+ [fullPublicId.slice(0, 5), fullPublicId.slice(0, 6)].includes(publicId),
33
+ ),
34
+ fullPublicId.slice(0, 7),
35
+ );
36
+ });
37
+
38
+ it('fails if all characters collide', () => {
39
+ assert.throws(() => {
40
+ getPublicId(id, () => true);
41
+ });
42
+ });
43
+ });