-
Notifications
You must be signed in to change notification settings - Fork 34
/
post-build.mjs
48 lines (46 loc) · 1.91 KB
/
post-build.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
import fs from 'fs';
import path from 'path';
import prettier from 'prettier';
import {dirFromImportMeta} from '@reskript/core';
import {getJestPresetConfig} from '../dist/index.js';
const dumpAsModule = (json, destination) => {
// 有几个东西是用了`resolve('xxx')`变成了绝对路径,在这里要换回来再替换成`require.resolve`
const modified = {
...json,
snapshotSerializers: [
'%REQUIRE(\'enzyme-to-json/serializer\')%',
],
moduleNameMapper: {
...json.moduleNameMapper,
'\\.(css|less)$': '%REQUIRE(\'identity-obj-proxy\')%',
},
transform: {
...json.transform,
'^.+\\.(md|mdx|txt|tpl)$': '%REQUIRE(\'jest-raw-loader\')%',
},
};
const jsonText = JSON.stringify(modified, null, ' ');
const body = jsonText
.replace(/"%REQUIRE\((.*)\)%"/g, 'require.resolve($1)')
.replace(/"%REQUIRE_PATH\((.*)\)%(.+)"/g, 'require.resolve($1).replace(/\\/[\\w.]+$/, \'\') + \'$2\'')
.replace(/"%RESKRIPT_NODE_MODULE_JEST_PATH%\/(.*)"/g, 'require.resolve(\'../dist/$1\')');
const moduleCode = `module.exports = ${body}`;
const prettyOptions = {
parser: 'babel',
tabWidth: 4,
singleQuote: true,
trailingComma: 'es5',
bracketSpacing: false,
};
fs.writeFileSync(
destination,
// 不要问为啥自动生成的代码还要格式化,问就是强迫症。
prettier.format(moduleCode, prettyOptions),
'utf-8'
);
};
const jestPresetConfig = target => getJestPresetConfig(target, '%RESKRIPT_NODE_MODULE_JEST_PATH%');
const destination = path.join(dirFromImportMeta(import.meta.url), '..', 'config');
fs.mkdirSync(destination, {recursive: true});
dumpAsModule(jestPresetConfig('react'), path.join(destination, 'jest-react.js'));
dumpAsModule(jestPresetConfig('node'), path.join(destination, 'jest-node.js'));