Skip to content

Commit 037919a

Browse files
jaysooFrozenPandaz
authored andcommittedApr 25, 2023
fix(js): update swc options so path mappings can work in all environments (#16390)
(cherry picked from commit ab609a2)
1 parent b6d1cad commit 037919a

File tree

4 files changed

+11
-29
lines changed

4 files changed

+11
-29
lines changed
 

‎e2e/js/src/js-swc.test.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ export function x() {
134134

135135
// update .swcrc to use path mappings
136136
updateJson(`libs/${lib}/.swcrc`, (json) => {
137+
json.jsc.baseUrl = '.';
137138
json.jsc.paths = {
138-
'src/*': ['src/*'],
139+
'~/*': ['./src/*'],
139140
};
140141
return json;
141142
});
@@ -144,7 +145,7 @@ export function x() {
144145
updateFile(`libs/${lib}/src/lib/${lib}.ts`, () => {
145146
return `
146147
// @ts-ignore
147-
import { x } from 'src/x';
148+
import { x } from '~/x';
148149
149150
export function myLib() {
150151
console.log(x());
@@ -154,8 +155,8 @@ myLib();
154155
`;
155156
});
156157

157-
// now run build
158-
runCLI(`build ${lib}`);
158+
// now run build without type checking (since we're using path mappings not in tsconfig)
159+
runCLI(`build ${lib} --skipTypeCheck`);
159160

160161
// invoke the lib with node
161162
const result = execSync(`node dist/libs/${lib}/src/lib/${lib}.js`, {

‎packages/js/src/executors/swc/swc.impl.ts

+1-15
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,7 @@ export function normalizeOptions(
6969
// default to current directory if projectRootParts is [].
7070
// Eg: when a project is at the root level, outside of layout dir
7171
const swcCwd = projectRootParts.join('/') || '.';
72-
let swcrcPath = getSwcrcPath(options, contextRoot, projectRoot);
73-
74-
try {
75-
const swcrcContent = readJsonFile(swcrcPath) as Options;
76-
// if we have path mappings setup but baseUrl isn't specified, then we're proceeding with the following logic
77-
if (
78-
swcrcContent.jsc &&
79-
swcrcContent.jsc.paths &&
80-
!swcrcContent.jsc.baseUrl
81-
) {
82-
swcrcContent.jsc.baseUrl = `./${projectDir}`;
83-
swcrcPath = getSwcrcPath(options, contextRoot, projectRoot, true);
84-
writeJsonFile(swcrcPath, swcrcContent);
85-
}
86-
} catch (e) {}
72+
const swcrcPath = getSwcrcPath(options, contextRoot, projectRoot);
8773

8874
const swcCliOptions = {
8975
srcPath: projectDir,

‎packages/js/src/utils/swc/compile-swc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function getSwcCmd(
1010
{ swcrcPath, srcPath, destPath }: SwcCliOptions,
1111
watch = false
1212
) {
13-
let swcCmd = `npx swc ${srcPath} -d ${destPath} --no-swcrc --config-file=${swcrcPath}`;
13+
let swcCmd = `npx swc ${srcPath} -d ${destPath} --config-file=${swcrcPath}`;
1414
return watch ? swcCmd.concat(' --watch') : swcCmd;
1515
}
1616

‎packages/js/src/utils/swc/get-swcrc-path.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@ import { SwcExecutorOptions } from '../schema';
44
export function getSwcrcPath(
55
options: SwcExecutorOptions,
66
contextRoot: string,
7-
projectRoot: string,
8-
temp = false
7+
projectRoot: string
98
) {
10-
let swcrcPath = options.swcrc ?? join(projectRoot, '.swcrc');
11-
12-
if (temp) {
13-
swcrcPath = join('tmp', swcrcPath.replace('.swcrc', '.generated.swcrc'));
14-
}
15-
16-
return join(contextRoot, swcrcPath);
9+
return options.swcrc
10+
? join(contextRoot, options.swcrc)
11+
: join(contextRoot, projectRoot, '.swcrc');
1712
}

0 commit comments

Comments
 (0)
Please sign in to comment.