-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.mjs
198 lines (169 loc) · 6.64 KB
/
plugin.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { writeFile } from 'node:fs/promises';
import { sep, join, relative, resolve } from 'node:path';
import importFrom from 'import-from';
import { logger, optimizeTracing } from "./utils/logger.mjs";
import { convertPathToImport } from "./utils/resolve-module.mjs";
import { isBuiltinReporter } from "./utils/is-builtin-reporter.mjs";
import { mapSourceToOutputFiles } from "./utils/map-inputs-outputs.mjs";
import { moveJsFile } from "./utils/move-js-file.mjs";
import { pruneDirectory } from "./utils/prune-directory.mjs";
import { JEST_DEPENDENCIES } from "./utils/jest-dependencies.mjs";
const noop = () => {};
const __CONTENT = optimizeTracing((log, content, message) => log.trace(
typeof content === 'object'
? content
: { content },
message
));
const __FILE_MAPPING_CREATING = (log, input) => __CONTENT(log, input, 'creating file mapping');
const __FILE_MAPPING_CREATED = (log, input) => __CONTENT(log, input, 'created file mapping');
const __JEST_CONFIG = (log, config) => __CONTENT(log, config, 'create jest config');
const __PACKAGE_JSON = (log, packageJson) => __CONTENT(log, packageJson, 'create package.json');
export default ({
package: packageMiddleware,
globalConfig,
projectConfig,
tests,
useTransformer = noop,
}) => {
return {
name: 'jest',
async setup(build) {
const rootDir = resolve(globalConfig.rootDir);
const outdir = resolve(build.initialOptions.outdir);
const external = build.initialOptions.external || [];
const { createScriptTransformer } = importFrom(rootDir, '@jest/transform');
const transformer = await createScriptTransformer(projectConfig);
await useTransformer({
build,
transformer,
});
build.onEnd(async (result) => {
const mappingInput = {
rootDir,
outdir,
sourceFiles: Object.keys(result.metafile.inputs),
outputFiles: Object.keys(result.metafile.outputs),
};
__FILE_MAPPING_CREATING(logger, mappingInput);
const mapping = mapSourceToOutputFiles(mappingInput);
__FILE_MAPPING_CREATED(logger, mapping);
/**
* @param {string} file
* @returns {string}
*/
function mapFile(file) {
if (!file) {
return;
}
const out = mapping[file];
if (out) {
return convertPathToImport(outdir, out);
} else {
return convertPathToImport(rootDir, file);
}
}
/**
* @param {string} reporter
* @returns {string}
*/
function mapSingleReporter(reporter) {
return isBuiltinReporter(reporter) ? reporter : mapFile(reporter);
}
/** @param {string | [string, any]} reporter */
function mapReporter(reporter) {
return Array.isArray(reporter)
? [mapSingleReporter(reporter[0]), reporter[1]]
: mapSingleReporter(reporter);
}
/** @param {string} testRunner */
function mapTestRunner(testRunner) {
if (testRunner) {
const segments = testRunner.split(sep);
const circusIndex = segments.indexOf('jest-circus');
const jasmine2Index = segments.indexOf('jest-jasmine2');
if (circusIndex > 0 && segments[circusIndex - 1] === 'node_modules') {
return 'jest-circus/runner';
}
if (jasmine2Index > 0 && segments[jasmine2Index - 1] === 'node_modules') {
return 'jest-jasmine2';
}
}
return testRunner;
}
async function moveExternalEntryPointsBackToRoot() {
for (const [input, output] of Object.entries(mapping)) {
const newPath = redirectExternalModule(output);
if (newPath !== output) {
await moveJsFile(output, newPath);
mapping[input] = newPath;
}
}
}
function redirectExternalModule(modulePath) {
let replacements = 0;
const segments = modulePath.split(sep);
return segments.map(x => x === 'node_modules' && replacements++ === 0 ? 'bundled_modules' : x).join(sep);
}
await moveExternalEntryPointsBackToRoot();
await pruneDirectory(join(outdir, 'node_modules'));
const flattenedConfig = {
maxWorkers: globalConfig.maxWorkers,
testTimeout: globalConfig.testTimeout,
reporters: globalConfig.reporters.map(mapReporter),
...projectConfig,
cacheDirectory: undefined,
cwd: undefined,
coverageDirectory: mapFile(projectConfig.coverageDirectory),
globalSetup: mapFile(projectConfig.globalSetup),
globalTeardown: mapFile(projectConfig.globalTeardown),
id: undefined,
moduleNameMapper: undefined,
rootDir: undefined,
roots: undefined,
runner: undefined,
setupFiles: projectConfig.setupFiles.map(mapFile),
setupFilesAfterEnv: projectConfig.setupFilesAfterEnv.map(mapFile),
testEnvironment: mapFile(projectConfig.testEnvironment),
testMatch: tests.map(mapFile),
testRunner: mapTestRunner(mapFile(projectConfig.testRunner)),
transform: undefined,
transformIgnorePatterns: undefined,
};
__JEST_CONFIG(logger, flattenedConfig);
await writeFile(join(outdir, 'jest.config.json'), JSON.stringify(flattenedConfig, null, 2));
});
build.onEnd(async (result) => {
const externalDependencies = Object.fromEntries(
['jest', ...external]
.map(dep => {
if (JEST_DEPENDENCIES.includes(dep)) {
return null;
}
const packageJson = importFrom.silent(rootDir, dep + '/package.json');
return packageJson ? [dep, packageJson.version] : null;
}).filter(Boolean));
const packageJson = packageMiddleware({
name: 'bundled-tests',
version: '0.0.0',
private: true,
scripts: {
test: "NODE_OPTIONS='-r @babel/register' jest"
},
dependencies: {
"@babel/core": "^7.24.6",
"@babel/plugin-transform-modules-commonjs": "^7.24.6",
"@babel/register": "^7.24.6",
...externalDependencies,
},
});
__PACKAGE_JSON(logger, packageJson);
await writeFile(join(outdir, 'package.json'), JSON.stringify(packageJson, null, 2));
await writeFile(join(outdir, '.babelrc'), JSON.stringify({
"compact": false,
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}, null, 2));
});
},
};
};