grocy/gulpfile.babel.js
Katharina Bogad f7bc6a3f6d Modernize Javascript
This *absolute commit monster* does the following things:

- Introduce gulp to build javascript and css files.

  This includes moving node_modules out of the public/ folder.
  Use `gulp --tasks` to get a list of all tasks; however some
  of them are automatically generated.

  Use `gulp live` to watch for changes and automatically recompile
  what's needed.

- Upgrade to yarn2
- Upgrade FullCalendar to 4.4.2

  I know that 5.x is the current version, but two major version upgrades
  are too much right now. Also v5 would break any custom css as they
  renamed a bunch of classes.

- Move Styles to sass

  (Most) global styles are now included in one sass file. This also
  means that we now compile our own bootstrap.

- Javascript is now in strict mode

  Because everything is a module now, `use strict` is now in effect
  for all javascript files. There are probably still some parts left
  where implicit variable declarations were used.

- grocy*.js were split up in modules.

  `window.Grocy` is now an instance of GrocyClass. API-wise nothing
  has changed (albeit some functions were added regarding Undo actions)
  At the Moment, this leaks a whole bunch of functions into window
  (that was easier than tracking those down).

- FindObjectIn... style functions were removed.

  Array.prototype.find and Array.prototype.filter suffice.

- Use babel to preprocess javascript.
- Use rollup to bundle javascript.

  rollup bundles and tree-shakes es6 javascript bundles.
  It also allows to "import" css files and generate css
  files specific to this javascript file. This is used
  in viewjs scripts, for example when importing FullCalendar,
  to generate an associated viewcss file.

- Use postcss to post-process css files.

  postcss uses autoprefixer to handle browser compatiblity.
  Internally this uses the package `browserslist`; and is currently
  configured to the default setting.

- Minify assets when building in production

  `gulp publish` builds all assets in production mode, that is,
  the assets get minified. This includes javascript as well as
  css files.

- css bundling

  concatCss is used to pull @imports of non-sass-files into one
  grocy.css

- animate.css is now in the main bundle

  animate.css was used in so many places that it is now located
  in the main style bundle.
2021-06-18 12:44:39 +02:00

193 lines
5.0 KiB
JavaScript

'use strict';
import { series, parallel, dest, src, watch, task } from 'gulp';
import rollup from '@rollup/stream';
import sourcemaps from 'gulp-sourcemaps';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import rollupCss from 'rollup-plugin-css-porter';
import gulpif from 'gulp-if';
import uglify from 'gulp-uglify';
import gulpsass from 'gulp-dart-sass'; // TODO: move to gulp-sass once they removed the node-sass depenency
import postcss from 'gulp-postcss';
import glob from 'glob';
import path from 'path';
// css post-processing
import cssnano from 'cssnano';
import autoprefixer from 'autoprefixer';
import concatCss from 'gulp-concat-css';
var minify = false;
var postcss_plugins = [
// always add autoprefixer
autoprefixer(),
];
// viewjs handling
var files = glob.sync('./js/viewjs/*.js');
var components = glob.sync('./js/viewjs/components/*.js');
var viewJStasks = [];
files.forEach(function(target)
{
task(target, cb => rollup({
input: target,
output: {
format: 'umd',
name: path.basename(target),
sourcemap: 'inline',
},
plugins: [resolve(), rollupCss({
dest: './public/css/viewcss/' + path.basename(target).replace(".js", ".css")
}), commonjs()],
})
.pipe(source(path.basename(target), "./js/viewjs"))
.pipe(gulpif(minify, uglify()))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(dest('./public/viewjs')));
viewJStasks.push(target);
});
components.forEach(function(target)
{
task(target, cb => rollup({
input: target,
output: {
format: 'umd',
name: path.basename(target),
sourcemap: 'inline',
},
plugins: [resolve(), rollupCss({
dest: './public/css/components/' + path.basename(target).replace(".js", ".css")
}), commonjs()],
})
.pipe(source(path.basename(target), "./js/viewjs/components"))
.pipe(gulpif(minify, uglify()))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(dest('./public/viewjs/components')));
viewJStasks.push(target);
});
// The `clean` function is not exported so it can be considered a private task.
// It can still be used within the `series()` composition.
function clean(cb)
{
// body omitted
cb();
}
// The `build` function is exported so it is public and can be run with the `gulp` command.
// It can also be used within the `series()` composition.
function build(cb)
{
// body omitted
return parallel(js, css, vendor, resourceFileCopy);
}
function publish(cb)
{
minify = true;
postcss_plugins.push(cssnano())
return build();
}
function js(cb)
{
return rollup({
input: './js/grocy.js',
output: {
format: 'umd',
name: 'grocy.js',
sourcemap: 'inline',
},
plugins: [resolve(), commonjs()],
})
.pipe(source('grocy.js', "./js"))
.pipe(gulpif(minify, uglify()))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(dest('./public/js'));
}
function viewjs(cb)
{
return parallel(viewJStasks, done => { done(); cb(); })();
}
function vendor(cb)
{
return rollup({
input: './js/vendor.js',
output: {
format: 'umd',
name: 'grocy.js',
sourcemap: 'inline',
},
plugins: [resolve(), commonjs()],
})
.pipe(source('vendor.js', "./js"))
.pipe(gulpif(minify, uglify()))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(dest('./public/js'));
}
function css(cb)
{
return src('./scss/grocy.scss')
.pipe(sourcemaps.init())
.pipe(gulpsass({ includePaths: ['./node_modules'], quietDeps: true }).on('error', gulpsass.logError))
.pipe(concatCss('grocy.css', { includePaths: ['./node_modules'], rebaseUrls: false }))
.pipe(postcss(postcss_plugins))
.pipe(sourcemaps.write('.'))
.pipe(dest('./public/css'))
}
function resourceFileCopy(cb)
{
return parallel(
cb => src([
'./node_modules/@fortawesome/fontawesome-free/webfonts/*'
]).pipe(dest('./public/webfonts')),
cb => src('./node_modules/summernote/dist/font/*').pipe(dest('./public/css/font')),
done => { done(); cb(); }
)();
}
function copyLocales(cb)
{
return parallel(
cb => src('./node_modules/timeago/locales/*.js').pipe(dest('./public/js/locales/timeago')),
cb => src('./node_modules/summernote/dist/lang/*.js').pipe(dest('./public/js/locales/summernote')),
cb => src('./node_modules/bootstrap-select/dist/js/i18n/*').pipe(dest('./public/js/locales/bootstrap-select')),
cb => src('./node_modules/fullcalendar/dist/locale/*').pipe(dest('./public/js/locales/fullcalendar')),
cb => src('./node_modules/@fullcalendar/core/locales/*').pipe(dest('./public/js/locales/fullcalendar-core')),
done => { done(); cb(); }
)();
}
function live(cb)
{
watch('./scss/**/*.scss', css);
watch(['./js/**/*.js', '!!./js/viewjs/**/*.js'], js);
watch('./js/vendor.js', vendor);
//watch('./js/viewjs/**/*.js', viewjs);
viewJStasks.forEach(elem => watch(elem, series([elem])));
}
export { build, js, vendor, viewjs, css, live, clean, resourceFileCopy, copyLocales }