Skip to content

Commit 2473a73

Browse files
authoredMay 4, 2020
Support devDependencies in templates (facebook#8838)
1 parent 5a019e6 commit 2473a73

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed
 

‎docusaurus/docs/custom-templates.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ You can add whatever files you want in here, but you must have at least the file
6262

6363
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.
6464

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.
65+
The `package` key lets you provide any keys/values that you want added to the new project's `package.json`, such as dependencies and any custom scripts that your template relies on.
6666

6767
Below is an example `template.json` file:
6868

‎packages/react-scripts/scripts/init.js

+30-10
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ module.exports = function(
125125

126126
const templatePackage = templateJson.package || {};
127127

128+
// TODO: Deprecate support for root-level `dependencies` and `scripts` in v4.
129+
// These should now be set under the `package` key.
130+
if (templateJson.dependencies || templateJson.scripts) {
131+
console.log();
132+
console.log(
133+
chalk.yellow(
134+
'Root-level `dependencies` and `scripts` keys in `template.json` are deprecated.\n' +
135+
'This template should be updated to use the new `package` key.'
136+
)
137+
);
138+
console.log(
139+
'For more information, visit https://create-react-app.dev/docs/custom-templates'
140+
);
141+
}
142+
if (templateJson.dependencies) {
143+
templatePackage.dependencies = templateJson.dependencies;
144+
}
145+
if (templateJson.scripts) {
146+
templatePackage.scripts = templateJson.scripts;
147+
}
148+
128149
// Keys to ignore in templatePackage
129150
const templatePackageBlacklist = [
130151
'name',
@@ -141,7 +162,6 @@ module.exports = function(
141162
'man',
142163
'directories',
143164
'repository',
144-
'devDependencies',
145165
'peerDependencies',
146166
'bundledDependencies',
147167
'optionalDependencies',
@@ -169,8 +189,7 @@ module.exports = function(
169189
appPackage.dependencies = appPackage.dependencies || {};
170190

171191
// Setup the script rules
172-
// TODO: deprecate 'scripts' key directly on templateJson
173-
const templateScripts = templatePackage.scripts || templateJson.scripts || {};
192+
const templateScripts = templatePackage.scripts || {};
174193
appPackage.scripts = Object.assign(
175194
{
176195
start: 'react-scripts start',
@@ -282,14 +301,15 @@ module.exports = function(
282301
args = ['install', '--save', verbose && '--verbose'].filter(e => e);
283302
}
284303

285-
// Install additional template dependencies, if present
286-
// TODO: deprecate 'dependencies' key directly on templateJson
287-
const templateDependencies =
288-
templatePackage.dependencies || templateJson.dependencies;
289-
if (templateDependencies) {
304+
// Install additional template dependencies, if present.
305+
const dependenciesToInstall = Object.entries({
306+
...templatePackage.dependencies,
307+
...templatePackage.devDependencies,
308+
});
309+
if (dependenciesToInstall.length) {
290310
args = args.concat(
291-
Object.keys(templateDependencies).map(key => {
292-
return `${key}@${templateDependencies[key]}`;
311+
dependenciesToInstall.map(([dependency, version]) => {
312+
return `${dependency}@${version}`;
293313
})
294314
);
295315
}

0 commit comments

Comments
 (0)
Please sign in to comment.