Skip to content

Commit 840dd8f

Browse files
authored
fix(deno): Add prepack for deno build (#12700)
fixes #12698 Adds back prepack script just for deno which was removed in #12656 because it's a breaking change for the deno package which relies on the directory structure for their import path.
1 parent f243167 commit 840dd8f

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed

packages/deno/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
"build:types": "run-s deno-types build:types:tsc build:types:bundle",
4242
"build:types:tsc": "tsc -p tsconfig.types.json",
4343
"build:types:bundle": "rollup -c rollup.types.config.mjs",
44-
"build:tarball": "npm pack",
44+
"build:tarball": "node ./scripts/prepack.js && npm pack",
4545
"circularDepCheck": "madge --circular src/index.ts",
46-
"clean": "rimraf build build-types build-test coverage",
46+
"clean": "rimraf build build-types build-test coverage sentry-deno-*.tgz",
4747
"prefix": "yarn deno-types",
4848
"fix": "eslint . --format stylish --fix",
4949
"prelint": "yarn deno-types",
@@ -55,7 +55,7 @@
5555
"test:types": "deno check ./build/index.mjs",
5656
"test:unit": "deno test --allow-read --allow-run",
5757
"test:unit:update": "deno test --allow-read --allow-write --allow-run -- --update",
58-
"yalc:publish": "yalc publish --push --sig"
58+
"yalc:publish": "node ./scripts/prepack.js && yalc publish --push --sig"
5959
},
6060
"volta": {
6161
"extends": "../../package.json"

packages/deno/scripts/prepack.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* eslint-disable no-console */
2+
3+
/**
4+
* This script prepares the central `build` directory for NPM package creation.
5+
* It first copies all non-code files into the `build` directory, including `package.json`, which
6+
* is edited to adjust entry point paths. These corrections are performed so that the paths align with
7+
* the directory structure inside `build`.
8+
*
9+
* TODO(v9): Remove this script and change the Deno SDK to import from build/X.
10+
*/
11+
12+
const fs = require('node:fs');
13+
const path = require('node:path');
14+
15+
const BUILD_DIR = 'build';
16+
17+
const ENTRY_POINTS = ['main', 'module', 'types', 'browser'];
18+
const EXPORT_MAP_ENTRY_POINT = 'exports';
19+
const TYPES_VERSIONS_ENTRY_POINT = 'typesVersions';
20+
21+
const PACKAGE_JSON = 'package.json';
22+
23+
/**
24+
* @typedef {Record<(typeof ENTRY_POINTS)[number], string>} PackageJsonEntryPoints - an object containing module details
25+
*/
26+
27+
/**
28+
* @typedef {Record<string, string>} ConditionalExportEntryPoints - an object containing module details
29+
*/
30+
31+
/**
32+
* @typedef {Record<string, Record<string, string[]>>} TypeVersions - an object containing module details
33+
*/
34+
35+
/**
36+
* @typedef {Partial<ConditionalExportEntryPoints> & Record<string, Partial<ConditionalExportEntryPoints>>} PackageJsonExports - types for `package.json` exports
37+
*/
38+
39+
/**
40+
* @typedef {Record<string, unknown> & PackageJsonEntryPoints & {[EXPORT_MAP_ENTRY_POINT]: PackageJsonExports} & {[TYPES_VERSIONS_ENTRY_POINT]: TypeVersions}} PackageJson - types for `package.json`
41+
*/
42+
43+
/**
44+
* @type {PackageJson}
45+
*/
46+
const pkgJson = require(path.resolve(PACKAGE_JSON));
47+
48+
// check if build dir exists
49+
if (!fs.existsSync(path.resolve(BUILD_DIR))) {
50+
console.error(`\nERROR: Directory '${BUILD_DIR}' does not exist in ${pkgJson.name}.`);
51+
console.error("This script should only be executed after you've run `yarn build`.");
52+
process.exit(1);
53+
}
54+
55+
// package.json modifications
56+
/**
57+
* @type {PackageJson}
58+
*/
59+
const newPkgJson = { ...pkgJson };
60+
61+
// modify entry points to point to correct paths (i.e. strip out the build directory)
62+
ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint => {
63+
newPkgJson[entryPoint] = newPkgJson[entryPoint].replace(`${BUILD_DIR}/`, '');
64+
});
65+
66+
/**
67+
* Recursively traverses the exports object and rewrites all string values to remove the build directory.
68+
*
69+
* @param {PackageJsonExports} exportsObject - the exports object to traverse
70+
* @param {string} key - the key of the current exports object
71+
*/
72+
function rewriteConditionalExportEntryPoint(exportsObject, key) {
73+
const exportsField = exportsObject[key];
74+
if (!exportsField) {
75+
return;
76+
}
77+
78+
if (typeof exportsField === 'string') {
79+
exportsObject[key] = exportsField.replace(`${BUILD_DIR}/`, '');
80+
return;
81+
}
82+
Object.keys(exportsField).forEach(subfieldKey => {
83+
rewriteConditionalExportEntryPoint(exportsField, subfieldKey);
84+
});
85+
}
86+
87+
if (newPkgJson[EXPORT_MAP_ENTRY_POINT]) {
88+
Object.keys(newPkgJson[EXPORT_MAP_ENTRY_POINT]).forEach(key => {
89+
rewriteConditionalExportEntryPoint(newPkgJson[EXPORT_MAP_ENTRY_POINT], key);
90+
});
91+
}
92+
93+
if (newPkgJson[TYPES_VERSIONS_ENTRY_POINT]) {
94+
Object.entries(newPkgJson[TYPES_VERSIONS_ENTRY_POINT]).forEach(([key, val]) => {
95+
newPkgJson[TYPES_VERSIONS_ENTRY_POINT][key] = Object.entries(val).reduce((acc, [key, val]) => {
96+
const newKey = key.replace(`${BUILD_DIR}/`, '');
97+
acc[newKey] = val.map(v => v.replace(`${BUILD_DIR}/`, ''));
98+
return acc;
99+
}, {});
100+
});
101+
}
102+
103+
const newPackageJsonPath = path.resolve(BUILD_DIR, PACKAGE_JSON);
104+
105+
// write modified package.json to file (pretty-printed with 2 spaces)
106+
try {
107+
fs.writeFileSync(newPackageJsonPath, JSON.stringify(newPkgJson, null, 2));
108+
} catch (error) {
109+
console.error(`\nERROR: Error while writing modified ${PACKAGE_JSON} to disk in ${pkgJson.name}:\n`, error);
110+
process.exit(1);
111+
}

0 commit comments

Comments
 (0)