-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
build.ts
211 lines (206 loc) · 6.29 KB
/
build.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import rollup from 'rollup';
import { build, IBuildTargetFormat, watchAndReBuild, copy } from './shared';
import { args } from './args';
import packageJson from '../package.json';
import { IRollupWatchOptions } from './interfaces';
import * as path from 'path';
import ChildProcess from 'child_process';
import * as fs from 'fs';
const BASE_DIR = process.cwd();
const DIST_DIR = path.join(BASE_DIR, 'dist');
const NODE_MODULES = 'node_modules';
const LIB_NAME = 'aurelia-i18n';
const DIST_FILE_NAME = `${LIB_NAME}.js`;
const TYPE_DIST_FILE_NAME = `${LIB_NAME}.d.ts`;
const ENTRY_PATH = `src/${LIB_NAME}.ts`;
const EXTERNAL_LIBS = [
'aurelia-framework',
'i18next',
...Object
.keys({ ...packageJson.dependencies, ...packageJson.devDependencies })
.filter(dev => /^(?:aurelia)/.test(dev) && dev !== LIB_NAME)
];
const configs: Record<IBuildTargetFormat, { input: string; outputs: rollup.OutputOptions[]; tsConfig?: { target: string } }> = {
es2017: {
input: ENTRY_PATH,
outputs: [
{ file: `dist/es2017/${DIST_FILE_NAME}`, format: 'esm' }
]
},
es2015: {
input: ENTRY_PATH,
outputs: [
{ file: `dist/es2015/${DIST_FILE_NAME}`, format: 'esm' },
{
file: `dist/umd-es2015/${DIST_FILE_NAME}`,
format: 'umd',
name: 'au.i18n',
globals: {
'aurelia-framework': 'au',
'aurelia-binding': 'au',
'aurelia-metadata': 'au',
'aurelia-templating': 'au',
'aurelia-loader': 'au',
'aurelia-event-aggregator': 'au',
'aurelia-pal': 'au',
'aurelia-templating-resources': 'au',
'aurelia-logging': 'au.LogManager',
'aurelia-dependency-injection': 'au',
'i18next': 'i18next'
}
}
]
},
es5: {
input: ENTRY_PATH,
outputs: [
{ file: `dist/commonjs/${DIST_FILE_NAME}`, format: 'cjs' },
{ file: `dist/amd/${DIST_FILE_NAME}`, format: 'amd', amd: { id: LIB_NAME } },
{ file: `dist/native-modules/${DIST_FILE_NAME}`, format: 'esm' },
{
file: `dist/umd/${DIST_FILE_NAME}`,
format: 'umd',
name: 'au.i18n',
globals: {
'aurelia-framework': 'au',
'aurelia-binding': 'au',
'aurelia-metadata': 'au',
'aurelia-templating': 'au',
'aurelia-loader': 'au',
'aurelia-event-aggregator': 'au',
'aurelia-pal': 'au',
'aurelia-templating-resources': 'au',
'aurelia-logging': 'au.LogManager',
'aurelia-dependency-injection': 'au',
'i18next': 'i18next'
}
},
{ file: `dist/system/${DIST_FILE_NAME}`, format: 'system' }
]
},
amd: {
input: ENTRY_PATH,
outputs: [
{ file: `dist/amd/${DIST_FILE_NAME}`, format: 'amd', amd: { id: LIB_NAME } }
],
tsConfig: {
target: 'es5'
}
},
system: {
input: ENTRY_PATH,
outputs: [
{ file: `dist/system/${DIST_FILE_NAME}`, format: 'system' }
],
tsConfig: {
target: 'es5'
}
},
commonjs: {
input: ENTRY_PATH,
outputs: [
{ file: `dist/commonjs/${DIST_FILE_NAME}`, format: 'cjs' }
],
tsConfig: {
target: 'es5'
}
},
'native-modules': {
input: ENTRY_PATH,
outputs: [
{ file: `dist/native-modules/${DIST_FILE_NAME}`, format: 'esm' }
],
tsConfig: {
target: 'es5'
}
}
};
if (args.dev) {
// watch mode
let generateDtsTO: any;
const targetFormats: IBuildTargetFormat[] = args.format || ['commonjs'];
const options = targetFormats.reduce((formats, targetFormat) => {
const { outputs, tsConfig } = configs[targetFormat];
formats[targetFormat] = {
input: ENTRY_PATH,
external: EXTERNAL_LIBS,
output: outputs,
tsConfig
};
return formats;
}, {} as Record<IBuildTargetFormat, IRollupWatchOptions>);
console.log('=============\nBuilding Started\n=============');
watchAndReBuild(
options,
() => {
console.log('=============\nFinished building\n=============');
clearTimeout(generateDtsTO);
generateDtsTO = setTimeout(() => {
generateDts().then(() => {
if (args.target) {
copyToTargetProject(targetFormats, args.target);
}
});
}, 1000);
}
);
} else {
// Single build
const targetFormats: IBuildTargetFormat[] = args.format || ['es5', 'es2015', 'es2017'];
Promise
.all(targetFormats.map(target => {
const { outputs, tsConfig, ...options } = configs[target];
return build(target, { ...options, external: EXTERNAL_LIBS }, outputs);
}))
.then(() => generateDts())
.then(() => {
if (args.target) {
copyToTargetProject(targetFormats, args.target);
}
})
.catch(ex => {
console.log(ex);
});
}
async function generateDts(): Promise<void> {
console.log('\n==============\nGenerating dts bundle...\n==============');
return new Promise<void>((resolve, reject) => {
ChildProcess.exec(`npm run build:dts`, (err, stdout, stderr) => {
if (err || stderr) {
console.log('Generating dts error:');
console.log(stderr);
} else {
console.log('Generated dts bundle successfully');
console.log(stdout);
}
try {
fixI18nDefaultImport(path.resolve(DIST_DIR, TYPE_DIST_FILE_NAME));
} catch (ex) {
console.log('Failure fixing default import.');
reject(ex);
}
resolve();
});
});
}
async function fixI18nDefaultImport(typeDefFileName: string) {
const importDeclaration = `import i18next from 'i18next';`;
const data = fs.readFileSync(typeDefFileName, 'utf-8')
.replace("import { i18next } from 'i18next';", importDeclaration);
const fd = fs.openSync(typeDefFileName, 'w+');
fs.writeSync(fd, Buffer.from(data, 'utf8'));
}
function copyToTargetProject(targetFormats: string[], targetProject: string) {
console.log('=============\nCopying to target\n=============');
targetFormats.forEach((targetFormat) => {
copy(
path.join(DIST_DIR, targetFormat, DIST_FILE_NAME),
path.join(BASE_DIR, targetProject, NODE_MODULES, LIB_NAME, 'dist', targetFormat, DIST_FILE_NAME)
);
});
copy(
path.join(DIST_DIR, TYPE_DIST_FILE_NAME),
path.join(BASE_DIR, targetProject, NODE_MODULES, LIB_NAME, 'dist', TYPE_DIST_FILE_NAME)
);
console.log('=============\nCopied to target\n=============');
}