Skip to content

Commit 3b821b0

Browse files
committed
Update custom template documentation, extend template package blacklist (#8082)
1 parent e2d7111 commit 3b821b0

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

docusaurus/docs/custom-templates.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,31 @@ You can add whatever files you want in here, but you must have at least the file
5858

5959
### The `template.json` file
6060

61-
This is the configuration file for your template. It allows you to define information that should become a part of the generated project's `package.json` file, such as dependencies (only dependencies are supported for now) and any custom scripts that your template relies on. It should be structured as follows:
61+
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.
62+
63+
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.
64+
65+
Below is an example `template.json` file:
6266

6367
```json
6468
{
6569
"package": {
6670
"dependencies": {
71+
"eslint-plugin-jsx-a11y": "^6.2.3",
6772
"serve": "^11.2.0"
6873
},
6974
"scripts": {
7075
"serve": "serve -s build",
7176
"build-and-serve": "npm run build && npm run serve"
7277
},
73-
"browserslist": [
74-
"defaults",
75-
"not IE 11",
76-
"not IE_Mob 11",
77-
"maintained node versions",
78-
]
78+
"eslintConfig": {
79+
"extends": ["react-app", "plugin:jsx-a11y/recommended"],
80+
"plugins": ["jsx-a11y"]
81+
}
7982
}
8083
}
8184
```
8285

83-
Any values you add for `"dependencies"` and `"scripts"` will be merged with the values used in the initialisation process of `react-scripts`. Any other information you add to `"package"` will be added to the generated project's `package.json` file, replacing any existing values associated with those keys.
86+
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.
8487

8588
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.

packages/react-scripts/scripts/init.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,45 @@ module.exports = function(
122122

123123
// Keys to ignore in templatePackage
124124
const templatePackageBlacklist = [
125+
'name',
126+
'version',
127+
'description',
128+
'keywords',
129+
'homepage',
130+
'bugs',
131+
'license',
132+
'author',
133+
'contributors',
134+
'files',
135+
'main',
136+
'browser',
137+
'bin',
138+
'man',
139+
'directories',
140+
'repository',
125141
'devDependencies',
126142
'peerDependencies',
127-
'name',
143+
'bundledDependencies',
144+
'optionalDependencies',
145+
'engineStrict',
146+
'os',
147+
'cpu',
148+
'preferGlobal',
149+
'private',
150+
'publishConfig',
128151
];
129152

130153
// Keys from templatePackage that will be merged with appPackage
131-
const templatePackageToMerge = [
132-
'dependencies',
133-
'scripts',
134-
];
154+
const templatePackageToMerge = ['dependencies', 'scripts'];
135155

136156
// Keys from templatePackage that will be added to appPackage,
137157
// replacing any existing entries.
138-
const templatePackageToReplace = Object.keys(templatePackage)
139-
.filter(key => {
140-
return (
141-
templatePackageBlacklist.indexOf(key) === -1 &&
142-
templatePackageToMerge.indexOf(key) === -1
143-
);
144-
});
158+
const templatePackageToReplace = Object.keys(templatePackage).filter(key => {
159+
return (
160+
templatePackageBlacklist.indexOf(key) === -1 &&
161+
templatePackageToMerge.indexOf(key) === -1
162+
);
163+
});
145164

146165
// Copy over some of the devDependencies
147166
appPackage.dependencies = appPackage.dependencies || {};
@@ -253,7 +272,8 @@ module.exports = function(
253272

254273
// Install additional template dependencies, if present
255274
// TODO: deprecate 'dependencies' key directly on templateJson
256-
const templateDependencies = templatePackage.dependencies || templateJson.dependencies;
275+
const templateDependencies =
276+
templatePackage.dependencies || templateJson.dependencies;
257277
if (templateDependencies) {
258278
args = args.concat(
259279
Object.keys(templateDependencies).map(key => {

0 commit comments

Comments
 (0)