-
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 migration to install @nrwl/webpack if needed by Story…
…book
- Loading branch information
Showing
11 changed files
with
158 additions
and
43 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
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
83 changes: 83 additions & 0 deletions
83
packages/storybook/src/migrations/update-15-5-3/ensure-webpack-package.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,83 @@ | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { | ||
readJson, | ||
writeJson, | ||
addProjectConfiguration, | ||
Tree, | ||
} from '@nrwl/devkit'; | ||
import update from './ensure-webpack-package'; | ||
|
||
describe('ensure-webpack-package', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
writeJson(tree, 'package.json', { | ||
dependencies: {}, | ||
devDependencies: { | ||
'@nrwl/react': '15.5.0', | ||
}, | ||
}); | ||
}); | ||
|
||
it.each` | ||
config | ||
${'main.ts'} | ||
${'main.js'} | ||
`( | ||
'should install @nrwl/webpack if it is needed by React project', | ||
async ({ config }) => { | ||
addProjectConfiguration(tree, 'myapp', { | ||
root: 'myapp', | ||
}); | ||
tree.write( | ||
`myapp/.storybook/${config}`, | ||
` | ||
module.exports = { | ||
addons: ['@nrwl/react/plugins/storybook'] | ||
}; | ||
` | ||
); | ||
|
||
await update(tree); | ||
|
||
const packageJson = readJson(tree, 'package.json'); | ||
expect(packageJson.devDependencies).toEqual({ | ||
'@nrwl/react': expect.any(String), | ||
'@nrwl/webpack': expect.any(String), | ||
}); | ||
} | ||
); | ||
|
||
it.each` | ||
config | ||
${'main.ts'} | ||
${'main.js'} | ||
${null} | ||
`( | ||
'should not install @nrwl/webpack if it is not needed by React project', | ||
async ({ config }) => { | ||
addProjectConfiguration(tree, 'myapp', { | ||
root: 'myapp', | ||
}); | ||
|
||
if (config) { | ||
tree.write( | ||
`myapp/.storybook/${config}`, | ||
` | ||
module.exports = { | ||
addons: [] | ||
}; | ||
` | ||
); | ||
} | ||
|
||
await update(tree); | ||
|
||
const packageJson = readJson(tree, 'package.json'); | ||
expect(packageJson.devDependencies).toEqual({ | ||
'@nrwl/react': expect.any(String), | ||
}); | ||
} | ||
); | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/storybook/src/migrations/update-15-5-3/ensure-webpack-package.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,41 @@ | ||
import { | ||
addDependenciesToPackageJson, | ||
getProjects, | ||
joinPathFragments, | ||
Tree, | ||
} from '@nrwl/devkit'; | ||
import { nxVersion } from '../../utils/versions'; | ||
|
||
// Add @nrwl/webpack as needed. | ||
// See: https://github.com/nrwl/nx/issues/14455 | ||
export default async function update(tree: Tree) { | ||
const projects = getProjects(tree); | ||
const reactPlugin = '@nrwl/react/plugins/storybook'; | ||
let shouldInstall = false; | ||
|
||
for (const [, config] of projects) { | ||
let sbConfigPath = joinPathFragments(config.root, '.storybook/main.ts'); | ||
|
||
if (!tree.exists(sbConfigPath)) { | ||
sbConfigPath = joinPathFragments(config.root, '.storybook/main.js'); | ||
} | ||
|
||
if (!tree.exists(sbConfigPath)) { | ||
continue; | ||
} | ||
|
||
const sbConfig = tree.read(sbConfigPath, 'utf-8'); | ||
if (sbConfig.includes(reactPlugin)) { | ||
shouldInstall = true; | ||
break; | ||
} | ||
} | ||
|
||
if (shouldInstall) { | ||
return addDependenciesToPackageJson( | ||
tree, | ||
{}, | ||
{ '@nrwl/webpack': nxVersion } | ||
); | ||
} | ||
} |
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