Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(js): adjust .swcrc so that it works with swc/jest and no temp .sw…
…crc needed (with migrations) (#9198) * fix(js): remove hardcoded source-maps and add default value to sourceMaps in swcrc ISSUES CLOSED: #9187 * fix(js): rename .swcrc to .lib.swcrc (with migrations) * fix(js): revert jest config js tmpl in jest generator and replace jest config manually in JS generator * chore(js): update snapshot Co-authored-by: Chau Tran <ctran@Chaus-MacBook-Pro.local>
- Loading branch information
Showing
20 changed files
with
205 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/js/src/generators/library/files/jest-config/jest.config.js__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const fs = require('fs'); | ||
|
||
// Reading the SWC compilation config and remove the "exclude" | ||
// for the test files to be compiled by SWC | ||
const { exclude: _, ...swcJestConfig } = JSON.parse( | ||
fs.readFileSync(`${__dirname}/.lib.swcrc`, 'utf-8') | ||
); | ||
|
||
module.exports = { | ||
displayName: '<%= project %>', | ||
preset: '<%= offsetFromRoot %>jest.preset.js', | ||
transform: { | ||
'^.+\\.[tj]s$': ['@swc/jest', swcJestConfig], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '<%= offsetFromRoot %>coverage/<%= projectRoot %>' | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/js/src/migrations/update-13-8-5/update-swcrc.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { | ||
ProjectConfiguration, | ||
readJson, | ||
readProjectConfiguration, | ||
Tree, | ||
updateProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { libraryGenerator } from '@nrwl/workspace'; | ||
import { defaultExclude } from '../../utils/swc/add-swc-config'; | ||
import update from './update-swcrc'; | ||
|
||
describe('Migration: adjust .swcrc', () => { | ||
let tree: Tree; | ||
let projectConfiguration: ProjectConfiguration; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
libraryGenerator(tree, { | ||
name: 'swc', | ||
buildable: true, | ||
linter: 'none', | ||
unitTestRunner: 'none', | ||
}); | ||
projectConfiguration = readProjectConfiguration(tree, 'swc'); | ||
updateProjectConfiguration(tree, 'swc', { | ||
...projectConfiguration, | ||
targets: { | ||
...projectConfiguration.targets, | ||
build: { | ||
...projectConfiguration.targets['build'], | ||
executor: '@nrwl/js:swc', | ||
}, | ||
}, | ||
}); | ||
// re-read the project configuration | ||
projectConfiguration = readProjectConfiguration(tree, 'swc'); | ||
}); | ||
|
||
it('should rename .swcrc to .lib.swcrc', async () => { | ||
addSwcrc(); | ||
await update(tree); | ||
|
||
expect(tree.exists('libs/swc/.swcrc')).toEqual(false); | ||
expect(tree.exists('libs/swc/.lib.swcrc')).toEqual(true); | ||
}); | ||
|
||
it('should assign default exclude if swcrc does not already have exclude', async () => { | ||
addSwcrc(); | ||
await update(tree); | ||
|
||
expect(readJson(tree, 'libs/swc/.lib.swcrc')['exclude']).toEqual( | ||
defaultExclude | ||
); | ||
}); | ||
|
||
it('should use swcExclude (deprecated) to assign to exclude', async () => { | ||
const swcExclude = ['./src/**/.*.spec.ts$']; | ||
updateProjectConfiguration(tree, 'swc', { | ||
...projectConfiguration, | ||
targets: { | ||
...projectConfiguration.targets, | ||
build: { | ||
...projectConfiguration.targets['build'], | ||
options: { | ||
...projectConfiguration.targets['build']['options'], | ||
swcExclude, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
addSwcrc(); | ||
await update(tree); | ||
|
||
expect(readJson(tree, 'libs/swc/.lib.swcrc')['exclude']).toEqual( | ||
swcExclude | ||
); | ||
}); | ||
|
||
it('should skip updating "exclude" if swcrc already has "exclude" field', async () => { | ||
addSwcrc(true); | ||
await update(tree); | ||
expect(readJson(tree, 'libs/swc/.lib.swcrc')['exclude']).toEqual([ | ||
'./**/.*.spec.ts$', | ||
]); | ||
}); | ||
|
||
function addSwcrc(withExclude = false) { | ||
tree.write( | ||
'libs/swc/.swcrc', | ||
JSON.stringify(withExclude ? { exclude: ['./**/.*.spec.ts$'] } : {}) | ||
); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
formatFiles, | ||
getProjects, | ||
readJson, | ||
Tree, | ||
updateJson, | ||
} from '@nrwl/devkit'; | ||
import { join } from 'path'; | ||
import { defaultExclude } from '../../utils/swc/add-swc-config'; | ||
|
||
export default async function update(host: Tree) { | ||
const projects = getProjects(host); | ||
|
||
for (const config of projects.values()) { | ||
if (config?.targets?.build?.executor !== '@nrwl/js:swc') continue; | ||
|
||
const swcrcPath = join(config.root, '.swcrc'); | ||
if (!host.exists(swcrcPath)) continue; | ||
|
||
// rename | ||
const libSwcrcPath = join(config.root, '.lib.swcrc'); | ||
host.rename(swcrcPath, libSwcrcPath); | ||
|
||
const swcrcContent = readJson(host, libSwcrcPath); | ||
|
||
// skip if swcrc already has "exclude" field | ||
if (swcrcContent['exclude']) continue; | ||
|
||
// check swcExclude build options | ||
const exclude = | ||
config?.targets?.build?.options['swcExclude'] || defaultExclude; | ||
updateJson(host, libSwcrcPath, (swcrc) => { | ||
swcrc['exclude'] = exclude; | ||
return swcrc; | ||
}); | ||
} | ||
|
||
await formatFiles(host); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters