Skip to content

Commit 053388d

Browse files
author
Orta
authored
Merge pull request #33584 from orta/small_ts
Creates a smaller build of TypeScript which just has the language services
2 parents fcd9334 + 4c4a683 commit 053388d

6 files changed

+147
-3
lines changed

.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ scripts/buildProtocol.js
2222
scripts/ior.js
2323
scripts/authors.js
2424
scripts/configurePrerelease.js
25+
scripts/configureTSCBuild.js
2526
scripts/open-user-pr.js
2627
scripts/open-cherry-pick-pr.js
2728
scripts/processDiagnosticMessages.d.ts
@@ -45,4 +46,4 @@ TEST-results.xml
4546
package-lock.json
4647
tests
4748
.vscode
48-
.git
49+
.git

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ scripts/buildProtocol.js
4545
scripts/ior.js
4646
scripts/authors.js
4747
scripts/configurePrerelease.js
48+
scripts/configureLanguageServiceBuild.js
49+
scripts/createLanguageServiceBuild.js
4850
scripts/open-user-pr.js
4951
scripts/open-cherry-pick-pr.js
5052
scripts/processDiagnosticMessages.d.ts
@@ -92,4 +94,4 @@ tests/cases/user/webpack/webpack
9294
tests/cases/user/puppeteer/puppeteer
9395
tests/cases/user/axios-src/axios-src
9496
tests/cases/user/prettier/prettier
95-
.eslintcache
97+
.eslintcache

.npmignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ yarn.lock
3131
CONTRIBUTING.md
3232
TEST-results.xml
3333
.dockerignore
34-
Dockerfile
34+
Dockerfile
35+
.DS_Store
36+
.eslintrc.json
37+
.yarnrc
38+
tmp

