Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support devDependencies in templates #8838

Merged
merged 1 commit into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 30 additions & 10 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this say v5 then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge this, and fix in the next PR (which is about deprecations) - thanks!

// 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,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}`;
})
);
}
Expand Down