/
swc.ts
174 lines (150 loc) · 4.39 KB
/
swc.ts
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
import { winPath } from '@umijs/utils';
import { lstatSync } from 'fs';
import path from 'path';
import { IFatherBundlessConfig, IFatherBundlessTypes } from '../../../../types';
import {
addSourceMappingUrl,
ensureRelativePath,
getBundlessTargets,
getSWCTransformReactOpts,
} from '../../../utils';
import { IJSTransformer } from '../types';
const isTs = (p: string): boolean => p.endsWith('.ts') || p.endsWith('.tsx');
const isDirectory = (path: string) => {
try {
return lstatSync(path).isDirectory();
} catch {
return false;
}
};
/**
* transform alias to relative path for swc paths
* @param opts
* @returns {Record<string, string[]>} alias
*/
const getSWCAlias = (opts: {
fileAbsPath: string;
alias: IFatherBundlessConfig['alias'];
cwd: string;
}): Record<string, string[]> => {
const { fileAbsPath, alias = {}, cwd } = opts;
return Object.entries(alias).reduce<Record<string, string[]>>(
(result, [name, target]) => {
if (path.isAbsolute(target)) {
const isDirAlias = isDirectory(target);
let relativePath = winPath(
isDirAlias
? path.relative(cwd, target)
: path.relative(path.dirname(fileAbsPath), target),
);
relativePath = ensureRelativePath(relativePath);
// suffix * for dir alias
const aliasName = isDirAlias ? `${name}/*` : name;
const aliasPath = isDirAlias ? `${relativePath}/*` : relativePath;
// fit path omit index
// eg: ./test/index.ts => ./test
if (isDirAlias) {
result[name] = [relativePath];
}
result[aliasName] = [aliasPath];
} else {
result[name] = [target];
}
return result;
},
{},
);
};
const getModuleType = (type: IFatherBundlessTypes) => {
if (type === IFatherBundlessTypes.CJS) {
return 'commonjs';
}
return 'es6';
};
/**
* replace absolute path with relative path
*/
export const replaceAbsPathWithRelativePath = (opts: {
content: string;
cwd: string;
fileAbsPath: string;
}) => {
const cwd = winPath(opts.cwd);
const fileAbsPath = winPath(opts.fileAbsPath);
const pathRegex = new RegExp('(\'|")((\\1|.)*?)\\1', 'g');
const replacer = (oText: string, quote: string, target: string) => {
if (!target.startsWith(cwd)) {
return oText;
}
let relativePath = winPath(
path.relative(path.dirname(fileAbsPath), target),
);
relativePath = ensureRelativePath(relativePath);
return `${quote}${relativePath}${quote}`;
};
return opts.content.replace(pathRegex, replacer);
};
/**
* swc transformer
*/
const swcTransformer: IJSTransformer = async function (content) {
// swc will install on demand, so should import dynamic
const { transform }: typeof import('@swc/core') = require('@swc/core');
const { alias: oAlias = {} } = this.config;
const isTSFile = isTs(this.paths.fileAbsPath);
const isJSXFile = this.paths.fileAbsPath.endsWith('x');
// transform alias to relative path for swc paths
const alias = getSWCAlias({
fileAbsPath: this.paths.fileAbsPath,
alias: oAlias,
cwd: this.paths.cwd,
});
let { code, map } = await transform(content, {
cwd: this.paths.cwd,
filename: this.paths.fileAbsPath,
sourceFileName: this.config.sourcemap
? winPath(
path.relative(
path.dirname(this.paths.itemDistAbsPath),
this.paths.fileAbsPath,
),
)
: undefined,
sourceMaps: this.config.sourcemap,
env: {
targets: getBundlessTargets(this.config),
},
jsc: {
baseUrl: this.paths.cwd,
paths: alias,
parser: {
syntax: isTSFile ? 'typescript' : 'ecmascript',
...(isTSFile && isJSXFile ? { tsx: true } : {}),
...(!isTSFile && isJSXFile ? { jsx: true } : {}),
},
transform: {
react: getSWCTransformReactOpts(
this.pkg,
path.dirname(this.paths.fileAbsPath),
),
},
},
module: {
type: getModuleType(this.config.format),
},
});
// fix: https://github.com/swc-project/swc/issues/5165
// remove this when swc fixes it.
if (process.platform === 'win32') {
code = replaceAbsPathWithRelativePath({
content: code,
cwd: this.paths.cwd,
fileAbsPath: this.paths.fileAbsPath,
});
}
if (map) {
return [addSourceMappingUrl(code!, this.paths.itemDistAbsPath), map];
}
return [code!];
};
export default swcTransformer;