copy-paste-bookmarks/gulpfile.js

315 lines
9.2 KiB
JavaScript
Raw Normal View History

const pkg = require('./package.json')
const path = require('path')
const glob = require('glob')
const yargs = require('yargs')
2020-03-31 13:23:51 +02:00
const colors = require('colors')
const through = require('through2');
const qunit = require('node-qunit-puppeteer')
const {rollup} = require('rollup')
const {terser} = require('rollup-plugin-terser')
2020-05-26 09:46:50 +02:00
const babel = require('@rollup/plugin-babel').default
const commonjs = require('@rollup/plugin-commonjs')
2020-05-26 09:46:50 +02:00
const resolve = require('@rollup/plugin-node-resolve').default
const sass = require('sass')
const gulp = require('gulp')
const tap = require('gulp-tap')
const zip = require('gulp-zip')
const header = require('gulp-header')
const eslint = require('gulp-eslint')
const minify = require('gulp-clean-css')
const connect = require('gulp-connect')
const autoprefixer = require('gulp-autoprefixer')
const root = yargs.argv.root || '.'
const port = yargs.argv.port || 8000
const host = yargs.argv.host || 'localhost'
const banner = `/*!
2020-05-25 16:21:27 +02:00
* reveal.js ${pkg.version}
* ${pkg.homepage}
* MIT licensed
*
2022-01-03 12:38:51 +01:00
* Copyright (C) 2011-2022 Hakim El Hattab, https://hakim.se
*/\n`
// Prevents warnings from opening too many test pages
process.setMaxListeners(20);
const babelConfig = {
2020-05-26 09:46:50 +02:00
babelHelpers: 'bundled',
ignore: ['node_modules'],
compact: false,
extensions: ['.js', '.html'],
2020-05-26 09:46:50 +02:00
plugins: [
'transform-html-import-to-string'
],
presets: [[
'@babel/preset-env',
{
corejs: 3,
2020-05-26 09:46:50 +02:00
useBuiltIns: 'usage',
modules: false
}
]]
};
2020-05-26 09:46:50 +02:00
// Our ES module bundle only targets newer browsers with
// module support. Browsers are targeted explicitly instead
// of using the "esmodule: true" target since that leads to
// polyfilling older browsers and a larger bundle.
const babelConfigESM = JSON.parse( JSON.stringify( babelConfig ) );
2020-05-26 09:46:50 +02:00
babelConfigESM.presets[0][1].targets = { browsers: [
2021-10-28 14:23:19 +02:00
'last 2 Chrome versions',
'last 2 Safari versions',
'last 2 iOS versions',
'last 2 Firefox versions',
'last 2 Edge versions',
2020-05-26 09:46:50 +02:00
] };
let cache = {};
2020-05-19 19:04:46 +02:00
// Creates a bundle with broad browser support, exposed
// as UMD
gulp.task('js-es5', () => {
return rollup({
cache: cache.umd,
input: 'js/index.js',
2020-05-26 09:46:50 +02:00
plugins: [
resolve(),
commonjs(),
babel( babelConfig ),
terser()
]
}).then( bundle => {
cache.umd = bundle.cache;
return bundle.write({
name: 'Reveal',
file: './dist/reveal.js',
format: 'umd',
banner: banner,
sourcemap: true
});
});
})
// Creates an ES module bundle
gulp.task('js-es6', () => {
return rollup({
cache: cache.esm,
input: 'js/index.js',
2020-05-26 09:46:50 +02:00
plugins: [
resolve(),
commonjs(),
babel( babelConfigESM ),
terser()
]
}).then( bundle => {
cache.esm = bundle.cache;
return bundle.write({
file: './dist/reveal.esm.js',
format: 'es',
banner: banner,
sourcemap: true
});
});
})
gulp.task('js', gulp.parallel('js-es5', 'js-es6'));
// Creates a UMD and ES module bundle for each of our
// built-in plugins
gulp.task('plugins', () => {
return Promise.all([
{ name: 'RevealHighlight', input: './plugin/highlight/plugin.js', output: './plugin/highlight/highlight' },
{ name: 'RevealMarkdown', input: './plugin/markdown/plugin.js', output: './plugin/markdown/markdown' },
{ name: 'RevealSearch', input: './plugin/search/plugin.js', output: './plugin/search/search' },
{ name: 'RevealNotes', input: './plugin/notes/plugin.js', output: './plugin/notes/notes' },
{ name: 'RevealZoom', input: './plugin/zoom/plugin.js', output: './plugin/zoom/zoom' },
{ name: 'RevealMath', input: './plugin/math/plugin.js', output: './plugin/math/math' },
].map( plugin => {
return rollup({
cache: cache[plugin.input],
input: plugin.input,
2020-05-26 09:46:50 +02:00
plugins: [
resolve(),
commonjs(),
babel({
...babelConfig,
ignore: [/node_modules\/(?!(highlight\.js|marked)\/).*/],
}),
terser()
]
}).then( bundle => {
cache[plugin.input] = bundle.cache;
bundle.write({
file: plugin.output + '.esm.js',
name: plugin.name,
format: 'es'
})
bundle.write({
file: plugin.output + '.js',
name: plugin.name,
format: 'umd'
})
});
} ));
})
// a custom pipeable step to transform Sass to CSS
function compileSass() {
return through.obj( ( vinylFile, encoding, callback ) => {
const transformedFile = vinylFile.clone();
sass.render({
data: transformedFile.contents.toString(),
includePaths: ['css/', 'css/theme/template']
}, ( err, result ) => {
if( err ) {
console.log( vinylFile.path );
console.log( err.formatted );
}
else {
transformedFile.extname = '.css';
transformedFile.contents = result.css;
callback( null, transformedFile );
}
});
});
}
gulp.task('css-themes', () => gulp.src(['./css/theme/source/*.{sass,scss}'])
.pipe(compileSass())
.pipe(gulp.dest('./dist/theme')))
gulp.task('css-core', () => gulp.src(['css/reveal.scss'])
.pipe(compileSass())
.pipe(autoprefixer())
.pipe(minify({compatibility: 'ie9'}))
.pipe(header(banner))
.pipe(gulp.dest('./dist')))
gulp.task('css', gulp.parallel('css-themes', 'css-core'))
gulp.task('qunit', () => {
2020-04-15 11:07:20 +02:00
let serverConfig = {
root,
port: 8009,
2021-10-05 22:48:46 +02:00
host: 'localhost',
2020-04-15 11:07:20 +02:00
name: 'test-server'
}
let server = connect.server( serverConfig )
2020-04-15 11:07:20 +02:00
let testFiles = glob.sync('test/*.html' )
2020-03-31 14:06:38 +02:00
let totalTests = 0;
let failingTests = 0;
let tests = Promise.all( testFiles.map( filename => {
return new Promise( ( resolve, reject ) => {
qunit.runQunitPuppeteer({
2020-04-15 11:07:20 +02:00
targetUrl: `http://${serverConfig.host}:${serverConfig.port}/${filename}`,
2020-04-07 09:57:28 +02:00
timeout: 20000,
redirectConsole: false,
puppeteerArgs: ['--allow-file-access-from-files']
})
.then(result => {
if( result.stats.failed > 0 ) {
2020-03-31 14:06:38 +02:00
console.log(`${'!'} ${filename} [${result.stats.passed}/${result.stats.total}] in ${result.stats.runtime}ms`.red);
// qunit.printResultSummary(result, console);
qunit.printFailedTests(result, console);
}
2020-03-31 14:06:38 +02:00
else {
console.log(`${'✔'} ${filename} [${result.stats.passed}/${result.stats.total}] in ${result.stats.runtime}ms`.green);
}
totalTests += result.stats.total;
failingTests += result.stats.failed;
resolve();
})
2020-03-31 14:06:38 +02:00
.catch(error => {
console.error(error);
reject();
});
} )
2020-03-31 14:06:38 +02:00
} ) );
return new Promise( ( resolve, reject ) => {
tests.then( () => {
if( failingTests > 0 ) {
reject( new Error(`${failingTests}/${totalTests} tests failed`.red) );
}
else {
console.log(`${'✔'} Passed ${totalTests} tests`.green.bold);
resolve();
}
} )
.catch( () => {
reject();
2020-04-15 11:07:20 +02:00
} )
.finally( () => {
server.close();
2020-03-31 14:06:38 +02:00
} );
} );
} )
gulp.task('eslint', () => gulp.src(['./js/**', 'gulpfile.js'])
.pipe(eslint())
.pipe(eslint.format()))
gulp.task('test', gulp.series( 'eslint', 'qunit' ))
gulp.task('default', gulp.series(gulp.parallel('js', 'css', 'plugins'), 'test'))
gulp.task('build', gulp.parallel('js', 'css', 'plugins'))
2020-04-07 10:00:28 +02:00
gulp.task('package', gulp.series('default', () =>
gulp.src([
'./index.html',
'./dist/**',
'./lib/**',
'./images/**',
'./plugin/**',
'./**.md'
]).pipe(zip('reveal-js-presentation.zip')).pipe(gulp.dest('./'))
))
2020-05-20 12:08:57 +02:00
gulp.task('reload', () => gulp.src(['*.html', '*.md'])
.pipe(connect.reload()));
gulp.task('serve', () => {
connect.server({
root: root,
port: port,
host: host,
livereload: true
})
2020-05-20 12:08:57 +02:00
gulp.watch(['*.html', '*.md'], gulp.series('reload'))
2021-11-10 21:14:05 +01:00
gulp.watch(['js/**'], gulp.series('js', 'reload', 'eslint'))
2022-02-28 09:19:58 +01:00
gulp.watch(['plugin/**/plugin.js', 'plugin/**/*.html'], gulp.series('plugins', 'reload'))
gulp.watch([
'css/theme/source/*.{sass,scss}',
'css/theme/template/*.{sass,scss}',
2020-05-20 12:08:57 +02:00
], gulp.series('css-themes', 'reload'))
gulp.watch([
2020-05-19 10:22:19 +02:00
'css/*.scss',
'css/print/*.{sass,scss,css}'
2020-05-20 12:08:57 +02:00
], gulp.series('css-core', 'reload'))
gulp.watch(['test/*.html'], gulp.series('test'))
})