UNPKG

1.95 kBJavaScriptView Raw
1const { readFile } = require('fs-extra');
2
3const getJsPathsExceptTests = require('./helpers/getJsPathsExceptTests');
4const bundleJsImports = require('./helpers/bundleJsImports');
5const transformImportToCjs = require('./helpers/transformImportToCjs');
6const transformEs6ToEs5 = require('./helpers/transformEs6ToEs5');
7const safeWriteFile = require('./helpers/safeWriteFile');
8
9console.log('Building');
10
11(async () => {
12 console.log('Started');
13
14 let context;
15
16 // Outputs `dist/es/es6/` and `dist/es/es5/` from `src/`
17
18 context = await getJsPathsExceptTests('./src');
19
20 await Promise.all(context.map((filePath) => {
21 const es6Path = filePath.replace('/src/', '/dist/es/es6/');
22 const es5Path = filePath.replace('/src/', '/dist/es/es5/');
23
24 return readFile(filePath, 'utf8')
25 .then(safeWriteFile(es6Path))
26 .then(transformEs6ToEs5)
27 .then(safeWriteFile(es5Path));
28 }));
29
30 // Outputs `dist/cjs/es6/` and `dist/cjs/es5/` from `dist/es/es6/`
31
32 context = await getJsPathsExceptTests('./dist/es/es6');
33
34 await Promise.all(context.map((filePath) => {
35 const es6Path = filePath.replace('/dist/es/es6/', '/dist/cjs/es6/');
36 const es5Path = filePath.replace('/dist/es/es6/', '/dist/cjs/es5/');
37
38 return readFile(filePath, 'utf8')
39 .then(transformImportToCjs)
40 .then(safeWriteFile(es6Path))
41 .then(transformEs6ToEs5)
42 .then(safeWriteFile(es5Path));
43 }));
44
45 // Outputs `dist/iife/es6/` and `dist/iife/es5/` from `dist/es/es6/index.js`
46
47 context = ['./dist/es/es6/index.js'];
48
49 await Promise.all(context.map((filePath) => {
50 const es6Path = filePath.replace('/dist/es/es6/', '/dist/iife/es6/');
51 const es5Path = filePath.replace('/dist/es/es6/', '/dist/iife/es5/');
52
53 return Promise.resolve(filePath)
54 .then(bundleJsImports('SlingSdk'))
55 .then(safeWriteFile(es6Path))
56 .then(transformEs6ToEs5)
57 .then(safeWriteFile(es5Path));
58 }));
59
60 console.log('Finished');
61})();