From 51efbd8472de4f7df23338aeed8368b9ca97b864 Mon Sep 17 00:00:00 2001 From: Patrick Rodgers Date: Wed, 27 Dec 2023 15:43:00 -0500 Subject: [PATCH] commonjs work --- buildsystem-config.ts | 58 ++++---- package-lock.json | 133 +++++++++--------- package.json | 3 +- packages/azidjsclient/tsconfig-commonjs.json | 12 ++ packages/core/tsconfig-commonjs.json | 10 ++ packages/graph/tsconfig-commonjs.json | 16 +++ packages/logging/tsconfig-commonjs.json | 7 + packages/msaljsclient/tsconfig-commonjs.json | 12 ++ packages/nodejs/tsconfig-commonjs.json | 28 ++++ packages/queryable/tsconfig-commonjs.json | 11 ++ packages/sp-admin/tsconfig-commonjs.json | 20 +++ packages/sp/tsconfig-commonjs.json | 16 +++ packages/tsconfig-commonjs.json | 38 +++++ packages/tsconfig.json | 2 +- tools/buildsystem/bin/buildsystem.ts | 3 +- tools/buildsystem/index.ts | 1 + tools/buildsystem/package.json | 2 +- tools/buildsystem/src/behaviors/build.ts | 2 +- .../src/behaviors/copy-package-files.ts | 3 +- .../create-resolution-package-files.ts | 43 ++++++ .../src/behaviors/replace-version.ts | 2 +- tools/buildsystem/src/types.ts | 2 + tools/buildsystem/tsconfig.json | 3 +- tools/local-module-resolver/esm-packages.ts | 2 +- tools/local-module-resolver/esm.ts | 9 +- tsconfig.buildsystem.json | 2 +- 26 files changed, 333 insertions(+), 107 deletions(-) create mode 100644 packages/azidjsclient/tsconfig-commonjs.json create mode 100644 packages/core/tsconfig-commonjs.json create mode 100644 packages/graph/tsconfig-commonjs.json create mode 100644 packages/logging/tsconfig-commonjs.json create mode 100644 packages/msaljsclient/tsconfig-commonjs.json create mode 100644 packages/nodejs/tsconfig-commonjs.json create mode 100644 packages/queryable/tsconfig-commonjs.json create mode 100644 packages/sp-admin/tsconfig-commonjs.json create mode 100644 packages/sp/tsconfig-commonjs.json create mode 100644 packages/tsconfig-commonjs.json create mode 100644 tools/buildsystem/src/behaviors/create-resolution-package-files.ts diff --git a/buildsystem-config.ts b/buildsystem-config.ts index 1c84a727b..6fbd8f372 100644 --- a/buildsystem-config.ts +++ b/buildsystem-config.ts @@ -33,24 +33,7 @@ function PnPBuild(buildFlags?: string[]): (b: BuildTimeline) => BuildTimeline { return (instance: BuildTimeline) => { Build(buildFlags)(instance); - ReplaceVersion(["sp/behaviors/telemetry.js", "graph/behaviors/telemetry.js"])(instance); - - return instance; - } -} - -function PnPBuildCommonJS(buildFlags?: string[]): (b: BuildTimeline) => BuildTimeline { - - if (!buildFlags) { - buildFlags = []; - } - - buildFlags.push("--module", "commonjs", "--outDir", "./buildcjs") - - return (instance: BuildTimeline) => { - - Build(buildFlags)(instance); - ReplaceVersion([resolve("./buildcjs/packages/sp/behaviors/telemetry.js"), resolve("./buildcjs/packages/graph/behaviors/telemetry.js")], { pathsResolved: true })(instance); + ReplaceVersion(["sp/behaviors/telemetry.js", "graph/behaviors/telemetry.js"], {})(instance); return instance; } @@ -66,13 +49,11 @@ function PnPPackage(): (b: BuildTimeline) => BuildTimeline { CopyPackageFiles("built", ["**/*.d.ts", "**/*.js", "**/*.js.map", "**/*.d.ts.map"])(instance); WritePackageJSON((p) => { return Object.assign({}, p, { - funding: { - type: "individual", - url: "https://github.com/sponsors/patrick-rodgers/", - }, type: "module", + main: "./esm/index.js", + typings: "./esm/index", engines: { - node: ">=14.15.1" + node: ">=18.12.0" }, author: { name: "Microsoft and other contributors" @@ -85,7 +66,24 @@ function PnPPackage(): (b: BuildTimeline) => BuildTimeline { repository: { type: "git", url: "git:github.com/pnp/pnpjs" - } + }, + exports: { + ".": { + "import": { + "types": "./esm/index", + "default": "./esm/index.js" + }, + "require": { + "types": "./commonjs/index", + "default": "./commonjs/index.js" + }, + "default": "./esm/index.js" + } + }, + funding: { + type: "individual", + url: "https://github.com/sponsors/patrick-rodgers/", + }, }); })(instance); @@ -112,6 +110,7 @@ export default [{ distFolder, targets: [ resolve("./packages/tsconfig.json"), + resolve("./packages/tsconfig-commonjs.json"), ], behaviors: [PnPBuild(), ...commonBehaviors], }, @@ -121,15 +120,16 @@ export default [{ targets: [ resolve("./debug/launch/tsconfig.json"), ], - behaviors: [Build(), ReplaceVersion(["packages/sp/behaviors/telemetry.js", "packages/graph/behaviors/telemetry.js"]), ...commonBehaviors], + behaviors: [Build(), ReplaceVersion(["packages/sp/behaviors/telemetry.js", "packages/graph/behaviors/telemetry.js"], {}), ...commonBehaviors], }, { name: "package", distFolder, targets: [ resolve("./packages/tsconfig.json"), + resolve("./packages/tsconfig-commonjs.json"), ], - behaviors: [PnPBuild(), PnPBuildCommonJS(), PnPPackage(), ...commonBehaviors], + behaviors: [PnPBuild(), PnPPackage(), ...commonBehaviors], }, { name: "publish", @@ -137,7 +137,7 @@ export default [{ targets: [ resolve("./packages/tsconfig.json"), ], - behaviors: [PnPBuild(), PnPBuildCommonJS(), PnPPackage(), PnPPublish(commonPublishTags), ...commonBehaviors], + behaviors: [PnPBuild(), PnPPackage(), PnPPublish(commonPublishTags), ...commonBehaviors], }, { name: "publish-beta", @@ -145,7 +145,7 @@ export default [{ targets: [ resolve("./packages/tsconfig.json"), ], - behaviors: [PnPBuild(), PnPBuildCommonJS(), PnPPackage(), PnPPublish([...commonPublishTags, "--tag", "beta"]), ...commonBehaviors], + behaviors: [PnPBuild(), PnPPackage(), PnPPublish([...commonPublishTags, "--tag", "beta"]), ...commonBehaviors], }, { name: "publish-v3nightly", @@ -161,5 +161,5 @@ export default [{ targets: [ resolve("./packages/tsconfig.json"), ], - behaviors: [PnPBuild(), PnPBuildCommonJS(), PnPPackage(), PublishNightly([...commonPublishTags], "v4nightly"), ...commonBehaviors], + behaviors: [PnPBuild(), PnPPackage(), PublishNightly([...commonPublishTags], "v4nightly"), ...commonBehaviors], }]; diff --git a/package-lock.json b/package-lock.json index 16ae6bce0..0ebe60470 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@azure/identity": "3.3.0", "@azure/msal-node": "1.18.3", "@microsoft/microsoft-graph-types": "2.38.0", - "@pnp/buildsystem": "^4.0.0-beta5", + "@pnp/buildsystem": "^4.0.0-beta7", "@pnp/logging": "^3.21.0", "@types/chai": "4.3.6", "@types/chai-as-promised": "7.1.6", @@ -30,6 +30,7 @@ "del-cli": "5.1.0", "eslint": "8.49.0", "findup-sync": "5.0.0", + "globby": "^14.0.0", "mocha": "10.2.0", "msal": "1.4.18", "node-fetch": "3.3.2", @@ -600,12 +601,12 @@ } }, "node_modules/@pnp/buildsystem": { - "version": "4.0.0-beta5", - "resolved": "https://registry.npmjs.org/@pnp/buildsystem/-/buildsystem-4.0.0-beta5.tgz", - "integrity": "sha512-OhMFUkR/LeOJjqLXd/c6D59FjCmpvOndKuNFL27CnC70SnVlDLMxYu2RMkOBPWUgtRJ2ZJUO3r/rNrS2OxKBQw==", + "version": "4.0.0-beta7", + "resolved": "https://registry.npmjs.org/@pnp/buildsystem/-/buildsystem-4.0.0-beta7.tgz", + "integrity": "sha512-2TNJ/lFkf04ebkMUUVgNeDvLWBCK490/0TV88lCVJyin9N3e1SwD2hPusJGPqPqZ6EKW0AenCeFRLXzwBdgfpA==", "dev": true, "dependencies": { - "@pnp/core": "^3.21.0", + "@pnp/core": "^4.0.0-alpha0-v4nightly.20231227", "globby": "^14.0.0", "liftoff": "^4.0.0", "webpack": "^5.89.0", @@ -644,50 +645,6 @@ "node": ">=4.0" } }, - "node_modules/@pnp/buildsystem/node_modules/globby": { - "version": "14.0.0", - "resolved": "https://registry.npmjs.org/globby/-/globby-14.0.0.tgz", - "integrity": "sha512-/1WM/LNHRAOH9lZta77uGbq0dAEQM+XjNesWwhlERDVenqothRbnzTrL3/LrIoEPPjeUHC3vrS6TwoyxeHs7MQ==", - "dev": true, - "dependencies": { - "@sindresorhus/merge-streams": "^1.0.0", - "fast-glob": "^3.3.2", - "ignore": "^5.2.4", - "path-type": "^5.0.0", - "slash": "^5.1.0", - "unicorn-magic": "^0.1.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/@pnp/buildsystem/node_modules/path-type": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/path-type/-/path-type-5.0.0.tgz", - "integrity": "sha512-5HviZNaZcfqP95rwpv+1HDgUamezbqdSYTyzjTvwtJSnIH+3vnbmWsItli8OFEndS984VT55M3jduxZbX351gg==", - "dev": true, - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/@pnp/buildsystem/node_modules/slash": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/slash/-/slash-5.1.0.tgz", - "integrity": "sha512-ZA6oR3T/pEyuqwMgAKT0/hAv8oAXckzbkmR0UkUosQ+Mc4RxGoJkRmwHgHufaenlyAgE1Mxgpdcrf75y6XcnDg==", - "dev": true, - "engines": { - "node": ">=14.16" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/@pnp/buildsystem/node_modules/webpack": { "version": "5.89.0", "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.89.0.tgz", @@ -736,9 +693,9 @@ } }, "node_modules/@pnp/core": { - "version": "3.21.0", - "resolved": "https://registry.npmjs.org/@pnp/core/-/core-3.21.0.tgz", - "integrity": "sha512-4R+MGo7aitBPUQ4eapLbsiZwPFrMnFZ/6OB4lvXQrtl2IAdZC922gR+bB05O00iEplOcUW9EZJlitLfzTdNuTg==", + "version": "4.0.0-alpha0-v4nightly.20231227", + "resolved": "https://registry.npmjs.org/@pnp/core/-/core-4.0.0-alpha0-v4nightly.20231227.tgz", + "integrity": "sha512-k3oS1swDBL582L7F7YISURVe6RB5U8kgC6E8vzVgbFE6mmW5KV1jP7HG6EhDc6yvAeryL0mPKXvz0OlKu51ntQ==", "dev": true, "dependencies": { "tslib": "2.4.1" @@ -1310,6 +1267,35 @@ } } }, + "node_modules/@typescript-eslint/typescript-estree/node_modules/globby": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/globby/-/globby-11.1.0.tgz", + "integrity": "sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==", + "dev": true, + "dependencies": { + "array-union": "^2.1.0", + "dir-glob": "^3.0.1", + "fast-glob": "^3.2.9", + "ignore": "^5.2.0", + "merge2": "^1.4.1", + "slash": "^3.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/@typescript-eslint/typescript-estree/node_modules/slash": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz", + "integrity": "sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/@typescript-eslint/utils": { "version": "6.4.1", "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-6.4.1.tgz", @@ -3578,32 +3564,47 @@ } }, "node_modules/globby": { - "version": "11.1.0", - "resolved": "https://registry.npmjs.org/globby/-/globby-11.1.0.tgz", - "integrity": "sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==", + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/globby/-/globby-14.0.0.tgz", + "integrity": "sha512-/1WM/LNHRAOH9lZta77uGbq0dAEQM+XjNesWwhlERDVenqothRbnzTrL3/LrIoEPPjeUHC3vrS6TwoyxeHs7MQ==", "dev": true, "dependencies": { - "array-union": "^2.1.0", - "dir-glob": "^3.0.1", - "fast-glob": "^3.2.9", - "ignore": "^5.2.0", - "merge2": "^1.4.1", - "slash": "^3.0.0" + "@sindresorhus/merge-streams": "^1.0.0", + "fast-glob": "^3.3.2", + "ignore": "^5.2.4", + "path-type": "^5.0.0", + "slash": "^5.1.0", + "unicorn-magic": "^0.1.0" }, "engines": { - "node": ">=10" + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/globby/node_modules/path-type": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/path-type/-/path-type-5.0.0.tgz", + "integrity": "sha512-5HviZNaZcfqP95rwpv+1HDgUamezbqdSYTyzjTvwtJSnIH+3vnbmWsItli8OFEndS984VT55M3jduxZbX351gg==", + "dev": true, + "engines": { + "node": ">=12" }, "funding": { "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/globby/node_modules/slash": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz", - "integrity": "sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==", + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/slash/-/slash-5.1.0.tgz", + "integrity": "sha512-ZA6oR3T/pEyuqwMgAKT0/hAv8oAXckzbkmR0UkUosQ+Mc4RxGoJkRmwHgHufaenlyAgE1Mxgpdcrf75y6XcnDg==", "dev": true, "engines": { - "node": ">=8" + "node": ">=14.16" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/graceful-fs": { diff --git a/package.json b/package.json index 06209e047..aedfc0acc 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "@azure/identity": "3.3.0", "@azure/msal-node": "1.18.3", "@microsoft/microsoft-graph-types": "2.38.0", - "@pnp/buildsystem": "^4.0.0-beta5", + "@pnp/buildsystem": "^4.0.0-beta7", "@pnp/logging": "^3.21.0", "@types/chai": "4.3.6", "@types/chai-as-promised": "7.1.6", @@ -26,6 +26,7 @@ "del-cli": "5.1.0", "eslint": "8.49.0", "findup-sync": "5.0.0", + "globby": "^14.0.0", "mocha": "10.2.0", "msal": "1.4.18", "node-fetch": "3.3.2", diff --git a/packages/azidjsclient/tsconfig-commonjs.json b/packages/azidjsclient/tsconfig-commonjs.json new file mode 100644 index 000000000..1f8940be1 --- /dev/null +++ b/packages/azidjsclient/tsconfig-commonjs.json @@ -0,0 +1,12 @@ +{ + "extends": "../tsconfig-commonjs.json", + "include": [ + "./**/*.ts", + "../queryable/**/*.ts" + ], + "references": [ + { + "path": "../queryable/tsconfig-commonjs.json" + } + ] +} \ No newline at end of file diff --git a/packages/core/tsconfig-commonjs.json b/packages/core/tsconfig-commonjs.json new file mode 100644 index 000000000..733a3c9c7 --- /dev/null +++ b/packages/core/tsconfig-commonjs.json @@ -0,0 +1,10 @@ +{ + "extends": "../tsconfig-commonjs.json", + "include": [ + "./**/*.ts" + ], + "compilerOptions": { + "esModuleInterop": true + }, + "references": [] +} \ No newline at end of file diff --git a/packages/graph/tsconfig-commonjs.json b/packages/graph/tsconfig-commonjs.json new file mode 100644 index 000000000..e449c844e --- /dev/null +++ b/packages/graph/tsconfig-commonjs.json @@ -0,0 +1,16 @@ +{ + "extends": "../tsconfig-commonjs.json", + "include": [ + "./**/*.ts", + "../core/**/*.ts", + "../queryable/**/*.ts" + ], + "references": [ + { + "path": "../core/tsconfig-commonjs.json" + }, + { + "path": "../queryable/tsconfig-commonjs.json" + } + ] +} \ No newline at end of file diff --git a/packages/logging/tsconfig-commonjs.json b/packages/logging/tsconfig-commonjs.json new file mode 100644 index 000000000..141ccf4a2 --- /dev/null +++ b/packages/logging/tsconfig-commonjs.json @@ -0,0 +1,7 @@ +{ + "extends": "../tsconfig-commonjs.json", + "include": [ + "./**/*.ts" + ], + "references": [] +} \ No newline at end of file diff --git a/packages/msaljsclient/tsconfig-commonjs.json b/packages/msaljsclient/tsconfig-commonjs.json new file mode 100644 index 000000000..1f8940be1 --- /dev/null +++ b/packages/msaljsclient/tsconfig-commonjs.json @@ -0,0 +1,12 @@ +{ + "extends": "../tsconfig-commonjs.json", + "include": [ + "./**/*.ts", + "../queryable/**/*.ts" + ], + "references": [ + { + "path": "../queryable/tsconfig-commonjs.json" + } + ] +} \ No newline at end of file diff --git a/packages/nodejs/tsconfig-commonjs.json b/packages/nodejs/tsconfig-commonjs.json new file mode 100644 index 000000000..16332a169 --- /dev/null +++ b/packages/nodejs/tsconfig-commonjs.json @@ -0,0 +1,28 @@ +{ + "extends": "../tsconfig-commonjs.json", + "include": [ + "./**/*.ts", + "../core/**/*.ts", + "../graph/**/*.ts", + "../queryable/**/*.ts", + "../logging/**/*.ts", + "../sp/**/*.ts" + ], + "references": [ + { + "path": "../core/tsconfig-commonjs.json" + }, + { + "path": "../graph/tsconfig-commonjs.json" + }, + { + "path": "../logging/tsconfig-commonjs.json" + }, + { + "path": "../queryable/tsconfig-commonjs.json" + }, + { + "path": "../sp/tsconfig-commonjs.json" + } + ] +} \ No newline at end of file diff --git a/packages/queryable/tsconfig-commonjs.json b/packages/queryable/tsconfig-commonjs.json new file mode 100644 index 000000000..aad0a4c72 --- /dev/null +++ b/packages/queryable/tsconfig-commonjs.json @@ -0,0 +1,11 @@ +{ + "extends": "../tsconfig-commonjs.json", + "include": [ + "./**/*.ts" + ], + "references": [ + { + "path": "../core/tsconfig-commonjs.json" + } + ] +} \ No newline at end of file diff --git a/packages/sp-admin/tsconfig-commonjs.json b/packages/sp-admin/tsconfig-commonjs.json new file mode 100644 index 000000000..57b5a3fa3 --- /dev/null +++ b/packages/sp-admin/tsconfig-commonjs.json @@ -0,0 +1,20 @@ +{ + "extends": "../tsconfig-commonjs.json", + "include": [ + "./**/*.ts", + "../core/**/*.ts", + "../queryable/**/*.ts", + "./presets/**/*.ts" + ], + "references": [ + { + "path": "../core/tsconfig-commonjs.json" + }, + { + "path": "../queryable/tsconfig-commonjs.json" + }, + { + "path": "../sp/tsconfig-commonjs.json" + } + ] +} \ No newline at end of file diff --git a/packages/sp/tsconfig-commonjs.json b/packages/sp/tsconfig-commonjs.json new file mode 100644 index 000000000..e449c844e --- /dev/null +++ b/packages/sp/tsconfig-commonjs.json @@ -0,0 +1,16 @@ +{ + "extends": "../tsconfig-commonjs.json", + "include": [ + "./**/*.ts", + "../core/**/*.ts", + "../queryable/**/*.ts" + ], + "references": [ + { + "path": "../core/tsconfig-commonjs.json" + }, + { + "path": "../queryable/tsconfig-commonjs.json" + } + ] +} \ No newline at end of file diff --git a/packages/tsconfig-commonjs.json b/packages/tsconfig-commonjs.json new file mode 100644 index 000000000..f638e5f58 --- /dev/null +++ b/packages/tsconfig-commonjs.json @@ -0,0 +1,38 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "rootDir": ".", + "outDir": "../build/packages/commonjs", + "module": "CommonJS" + }, + "include": [], + "references": [ + { + "path": "./azidjsclient/tsconfig-commonjs.json" + }, + { + "path": "./core/tsconfig-commonjs.json" + }, + { + "path": "./graph/tsconfig-commonjs.json" + }, + { + "path": "./logging/tsconfig-commonjs.json" + }, + { + "path": "./msaljsclient/tsconfig-commonjs.json" + }, + { + "path": "./nodejs/tsconfig-commonjs.json" + }, + { + "path": "./queryable/tsconfig-commonjs.json" + }, + { + "path": "./sp/tsconfig-commonjs.json" + }, + { + "path": "./sp-admin/tsconfig-commonjs.json" + } + ] +} \ No newline at end of file diff --git a/packages/tsconfig.json b/packages/tsconfig.json index be33142ea..995aac1eb 100644 --- a/packages/tsconfig.json +++ b/packages/tsconfig.json @@ -2,7 +2,7 @@ "extends": "../tsconfig.json", "compilerOptions": { "rootDir": ".", - "outDir": "../build/packages" + "outDir": "../build/packages/esm" }, "include": [], "references": [ diff --git a/tools/buildsystem/bin/buildsystem.ts b/tools/buildsystem/bin/buildsystem.ts index 682d6b7aa..a4ebdfc5d 100644 --- a/tools/buildsystem/bin/buildsystem.ts +++ b/tools/buildsystem/bin/buildsystem.ts @@ -33,7 +33,6 @@ BuildSystem.prepare({}, function (env) { context.version = pkg.version; - let name = (args.n || args.name); if (typeof name === "undefined" || name === null || name === "") { // default to build if no name is supplied @@ -84,6 +83,7 @@ BuildSystem.prepare({}, function (env) { resolvedPkgSrcRoot: dirname(resolve(tsconfigRoot, ref.path)), resolvedPkgOutRoot: resolve(resolvedOutDir, dirname(ref.path)), resolvedPkgDistRoot: resolve(context.distRoot, dirname(ref.path)), + relativePkgDistModulePath: resolvedOutDir.replace(dirname(resolvedOutDir), "").replace(/^\\|\//, ""), }))); } else { // we have a single package (debug for example) @@ -93,6 +93,7 @@ BuildSystem.prepare({}, function (env) { resolvedPkgSrcRoot: tsconfigRoot, resolvedPkgOutRoot: resolvedOutDir, resolvedPkgDistRoot: context.distRoot, + relativePkgDistModulePath: context.distRoot, }); } diff --git a/tools/buildsystem/index.ts b/tools/buildsystem/index.ts index 38095fb4d..f77389f8b 100644 --- a/tools/buildsystem/index.ts +++ b/tools/buildsystem/index.ts @@ -7,6 +7,7 @@ export { Publish } from "./src/behaviors/publish.js"; export { ReplaceVersion, IReplaceVersionOptions } from "./src/behaviors/replace-version.js"; export { Webpack } from "./src/behaviors/webpack.js"; export { WritePackageJSON } from "./src/behaviors/write-packagejson.js"; +export { CreateResolutionPackageFiles } from "./src/behaviors/create-resolution-package-files.js"; export { BuildObserver, diff --git a/tools/buildsystem/package.json b/tools/buildsystem/package.json index d2bded099..33bba9a1c 100644 --- a/tools/buildsystem/package.json +++ b/tools/buildsystem/package.json @@ -1,6 +1,6 @@ { "name": "@pnp/buildsystem", - "version": "4.0.0-beta6", + "version": "4.0.0-beta8", "bin": { "pnpbuild": "bin/buildsystem.js" }, diff --git a/tools/buildsystem/src/behaviors/build.ts b/tools/buildsystem/src/behaviors/build.ts index d93bd0186..dbe88b055 100644 --- a/tools/buildsystem/src/behaviors/build.ts +++ b/tools/buildsystem/src/behaviors/build.ts @@ -1,7 +1,7 @@ import { TimelinePipe } from "@pnp/core"; import { exec } from "child_process"; import { resolve } from "path"; -import { BuildTimeline } from "src/build-timeline"; +import { BuildTimeline } from "../build-timeline.js"; const tscPath = resolve("./node_modules/.bin/tsc"); diff --git a/tools/buildsystem/src/behaviors/copy-package-files.ts b/tools/buildsystem/src/behaviors/copy-package-files.ts index 492fe7f0f..294d37a25 100644 --- a/tools/buildsystem/src/behaviors/copy-package-files.ts +++ b/tools/buildsystem/src/behaviors/copy-package-files.ts @@ -6,6 +6,7 @@ import buildCopyFile from "../lib/copy-file.js"; /** * Copies files from the target's directory based on the supplied patterns to the same directory in the dist folder + * @param source 'src' copies files from package src root to dist package root, 'built' copies files from built output to their relative to /packages path within the dist package root (esm/commonjs) * @param pattern glob patterns for files (see https://www.npmjs.com/package/globby) * @returns */ @@ -31,7 +32,7 @@ export function CopyPackageFiles(source: "src" | "built", pattern: string[]): Ti a.push(...temp.map(t => ({ src: resolve(fileSourceRoot, t), - dest: resolve(pkg.resolvedPkgDistRoot, t), + dest: resolve(pkg.resolvedPkgDistRoot, source === "built" ? pkg.relativePkgDistModulePath : "", t), }))); return a; diff --git a/tools/buildsystem/src/behaviors/create-resolution-package-files.ts b/tools/buildsystem/src/behaviors/create-resolution-package-files.ts new file mode 100644 index 000000000..7b14fe798 --- /dev/null +++ b/tools/buildsystem/src/behaviors/create-resolution-package-files.ts @@ -0,0 +1,43 @@ +import { TimelinePipe } from "@pnp/core"; +import { resolve } from "path"; +import { BuildTimeline } from "../build-timeline.js"; +import buildWriteFile from "../lib/write-file.js"; + +/** + * Because the TypeScript team will not make a simple change we have to add package.json's to each package's module sub/folder to ensure the resolution works with just *.js extensions. + * + * It is stupid this has to exist, but well here we are. + * @returns + */ +export function CreateResolutionPackageFiles(): TimelinePipe { + + return (instance: BuildTimeline) => { + + instance.on.package(async function (this: BuildTimeline) { + + this.log("Creating Resolution package.json files.", 1); + + const { target } = this.context; + + const promises = []; + + target.packages.forEach((pkg) => { + + const filePath = resolve(pkg.resolvedPkgDistRoot, pkg.relativePkgDistModulePath, "package.json"); + + let pkgFile = { + name: pkg.name, + type: /commonjs/i.test(target.parsedTSConfig.compilerOptions.module) ? "commonjs" : "module", + } + + this.log(`Writing module resolution package.json for ${filePath} as ${pkgFile.type}`, 0); + + promises.push(buildWriteFile(filePath, JSON.stringify(pkgFile))); + }); + + await Promise.all(promises); + }); + + return instance; + } +} diff --git a/tools/buildsystem/src/behaviors/replace-version.ts b/tools/buildsystem/src/behaviors/replace-version.ts index c530c89ad..97a84b7fe 100644 --- a/tools/buildsystem/src/behaviors/replace-version.ts +++ b/tools/buildsystem/src/behaviors/replace-version.ts @@ -9,7 +9,7 @@ export interface IReplaceVersionOptions { pathsResolved?: boolean; } -export function ReplaceVersion(paths: string[], options: IReplaceVersionOptions): TimelinePipe { +export function ReplaceVersion(paths: string[], options?: IReplaceVersionOptions): TimelinePipe { options = { versionMask: /\$\$Version\$\$/img, diff --git a/tools/buildsystem/src/types.ts b/tools/buildsystem/src/types.ts index 754beeb23..eda33360f 100644 --- a/tools/buildsystem/src/types.ts +++ b/tools/buildsystem/src/types.ts @@ -20,6 +20,7 @@ export interface BuildSchema { export interface TSConfig { compilerOptions: { outDir: string; + module: string; }; references?: { path: string; @@ -43,6 +44,7 @@ export interface IBuildContext { resolvedPkgSrcRoot: string; resolvedPkgOutRoot: string; resolvedPkgDistRoot: string; + relativePkgDistModulePath: string; }[]; }; } diff --git a/tools/buildsystem/tsconfig.json b/tools/buildsystem/tsconfig.json index a06704fd4..8a21a49ac 100644 --- a/tools/buildsystem/tsconfig.json +++ b/tools/buildsystem/tsconfig.json @@ -32,7 +32,6 @@ "resolveJsonModule": true }, "include": [ - "./index.ts", - "./**/*.*" + "./**/*.ts" ] } \ No newline at end of file diff --git a/tools/local-module-resolver/esm-packages.ts b/tools/local-module-resolver/esm-packages.ts index b2d6b29e9..bfd6d94aa 100644 --- a/tools/local-module-resolver/esm-packages.ts +++ b/tools/local-module-resolver/esm-packages.ts @@ -1,2 +1,2 @@ import { createResolve} from "./esm.js"; -export const resolve = createResolve("/build/packages"); +export const resolve = createResolve("/build/packages/esm"); diff --git a/tools/local-module-resolver/esm.ts b/tools/local-module-resolver/esm.ts index 6398cef4a..53c7be86f 100644 --- a/tools/local-module-resolver/esm.ts +++ b/tools/local-module-resolver/esm.ts @@ -18,16 +18,23 @@ export function createResolve(innerPath: string): ResolverFunc { return async function (specifier: string, context: ResolveContext, defaultResolve: ResolverFunc): Promise { - if (specifier.startsWith("@pnp") && specifier !== "@pnp/buildsystem") { + if (specifier.startsWith("@pnp")) { const modulePath = specifier.substring(4); + log(`modulePath: ${modulePath}`); + if (cache.has(modulePath)) { return cache.get(modulePath)!; } let candidate = join(projectRoot, innerPath, modulePath); + // hack to enable debugging the buildsystem + if (modulePath === "/buildsystem") { + candidate = resolve("./build/build-system") + } + if (existsSync(candidate + ".js")) { candidate = candidate + ".js" diff --git a/tsconfig.buildsystem.json b/tsconfig.buildsystem.json index 128fa538d..3fc021c70 100644 --- a/tsconfig.buildsystem.json +++ b/tsconfig.buildsystem.json @@ -8,7 +8,7 @@ "declaration": false, "declarationMap": false, "sourceMap": false, - "allowSyntheticDefaultImports": true, + "allowSyntheticDefaultImports": true }, "include": [ "buildsystem-config.ts"