@ -3,10 +3,13 @@
|
|||||||
*/
|
*/
|
||||||
// Load modules
|
// Load modules
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const cp = require('child_process');
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
const util = require('./util');
|
const util = require('./util');
|
||||||
|
const { rollup } = require('rollup');
|
||||||
|
const babel = require('rollup-plugin-babel');
|
||||||
|
const minify = require('rollup-plugin-babel-minify');
|
||||||
|
// Check Travis builds - needs some extra work
|
||||||
if (util.isTravisCI() && util.isNotTravisCronOrAPI()) {
|
if (util.isTravisCI() && util.isNotTravisCronOrAPI()) {
|
||||||
console.log(
|
console.log(
|
||||||
`${chalk.green('NOBUILD')} Module build terminated, not a cron job or a custom build!`
|
`${chalk.green('NOBUILD')} Module build terminated, not a cron job or a custom build!`
|
||||||
@ -15,51 +18,82 @@ if (util.isTravisCI() && util.isNotTravisCronOrAPI()) {
|
|||||||
}
|
}
|
||||||
// Set variables for paths
|
// Set variables for paths
|
||||||
const SNIPPETS_PATH = './snippets';
|
const SNIPPETS_PATH = './snippets';
|
||||||
|
const SNIPPETS_ARCHIVE_PATH = './snippets_archive';
|
||||||
const TEMP_PATH = './temp';
|
const TEMP_PATH = './temp';
|
||||||
const IMPORTS = './imports.js';
|
const IMPORTS = './imports.js';
|
||||||
|
const MODULE_NAME = '_30s';
|
||||||
|
const DIST = './dist';
|
||||||
// Regex for selecting code blocks
|
// Regex for selecting code blocks
|
||||||
const codeRE = /```\s*js([\s\S]*?)```/;
|
const codeRE = /```\s*js([\s\S]*?)```/;
|
||||||
// Start the timer of the script
|
// Read snippets, build packages
|
||||||
console.time('Packager');
|
(async () => {
|
||||||
// Load tag data from the database and snippets from their folder
|
// Start the timer of the script
|
||||||
try {
|
console.time('Packager');
|
||||||
|
try {
|
||||||
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 snippets = fs.readdirSync(SNIPPETS_PATH);
|
const snippets = fs.readdirSync(SNIPPETS_PATH);
|
||||||
// Create `temp` folder if it doesn't already exist.
|
const archivedSnippets = fs.readdirSync(SNIPPETS_ARCHIVE_PATH);
|
||||||
if (!fs.existsSync(TEMP_PATH)) {
|
// Create `temp` and `dist` folders if they don't already exist.
|
||||||
fs.mkdirSync(TEMP_PATH);
|
if (!fs.existsSync(TEMP_PATH)) fs.mkdirSync(TEMP_PATH);
|
||||||
}
|
if (!fs.existsSync(DIST)) fs.mkdirSync(DIST);
|
||||||
// Write `imports.js`
|
// Write `imports.js`
|
||||||
fs.writeFileSync(IMPORTS, '');
|
fs.writeFileSync(IMPORTS, '');
|
||||||
// Read all snippets and store them appropriately
|
|
||||||
for (const snippet of snippets) {
|
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', '');
|
||||||
// Check if a snippet is Node-only
|
|
||||||
const isNodeSnippet = tagDatabase
|
|
||||||
.slice(tagDatabase.indexOf(snippetName) + snippetName.length + 1)
|
|
||||||
.split('\n')[0]
|
|
||||||
.includes('node');
|
|
||||||
// Read `imports.js` and write the data
|
|
||||||
const importData = fs.readFileSync(IMPORTS, 'utf8');
|
|
||||||
fs.writeFileSync(
|
|
||||||
IMPORTS,
|
|
||||||
importData + `\nexport { ${snippetName} } from './temp/${snippetName}.js'`
|
|
||||||
);
|
|
||||||
// Find the code in each snippet
|
|
||||||
const code = snippetData.match(codeRE)[1].replace('\n', '');
|
const code = snippetData.match(codeRE)[1].replace('\n', '');
|
||||||
// Store the data to be written
|
const toWrite = nodeSnippets.includes(snippetName)
|
||||||
const toWrite = isNodeSnippet
|
|
||||||
? `${code
|
? `${code
|
||||||
.replace(`const ${snippetName}`, `export const ${snippetName}`)
|
.replace(`const ${snippetName}`, `export const ${snippetName}`)
|
||||||
// Prevents errors from being thrown in browser environment
|
// Prevents errors from being thrown in browser environment
|
||||||
.replace('require(', 'typeof require !== "undefined" && require(')}`
|
.replace('require(', 'typeof require !== "undefined" && require(')}`
|
||||||
: `export ${code}`;
|
: `export ${code}`;
|
||||||
// Write data to the proper file
|
// Write data to file and append to the imports file
|
||||||
fs.writeFileSync(`${TEMP_PATH}/${snippetName}.js`, toWrite);
|
fs.writeFileSync(`${TEMP_PATH}/${snippetName}.js`, toWrite);
|
||||||
}
|
fs.appendFileSync(IMPORTS, `\nexport { ${snippetName} } from './temp/${snippetName}.js'`);
|
||||||
|
})
|
||||||
|
|
||||||
// Write to the proper files and start the `rollup` script
|
// Write to the proper files and start the `rollup` script
|
||||||
cp.execSync('node ./scripts/rollup.js');
|
const es5 = babel({
|
||||||
|
presets: ['@babel/preset-env']
|
||||||
|
});
|
||||||
|
const min = minify({ comments: false });
|
||||||
|
const bundle = await rollup({ input: IMPORTS });
|
||||||
|
const bundleES5 = await rollup({ input: IMPORTS, plugins: [es5] });
|
||||||
|
const bundleES5Min = await rollup({
|
||||||
|
input: IMPORTS,
|
||||||
|
plugins: [es5, min]
|
||||||
|
});
|
||||||
|
// UMD ES2018
|
||||||
|
await bundle.write({
|
||||||
|
file: `${DIST}/${MODULE_NAME}.js`,
|
||||||
|
name: MODULE_NAME,
|
||||||
|
format: 'umd'
|
||||||
|
});
|
||||||
|
|
||||||
|
// ESM ES2018
|
||||||
|
await bundle.write({
|
||||||
|
file: `${DIST}/${MODULE_NAME}.esm.js`,
|
||||||
|
name: MODULE_NAME,
|
||||||
|
format: 'es'
|
||||||
|
});
|
||||||
|
|
||||||
|
// UMD ES5
|
||||||
|
await bundleES5.write({
|
||||||
|
file: `${DIST}/${MODULE_NAME}.es5.js`,
|
||||||
|
name: MODULE_NAME,
|
||||||
|
format: 'umd'
|
||||||
|
});
|
||||||
|
|
||||||
|
// UMD ES5 min
|
||||||
|
await bundleES5Min.write({
|
||||||
|
file: `${DIST}/${MODULE_NAME}.es5.min.js`,
|
||||||
|
name: MODULE_NAME,
|
||||||
|
format: 'umd'
|
||||||
|
});
|
||||||
|
|
||||||
// Clean up temporary data
|
// Clean up temporary data
|
||||||
fs.removeSync(TEMP_PATH);
|
fs.removeSync(TEMP_PATH);
|
||||||
fs.unlink(IMPORTS);
|
fs.unlink(IMPORTS);
|
||||||
@ -67,8 +101,9 @@ try {
|
|||||||
console.log(`${chalk.green('SUCCESS!')} Snippet module built!`);
|
console.log(`${chalk.green('SUCCESS!')} Snippet module built!`);
|
||||||
// Log the time taken
|
// Log the time taken
|
||||||
console.timeEnd('Packager');
|
console.timeEnd('Packager');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Handle errors (hopefully not!)
|
// Handle errors (hopefully not!)
|
||||||
console.log(`${chalk.red('ERROR!')} During module creation: ${err}`);
|
console.log(`${chalk.red('ERROR!')} During module creation: ${err}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
})();
|
||||||
@ -1,56 +0,0 @@
|
|||||||
/*
|
|
||||||
Part of the process for building the `_30s` module.
|
|
||||||
*/
|
|
||||||
// Load modules
|
|
||||||
const fs = require('fs-extra');
|
|
||||||
const { rollup } = require('rollup');
|
|
||||||
const babel = require('rollup-plugin-babel');
|
|
||||||
const minify = require('rollup-plugin-babel-minify');
|
|
||||||
// Set variables for paths
|
|
||||||
const INPUT_FILE = './imports.js';
|
|
||||||
const MODULE_NAME = '_30s';
|
|
||||||
const DIST = './dist';
|
|
||||||
// Create `dist` folder if not existing
|
|
||||||
if (!fs.existsSync(DIST)) fs.mkdirSync(DIST);
|
|
||||||
// Setup babel and minification
|
|
||||||
const es5 = babel({
|
|
||||||
presets: ['@babel/preset-env']
|
|
||||||
});
|
|
||||||
const min = minify({ comments: false });
|
|
||||||
// Create the bundles
|
|
||||||
(async () => {
|
|
||||||
const bundle = await rollup({ input: INPUT_FILE });
|
|
||||||
const bundleES5 = await rollup({ input: INPUT_FILE, plugins: [es5] });
|
|
||||||
const bundleES5Min = await rollup({
|
|
||||||
input: INPUT_FILE,
|
|
||||||
plugins: [es5, min]
|
|
||||||
});
|
|
||||||
|
|
||||||
// UMD ES2018
|
|
||||||
await bundle.write({
|
|
||||||
file: `${DIST}/${MODULE_NAME}.js`,
|
|
||||||
name: MODULE_NAME,
|
|
||||||
format: 'umd'
|
|
||||||
});
|
|
||||||
|
|
||||||
// ESM ES2018
|
|
||||||
await bundle.write({
|
|
||||||
file: `${DIST}/${MODULE_NAME}.esm.js`,
|
|
||||||
name: MODULE_NAME,
|
|
||||||
format: 'es'
|
|
||||||
});
|
|
||||||
|
|
||||||
// UMD ES5
|
|
||||||
await bundleES5.write({
|
|
||||||
file: `${DIST}/${MODULE_NAME}.es5.js`,
|
|
||||||
name: MODULE_NAME,
|
|
||||||
format: 'umd'
|
|
||||||
});
|
|
||||||
|
|
||||||
// UMD ES5 min
|
|
||||||
await bundleES5Min.write({
|
|
||||||
file: `${DIST}/${MODULE_NAME}.es5.min.js`,
|
|
||||||
name: MODULE_NAME,
|
|
||||||
format: 'umd'
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
Reference in New Issue
Block a user