This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from microsoft/feat/react-components-unstable
feat: Configuration for dependency replacement
- Loading branch information
Showing
15 changed files
with
1,859 additions
and
1,684 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
49 changes: 49 additions & 0 deletions
49
storybook-addon-export-to-codesandbox/src/getDepdencies.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,49 @@ | ||
export type PackageDependencies = { [dependencyName: string]: string }; | ||
|
||
/** | ||
* | ||
* @param fileContent - code | ||
* @param requiredDependencies - dependencies that will always be incldued in package.json | ||
* @param optionalDependencies - whose versions will override those found in the code | ||
* @returns - Map of dependencies and their versions to include in package.json | ||
*/ | ||
export const getDependencies = ( | ||
fileContent: string, | ||
requiredDependencies: PackageDependencies, | ||
optionalDependencies: PackageDependencies, | ||
) => { | ||
const matches = fileContent.matchAll(/import .* from ['"](.*?)['"];/g); | ||
|
||
const dependenciesInCode = Array.from(matches).reduce((dependencies, match) => { | ||
if (!match[1].startsWith('react/')) { | ||
const dependency = parsePackageName(match[1]).name; | ||
|
||
if (!dependencies.hasOwnProperty(dependency)) { | ||
dependencies[dependency] = optionalDependencies[dependency] ?? 'latest'; | ||
} | ||
} | ||
|
||
return dependencies; | ||
}, {} as PackageDependencies); | ||
|
||
return { ...dependenciesInCode, ...requiredDependencies }; | ||
}; | ||
|
||
// Parsed a scoped package name into name, version, and path. | ||
const RE_SCOPED = /^(@[^\/]+\/[^@\/]+)(?:@([^\/]+))?(\/.*)?$/; | ||
// Parsed a non-scoped package name into name, version, path | ||
const RE_NON_SCOPED = /^([^@\/]+)(?:@([^\/]+))?(\/.*)?$/; | ||
|
||
function parsePackageName(input: string) { | ||
const m = RE_SCOPED.exec(input) || RE_NON_SCOPED.exec(input); | ||
|
||
if (!m) { | ||
throw new Error(`[parse-package-name] invalid package name: ${input}`); | ||
} | ||
|
||
return { | ||
name: m[1] || '', | ||
version: m[2] || 'latest', | ||
path: m[3] || '', | ||
}; | ||
} |
67 changes: 67 additions & 0 deletions
67
storybook-addon-export-to-codesandbox/src/getDependencies.test.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,67 @@ | ||
import { getDependencies } from './getDepdencies'; | ||
|
||
describe('getDependencies', () => { | ||
it('should find all dependencies in a file', () => { | ||
const code = ` | ||
import { stuff } from 'dependency'; | ||
import * as allStuff from 'dependency1'; | ||
import { moreStuff } from '@dependency/dependency'; | ||
`; | ||
const deps = getDependencies(code, {}, {}); | ||
|
||
expect(deps).toEqual({ | ||
'@dependency/dependency': 'latest', | ||
dependency: 'latest', | ||
dependency1: 'latest', | ||
}); | ||
}); | ||
|
||
it('should ignore separate entrypoints', () => { | ||
const code = ` | ||
import { stuff } from 'dependency/unstable'; | ||
import { moreStuff } from 'dependency/unstable/component'; | ||
import { moreStuff2 } from '@dependency/unstable'; | ||
import { moreStuff3 } from '@dependency/unstable/component'; | ||
`; | ||
const deps = getDependencies(code, {}, {}); | ||
|
||
expect(deps).toEqual({ | ||
dependency: 'latest', | ||
'@dependency/unstable': 'latest', | ||
}); | ||
}); | ||
|
||
it('versions in optionalDependencies should win ', () => { | ||
const code = ` | ||
import { stuff } from 'dependency'; | ||
`; | ||
const deps = getDependencies(code, {}, { dependency: '1.0.0' }); | ||
|
||
expect(deps).toEqual({ | ||
dependency: '1.0.0', | ||
}); | ||
}); | ||
|
||
it('versions in requiredDependencies should win ', () => { | ||
const code = ` | ||
import { stuff } from 'dependency'; | ||
`; | ||
const deps = getDependencies(code, { dependency: '1.0.0' }, {}); | ||
|
||
expect(deps).toEqual({ | ||
dependency: '1.0.0', | ||
}); | ||
}); | ||
|
||
it('versions in requiredDependencies should win ', () => { | ||
const code = ` | ||
import { stuff } from 'dependency'; | ||
`; | ||
const deps = getDependencies(code, { required: '1.0.0' }, {}); | ||
|
||
expect(deps).toEqual({ | ||
dependency: 'latest', | ||
required: '1.0.0', | ||
}); | ||
}); | ||
}); |
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
3 changes: 3 additions & 0 deletions
3
...esandbox/src/plugins/__fixtures__/storybook-stories-modifyImports/import-override/code.js
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,3 @@ | ||
import { Button } from '@fluentui/react-unstable-component'; | ||
|
||
export const ButtonStories = () => console.log(Button); |
2 changes: 2 additions & 0 deletions
2
...andbox/src/plugins/__fixtures__/storybook-stories-modifyImports/import-override/output.js
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,2 @@ | ||
import { Button } from '@fluentui/react-components/unstable'; | ||
export const ButtonStories = () => console.log(Button); |
3 changes: 3 additions & 0 deletions
3
...ins/__fixtures__/storybook-stories-modifyImports/relative-import-declaration/package.json
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,3 @@ | ||
{ | ||
"name": "@fluentui/react-button" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as fullSourcePlugin } from './fullsource'; | ||
export { default as fullSourcePlugin } from './fullsource'; | ||
export type { BabelPluginOptions } from './fullsource'; |
11 changes: 9 additions & 2 deletions
11
storybook-addon-export-to-codesandbox/src/plugins/modifyImports.test.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 |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import pluginTester from 'babel-plugin-tester'; | ||
import * as path from 'path'; | ||
import plugin, { PLUGIN_NAME} from './modifyImports'; | ||
import plugin, { PLUGIN_NAME } from './modifyImports'; | ||
|
||
const defaultDependencyReplace = { replace: '@fluentui/react-components' }; | ||
const fixturesDir = path.join(__dirname, `__fixtures__/${PLUGIN_NAME}`); | ||
pluginTester({ | ||
pluginOptions: { | ||
'@fluentui/react-button': defaultDependencyReplace, | ||
'@fluentui/react-menu': defaultDependencyReplace, | ||
'@fluentui/react-link': defaultDependencyReplace, | ||
'@fluentui/react-unstable-component': { replace: '@fluentui/react-components/unstable' }, | ||
}, | ||
pluginName: PLUGIN_NAME, | ||
plugin, | ||
fixtures: fixturesDir, | ||
}) | ||
}); |
Oops, something went wrong.