Skip to content

Commit 94932be

Browse files
tomvalorsamrmckeb
authored andcommitted
Allow additional package keys and add blacklist (#8082) (#8219)
1 parent fa85f03 commit 94932be

File tree

5 files changed

+99
-29
lines changed

5 files changed

+99
-29
lines changed

docusaurus/docs/custom-templates.md

+20-7
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,31 @@ You can add whatever files you want in here, but you must have at least the file
6060

6161
### The `template.json` file
6262

63-
This is where you can define dependencies (only dependencies are supported for now), as well as any custom scripts that your template relies on.
63+
This is the configuration file for your template. As this is a new feature, more options will be added over time. For now, only a `package` key is supported.
64+
65+
The `package` key lets you provide any keys/values that you want added to the new project's `package.json`, such as dependencies (only dependencies are supported for now) and any custom scripts that your template relies on.
66+
67+
Below is an example `template.json` file:
6468

6569
```json
6670
{
67-
"dependencies": {
68-
"serve": "^11.2.0"
69-
},
70-
"scripts": {
71-
"serve": "serve -s build",
72-
"build-and-serve": "npm run build && npm run serve"
71+
"package": {
72+
"dependencies": {
73+
"eslint-plugin-jsx-a11y": "^6.2.3",
74+
"serve": "^11.2.0"
75+
},
76+
"scripts": {
77+
"serve": "serve -s build",
78+
"build-and-serve": "npm run build && npm run serve"
79+
},
80+
"eslintConfig": {
81+
"extends": ["react-app", "plugin:jsx-a11y/recommended"],
82+
"plugins": ["jsx-a11y"]
83+
}
7384
}
7485
}
7586
```
7687

88+
Any values you add for `"dependencies"` and `"scripts"` will be merged with the Create React App defaults. Values for any other keys will be used as-is, replacing any matching Create React App defaults.
89+
7790
For convenience, we always replace `npm run` with `yarn` in your custom `"scripts"`, as well as in your `README` when projects are initialized with yarn.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
2-
"dependencies": {
3-
"@testing-library/react": "^9.3.2",
4-
"@testing-library/jest-dom": "^4.2.4",
5-
"@testing-library/user-event": "^7.1.2",
6-
"@types/node": "^12.0.0",
7-
"@types/react": "^16.9.0",
8-
"@types/react-dom": "^16.9.0",
9-
"@types/jest": "^24.0.0",
10-
"typescript": "~3.7.2"
2+
"package": {
3+
"dependencies": {
4+
"@testing-library/react": "^9.3.2",
5+
"@testing-library/jest-dom": "^4.2.4",
6+
"@testing-library/user-event": "^7.1.2",
7+
"@types/node": "^12.0.0",
8+
"@types/react": "^16.9.0",
9+
"@types/react-dom": "^16.9.0",
10+
"@types/jest": "^24.0.0",
11+
"typescript": "~3.7.2"
12+
}
1113
}
1214
}

packages/cra-template/template.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
2-
"dependencies": {
3-
"@testing-library/react": "^9.3.2",
4-
"@testing-library/jest-dom": "^4.2.4",
5-
"@testing-library/user-event": "^7.1.2"
2+
"package": {
3+
"dependencies": {
4+
"@testing-library/react": "^9.3.2",
5+
"@testing-library/jest-dom": "^4.2.4",
6+
"@testing-library/user-event": "^7.1.2"
7+
}
68
}
79
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
2-
"dependencies": {
3-
"bootstrap": "4.3.1",
4-
"jest": "24.9.0",
5-
"node-sass": "4.12.0",
6-
"normalize.css": "7.0.0",
7-
"prop-types": "15.7.2",
8-
"test-integrity": "2.0.1"
2+
"package": {
3+
"dependencies": {
4+
"bootstrap": "4.3.1",
5+
"jest": "24.9.0",
6+
"node-sass": "4.12.0",
7+
"normalize.css": "7.0.0",
8+
"prop-types": "15.7.2",
9+
"test-integrity": "2.0.1"
10+
}
911
}
1012
}

packages/react-scripts/scripts/init.js

+53-2
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,55 @@ module.exports = function(
118118
templateJson = require(templateJsonPath);
119119
}
120120

121+
const templatePackage = templateJson.package || {};
122+
123+
// Keys to ignore in templatePackage
124+
const templatePackageBlacklist = [
125+
'name',
126+
'version',
127+
'description',
128+
'keywords',
129+
'bugs',
130+
'license',
131+
'author',
132+
'contributors',
133+
'files',
134+
'main',
135+
'browser',
136+
'bin',
137+
'man',
138+
'directories',
139+
'repository',
140+
'devDependencies',
141+
'peerDependencies',
142+
'bundledDependencies',
143+
'optionalDependencies',
144+
'engineStrict',
145+
'os',
146+
'cpu',
147+
'preferGlobal',
148+
'private',
149+
'publishConfig',
150+
];
151+
152+
// Keys from templatePackage that will be merged with appPackage
153+
const templatePackageToMerge = ['dependencies', 'scripts'];
154+
155+
// Keys from templatePackage that will be added to appPackage,
156+
// replacing any existing entries.
157+
const templatePackageToReplace = Object.keys(templatePackage).filter(key => {
158+
return (
159+
!templatePackageBlacklist.includes(key) &&
160+
!templatePackageToMerge.includes(key)
161+
);
162+
});
163+
121164
// Copy over some of the devDependencies
122165
appPackage.dependencies = appPackage.dependencies || {};
123166

124167
// Setup the script rules
125-
const templateScripts = templateJson.scripts || {};
168+
// TODO: deprecate 'scripts' key directly on templateJson
169+
const templateScripts = templatePackage.scripts || templateJson.scripts || {};
126170
appPackage.scripts = Object.assign(
127171
{
128172
start: 'react-scripts start',
@@ -152,6 +196,11 @@ module.exports = function(
152196
// Setup the browsers list
153197
appPackage.browserslist = defaultBrowsers;
154198

199+
// Add templatePackage keys/values to appPackage, replacing existing entries
200+
templatePackageToReplace.forEach(key => {
201+
appPackage[key] = templatePackage[key];
202+
});
203+
155204
fs.writeFileSync(
156205
path.join(appPath, 'package.json'),
157206
JSON.stringify(appPackage, null, 2) + os.EOL
@@ -221,7 +270,9 @@ module.exports = function(
221270
}
222271

223272
// Install additional template dependencies, if present
224-
const templateDependencies = templateJson.dependencies;
273+
// TODO: deprecate 'dependencies' key directly on templateJson
274+
const templateDependencies =
275+
templatePackage.dependencies || templateJson.dependencies;
225276
if (templateDependencies) {
226277
args = args.concat(
227278
Object.keys(templateDependencies).map(key => {

0 commit comments

Comments
 (0)