Skip to content

Commit 9c9c4db

Browse files
authored
feat(tools): replace npmignore with package.json#files for robust control over what we ship to npm (#29616)
* feat(tools): replace npmignore with package.json#files within migrate-converged-pkg generator * feat(tools): replace npmignore with package.json#files within react-library generator * feat(tools): replace npmignore with package.json#files within migrate-v8-pkg generator
1 parent 8ef637b commit 9c9c4db

File tree

8 files changed

+89
-297
lines changed

8 files changed

+89
-297
lines changed

tools/workspace-plugin/src/generators/migrate-converged-pkg/index.spec.ts

Lines changed: 30 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -977,95 +977,40 @@ describe('migrate-converged-pkg generator', () => {
977977
});
978978
});
979979

980-
describe(`npm config setup`, () => {
981-
it(`should update .npmignore config`, async () => {
982-
function getNpmIgnoreConfig(projectConfig: ReadProjectConfiguration) {
983-
return tree.read(`${projectConfig.root}/.npmignore`)?.toString('utf-8');
984-
}
980+
describe(`npm publish setup`, () => {
981+
it(`should replace .npmignore config with package.json#files`, async () => {
982+
const getNpmIgnoreConfigPath = (projectConfig: ReadProjectConfiguration) => `${projectConfig.root}/.npmignore`;
983+
985984
const projectConfig = readProjectConfiguration(tree, options.name);
986-
let npmIgnoreConfig = getNpmIgnoreConfig(projectConfig);
987-
988-
expect(npmIgnoreConfig).toMatchInlineSnapshot(`
989-
"*.api.json
990-
*.config.js
991-
*.log
992-
*.nuspec
993-
*.test.*
994-
*.yml
995-
.editorconfig
996-
.eslintrc*
997-
.eslintcache
998-
.gitattributes
999-
.gitignore
1000-
.vscode
1001-
coverage
1002-
dist/storybook
1003-
dist/*.stats.html
1004-
dist/*.stats.json
1005-
dist/demo
1006-
fabric-test*
1007-
gulpfile.js
1008-
images
1009-
index.html
1010-
jsconfig.json
1011-
node_modules
1012-
results
1013-
src/**/*
1014-
!src/**/examples/*.tsx
1015-
!src/**/docs/**/*.md
1016-
!src/**/*.types.ts
1017-
temp
1018-
tsconfig.json
1019-
tsd.json
1020-
tslint.json
1021-
typings
1022-
visualtests"
985+
const npmIgnoreConfigPath = getNpmIgnoreConfigPath(projectConfig);
986+
987+
expect(tree.exists(npmIgnoreConfigPath)).toBe(true);
988+
989+
await generator(tree, options);
990+
991+
expect(tree.exists(npmIgnoreConfigPath)).toBe(false);
992+
let pkgJson = readJson<PackageJson>(tree, `${projectConfig.root}/package.json`);
993+
994+
expect(pkgJson.files).toMatchInlineSnapshot(`
995+
Array [
996+
"lib",
997+
"lib-commonjs",
998+
"dist/*.d.ts",
999+
]
10231000
`);
10241001

1002+
updateProjectConfiguration(tree, projectConfig.name!, { ...projectConfig, tags: ['ships-amd'] });
10251003
await generator(tree, options);
10261004

1027-
npmIgnoreConfig = getNpmIgnoreConfig(projectConfig);
1028-
1029-
expect(npmIgnoreConfig).toMatchInlineSnapshot(`
1030-
".storybook/
1031-
.vscode/
1032-
bundle-size/
1033-
config/
1034-
coverage/
1035-
docs/
1036-
etc/
1037-
node_modules/
1038-
src/
1039-
stories/
1040-
dist/types/
1041-
temp/
1042-
__fixtures__
1043-
__mocks__
1044-
__tests__
1045-
1046-
*.api.json
1047-
*.log
1048-
*.spec.*
1049-
*.cy.*
1050-
*.test.*
1051-
*.yml
1052-
1053-
# config files
1054-
*config.*
1055-
*rc.*
1056-
.editorconfig
1057-
.eslint*
1058-
.git*
1059-
.prettierignore
1060-
.swcrc
1061-
project.json
1062-
1063-
# exclude gitignore patterns explicitly
1064-
!lib
1065-
!lib-commonjs
1066-
!lib-amd
1067-
!dist/*.d.ts
1068-
"
1005+
pkgJson = readJson<PackageJson>(tree, `${projectConfig.root}/package.json`);
1006+
1007+
expect(pkgJson.files).toMatchInlineSnapshot(`
1008+
Array [
1009+
"lib",
1010+
"lib-commonjs",
1011+
"lib-amd",
1012+
"dist/*.d.ts",
1013+
]
10691014
`);
10701015
});
10711016
});
@@ -1606,42 +1551,7 @@ function setupDummyPackage(
16061551
jestSetupFile: stripIndents`
16071552
/** Jest test setup file. */
16081553
`,
1609-
npmConfig: stripIndents`
1610-
*.api.json
1611-
*.config.js
1612-
*.log
1613-
*.nuspec
1614-
*.test.*
1615-
*.yml
1616-
.editorconfig
1617-
.eslintrc*
1618-
.eslintcache
1619-
.gitattributes
1620-
.gitignore
1621-
.vscode
1622-
coverage
1623-
dist/storybook
1624-
dist/*.stats.html
1625-
dist/*.stats.json
1626-
dist/demo
1627-
fabric-test*
1628-
gulpfile.js
1629-
images
1630-
index.html
1631-
jsconfig.json
1632-
node_modules
1633-
results
1634-
src/**/*
1635-
!src/**/examples/*.tsx
1636-
!src/**/docs/**/*.md
1637-
!src/**/*.types.ts
1638-
temp
1639-
tsconfig.json
1640-
tsd.json
1641-
tslint.json
1642-
typings
1643-
visualtests
1644-
`,
1554+
npmConfig: stripIndents``,
16451555
babelConfig: {
16461556
...normalizedOptions.babelConfig,
16471557
},

tools/workspace-plugin/src/generators/migrate-converged-pkg/index.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ function runMigrationOnProject(tree: Tree, schema: AssertedSchema, _userLog: Use
140140

141141
setupCypress(tree, options);
142142

143-
setupNpmIgnoreConfig(tree, options);
144143
setupBabel(tree, options);
145144

146145
updateNxProject(tree, options);
@@ -594,12 +593,6 @@ function updateNxProject(tree: Tree, options: NormalizedSchema) {
594593
return tree;
595594
}
596595

597-
function setupNpmIgnoreConfig(tree: Tree, options: NormalizedSchema) {
598-
tree.write(options.paths.npmConfig, templates.npmIgnoreConfig);
599-
600-
return tree;
601-
}
602-
603596
function setupSwcConfig(tree: Tree, options: NormalizedSchema) {
604597
const swcConfig = templates.swcConfig();
605598
writeJson(tree, joinPathFragments(options.projectConfig.root, '.swcrc'), swcConfig);
@@ -693,6 +686,7 @@ function updatePackageJson(tree: Tree, options: NormalizedSchemaWithTsConfigs) {
693686
packageJson = setupScripts(packageJson);
694687
packageJson = setupExportMaps(packageJson);
695688
packageJson = addSwcHelpers(packageJson);
689+
packageJson = setupNpmPublishFiles(packageJson);
696690

697691
writeJson(tree, options.paths.packageJson, packageJson);
698692

@@ -739,13 +733,27 @@ function updatePackageJson(tree: Tree, options: NormalizedSchemaWithTsConfigs) {
739733
return './' + path.posix.normalize(entryPath);
740734
}
741735
}
742-
}
743736

744-
//TODO: remove after migration to swc transpilation is complete
745-
function addSwcHelpers(json: PackageJson) {
746-
delete json.dependencies?.tslib;
747-
json.dependencies = { ...json.dependencies, '@swc/helpers': '^0.4.14' };
748-
return json;
737+
//TODO: remove after migration to swc transpilation is complete
738+
function addSwcHelpers(json: PackageJson) {
739+
delete json.dependencies?.tslib;
740+
json.dependencies = { ...json.dependencies, '@swc/helpers': '^0.5.1' };
741+
return json;
742+
}
743+
744+
function setupNpmPublishFiles(json: PackageJson) {
745+
json.files = json.files ?? [];
746+
json.files = [
747+
'lib',
748+
'lib-commonjs',
749+
options.projectConfig.tags?.includes('ships-amd') ? 'lib-amd' : '',
750+
'dist/*.d.ts',
751+
].filter(Boolean);
752+
753+
tree.delete(options.paths.npmConfig);
754+
755+
return json;
756+
}
749757
}
750758

751759
function updateApiExtractor(tree: Tree, options: NormalizedSchemaWithTsConfigs) {

tools/workspace-plugin/src/generators/migrate-v8-pkg/index.spec.ts

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import {
99
logger,
1010
readNxJson,
1111
NxJsonConfiguration,
12+
readJson,
1213
} from '@nx/devkit';
1314
import type { Linter } from 'eslint';
1415

15-
import type { TsConfig } from '../../types';
16+
import type { PackageJson, TsConfig } from '../../types';
1617
import generator from './index';
1718
import { MigrateV8PkgGeneratorSchema } from './schema';
1819

@@ -65,50 +66,24 @@ describe('migrate-v8-pkg generator', () => {
6566
});
6667

6768
describe(`--name`, () => {
68-
it(`should setup .npmignore`, async () => {
69-
await generator(tree, options);
69+
describe(`npm publish setup`, () => {
70+
it(`should replace .npmignore config with package.json#files`, async () => {
71+
expect(tree.exists(`packages/eight/.npmignore`)).toBe(true);
7072

71-
expect(tree.read(`packages/eight/.npmignore`, 'utf-8')).toMatchInlineSnapshot(`
72-
"*.api.json
73-
*.config.js
74-
*.log
75-
*.nuspec
76-
*.test.*
77-
*.yml
78-
.editorconfig
79-
.eslintrc*
80-
.eslintcache
81-
.gitattributes
82-
.gitignore
83-
.vscode
84-
coverage
85-
dist/storybook
86-
dist/*.stats.html
87-
dist/*.stats.json
88-
dist/demo
89-
fabric-test*
90-
gulpfile.js
91-
images
92-
index.html
93-
jsconfig.json
94-
node_modules
95-
results
96-
src/**/*
97-
!src/**/*.types.ts
98-
temp
99-
tsconfig.json
100-
tsd.json
101-
tslint.json
102-
typings
103-
visualtests
104-
project.json
73+
await generator(tree, options);
10574

106-
# exclude gitignore patterns explicitly
107-
!lib
108-
!lib-commonjs
109-
!lib-amd
110-
!dist"
111-
`);
75+
expect(tree.exists(`packages/eight/.npmignore`)).toBe(false);
76+
77+
const pkgJson = readJson<PackageJson>(tree, `packages/eight/package.json`);
78+
expect(pkgJson.files).toMatchInlineSnapshot(`
79+
Array [
80+
"lib",
81+
"lib-commonjs",
82+
"lib-amd",
83+
"dist",
84+
]
85+
`);
86+
});
11287
});
11388
});
11489

@@ -244,40 +219,7 @@ function setupDummyPackage(
244219
// Configure enzyme.
245220
configure({ adapter: new Adapter() });
246221
`,
247-
npmConfig: stripIndents`
248-
*.api.json
249-
*.config.js
250-
*.log
251-
*.nuspec
252-
*.test.*
253-
*.yml
254-
.editorconfig
255-
.eslintrc*
256-
.eslintcache
257-
.gitattributes
258-
.gitignore
259-
.vscode
260-
coverage
261-
dist/storybook
262-
dist/*.stats.html
263-
dist/*.stats.json
264-
dist/demo
265-
fabric-test*
266-
gulpfile.js
267-
images
268-
index.html
269-
jsconfig.json
270-
node_modules
271-
results
272-
src/**/*
273-
!src/**/*.types.ts
274-
temp
275-
tsconfig.json
276-
tsd.json
277-
tslint.json
278-
typings
279-
visualtests
280-
`,
222+
npmConfig: stripIndents``,
281223
};
282224

283225
if (typeof normalizedOptions.eslintConfig === 'string') {

0 commit comments

Comments
 (0)