From b75835b040bb58daaca584acc1792aef99191e9c Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Fri, 30 Sep 2022 17:46:26 +0200 Subject: [PATCH 1/3] feat(tools): add exports map support to migrate-converged-pkg generator --- .../migrate-converged-pkg/index.spec.ts | 71 ++++++++++++++++++- .../generators/migrate-converged-pkg/index.ts | 55 +++++++++++++- .../migrate-converged-pkg/schema.json | 3 +- tools/types.ts | 1 + tools/utils.ts | 4 ++ 5 files changed, 129 insertions(+), 5 deletions(-) diff --git a/tools/generators/migrate-converged-pkg/index.spec.ts b/tools/generators/migrate-converged-pkg/index.spec.ts index ec00bb46adfe2f..b475859e55e7c2 100644 --- a/tools/generators/migrate-converged-pkg/index.spec.ts +++ b/tools/generators/migrate-converged-pkg/index.spec.ts @@ -832,7 +832,7 @@ describe('migrate-converged-pkg generator', () => { await generator(tree, options); pkgJson = readJson(tree, `${projectConfig.root}/package.json`); - expect(pkgJson.typings).toEqual('dist/index.d.ts'); + expect(pkgJson.typings).toEqual('./dist/index.d.ts'); }); it(`should update package npm scripts`, async () => { @@ -882,6 +882,75 @@ describe('migrate-converged-pkg generator', () => { }); }); + it(`should update exports map`, async () => { + const projectConfig = readProjectConfiguration(tree, options.name); + const pkgJsonPath = `${projectConfig.root}/package.json`; + + let pkgJson = readJson(tree, pkgJsonPath); + + expect(pkgJson.exports).toBe(undefined); + + await generator(tree, options); + + pkgJson = readJson(tree, pkgJsonPath); + + expect(pkgJson.exports).toMatchInlineSnapshot(` + Object { + ".": Object { + "import": "./lib/index.js", + "require": "./lib-commonjs/index.js", + "types": "./dist/index.d.ts", + }, + "./package.json": "./package.json", + } + `); + }); + + it(`should update exports map if unstable API is present`, async () => { + const projectConfig = readProjectConfiguration(tree, options.name); + const pkgJsonPath = `${projectConfig.root}/package.json`; + const pkgJsonUnstablePath = `${projectConfig.root}/src/unstable/package.json__tmpl__`; + writeJson(tree, pkgJsonUnstablePath, { + typings: './../dist/unstable.d.ts', + }); + + let pkgJson = readJson(tree, pkgJsonPath); + let pkgUnstableJson = readJson(tree, pkgJsonUnstablePath); + + expect(pkgJson.exports).toBe(undefined); + expect(pkgUnstableJson.exports).toBe(undefined); + + await generator(tree, options); + + pkgJson = readJson(tree, pkgJsonPath); + pkgUnstableJson = readJson(tree, pkgJsonUnstablePath); + + expect(pkgJson.exports).toMatchInlineSnapshot(` + Object { + ".": Object { + "import": "./lib/index.js", + "require": "./lib-commonjs/index.js", + "types": "./dist/index.d.ts", + }, + "./package.json": "./package.json", + "./unstable": Object { + "import": "./lib/unstable/index.js", + "require": "./lib-commonjs/unstable/index.js", + "types": "./dist/unstable.d.ts", + }, + } + `); + expect(pkgUnstableJson.exports).toMatchInlineSnapshot(` + Object { + ".": Object { + "import": "./../lib/unstable/index.js", + "require": "./../lib-commonjs/unstable/index.js", + "types": "./../dist/unstable.d.ts", + }, + } + `); + }); + it(`should not add start scripts to node packages`, async () => { const nodePackageName = getScopedPkgName(tree, 'babel-make-styles'); const projectConfig = readProjectConfiguration(tree, nodePackageName); diff --git a/tools/generators/migrate-converged-pkg/index.ts b/tools/generators/migrate-converged-pkg/index.ts index 29c4beaa87d572..13be8ec87be6ca 100644 --- a/tools/generators/migrate-converged-pkg/index.ts +++ b/tools/generators/migrate-converged-pkg/index.ts @@ -548,10 +548,28 @@ function updatePackageJson(tree: Tree, options: NormalizedSchemaWithTsConfigs) { 'type-check': 'tsc -b tsconfig.json', }; + const unstableApiDefinitions = processUnstableApiDefinitions(); + + if (unstableApiDefinitions) { + updateJson(tree, unstableApiDefinitions.unstablePackageJsonPath, (json: PackageJson) => { + json.exports = unstableApiDefinitions.unstableExportMap; + return json; + }); + } + updateJson(tree, options.paths.packageJson, (json: PackageJson) => { - json.scripts = json.scripts || {}; - json.typings = 'dist/index.d.ts'; + json.typings = './dist/index.d.ts'; + json.exports = { + '.': { + types: json.typings, + import: './lib/index.js', + require: './lib-commonjs/index.js', + }, + ...(unstableApiDefinitions ? unstableApiDefinitions.rootExportMap : null), + './package.json': './package.json', + }; + json.scripts = json.scripts || {}; delete json.scripts['update-snapshots']; delete json.scripts['start-test']; delete json.scripts['test:watch']; @@ -569,6 +587,39 @@ function updatePackageJson(tree: Tree, options: NormalizedSchemaWithTsConfigs) { }); return tree; + + function processUnstableApiDefinitions() { + const unstablePackageJsonPath = joinPathFragments(options.paths.unstable.rootPackageJson); + const hasUnstableApi = tree.exists(unstablePackageJsonPath); + + if (!hasUnstableApi) { + return; + } + + const unstablePackageJson = readJson(tree, unstablePackageJsonPath); + const typePaths = { + rootExports: unstablePackageJson.typings?.replace(/\.\.\//g, ''), + unstableExports: unstablePackageJson.typings, + }; + + return { + unstablePackageJsonPath, + rootExportMap: { + './unstable': { + types: typePaths.rootExports, + import: './lib/unstable/index.js', + require: './lib-commonjs/unstable/index.js', + }, + }, + unstableExportMap: { + '.': { + types: typePaths.unstableExports, + import: './../lib/unstable/index.js', + require: './../lib-commonjs/unstable/index.js', + }, + }, + }; + } } function updateApiExtractorForLocalBuilds(tree: Tree, options: NormalizedSchemaWithTsConfigs) { diff --git a/tools/generators/migrate-converged-pkg/schema.json b/tools/generators/migrate-converged-pkg/schema.json index 5de3f09e10f36d..69ed9274fa1258 100644 --- a/tools/generators/migrate-converged-pkg/schema.json +++ b/tools/generators/migrate-converged-pkg/schema.json @@ -27,6 +27,5 @@ "pattern": "^@[/-\\w]+$" } }, - "required": [], - "additionalProperties": false + "required": [] } diff --git a/tools/types.ts b/tools/types.ts index 6a368ca50fe214..97f04fb1239211 100644 --- a/tools/types.ts +++ b/tools/types.ts @@ -30,6 +30,7 @@ export interface PackageJson { dependencies?: Record; devDependencies?: Record; peerDependencies?: Record; + exports?: Record>; } // =============== diff --git a/tools/utils.ts b/tools/utils.ts index e2cedbdfa9019b..ff2ededc93b3d4 100644 --- a/tools/utils.ts +++ b/tools/utils.ts @@ -94,6 +94,10 @@ export function getProjectConfig(tree: Tree, options: { packageName: string }) { test: joinPathFragments(projectConfig.root, 'tsconfig.spec.json'), }, sourceRoot: joinPathFragments(projectConfig.root, 'src'), + unstable: { + sourceRoot: joinPathFragments(projectConfig.root, 'src', 'unstable'), + rootPackageJson: joinPathFragments(projectConfig.root, 'src', 'unstable', 'package.json__tmpl__'), + }, conformanceSetup: joinPathFragments(projectConfig.root, 'src', 'common', 'isConformant.ts'), babelConfig: joinPathFragments(projectConfig.root, '.babelrc.json'), jestConfig: joinPathFragments(projectConfig.root, 'jest.config.js'), From 1a6adc5fe39a32637c2903d0b8201edd8fabd6bf Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Fri, 30 Sep 2022 17:47:19 +0200 Subject: [PATCH 2/3] refactor(react-components): normalize relative import for unstable package.json --- .../react-components/src/unstable/package.json__tmpl__ | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-components/react-components/src/unstable/package.json__tmpl__ b/packages/react-components/react-components/src/unstable/package.json__tmpl__ index f7038deddcf712..6a148f872a8cb9 100644 --- a/packages/react-components/react-components/src/unstable/package.json__tmpl__ +++ b/packages/react-components/react-components/src/unstable/package.json__tmpl__ @@ -2,7 +2,7 @@ "description": "Separate entrypoint for unstable Fluent UI components", "main": "../lib-commonjs/unstable/index.js", "module": "../lib/unstable/index.js", - "typings": "../dist/unstable.d.ts", + "typings": "./../dist/unstable.d.ts", "sideEffects": false, "license": "MIT", "exports": { From cad57a7b80dbc034c322c402ddd6faf2787c45df Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Fri, 30 Sep 2022 17:48:33 +0200 Subject: [PATCH 3/3] generate changefile --- ...ct-components-a03fef34-ebd4-4d4f-9203-384df97ccc9d.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 change/@fluentui-react-components-a03fef34-ebd4-4d4f-9203-384df97ccc9d.json diff --git a/change/@fluentui-react-components-a03fef34-ebd4-4d4f-9203-384df97ccc9d.json b/change/@fluentui-react-components-a03fef34-ebd4-4d4f-9203-384df97ccc9d.json new file mode 100644 index 00000000000000..d3e1d4a349f0d9 --- /dev/null +++ b/change/@fluentui-react-components-a03fef34-ebd4-4d4f-9203-384df97ccc9d.json @@ -0,0 +1,7 @@ +{ + "type": "none", + "comment": "refactor: normalize relative import for unstable package.json", + "packageName": "@fluentui/react-components", + "email": "martinhochel@microsoft.com", + "dependentChangeType": "none" +}