Skip to content

Instantly share code, notes, and snippets.

@renoirb
Created November 30, 2016 01:19
Show Gist options
  • Save renoirb/eb935d86d58cdf03f487a07deb0c8d83 to your computer and use it in GitHub Desktop.
Save renoirb/eb935d86d58cdf03f487a07deb0c8d83 to your computer and use it in GitHub Desktop.
Attempt making archivator to be bundled using Rollup
'use strict';
/**
* Source: https://medium.com/@tarkus/how-to-build-and-publish-es6-modules-today-with-babel-and-rollup-4426d9c7ca71
*/
const fs = require('fs');
const del = require('del');
const rollup = require('rollup');
const uglify = require('rollup-plugin-uglify');
const commonjs = require('rollup-plugin-commonjs');
const nodeResolve = require('rollup-plugin-node-resolve');
const includePaths = require('rollup-plugin-includepaths');
const pkg = require('../package.json');
const bundles = [
{
label: 'a',
format: 'es',
ext: '.mjs',
plugins: []
},
{
label: 'b',
format: 'umd',
ext: '.js',
plugins: []
},
{
label: 'c',
format: 'umd',
ext: '.min.js',
plugins: [uglify()],
minify: true
}
];
const includePathOptions = {
include: {},
paths: ['src/lib'],
external: [],
extensions: ['.js']
};
let promise = Promise.resolve();
// Clean up the output directory
promise = promise.then(() => del(['dist/*']));
// Compile source code into a distributable format with Babel and Rollup
for (const config of bundles) {
promise = promise.then(() => rollup.rollup({
entry: 'build/index.js',
external: Object.keys(pkg.dependencies),
plugins: [
includePaths(includePathOptions),
nodeResolve({
jsnext: true,
main: true
}),
commonjs({
include: 'node_modules/**'
})
].concat(config.plugins)
}).then(bundle => bundle.write({
dest: `dist/${config.label}/${config.moduleName || 'index'}${config.ext}`,
format: config.format,
sourceMap: !config.minify,
moduleName: config.moduleName
}), fail => console.error(fail)), fail => console.error(fail)).then(() => {
delete pkg.private;
delete pkg.devDependencies;
delete pkg.scripts;
delete pkg.xo;
fs.writeFileSync(`dist/${config.label}/package.json`, JSON.stringify(pkg, null, ' '), 'utf-8');
fs.writeFileSync(`dist/${config.label}/LICENSE.txt`, fs.readFileSync('LICENSE.txt', 'utf-8'), 'utf-8');
}, fail => console.error(fail));
}
promise.then(() => del(['build/*']));
promise.catch(err => console.error(err.stack)); // eslint-disable-line no-console
import { readFileSync, realpathSync } from 'fs';
import json from 'rollup-plugin-json';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import npm from 'rollup-plugin-npm';
import glob from 'glob';
var pkg = JSON.parse( readFileSync( 'package.json', 'utf-8' ) )
, sourceDir = "lib/frontend/"
, destDir = "static/assets/js/"
, banner = readFileSync( 'lib/frontend/_banner.js', 'utf-8' );
banner = banner.replace( '${time}', new Date() );
banner = banner.replace( '${version}', pkg.version );
banner = banner.replace( '${repository}', pkg.repository.url );
var rollemFiles = [];
var entryPoints = glob.sync(sourceDir+"**/entry-*.js")
entryPoints.forEach( match => {
let iter = {
format: 'cjs',
plugins: [
json()
,babel()
,npm({
jsnext: true
,main: true
})
,commonjs({
include: 'node_modules/**'
})
],
sourceMap: true,
banner: banner
};
iter.dest = match.replace(sourceDir, destDir).replace('entry-','');
iter.entry = match;
rollemFiles.push(iter);
});
export default rollemFiles;
import glob from 'glob';
import json from 'rollup-plugin-json';
import babel from 'rollup-plugin-babel';
import npm from 'rollup-plugin-npm';
import commonjs from 'rollup-plugin-commonjs';
var rollemFiles = []
, sourceDir = "lib/frontend/"
, destDir = "static/assets/js/";
glob.sync( sourceDir + "**/entry-*.js").forEach( match => {
let iter = {};
iter.dest = match.replace(sourceDir, destDir).replace("entry-","");
iter.entry = match;
iter.format = 'cjs';
iter.plugins = [
json()
,babel()
,npm({
jsnext: true
,main: true
})
,commonjs({
include: 'node_modules/**'
})
];
iter.sourceMap = true;
rollemFiles.push(iter);
});
export default rollemFiles;
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const join = require('path').join
const argv = require('minimist')(process.argv.slice(2))
const configFilename = argv.c || 'rollem.config.js'
const configName = join(process.cwd(), configFilename)
if (!fs.existsSync(configName)) {
console.error('Cannot find', configName)
process.exit(-1)
}
const rollup = require('rollup')
const rollem = require(join(__dirname, '..'))
function isWatchArgument (arg) {
return arg === '-w' || arg === '--watch'
}
const isWatching = process.argv.some(isWatchArgument)
const options = {
watch: isWatching
}
rollup.rollup({
entry: configFilename
}).then(function (bundle) {
const {code} = bundle.generate({
format: 'cjs'
})
const config = eval(code) // eslint-disable-line no-eval
rollem(config, options)
.catch((err) => {
console.error('Problem rolling them')
console.error(err.message)
console.error(err.stack)
process.exit(-1)
})
}).catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment