From 68d018752c938340660b561822107e6dbb56e195 Mon Sep 17 00:00:00 2001 From: Brody McKee Date: Wed, 15 Apr 2020 20:56:30 +0300 Subject: [PATCH] Support devDependencies in templates --- docusaurus/docs/custom-templates.md | 2 +- packages/react-scripts/scripts/init.js | 40 +++++++++++++++++++------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/docusaurus/docs/custom-templates.md b/docusaurus/docs/custom-templates.md index de7906a5b9f..9ae4ae69741 100644 --- a/docusaurus/docs/custom-templates.md +++ b/docusaurus/docs/custom-templates.md @@ -62,7 +62,7 @@ You can add whatever files you want in here, but you must have at least the file 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. -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. +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. Below is an example `template.json` file: diff --git a/packages/react-scripts/scripts/init.js b/packages/react-scripts/scripts/init.js index 58ffeb59c3f..3411da29179 100644 --- a/packages/react-scripts/scripts/init.js +++ b/packages/react-scripts/scripts/init.js @@ -125,6 +125,27 @@ module.exports = function( const templatePackage = templateJson.package || {}; + // TODO: Deprecate support for root-level `dependencies` and `scripts` in v4. + // These should now be set under the `package` key. + if (templateJson.dependencies || templateJson.scripts) { + console.log(); + console.log( + chalk.yellow( + 'Root-level `dependencies` and `scripts` keys in `template.json` are deprecated.\n' + + 'This template should be updated to use the new `package` key.' + ) + ); + console.log( + 'For more information, visit https://create-react-app.dev/docs/custom-templates' + ); + } + if (templateJson.dependencies) { + templatePackage.dependencies = templateJson.dependencies; + } + if (templateJson.scripts) { + templatePackage.scripts = templateJson.scripts; + } + // Keys to ignore in templatePackage const templatePackageBlacklist = [ 'name', @@ -141,7 +162,6 @@ module.exports = function( 'man', 'directories', 'repository', - 'devDependencies', 'peerDependencies', 'bundledDependencies', 'optionalDependencies', @@ -169,8 +189,7 @@ module.exports = function( appPackage.dependencies = appPackage.dependencies || {}; // Setup the script rules - // TODO: deprecate 'scripts' key directly on templateJson - const templateScripts = templatePackage.scripts || templateJson.scripts || {}; + const templateScripts = templatePackage.scripts || {}; appPackage.scripts = Object.assign( { start: 'react-scripts start', @@ -282,14 +301,15 @@ module.exports = function( args = ['install', '--save', verbose && '--verbose'].filter(e => e); } - // Install additional template dependencies, if present - // TODO: deprecate 'dependencies' key directly on templateJson - const templateDependencies = - templatePackage.dependencies || templateJson.dependencies; - if (templateDependencies) { + // Install additional template dependencies, if present. + const dependenciesToInstall = Object.entries({ + ...templatePackage.dependencies, + ...templatePackage.devDependencies, + }); + if (dependenciesToInstall.length) { args = args.concat( - Object.keys(templateDependencies).map(key => { - return `${key}@${templateDependencies[key]}`; + dependenciesToInstall.map(([dependency, version]) => { + return `${dependency}@${version}`; }) ); }