Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
142 lines (123 loc) · 5 KB

File metadata and controls

142 lines (123 loc) · 5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* eslint-disable no-console */
/*
This script prepares the central `build` directory for NPM package creation.
It first copies all non-code files into the `build` directory, including `package.json`, which
is edited to adjust entry point paths. These corrections are performed so that the paths align with
the directory structure inside `build`.
*/
import * as fs from 'fs';
import * as path from 'path';
const NPM_BUILD_DIR = 'build/npm';
const BUILD_DIR = 'build';
const NPM_IGNORE = fs.existsSync('.npmignore') ? '.npmignore' : '../../.npmignore';
const ASSETS = ['README.md', 'LICENSE', 'package.json', NPM_IGNORE] as const;
const ENTRY_POINTS = ['main', 'module', 'types', 'browser'] as const;
const EXPORT_MAP_ENTRY_POINT = 'exports';
const TYPES_VERSIONS_ENTRY_POINT = 'typesVersions';
const packageWithBundles = process.argv.includes('--bundles');
const buildDir = packageWithBundles ? NPM_BUILD_DIR : BUILD_DIR;
type PackageJsonEntryPoints = Record<(typeof ENTRY_POINTS)[number], string>;
interface TypeVersions {
[key: string]: {
[key: string]: string[];
};
}
interface PackageJson extends Record<string, unknown>, PackageJsonEntryPoints {
[EXPORT_MAP_ENTRY_POINT]: {
[key: string]: {
import: string;
require: string;
types: string;
node: string;
browser: string;
default: string;
};
};
[TYPES_VERSIONS_ENTRY_POINT]: TypeVersions;
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkgJson: PackageJson = require(path.resolve('package.json'));
// check if build dir exists
if (!fs.existsSync(path.resolve(buildDir))) {
console.error(`\nERROR: Directory '${buildDir}' does not exist in ${pkgJson.name}.`);
console.error("This script should only be executed after you've run `yarn build`.");
process.exit(1);
}
// copy non-code assets to build dir
ASSETS.forEach(asset => {
const assetPath = path.resolve(asset);
if (!fs.existsSync(assetPath)) {
console.error(`\nERROR: Asset '${asset}' does not exist.`);
process.exit(1);
}
const destinationPath = path.resolve(buildDir, path.basename(asset));
console.log(`Copying ${path.basename(asset)} to ${path.relative('../..', destinationPath)}.`);
fs.copyFileSync(assetPath, destinationPath);
});
// package.json modifications
const newPackageJsonPath = path.resolve(buildDir, 'package.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const newPkgJson: PackageJson = require(newPackageJsonPath);
// modify entry points to point to correct paths (i.e. strip out the build directory)
ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint => {
newPkgJson[entryPoint] = newPkgJson[entryPoint].replace(`${buildDir}/`, '');
});
if (newPkgJson[EXPORT_MAP_ENTRY_POINT]) {
Object.entries(newPkgJson[EXPORT_MAP_ENTRY_POINT]).forEach(([key, val]) => {
newPkgJson[EXPORT_MAP_ENTRY_POINT][key] = Object.entries(val).reduce(
(acc, [key, val]) => {
return { ...acc, [key]: val.replace(`${buildDir}/`, '') };
},
{} as typeof val,
);
});
}
if (newPkgJson[TYPES_VERSIONS_ENTRY_POINT]) {
Object.entries(newPkgJson[TYPES_VERSIONS_ENTRY_POINT]).forEach(([key, val]) => {
newPkgJson[TYPES_VERSIONS_ENTRY_POINT][key] = Object.entries(val).reduce((acc, [key, val]) => {
const newKey = key.replace(`${buildDir}/`, '');
return { ...acc, [newKey]: val.map(v => v.replace(`${buildDir}/`, '')) };
}, {});
});
}
delete newPkgJson.scripts;
delete newPkgJson.volta;
delete newPkgJson.jest;
// write modified package.json to file (pretty-printed with 2 spaces)
try {
fs.writeFileSync(newPackageJsonPath, JSON.stringify(newPkgJson, null, 2));
} catch (error) {
console.error(`\nERROR: Error while writing modified 'package.json' to disk in ${pkgJson.name}:\n`, error);
process.exit(1);
}
async function runPackagePrepack(packagePrepackPath: string): Promise<void> {
const { prepack } = await import(packagePrepackPath);
if (prepack && typeof prepack === 'function') {
const isSuccess = prepack(buildDir);
if (!isSuccess) {
process.exit(1);
}
} else {
console.error(`\nERROR: Could not find a \`prepack\` function in './scripts/prepack.ts' in ${pkgJson.name}.`);
console.error(
'Make sure your package-specific prepack script exports `function prepack(buildDir: string): boolean`.',
);
process.exit(1);
}
}
// execute package specific settings
// 1. check if a script called `<package-root>/scripts/prepack.ts` exists
// if yes, 2.) execute that script for things that are package-specific
async function runPackageSpecificScripts(): Promise<void> {
const packagePrepackPath = path.resolve('scripts', 'prepack.ts');
try {
if (fs.existsSync(packagePrepackPath)) {
await runPackagePrepack(packagePrepackPath);
}
} catch (error) {
console.error(`\nERROR: Error while trying to load and run ./scripts/prepack.ts in ${pkgJson.name}:\n`, error);
process.exit(1);
}
console.log(`\nSuccessfully finished prepack commands for ${pkgJson.name}\n`);
}
void runPackageSpecificScripts();
Morty Proxy This is a proxified and sanitized view of the page, visit original site.