From a6b98e7649d54f3c7f212acc0aa851b067b60588 Mon Sep 17 00:00:00 2001 From: Alberto Brusa Date: Thu, 27 Apr 2023 11:25:03 +0100 Subject: [PATCH 1/4] Inherit package.json's engines field when building --- .../src/build-scripts/build-package/makeBundle.ts | 6 ++++++ .../modular-scripts/src/build-scripts/buildStandalone.ts | 7 +++++++ packages/modular-scripts/src/utils/getPackageMetadata.ts | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/packages/modular-scripts/src/build-scripts/build-package/makeBundle.ts b/packages/modular-scripts/src/build-scripts/build-package/makeBundle.ts index b433c9c1f..1ad18a611 100644 --- a/packages/modular-scripts/src/build-scripts/build-package/makeBundle.ts +++ b/packages/modular-scripts/src/build-scripts/build-package/makeBundle.ts @@ -39,6 +39,7 @@ export async function makeBundle( const { rootPackageWorkspaceDefinitions, rootPackageJsonDependencies, + rootPackageJsonEngines, packageJsons, packageJsonsByPackagePath, packageNames, @@ -56,6 +57,11 @@ export async function makeBundle( const packageJson = packageJsonsByPackagePath[packagePath]; + // If engines field is not defined in the package's package.json, inherit it from the root package.json + if (!packageJson.engines) { + packageJson.engines = rootPackageJsonEngines; + } + const main = await getPackageEntryPoints(packagePath, includePrivate); logger.log(`building ${packageName}...`); diff --git a/packages/modular-scripts/src/build-scripts/buildStandalone.ts b/packages/modular-scripts/src/build-scripts/buildStandalone.ts index b4404a388..0cf5b7cbc 100644 --- a/packages/modular-scripts/src/build-scripts/buildStandalone.ts +++ b/packages/modular-scripts/src/build-scripts/buildStandalone.ts @@ -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, @@ -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'), @@ -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 }, ); diff --git a/packages/modular-scripts/src/utils/getPackageMetadata.ts b/packages/modular-scripts/src/utils/getPackageMetadata.ts index 57971b32c..376b6734b 100644 --- a/packages/modular-scripts/src/utils/getPackageMetadata.ts +++ b/packages/modular-scripts/src/utils/getPackageMetadata.ts @@ -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(); @@ -106,6 +109,7 @@ async function getPackageMetadata() { packageNames, rootPackageWorkspaceDefinitions, rootPackageJsonDependencies, + rootPackageJsonEngines, packageJsons, typescriptConfig, packageJsonsByPackagePath, From 9745c266f527cd1b97b7bbc08b35457767e61e68 Mon Sep 17 00:00:00 2001 From: Alberto Brusa Date: Thu, 27 Apr 2023 12:10:34 +0100 Subject: [PATCH 2/4] Update snapshots --- packages/modular-scripts/src/__tests__/build.test.ts | 3 +++ packages/modular-scripts/src/__tests__/index.test.ts | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/packages/modular-scripts/src/__tests__/build.test.ts b/packages/modular-scripts/src/__tests__/build.test.ts index 96c155e06..23738b13e 100644 --- a/packages/modular-scripts/src/__tests__/build.test.ts +++ b/packages/modular-scripts/src/__tests__/build.test.ts @@ -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", diff --git a/packages/modular-scripts/src/__tests__/index.test.ts b/packages/modular-scripts/src/__tests__/index.test.ts index bedd83168..c770e919c 100644 --- a/packages/modular-scripts/src/__tests__/index.test.ts +++ b/packages/modular-scripts/src/__tests__/index.test.ts @@ -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", @@ -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", @@ -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", From c675f8efb479b6ed38989ecf76cb074bc1131236 Mon Sep 17 00:00:00 2001 From: Alberto Brusa Date: Thu, 27 Apr 2023 13:12:46 +0100 Subject: [PATCH 3/4] Changeset --- .changeset/good-brooms-poke.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/good-brooms-poke.md diff --git a/.changeset/good-brooms-poke.md b/.changeset/good-brooms-poke.md new file mode 100644 index 000000000..2645f76e6 --- /dev/null +++ b/.changeset/good-brooms-poke.md @@ -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 From 0c433d9d5bd6e1be9c4dfef1c473ce614a62850f Mon Sep 17 00:00:00 2001 From: Alberto Brusa Date: Thu, 27 Apr 2023 15:18:33 +0100 Subject: [PATCH 4/4] Refactor --- .../src/build-scripts/build-package/makeBundle.ts | 6 ------ packages/modular-scripts/src/utils/getPackageMetadata.ts | 4 ++++ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/modular-scripts/src/build-scripts/build-package/makeBundle.ts b/packages/modular-scripts/src/build-scripts/build-package/makeBundle.ts index 1ad18a611..b433c9c1f 100644 --- a/packages/modular-scripts/src/build-scripts/build-package/makeBundle.ts +++ b/packages/modular-scripts/src/build-scripts/build-package/makeBundle.ts @@ -39,7 +39,6 @@ export async function makeBundle( const { rootPackageWorkspaceDefinitions, rootPackageJsonDependencies, - rootPackageJsonEngines, packageJsons, packageJsonsByPackagePath, packageNames, @@ -57,11 +56,6 @@ export async function makeBundle( const packageJson = packageJsonsByPackagePath[packagePath]; - // If engines field is not defined in the package's package.json, inherit it from the root package.json - if (!packageJson.engines) { - packageJson.engines = rootPackageJsonEngines; - } - const main = await getPackageEntryPoints(packagePath, includePrivate); logger.log(`building ${packageName}...`); diff --git a/packages/modular-scripts/src/utils/getPackageMetadata.ts b/packages/modular-scripts/src/utils/getPackageMetadata.ts index 376b6734b..0c7566b58 100644 --- a/packages/modular-scripts/src/utils/getPackageMetadata.ts +++ b/packages/modular-scripts/src/utils/getPackageMetadata.ts @@ -48,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; });