From 6d1c2f79da9e034308c7f2cfbd16499e0cfd51f9 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Mon, 4 Oct 2021 12:03:19 -0400 Subject: [PATCH] refactor(migrations): ensure CommonJS migrations can be accessed (#43657) The `@angular/core` migrations are currently published as CommonJS and also not bundled. Schematics, of which migrations are a type, are currently required to be CommonJS modules due to the Schematics Runtime not yet supporting ESM-based schematics. To ensure that the migration files are loaded as CommonJS modules, a nested `package.json` file has been introduced within the `schematics` directory of the package to set the `type` field for all containing JavaScript files as CommonJS. Since the migrations are also not currently bundled and the `devmode` output used to build the migrations will replace all relative imports with fully-qualified module specifiers, an additional `exports` entry must be added to allow the fully-qualified module specifiers to correctly resolve. If `devmode` did not change the relative imports the additional entry would not be needed. Bundling would also remedy the situation and is the long-term path, especially once the migrations are converted to ESM. Additional followup changes should investigate bundling each migration. The additional `exports` entry should be removed if either `devmode` behavior is changed or the migrations are bundled. PR Close #43657 --- packages/bazel/test/ng_package/core_package.spec.ts | 3 +++ packages/core/package.json | 5 +++++ packages/core/schematics/BUILD.bazel | 5 ++++- packages/core/schematics/package.json | 3 +++ 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 packages/core/schematics/package.json diff --git a/packages/bazel/test/ng_package/core_package.spec.ts b/packages/bazel/test/ng_package/core_package.spec.ts index 71200f55334..ea19bf0fba8 100644 --- a/packages/bazel/test/ng_package/core_package.spec.ts +++ b/packages/bazel/test/ng_package/core_package.spec.ts @@ -72,6 +72,9 @@ describe('@angular/core ng_package', () => { node: './fesm2015/testing.mjs', default: './fesm2020/testing.mjs', }, + './schematics/*': { + 'default': './schematics/*.js', + }, } })); }); diff --git a/packages/core/package.json b/packages/core/package.json index 9ea40ad28a6..0dde09a527c 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -7,6 +7,11 @@ "engines": { "node": "^12.14.1 || >=14.0.0" }, + "exports": { + "./schematics/*": { + "default": "./schematics/*.js" + } + }, "dependencies": { "tslib": "^2.3.0" }, diff --git a/packages/core/schematics/BUILD.bazel b/packages/core/schematics/BUILD.bazel index 66c250a589d..a1397b8bb0a 100644 --- a/packages/core/schematics/BUILD.bazel +++ b/packages/core/schematics/BUILD.bazel @@ -7,7 +7,10 @@ exports_files([ pkg_npm( name = "npm_package", - srcs = ["migrations.json"], + srcs = [ + "migrations.json", + "package.json", + ], visibility = ["//packages/core:__pkg__"], deps = [ "//packages/core/schematics/migrations/abstract-control-parent", diff --git a/packages/core/schematics/package.json b/packages/core/schematics/package.json new file mode 100644 index 00000000000..5bbefffbabe --- /dev/null +++ b/packages/core/schematics/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +}