Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tools): add exports map support to migrate-converged-pkg generator #25033

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "refactor: normalize relative import for unstable package.json",
"packageName": "@fluentui/react-components",
"email": "[email protected]",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normalizing path with export maps

"sideEffects": false,
"license": "MIT",
"exports": {
Expand Down
71 changes: 70 additions & 1 deletion tools/generators/migrate-converged-pkg/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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);
Expand Down
55 changes: 53 additions & 2 deletions tools/generators/migrate-converged-pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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<PackageJson>(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) {
Expand Down
3 changes: 1 addition & 2 deletions tools/generators/migrate-converged-pkg/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@
"pattern": "^@[/-\\w]+$"
}
},
"required": [],
"additionalProperties": false
"required": []
}
1 change: 1 addition & 0 deletions tools/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface PackageJson {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
exports?: Record<string, string | Partial<{ types: string; import: string; require: string }>>;
}

// ===============
Expand Down
4 changes: 4 additions & 0 deletions tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down