Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const config = require('./config');
- if (!config.javascript) return;
- const gulp = require('gulp');
- const sourcemaps = require('gulp-sourcemaps');
- const rename = require("gulp-rename");
- const notify = require('gulp-notify');
- const concat = require('gulp-concat');
- const babel = require('gulp-babel');
- const plumber = require('gulp-plumber');
- const fs = require('fs');
- const rollup = require('rollup-stream'); // specifically for rollup
- const source = require('vinyl-source-stream'); // specifically for rollup
- const buffer = require('vinyl-buffer'); // specifically for rollup
- const eslintRollup = require('rollup-plugin-eslint'); // specifically for rollup
- const eslint = require('gulp-eslint');
- const orderOfJavaScript = require('./_order-of-javascript.js');
- const reportErrorsForJavaScript = require('./_report-errors-for-javascript.js');
- const javascriptTask = function() {
- // package.json contains information about project
- // no try catch - fail dramatically if package.json is not present
- const pkgAll = fs.readFileSync('./../../package.json', 'utf8');
- // covert string to object
- const pkg = JSON.parse(pkgAll);
- // developer choosing old school approach
- const traditional = function(order) {
- // see if developer needs js files compiled in a specific order
- const queuedJavaScript = orderOfJavaScript(order);
- // then add all other JS files
- queuedJavaScript.push('src/**/*.js');
- return gulp.src(queuedJavaScript)
- .pipe(eslint())
- .pipe(plumber())
- .pipe(notify(reportErrorsForJavaScript))
- .pipe(eslint.format());
- };
- // rollup setup
- const newThreads = function( src ) {
- // fail dramatically if _rollup.js is not present
- fs.readFileSync( 'src/_rollup.js', 'utf8' );
- // get rolling
- return rollup({
- input: src, // look at this specific file src/_rollup.js
- format: 'iife', // automatically encapsulates code in an iife and invokes strict mode
- sourcemap: true, // locate the line number where the issue is occurring
- plugins: [
- eslintRollup({
- "parserOptions": {
- "sourceType": "module" // tell ESLint ES6 modules are in play
- }
- })
- ]
- })
- // point to the entry file again
- .pipe(source(src))
- // output the buffer, rollup doesn't support streams
- .pipe(buffer());
- };
- // if rollup in play throw on some new threads
- // otherwise go old school
- return ( pkg.rollup ? newThreads('src/_rollup.js') : traditional(pkg.orderofjavascript) )
- .pipe(rename(function(path) {
- path.dirname = "";
- }))
- .pipe(sourcemaps.init({ loadMaps: true }))
- .pipe(babel({
- "presets": [
- ["env", {
- "targets": {
- "browsers": config.sass.settings
- },
- "modules": false
- }]
- ]
- }))
- .pipe(concat('main.js'))
- .pipe(sourcemaps.write('/'))
- .pipe(gulp.dest(config.javascript.dest))
- .pipe(browserSync.stream());
- };
- gulp.task('javascript', javascriptTask);
- module.exports = javascriptTask;
Add Comment
Please, Sign In to add comment