-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): add migrations to fix babel setup for workspaces using re…
…act, next.js, and gatsby (#5041)
- Loading branch information
1 parent
34710a5
commit 27f168e
Showing
10 changed files
with
443 additions
and
135 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
117 changes: 0 additions & 117 deletions
117
packages/web/src/migrations/update-11-5-0/update-babel-config.spec.ts
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
packages/web/src/migrations/update-11-5-2/create-babelrc-for-workspace-libs.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,64 @@ | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { readJson, Tree } from '@nrwl/devkit'; | ||
import { createBabelrcForWorkspaceLibs } from './create-babelrc-for-workspace-libs'; | ||
|
||
describe('Create missing .babelrc files', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(async () => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
it(`should create .babelrc files for libs that are used in '@nrwl/web:build'`, async () => { | ||
tree.write( | ||
'workspace.json', | ||
JSON.stringify({ | ||
projects: { | ||
webapp: { | ||
root: 'apps/webapp', | ||
projectType: 'application', | ||
targets: { | ||
build: { executor: '@nrwl/web:build' }, | ||
}, | ||
}, | ||
nodeapp: { | ||
root: 'apps/nodeapp', | ||
projectType: 'application', | ||
targets: { | ||
build: { executor: '@nrwl/node:build' }, | ||
}, | ||
}, | ||
weblib: { | ||
root: 'libs/weblib', | ||
projectType: 'library', | ||
}, | ||
nodelib: { | ||
root: 'libs/nodelib', | ||
projectType: 'library', | ||
}, | ||
}, | ||
}) | ||
); | ||
tree.write( | ||
'nx.json', | ||
JSON.stringify({ | ||
npmScope: 'proj', | ||
projects: { | ||
webapp: {}, | ||
nodeapp: {}, | ||
weblib: {}, | ||
nodelib: {}, | ||
}, | ||
}) | ||
); | ||
tree.write('apps/webapp/index.ts', `import '@proj/weblib';`); | ||
|
||
await createBabelrcForWorkspaceLibs(tree); | ||
|
||
expect(readJson(tree, 'libs/weblib/.babelrc')).toMatchObject({ | ||
presets: ['@nrwl/web/babel'], | ||
}); | ||
|
||
expect(tree.exists('libs/nodelib/.babelrc')).toBeFalsy(); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/web/src/migrations/update-11-5-2/create-babelrc-for-workspace-libs.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,29 @@ | ||
import { formatFiles, getProjects, Tree } from '@nrwl/devkit'; | ||
import { reverse } from '@nrwl/workspace/src/core/project-graph'; | ||
import { createProjectGraphFromTree } from '@nrwl/workspace/src/utilities/create-project-graph-from-tree'; | ||
import { hasDependentAppUsingWebBuild } from '@nrwl/web/src/migrations/update-11-5-2/utils'; | ||
|
||
export async function createBabelrcForWorkspaceLibs(host: Tree) { | ||
const projects = getProjects(host); | ||
const graph = reverse(createProjectGraphFromTree(host)); | ||
|
||
for (const [name, p] of projects.entries()) { | ||
if (!hasDependentAppUsingWebBuild(name, graph, projects)) { | ||
continue; | ||
} | ||
|
||
const babelrcPath = `${p.root}/.babelrc`; | ||
if (p.projectType === 'library' && !host.exists(babelrcPath)) { | ||
// Library is included in applications that require .babelrc to | ||
// exist and contain '@nrwl/web/babel' preset. | ||
host.write( | ||
babelrcPath, | ||
JSON.stringify({ presets: ['@nrwl/web/babel'] }, null, 2) | ||
); | ||
} | ||
} | ||
|
||
await formatFiles(host); | ||
} | ||
|
||
export default createBabelrcForWorkspaceLibs; |
107 changes: 107 additions & 0 deletions
107
packages/web/src/migrations/update-11-5-2/update-existing-babelrc-files.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,107 @@ | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { readJson, Tree } from '@nrwl/devkit'; | ||
import { updateExistingBabelrcFiles } from './update-existing-babelrc-files'; | ||
|
||
describe('Create missing .babelrc files', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(async () => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
it(`should add web babel preset if it does not exist`, async () => { | ||
tree.write( | ||
'workspace.json', | ||
JSON.stringify({ | ||
projects: { | ||
'missing-babel-presets': { | ||
root: 'apps/missing-babel-presets', | ||
projectType: 'application', | ||
}, | ||
'web-app': { | ||
root: 'apps/web-app', | ||
projectType: 'application', | ||
}, | ||
'react-app': { | ||
root: 'apps/react-app', | ||
projectType: 'application', | ||
}, | ||
'gatsby-app': { | ||
root: 'apps/gatsby-app', | ||
projectType: 'application', | ||
}, | ||
'not-using-babel': { | ||
root: 'apps/not-using-babel', | ||
projectType: 'application', | ||
}, | ||
'next-app': { | ||
root: 'apps/next-app', | ||
projectType: 'application', | ||
}, | ||
}, | ||
}) | ||
); | ||
tree.write( | ||
'nx.json', | ||
JSON.stringify({ | ||
projects: { | ||
'missing-babel-presets': {}, | ||
'web-app': {}, | ||
'react-app': {}, | ||
'gatsby-app': {}, | ||
'not-using-babel': {}, | ||
'next-app': {}, | ||
}, | ||
}) | ||
); | ||
tree.write( | ||
'babel.config.json', | ||
JSON.stringify({ | ||
presets: ['@nrwl/web/babel'], | ||
}) | ||
); | ||
tree.write('apps/missing-babel-presets/.babelrc', JSON.stringify({})); | ||
tree.write( | ||
'apps/web-app/.babelrc', | ||
JSON.stringify({ presets: ['@nrwl/web/babel'] }) | ||
); | ||
tree.write( | ||
'apps/react-app/.babelrc', | ||
JSON.stringify({ presets: ['@nrwl/react/babel'] }) | ||
); | ||
tree.write( | ||
'apps/gatsby-app/.babelrc', | ||
JSON.stringify({ presets: ['@nrwl/gatsby/babel'] }) | ||
); | ||
tree.write( | ||
'apps/next-app/.babelrc', | ||
JSON.stringify({ presets: ['@nrwl/next/babel'] }) | ||
); | ||
|
||
await updateExistingBabelrcFiles(tree); | ||
|
||
expect(readJson(tree, 'apps/missing-babel-presets/.babelrc')).toMatchObject( | ||
{ | ||
presets: ['@nrwl/web/babel'], | ||
} | ||
); | ||
|
||
expect(readJson(tree, 'apps/web-app/.babelrc')).toMatchObject({ | ||
presets: ['@nrwl/web/babel'], | ||
}); | ||
|
||
expect(readJson(tree, 'apps/react-app/.babelrc')).toMatchObject({ | ||
presets: ['@nrwl/react/babel'], | ||
}); | ||
|
||
expect(readJson(tree, 'apps/gatsby-app/.babelrc')).toMatchObject({ | ||
presets: ['@nrwl/gatsby/babel'], | ||
}); | ||
|
||
expect(tree.exists('apps/not-using-babel/.babelrc')).not.toBeTruthy(); | ||
|
||
expect(readJson(tree, 'apps/next-app/.babelrc')).toMatchObject({ | ||
presets: ['@nrwl/next/babel'], | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.