Skip to content

Commit

Permalink
Support devDependencies in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmckeb committed Apr 15, 2020
1 parent e89f153 commit 78248ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docusaurus/docs/custom-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
38 changes: 30 additions & 8 deletions packages/react-scripts/scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -141,7 +162,6 @@ module.exports = function(
'man',
'directories',
'repository',
'devDependencies',
'peerDependencies',
'bundledDependencies',
'optionalDependencies',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -282,11 +301,14 @@ 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 templateDependencies = templatePackage.dependencies || [];
const templateDevDependencies = templatePackage.devDependencies || [];
const dependenciesToInstall = [
...templateDependencies,
...templateDevDependencies,
];
if (dependenciesToInstall.length) {
args = args.concat(
Object.keys(templateDependencies).map(key => {
return `${key}@${templateDependencies[key]}`;
Expand Down

0 comments on commit 78248ed

Please sign in to comment.