forked from Chalarangelo/30-seconds-of-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.js
More file actions
144 lines (123 loc) · 4.19 KB
/
Copy pathmodule.js
File metadata and controls
144 lines (123 loc) · 4.19 KB
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
/**
* Builds the `_30s` module in UMD and ESM formats.
* Also builds the test module file for testing snippets.
*/
const fs = require('fs-extra');
const path = require('path');
const { green, red } = require('kleur');
const util = require('./util');
const { rollup } = require('rollup');
const babel = require('rollup-plugin-babel');
const minify = require('rollup-plugin-babel-minify');
const config = require('../config');
const MODULE_NAME = `./${config.moduleName}`;
const SNIPPETS_PATH = `./${config.snippetPath}`;
const SNIPPETS_ARCHIVE_PATH = `./${config.snippetArchivePath}`;
const DIST_PATH = `./${config.distPath}`;
const ROLLUP_INPUT_FILE = `./${config.rollupInputFile}`;
const TEST_MODULE_FILE = `./${config.testModuleFile}`;
const CODE_RE = /```\s*js([\s\S]*?)```/;
/**
* Builds the UMD + ESM files to the ./dist directory.
*/
async function doRollup() {
// Plugins
const es5 = babel({ presets: ['@babel/preset-env'], plugins: ['transform-object-rest-spread'] });
const min = minify({ comments: false });
const output = format => file => ({
format,
file,
name: MODULE_NAME
});
const umd = output('umd');
const esm = output('es');
const bundle = await rollup({ input: ROLLUP_INPUT_FILE });
const bundleES5 = await rollup({ input: ROLLUP_INPUT_FILE, plugins: [es5] });
const bundleES5Min = await rollup({
input: ROLLUP_INPUT_FILE,
plugins: [es5, min]
});
const baseName = `${DIST_PATH}/${MODULE_NAME}`;
// UMD ES2018
await bundle.write(umd(`${baseName}.js`));
// ESM ES2018
await bundle.write(esm(`${baseName}.esm.js`));
// UMD ES5
await bundleES5.write(umd(`${baseName}.es5.js`));
// UMD ES5 min
await bundleES5Min.write(umd(`${baseName}.es5.min.js`));
}
/**
* Starts the build process.
*/
async function build() {
console.time('Packager');
let requires = [];
let esmExportString = '';
let cjsExportString = '';
try {
if (!fs.existsSync(DIST_PATH)) fs.mkdirSync(DIST_PATH);
fs.writeFileSync(ROLLUP_INPUT_FILE, '');
fs.writeFileSync(TEST_MODULE_FILE, '');
// Synchronously read all snippets from snippets folder and sort them as necessary (case-insensitive)
snippets = util.readSnippets(SNIPPETS_PATH);
snippetsArray = Object.keys(snippets).reduce((acc, key) => {
acc.push(snippets[key]);
return acc;
}, []);
archivedSnippets = util.readSnippets(SNIPPETS_ARCHIVE_PATH);
archivedSnippetsArray = Object.keys(archivedSnippets).reduce((acc, key) => {
acc.push(archivedSnippets[key]);
return acc;
}, []);
[...snippetsArray, ...archivedSnippetsArray].forEach(snippet => {
let code = `${snippet.attributes.codeBlocks.es6}\n`;
if(snippet.attributes.tags.includes('node')) {
requires.push(code.match(/const.*=.*require\(([^\)]*)\);/g));
code = code.replace(/const.*=.*require\(([^\)]*)\);/g, '');
}
esmExportString += `export ${code}`;
cjsExportString += code;
});
requires = [
...new Set(
requires
.filter(Boolean)
.map(v =>
v[0].replace(
'require(',
'typeof require !== "undefined" && require('
)
)
)
].join('\n');
fs.writeFileSync(ROLLUP_INPUT_FILE, `${requires}\n\n${esmExportString}`);
const testExports = `module.exports = {${[...snippetsArray, ...archivedSnippetsArray]
.map(v => v.id)
.join(',')}}`;
fs.writeFileSync(
TEST_MODULE_FILE,
`${requires}\n\n${cjsExportString}\n\n${testExports}`
);
// Check Travis builds - Will skip builds on Travis if not CRON/API
if (util.isTravisCI() && util.isNotTravisCronOrAPI()) {
fs.unlink(ROLLUP_INPUT_FILE);
console.log(
`${green(
'NOBUILD'
)} Module build terminated, not a cron job or a custom build!`
);
console.timeEnd('Packager');
process.exit(0);
}
await doRollup();
// Clean up the temporary input file Rollup used for building the module
fs.unlink(ROLLUP_INPUT_FILE);
console.log(`${green('SUCCESS!')} Snippet module built!`);
console.timeEnd('Packager');
} catch (err) {
console.log(`${red('ERROR!')} During module creation: ${err}`);
process.exit(1);
}
}
build();