Skip to content

Commit

Permalink
Ensure dist/utilities/globals/global.d.ts preserves pragma comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Feb 2, 2022
1 parent a69c35f commit e9d389d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
32 changes: 32 additions & 0 deletions config/distSpotFixes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as fs from "fs";
import { EOL } from "os";
import { distDir } from './helpers';

export function applyDistSpotFixes() {
const globalDTsPath = `${distDir}/utilities/globals/global.d.ts`;
const globalDTs = fs.readFileSync(globalDTsPath, "utf8");
const dtsLines = globalDTs.split(EOL);

const found = dtsLines.some((line, i) => {
// As explained in ../src/utilities/globals/global.ts, we actually do want the
// @ts-ignore comment to be propagated to the .d.ts file, so it won't conflict
// with React Native's declaration of a global type for __DEV__.
if (/__DEV__/.test(line)) {
const prevLine = dtsLines[i - 1];
if (!/@ts-ignore/.test(prevLine)) {
const leadingSpace = /^\s*/.exec(line);
dtsLines.splice(i, 0, leadingSpace + "// @ts-ignore");
}
return true;
}
});

if (found) {
const fixed = dtsLines.join(EOL);
if (fixed !== globalDTs) {
fs.writeFileSync(globalDTsPath, fixed);
}
} else {
throw Error(`Could not find/fix __DEV__ type declaration in ${globalDTsPath}`);
}
}
3 changes: 3 additions & 0 deletions config/resolveModuleIds.ts → config/postprocessDist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import * as path from "path";
import resolve from "resolve";
import { distDir, eachFile, reparse, reprint } from './helpers';

import { applyDistSpotFixes } from "./distSpotFixes";
applyDistSpotFixes();

// The primary goal of the 'npm run resolve' script is to make ECMAScript
// modules exposed by Apollo Client easier to consume natively in web browsers,
// without bundling, and without help from package.json files. It accomplishes
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
"scripts": {
"prebuild": "npm run clean",
"build": "tsc",
"postbuild": "npm run update-version && npm run invariants && npm run sourcemaps && npm run rollup && npm run prepdist && npm run resolve && npm run verify-version",
"postbuild": "npm run update-version && npm run invariants && npm run sourcemaps && npm run rollup && npm run prepdist && npm run postprocess-dist && npm run verify-version",
"update-version": "node config/version.js update",
"verify-version": "node config/version.js verify",
"invariants": "ts-node-script config/processInvariants.ts",
"sourcemaps": "ts-node-script config/rewriteSourceMaps.ts",
"rollup": "rollup -c ./config/rollup.config.js",
"prepdist": "node ./config/prepareDist.js",
"resolve": "ts-node-script config/resolveModuleIds.ts",
"postprocess-dist": "ts-node-script config/postprocessDist.ts",
"clean": "rimraf -r dist coverage lib temp",
"ci:precheck": "node config/precheck.js",
"test": "jest --config ./config/jest.config.js",
Expand Down

0 comments on commit e9d389d

Please sign in to comment.