-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbuild.js
120 lines (103 loc) · 3.43 KB
/
build.js
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
const fs = require('fs-extra');
const ts = require('typescript');
const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor');
const skipClosureBuild = process.env.npm_config_skip_closure;
const tscc = require('@tscc/tscc').default;
function outputDeclarations() {
console.log('Starting generation of typings');
const options = {
outDir: './built',
baseUrl: '.',
lib: ['es6', 'es2017', 'esnext.asynciterable'],
target: 'es2017',
module: 'commonjs',
declaration: true,
emitDeclarationOnly: true,
};
const host = ts.createCompilerHost(options);
const program = ts.createProgram(['./src/index.ts'], options, host);
program.emit();
const apiExtractorConfig = ExtractorConfig.loadFileAndPrepare('./api-extractor.json');
const extractorResult = Extractor.invoke(apiExtractorConfig, {});
if (!extractorResult.succeeded) {
throw new Error('Typing extraction failed');
}
console.log('Typings generated');
}
function doTSCCBuild() {
return tscc(
{
modules: {
'dist/fontoxpath-raw': 'src/index.ts',
},
prefix: './',
compilerFlags: {
debug: false,
assume_function_wrapper: true,
compilation_level: 'ADVANCED',
output_wrapper: `function (xspattern, prsc) {
const VERSION='${require('./package.json').version}';
const fontoxpathGlobal = {};
%output%
return fontoxpathGlobal;
}
`,
},
external: { xspattern: 'xspattern', prsc: 'prsc' },
},
'./tsconfig.json',
{
declaration: false,
}
).then(() => console.log('Done'));
}
function doModuleBuild() {
// Feels dirty but works like a charm: get the public exports of the bundle from the typings and generate a bundle that wraps the umd
const api = JSON.parse(fs.readFileSync('dist/fontoxpath.api.json', 'utf-8'));
const fontoxpathAPI = api.members.find((member) => member.kind === 'EntryPoint');
const members = fontoxpathAPI.members.filter(
(member) =>
member.kind === 'Function' || member.kind === 'Variable' || member.kind === 'Enum'
);
// Some part of the compiler is aliasing types with a `_2` postfix. Fix them.
const memberNames = members.map(({ name }) =>
name.endsWith('_2') ? name.substring(0, name.length - 2) : name
);
const exports = memberNames.map((name) => `export const ${name} = fontoxpath.${name};`);
const fontoxpathFunction = fs.readFileSync('./dist/fontoxpath-raw.js', 'utf8');
const fullModule = `import * as xspattern from 'xspattern';
import * as prsc from 'prsc';
const fontoxpath = (${fontoxpathFunction})
.call(typeof window === 'undefined' ? undefined : window, xspattern, prsc);
${exports.join('\n')}
export default fontoxpath;
`;
const umdModule = `(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['xspattern', 'prsc'], factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory(require('xspattern'), require('prsc'));
} else {
// Browser globals (root is window)
// Maybe it is in scope:
root.fontoxpath = factory(root.xspattern, root.prsc);
}
})(this, function (xspattern, prsc) {
return (${fontoxpathFunction})(xspattern, prsc);
});
`;
fs.writeFileSync('./dist/fontoxpath.esm.js', fullModule, 'utf8');
fs.writeFileSync('./dist/fontoxpath.js', umdModule, 'utf8');
}
let chain = Promise.resolve();
if (!skipClosureBuild) {
chain = chain.then(outputDeclarations);
chain = chain.then(doTSCCBuild);
chain = chain.then(doModuleBuild);
}
chain.catch((err) => {
console.error('Err: ' + err);
process.exit(1);
});