Gulpfile.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ const configureExperimental = () => exec(process.execPath, ["scripts/configurePr
590590
task("configure-experimental", series(buildScripts, configureExperimental));
591591
task("configure-experimental").description = "Runs scripts/configurePrerelease.ts to prepare a build for experimental publishing";
592592

593+
const createLanguageServicesBuild = () => exec(process.execPath, ["scripts/createLanguageServicesBuild.js"]);
594+
task("create-language-services-build", series(buildScripts, createLanguageServicesBuild));
595+
task("create-language-services-build").description = "Runs scripts/createLanguageServicesBuild.ts to prepare a build which only has the require('typescript') JS.";
596+
593597
const publishNightly = () => exec("npm", ["publish", "--tag", "next"]);
594598
task("publish-nightly", series(task("clean"), task("LKG"), task("clean"), task("runtests-parallel"), publishNightly));
595599
task("publish-nightly").description = "Runs `npm publish --tag next` to create a new nightly build on npm";
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/// <reference types="node"/>
2+
import { normalize, dirname, join } from "path";
3+
import { readFileSync, writeFileSync, unlinkSync, existsSync } from "fs";
4+
import assert = require("assert");
5+
import { execSync } from "child_process";
6+
const args = process.argv.slice(2);
7+
8+
/**
9+
* A minimal description for a parsed package.json object.
10+
*/
11+
interface PackageJson {
12+
name: string;
13+
bin: {};
14+
main: string;
15+
scripts: {
16+
prepare: string
17+
postpublish: string
18+
}
19+
}
20+
21+
function main(): void {
22+
if (args.length < 1) {
23+
console.log("Usage:");
24+
console.log("\tnode configureTSCBuild.js <package.json location>");
25+
return;
26+
}
27+
28+
// Acquire the version from the package.json file and modify it appropriately.
29+
const packageJsonFilePath = normalize(args[0]);
30+
const packageJsonValue: PackageJson = JSON.parse(readFileSync(packageJsonFilePath).toString());
31+
32+
// Remove the bin section from the current package
33+
delete packageJsonValue.bin;
34+
// We won't be running eslint which would run before publishing
35+
delete packageJsonValue.scripts.prepare;
36+
// No infinite loops
37+
delete packageJsonValue.scripts.postpublish;
38+
39+
// Set the new name
40+
packageJsonValue.name = "@typescript/language-services";
41+
42+
writeFileSync(packageJsonFilePath, JSON.stringify(packageJsonValue, /*replacer:*/ undefined, /*space:*/ 4));
43+
44+
// Remove the files which aren't use when just using the API
45+
const toRemove = [
46+
// JS Files
47+
"tsserver.js",
48+
"tsserverlibrary.js",
49+
"typescriptServices.js",
50+
"typingsInstaller.js",
51+
"tsc.js",
52+
// DTS files
53+
"typescriptServices.d.ts",
54+
"tsserverlibrary.d.ts"
55+
];
56+
57+
// Get a link to the main dependency JS file
58+
const lib = join(dirname(packageJsonFilePath), packageJsonValue.main);
59+
const libPath = dirname(lib);
60+
61+
// Remove the sibling JS large files referenced above
62+
toRemove.forEach(file => {
63+
const path = join(libPath, file);
64+
if (existsSync(path)) unlinkSync(path);
65+
});
66+
67+
// Remove VS-specific localization keys
68+
execSync("rm -rf loc", { cwd: dirname(packageJsonFilePath) });
69+
70+
// Remove runnable file reference
71+
execSync("rm -rf bin", { cwd: dirname(packageJsonFilePath) });
72+
73+
///////////////////////////////////
74+
75+
// This section verifies that the build of TypeScript compiles and emits
76+
77+
const ts = require(lib);
78+
const source = "let x: string = 'string'";
79+
80+
const results = ts.transpileModule(source, {
81+
compilerOptions: { module: ts.ModuleKind.CommonJS }
82+
});
83+
84+
assert(results.outputText.trim() === "var x = 'string';", `Running typescript with ${packageJsonValue.name} did not return the expected results, got: ${results.outputText}`);
85+
}
86+
87+
main();

scripts/createLanguageServiceBuild.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/// <reference types="node"/>
2+
import { join } from "path";
3+
import { readFileSync, unlinkSync } from "fs";
4+
import { tmpdir } from "os";
5+
import { execSync, ExecSyncOptions } from "child_process";
6+
import chalk from "chalk";
7+
8+
interface PackageJson {
9+
name: string;
10+
version: string
11+
}
12+
13+
const exec = (cmd: string, opts?: ExecSyncOptions) => {
14+
console.log(chalk.gray(`> ${cmd} ${opts ? JSON.stringify(opts) : ""}`));
15+
execSync(cmd, opts);
16+
};
17+
18+
const step = (msg: string) => {
19+
console.log("\n\n" + chalk.bold("- ") + msg);
20+
};
21+
22+
function main(): void {
23+
console.log(chalk.bold("## Creating the language services build of TypeScript"));
24+
process.stdout.write(chalk.grey("> node /scripts/createLanguageServiceBuild.ts"));
25+
26+
// Create a tarball of the current version
27+
step("Packing the current TypeScript via npm.");
28+
exec("npm pack");
29+
30+
const packageJsonValue: PackageJson = JSON.parse(readFileSync("package.json", "utf8"));
31+
const tarballFileName = `${packageJsonValue.name}-${packageJsonValue.version}.tgz`;
32+
33+
const unzipDir = tmpdir();
34+
step(`Extracting the built version into a temporary folder. ${unzipDir}/package`);
35+
exec(`tar -xvzf ${tarballFileName} -C ${unzipDir}`);
36+
unlinkSync(tarballFileName);
37+
38+
step(`Updating the build metadata`);
39+
const packagePath = join(unzipDir, "package");
40+
exec(`node scripts/configureLanguageServiceBuild.js ${join(packagePath, "package.json")}`);
41+
42+
step(`Deploying the language service`);
43+
exec("npm publish --access public", { cwd: packagePath });
44+
}
45+
46+
main();

0 commit comments

Comments
 (0)