Skip to content

Commit

Permalink
fix(ng-dev): yarnpkg lockfile package cannot be bundled with ESM
Browse files Browse the repository at this point in the history
ESBuild bundles CommonJS packages, but some cannot be due to use of
CJS-only features. The yarn lockfile is one package that needs to be
kept external for it to leverage CJS features through NodeJS built-in
ESM -> CJS interop.
  • Loading branch information
devversion committed Jun 16, 2022
1 parent 70b51cd commit 4505df9
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ng-dev/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ NG_DEV_EXTERNALS = [
# `typescript` is external because we want the project to provide a TypeScript version.
# TODO: Figure out how we want to manage dependencies for the dev-infra tool.
"typescript",
# Package uses `__filename` and `__dirname` and cannot be bundled in ESM. We do not
# intend to provide interop globals for this as it could hide other significant issues.
"@yarnpkg/lockfile",
]

ts_library(
Expand Down
4 changes: 2 additions & 2 deletions ng-dev/utils/resolve-yarn-bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as path from 'path';
import which from 'which';

import {isNodeJSWrappedError} from './nodejs-errors.js';
import {parse as parseLockfile} from '@yarnpkg/lockfile';
import lockfile from '@yarnpkg/lockfile';
import {parse as parseYaml} from 'yaml';
import {ChildProcess} from './child-process.js';
import {Log} from './logging.js';
Expand All @@ -33,7 +33,7 @@ export interface YarnCommandInfo {

/** List of Yarn configuration files and their parsing mechanisms. */
export const yarnConfigFiles: ConfigWithParser[] = [
{fileName: '.yarnrc', parse: (c) => parseLockfile(c).object},
{fileName: '.yarnrc', parse: (c) => lockfile.parse(c).object},
{fileName: '.yarnrc.yml', parse: (c) => parseYaml(c)},
];

Expand Down
6 changes: 3 additions & 3 deletions ng-dev/utils/version-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import * as path from 'path';
import * as fs from 'fs';
import {LockFileObject, parse as parseYarnLockfile} from '@yarnpkg/lockfile';
import lockfile from '@yarnpkg/lockfile';
import {
ngDevNpmPackageName,
workspaceRelativePackageJsonPath,
Expand All @@ -34,7 +34,7 @@ export async function verifyNgDevToolIsUpToDate(workspacePath: string): Promise<
try {
const lockFileContent = fs.readFileSync(workspaceDirLockFile, 'utf8');
const packageJson = JSON.parse(fs.readFileSync(workspacePackageJsonFile, 'utf8')) as any;
const lockFile = parseYarnLockfile(lockFileContent);
const lockFile = lockfile.parse(lockFileContent);

if (lockFile.type !== 'success') {
throw Error('Unable to parse workspace lock file. Please ensure the file is valid.');
Expand All @@ -45,7 +45,7 @@ export async function verifyNgDevToolIsUpToDate(workspacePath: string): Promise<
return true;
}

const lockFileObject = lockFile.object as LockFileObject;
const lockFileObject = lockFile.object as lockfile.LockFileObject;
const devInfraPkgVersion =
packageJson?.dependencies?.[ngDevNpmPackageName] ??
packageJson?.devDependencies?.[ngDevNpmPackageName] ??
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/tmp": "^0.2.1",
"@types/uuid": "^8.3.1",
"@types/yargs": "^17.0.0",
"@yarnpkg/lockfile": "^1.1.0",
"browser-sync": "^2.27.7",
"clang-format": "1.8.0",
"prettier": "2.7.0",
Expand Down Expand Up @@ -105,7 +106,6 @@
"@types/wait-on": "^5.3.1",
"@types/which": "^2.0.1",
"@types/yarnpkg__lockfile": "^1.1.5",
"@yarnpkg/lockfile": "^1.1.0",
"chalk": "^5.0.1",
"cli-progress": "^3.7.0",
"conventional-commits-parser": "^3.2.1",
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
true,
{
"noNamedExports": [
"@yarnpkg/lockfile",
"typescript",
"multimatch",
"semver",
Expand Down

0 comments on commit 4505df9

Please sign in to comment.