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

Inherit package.json's engines field when building #2364

Merged
merged 4 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function makeBundle(
const {
rootPackageWorkspaceDefinitions,
rootPackageJsonDependencies,
rootPackageJsonEngines,
packageJsons,
packageJsonsByPackagePath,
packageNames,
Expand All @@ -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;
AlbertoBrusa marked this conversation as resolved.
Show resolved Hide resolved
}

const main = await getPackageEntryPoints(packagePath, includePrivate);

logger.log(`building ${packageName}...`);
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
4 changes: 4 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 Down Expand Up @@ -106,6 +109,7 @@ async function getPackageMetadata() {
packageNames,
rootPackageWorkspaceDefinitions,
rootPackageJsonDependencies,
rootPackageJsonEngines,
packageJsons,
typescriptConfig,
packageJsonsByPackagePath,
Expand Down