Guest User

Untitled

a guest
Nov 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const config = require('./config');
  4. if (!config.javascript) return;
  5.  
  6. const gulp = require('gulp');
  7. const sourcemaps = require('gulp-sourcemaps');
  8. const rename = require("gulp-rename");
  9. const notify = require('gulp-notify');
  10. const concat = require('gulp-concat');
  11. const babel = require('gulp-babel');
  12. const plumber = require('gulp-plumber');
  13. const fs = require('fs');
  14. const rollup = require('rollup-stream'); // specifically for rollup
  15. const source = require('vinyl-source-stream'); // specifically for rollup
  16. const buffer = require('vinyl-buffer'); // specifically for rollup
  17. const eslintRollup = require('rollup-plugin-eslint'); // specifically for rollup
  18. const eslint = require('gulp-eslint');
  19. const orderOfJavaScript = require('./_order-of-javascript.js');
  20. const reportErrorsForJavaScript = require('./_report-errors-for-javascript.js');
  21.  
  22.  
  23.  
  24. const javascriptTask = function() {
  25.  
  26.  
  27.  
  28. // package.json contains information about project
  29. // no try catch - fail dramatically if package.json is not present
  30. const pkgAll = fs.readFileSync('./../../package.json', 'utf8');
  31.  
  32.  
  33.  
  34. // covert string to object
  35. const pkg = JSON.parse(pkgAll);
  36.  
  37.  
  38.  
  39. // developer choosing old school approach
  40. const traditional = function(order) {
  41.  
  42.  
  43.  
  44. // see if developer needs js files compiled in a specific order
  45. const queuedJavaScript = orderOfJavaScript(order);
  46.  
  47.  
  48.  
  49. // then add all other JS files
  50. queuedJavaScript.push('src/**/*.js');
  51.  
  52.  
  53.  
  54. return gulp.src(queuedJavaScript)
  55.  
  56.  
  57.  
  58. .pipe(eslint())
  59.  
  60.  
  61.  
  62. .pipe(plumber())
  63.  
  64.  
  65.  
  66. .pipe(notify(reportErrorsForJavaScript))
  67.  
  68.  
  69.  
  70. .pipe(eslint.format());
  71.  
  72.  
  73.  
  74. };
  75.  
  76.  
  77.  
  78. // rollup setup
  79. const newThreads = function( src ) {
  80.  
  81.  
  82.  
  83. // fail dramatically if _rollup.js is not present
  84. fs.readFileSync( 'src/_rollup.js', 'utf8' );
  85.  
  86.  
  87.  
  88. // get rolling
  89. return rollup({
  90. input: src, // look at this specific file src/_rollup.js
  91. format: 'iife', // automatically encapsulates code in an iife and invokes strict mode
  92. sourcemap: true, // locate the line number where the issue is occurring
  93. plugins: [
  94. eslintRollup({
  95. "parserOptions": {
  96. "sourceType": "module" // tell ESLint ES6 modules are in play
  97. }
  98. })
  99. ]
  100. })
  101.  
  102.  
  103.  
  104. // point to the entry file again
  105. .pipe(source(src))
  106.  
  107.  
  108.  
  109. // output the buffer, rollup doesn't support streams
  110. .pipe(buffer());
  111.  
  112.  
  113.  
  114. };
  115.  
  116.  
  117.  
  118. // if rollup in play throw on some new threads
  119. // otherwise go old school
  120. return ( pkg.rollup ? newThreads('src/_rollup.js') : traditional(pkg.orderofjavascript) )
  121.  
  122.  
  123.  
  124. .pipe(rename(function(path) {
  125. path.dirname = "";
  126. }))
  127.  
  128.  
  129.  
  130. .pipe(sourcemaps.init({ loadMaps: true }))
  131.  
  132.  
  133.  
  134. .pipe(babel({
  135. "presets": [
  136. ["env", {
  137. "targets": {
  138. "browsers": config.sass.settings
  139. },
  140. "modules": false
  141. }]
  142. ]
  143. }))
  144.  
  145.  
  146.  
  147. .pipe(concat('main.js'))
  148.  
  149.  
  150.  
  151. .pipe(sourcemaps.write('/'))
  152.  
  153.  
  154.  
  155. .pipe(gulp.dest(config.javascript.dest))
  156.  
  157.  
  158.  
  159. .pipe(browserSync.stream());
  160.  
  161.  
  162.  
  163. };
  164.  
  165. gulp.task('javascript', javascriptTask);
  166. module.exports = javascriptTask;
Add Comment
Please, Sign In to add comment