Skip to content

Commit

Permalink
Inherit package.json's engines field when building (#2364)
Browse files Browse the repository at this point in the history
* Inherit package.json's engines field when building
  • Loading branch information
AlbertoBrusa authored Apr 28, 2023
1 parent 89097fc commit fdf2f71
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/good-brooms-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'modular-scripts': minor
---

`modular build` now includes the 'engines' field in the dist package.json when
provided in the workspace or root package.json
3 changes: 3 additions & 0 deletions packages/modular-scripts/src/__tests__/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ describe('WHEN building with preserve modules', () => {
).toMatchInlineSnapshot(`
{
"dependencies": {},
"engines": {
"node": "^14.18.0 || >=16.10.0 || >=18.0.0",
},
"files": [
"dist-cjs",
"dist-es",
Expand Down
9 changes: 9 additions & 0 deletions packages/modular-scripts/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ describe('modular-scripts', () => {
"dependencies": {
"react": "17.0.2",
},
"engines": {
"node": "^14.18.0 || >=16.10.0 || >=18.0.0",
},
"files": [
"dist-cjs",
"dist-es",
Expand Down Expand Up @@ -360,6 +363,9 @@ describe('modular-scripts', () => {
).toMatchInlineSnapshot(`
{
"dependencies": {},
"engines": {
"node": "^14.18.0 || >=16.10.0 || >=18.0.0",
},
"files": [
"dist-cjs",
"dist-es",
Expand Down Expand Up @@ -441,6 +447,9 @@ describe('modular-scripts', () => {
).toMatchInlineSnapshot(`
{
"dependencies": {},
"engines": {
"node": "^14.18.0 || >=16.10.0 || >=18.0.0",
},
"files": [
"dist-cjs",
"dist-es",
Expand Down
7 changes: 7 additions & 0 deletions packages/modular-scripts/src/build-scripts/buildStandalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { getDependencyInfo } from '../utils/getDependencyInfo';
import { isReactNewApi } from '../utils/isReactNewApi';
import { getConfig } from '../utils/config';
import buildWebpack from './webpack-scripts/buildWebpack';
import getModularRoot from '../utils/getModularRoot';

export async function buildStandalone(
target: string,
Expand Down Expand Up @@ -176,6 +177,11 @@ export async function buildStandalone(
const targetPackageJson = (await fs.readJSON(
path.join(targetDirectory, 'package.json'),
)) as ModularPackageJson;

const rootPackageJson = (await fs.readJSON(
path.join(getModularRoot(), 'package.json'),
)) as ModularPackageJson;

// Copy selected fields of package.json over
await fs.writeJson(
path.join(paths.appBuild, 'package.json'),
Expand All @@ -191,6 +197,7 @@ export async function buildStandalone(
module: jsEntryPoint ? paths.publicUrlOrPath + jsEntryPoint : undefined,
style: cssEntryPoint ? paths.publicUrlOrPath + cssEntryPoint : undefined,
styleImports: styleImports?.size ? [...styleImports] : undefined,
engines: targetPackageJson.engines ?? rootPackageJson.engines,
},
{ spaces: 2 },
);
Expand Down
8 changes: 8 additions & 0 deletions packages/modular-scripts/src/utils/getPackageMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ async function getPackageMetadata() {
// dependencies defined at the root
const rootPackageJsonDependencies = rootPackageJson.dependencies || {};

// engines defined at the root
const rootPackageJsonEngines = rootPackageJson.engines;

// let's populate the above three
const [workspaces] = await getAllWorkspaces();

Expand All @@ -45,6 +48,10 @@ async function getPackageMetadata() {

Array.from(workspaces).forEach(([packageName, workspace]) => {
packageJsons[packageName] = workspace.rawPackageJson;
if (!packageJsons[packageName].engines) {
// If engines field is not defined in the package's package.json, inherit it from the root package.json
packageJsons[packageName].engines = rootPackageJsonEngines;
}
packageJsonsByPackagePath[workspace.location] = workspace.rawPackageJson;
});

Expand Down Expand Up @@ -106,6 +113,7 @@ async function getPackageMetadata() {
packageNames,
rootPackageWorkspaceDefinitions,
rootPackageJsonDependencies,
rootPackageJsonEngines,
packageJsons,
typescriptConfig,
packageJsonsByPackagePath,
Expand Down

0 comments on commit fdf2f71

Please sign in to comment.