Skip to content

Commit

Permalink
fix(js): update swc options so path mappings can work in all environm…
Browse files Browse the repository at this point in the history
…ents (#16390)
  • Loading branch information
jaysoo committed Apr 19, 2023
1 parent 157b35b commit ab609a2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
9 changes: 5 additions & 4 deletions e2e/js/src/js-swc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ export function x() {

// update .swcrc to use path mappings
updateJson(`libs/${lib}/.swcrc`, (json) => {
json.jsc.baseUrl = '.';
json.jsc.paths = {
'src/*': ['src/*'],
'~/*': ['./src/*'],
};
return json;
});
Expand All @@ -144,7 +145,7 @@ export function x() {
updateFile(`libs/${lib}/src/lib/${lib}.ts`, () => {
return `
// @ts-ignore
import { x } from 'src/x';
import { x } from '~/x';
export function myLib() {
console.log(x());
Expand All @@ -154,8 +155,8 @@ myLib();
`;
});

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

// invoke the lib with node
const result = execSync(`node dist/libs/${lib}/src/lib/${lib}.js`, {
Expand Down
16 changes: 1 addition & 15 deletions packages/js/src/executors/swc/swc.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,7 @@ export function normalizeOptions(
// default to current directory if projectRootParts is [].
// Eg: when a project is at the root level, outside of layout dir
const swcCwd = projectRootParts.join('/') || '.';
let swcrcPath = getSwcrcPath(options, contextRoot, projectRoot);

try {
const swcrcContent = readJsonFile(swcrcPath) as Options;
// if we have path mappings setup but baseUrl isn't specified, then we're proceeding with the following logic
if (
swcrcContent.jsc &&
swcrcContent.jsc.paths &&
!swcrcContent.jsc.baseUrl
) {
swcrcContent.jsc.baseUrl = `./${projectDir}`;
swcrcPath = getSwcrcPath(options, contextRoot, projectRoot, true);
writeJsonFile(swcrcPath, swcrcContent);
}
} catch (e) {}
const swcrcPath = getSwcrcPath(options, contextRoot, projectRoot);

const swcCliOptions = {
srcPath: projectDir,
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/utils/swc/compile-swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function getSwcCmd(
{ swcrcPath, srcPath, destPath }: SwcCliOptions,
watch = false
) {
let swcCmd = `npx swc ${srcPath} -d ${destPath} --no-swcrc --config-file=${swcrcPath}`;
let swcCmd = `npx swc ${srcPath} -d ${destPath} --config-file=${swcrcPath}`;
return watch ? swcCmd.concat(' --watch') : swcCmd;
}

Expand Down
13 changes: 4 additions & 9 deletions packages/js/src/utils/swc/get-swcrc-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ import { SwcExecutorOptions } from '../schema';
export function getSwcrcPath(
options: SwcExecutorOptions,
contextRoot: string,
projectRoot: string,
temp = false
projectRoot: string
) {
let swcrcPath = options.swcrc ?? join(projectRoot, '.swcrc');

if (temp) {
swcrcPath = join('tmp', swcrcPath.replace('.swcrc', '.generated.swcrc'));
}

return join(contextRoot, swcrcPath);
return options.swcrc
? join(contextRoot, options.swcrc)
: join(contextRoot, projectRoot, '.swcrc');
}

0 comments on commit ab609a2

Please sign in to comment.