/
build.js
executable file
·78 lines (66 loc) · 1.94 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
#!/usr/bin/env node
/* eslint-disable truc */
import fs from 'fs';
import path from 'path';
import childProcess from 'child_process';
import { LANGUAGES, isCommon } from './src/componentDefinitions';
/*
BUILD SCRIPT
This script builds the following files:
src/components/
src/all-components.js
src/common-components.js
*/
const LIST = Object.keys(LANGUAGES);
/**
* Wrap a JS source in a closure function taking Prism as argument
* @return {String}
*/
function prismClosure(source) {
return `function (Prism) {\n${source}\n}\n`;
}
/**
* Make a JS module that export the given JS expression
* @return {String}
*/
function moduleExport(source) {
return `export default ${source}`;
}
/*
* Generate the sources for closured components
*/
function generateClosuredComponents() {
const componentsDir = './src/components';
// Cleanup
childProcess.execSync(`rm -rf ${componentsDir}`);
// Generate
fs.mkdirSync(componentsDir);
LIST.forEach(name => {
const componentSrcPath = `./node_modules/prismjs/components/prism-${name}.js`;
const componentSrc = fs.readFileSync(componentSrcPath);
const moduleSrc = moduleExport(prismClosure(componentSrc));
const outputPath = path.join(componentsDir, `prism-${name}.js`);
fs.writeFileSync(outputPath, moduleSrc);
});
}
/*
* Generate a module exposing all components
*/
function generateComponentIndex() {
function generateSource(list) {
const requires = list
.map(
name =>
` '${name}': require('./components/prism-${name}.js').default`
)
.join(',\n');
return moduleExport(`{\n${requires}\n};\n`);
}
fs.writeFileSync('./src/all-components.js', generateSource(LIST));
fs.writeFileSync(
'./src/common-components.js',
generateSource(LIST.filter(isCommon))
);
}
generateClosuredComponents();
generateComponentIndex();