Update module.js to cut down I/O operations
This commit is contained in:
@ -33,6 +33,9 @@ const codeRE = /```\s*js([\s\S]*?)```/;
|
|||||||
const tagDatabase = fs.readFileSync('tag_database', 'utf8');
|
const tagDatabase = fs.readFileSync('tag_database', 'utf8');
|
||||||
const nodeSnippets = tagDatabase.split('\n').filter(v => v.search(/:.*node/g) !== -1).map(v => v.slice(0,v.indexOf(':')));
|
const nodeSnippets = tagDatabase.split('\n').filter(v => v.search(/:.*node/g) !== -1).map(v => v.slice(0,v.indexOf(':')));
|
||||||
const snippets = fs.readdirSync(SNIPPETS_PATH);
|
const snippets = fs.readdirSync(SNIPPETS_PATH);
|
||||||
|
const snippetExports = `module.exports = {${snippets.map(v => v.replace('.md', '')).join(',')}}`;
|
||||||
|
let requires = [];
|
||||||
|
let importData = '';
|
||||||
const archivedSnippets = fs.readdirSync(SNIPPETS_ARCHIVE_PATH);
|
const archivedSnippets = fs.readdirSync(SNIPPETS_ARCHIVE_PATH);
|
||||||
// Create `temp` and `dist` folders if they don't already exist.
|
// Create `temp` and `dist` folders if they don't already exist.
|
||||||
if (!fs.existsSync(TEMP_PATH)) fs.mkdirSync(TEMP_PATH);
|
if (!fs.existsSync(TEMP_PATH)) fs.mkdirSync(TEMP_PATH);
|
||||||
@ -43,17 +46,16 @@ const codeRE = /```\s*js([\s\S]*?)```/;
|
|||||||
snippets.forEach(snippet => {
|
snippets.forEach(snippet => {
|
||||||
const snippetData = fs.readFileSync(path.join(SNIPPETS_PATH, snippet), 'utf8');
|
const snippetData = fs.readFileSync(path.join(SNIPPETS_PATH, snippet), 'utf8');
|
||||||
const snippetName = snippet.replace('.md', '');
|
const snippetName = snippet.replace('.md', '');
|
||||||
const code = snippetData.match(codeRE)[1].replace('\n', '');
|
let code = snippetData.match(codeRE)[1].replace('\n', '');
|
||||||
const toWrite = nodeSnippets.includes(snippetName)
|
if (nodeSnippets.includes(snippetName)) {
|
||||||
? `${code
|
requires.push(code.match(/const.*=.*require\(([^\)]*)\);/g));
|
||||||
.replace(`const ${snippetName}`, `export const ${snippetName}`)
|
code = code.replace(/const.*=.*require\(([^\)]*)\);/g,'');
|
||||||
// Prevents errors from being thrown in browser environment
|
}
|
||||||
.replace('require(', 'typeof require !== "undefined" && require(')}`
|
importData += code;
|
||||||
: `export ${code}`;
|
});
|
||||||
// Write data to file and append to the imports file
|
// Write the data to the imports file
|
||||||
fs.writeFileSync(`${TEMP_PATH}/${snippetName}.js`, toWrite);
|
requires = [...new Set(requires.filter(Boolean).map(v => v[0].replace('require(', 'typeof require !== "undefined" && require(')))].join('\n');
|
||||||
fs.appendFileSync(IMPORTS, `\nexport { ${snippetName} } from './temp/${snippetName}.js'`);
|
fs.writeFileSync(IMPORTS, `${requires}\n\n${importData}\n\n${snippetExports}`)
|
||||||
})
|
|
||||||
|
|
||||||
// Write to the proper files and start the `rollup` script
|
// Write to the proper files and start the `rollup` script
|
||||||
const es5 = babel({
|
const es5 = babel({
|
||||||
|
|||||||
Reference in New Issue
Block a user