Skip to content

Commit 06e3ced

Browse files
Keen Yee Liauayazhafiz
authored andcommitted
build: Rewrite banner.js in TS (#446)
This commit removes the code duplication in banner.js by rewriting it in TS. The workflow is as follows: 1. Compile banner.ts 2. rollup server/out/banner/banner.js to produce dist/server/banner.js 3. Compile the server 4. rollup server and concat banner.js to produce dist/server/index.js In order to tree-shake unneeded code in banner.ts, a separate tsconfig `server/banner.tsconfig.json` is needed to produce es2015 modules.
1 parent 550493b commit 06e3ced

File tree

7 files changed

+62
-29
lines changed

7 files changed

+62
-29
lines changed

banner.rollup.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = [
2+
{
3+
input: 'server/out/banner/banner.js',
4+
output: {
5+
file: 'dist/server/banner.js',
6+
format: 'cjs',
7+
},
8+
external: [
9+
'path',
10+
],
11+
},
12+
13+
];

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
"title": "Angular Language Service",
2929
"properties": {
3030
"angular.ngdk": {
31-
"type": ["string", "null"],
31+
"type": [
32+
"string",
33+
"null"
34+
],
3235
"default": null,
3336
"description": "Specifies the folder path to @angular/language-service."
3437
},
@@ -52,7 +55,8 @@
5255
],
5356
"main": "./client/out/extension",
5457
"scripts": {
55-
"compile": "tsc -b && rollup -c",
58+
"compile": "yarn compile:banner && tsc -b && rollup -c",
59+
"compile:banner": "tsc -p server/banner.tsconfig.json && rollup -c banner.rollup.config.js",
5660
"compile:test": "tsc -b server/src/tests",
5761
"compile:integration": "tsc -b integration",
5862
"format": "scripts/format.sh",

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = [
2323
output: {
2424
file: 'dist/server/index.js',
2525
format: 'amd',
26-
banner: fs.readFileSync('server/src/banner.js', 'utf8'),
26+
banner: fs.readFileSync('dist/server/banner.js', 'utf8'),
2727
},
2828
external: [
2929
'fs',

server/banner.tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2015",
4+
"module": "es2015",
5+
"moduleResolution": "node",
6+
"sourceMap": false,
7+
"strict": true,
8+
"outDir": "out/banner"
9+
},
10+
"files": [
11+
"src/banner.ts"
12+
]
13+
}

server/src/banner.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

server/src/banner.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {parseCommandLine} from './cmdline_utils';
2+
3+
/**
4+
* This method provides a custom implementation for the AMD loader to resolve
5+
* `typescript` module at runtime.
6+
* @param modules modules to resolve
7+
* @param cb function to invoke with resolved modules
8+
*/
9+
export function define(modules: string[], cb: (...modules: any[]) => void) {
10+
function resolve(packageName: string, paths: string[]) {
11+
try {
12+
return require.resolve(packageName, {paths});
13+
} catch {
14+
}
15+
}
16+
const TSSERVER = 'typescript/lib/tsserverlibrary';
17+
const resolvedModules = modules.map(m => {
18+
if (m === TSSERVER || m === 'typescript') {
19+
const {tsProbeLocations} = parseCommandLine(process.argv);
20+
m = resolve(TSSERVER, tsProbeLocations) || TSSERVER;
21+
}
22+
return require(m);
23+
});
24+
cb(...resolvedModules);
25+
}

server/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
"rootDir": "src"
77
},
88
"include": ["src/*.ts"],
9-
"exclude": ["node_modules"]
9+
"exclude": [
10+
"src/banner.ts",
11+
"node_modules"
12+
]
1013
}

0 commit comments

Comments
 (0)