diff --git a/lib/sake.js b/lib/sake.js index 4367b84..881dab2 100644 --- a/lib/sake.js +++ b/lib/sake.js @@ -577,5 +577,19 @@ module.exports = (config, options) => { }) } + /** + * Checks if the current CLI invocation includes the build task or deploy tasks. + * Used to conditionally skip sourcemap generation during builds and deployments. + * + * @return boolean + */ + exports.isBuildTask = () => { + const args = process.argv.slice(2) + return args.includes('build') || + args.includes('deploy') || + args.includes('copy_to_wc_repo') || + return ['build', 'deploy', 'copy_to_wc_repo', 'copy_to_wp_repo'].some(task => args.includes(task)) + } + return exports } diff --git a/pipes/scripts.js b/pipes/scripts.js index 9586fc4..879c591 100644 --- a/pipes/scripts.js +++ b/pipes/scripts.js @@ -3,12 +3,13 @@ const lazypipe = require('lazypipe') module.exports = (plugins, sake) => { const pipes = {} - // transpile, minify and write sourcemaps + // transpile, minify and conditionally write sourcemaps // 1. Because CoffeeScript 2 will compile to ES6, we need to use babel to transpile it to ES2015, // note that this will also enable us to use ES6 in our plain JS. // 2. When not using CoffeeScript, regular JavaScript files that may contain ES6 code will also be transpiled to ES2015 automatically. // 3. We need to tell Babel to find the preset from this project, not from the current working directory, // see https://github.com/babel/babel-loader/issues/299#issuecomment-259713477. + pipes.compileJs = lazypipe() .pipe(plugins.babel, { presets: ['@babel/preset-env', '@babel/preset-react'].map(require.resolve) }) .pipe(() => { @@ -16,10 +17,16 @@ module.exports = (plugins, sake) => { return plugins.if(sake.options.minify, plugins.uglify()) }) .pipe(plugins.rename, { suffix: '.min' }) - // ensure admin/ and frontend/ are removed from the source paths - // see https://www.npmjs.com/package/gulp-sourcemaps#alter-sources-property-on-sourcemaps - .pipe(plugins.sourcemaps.mapSources, (sourcePath) => '../' + sourcePath) - .pipe(plugins.sourcemaps.write, '.', { includeContent: false }) + .pipe(() => { + // Only generate sourcemaps if not building + // ensure admin/ and frontend/ are removed from the source paths + // see https://www.npmjs.com/package/gulp-sourcemaps#alter-sources-property-on-sourcemaps + return plugins.if(!sake.isBuildTask(), plugins.sourcemaps.mapSources((sourcePath) => '../' + sourcePath)) + }) + .pipe(() => { + // Only write sourcemaps if not building + return plugins.if(!sake.isBuildTask(), plugins.sourcemaps.write('.', { includeContent: false })) + }) return pipes } diff --git a/tasks/compile.js b/tasks/compile.js index 5d7c92a..bc4f338 100644 --- a/tasks/compile.js +++ b/tasks/compile.js @@ -34,9 +34,10 @@ module.exports = (gulp, plugins, sake) => { return Promise.resolve() } + return gulp.src(`${sake.config.paths.assetPaths.js}/**/*.coffee`) // plugins.if(() => sake.isWatching, plugins.newer({dest: sake.config.paths.assetPaths.js + '/**', ext: 'min.js'})), - .pipe(plugins.sourcemaps.init()) + .pipe(plugins.if(!sake.isBuildTask(), plugins.sourcemaps.init())) // compile coffee files to JS .pipe(plugins.coffee({ bare: false })) // transpile & minify, write sourcemaps @@ -51,9 +52,10 @@ module.exports = (gulp, plugins, sake) => { return Promise.resolve() } + return gulp.src(sake.config.paths.assetPaths.javascriptSources) // plugins.if(() => sake.isWatching, plugins.newer({dest: sake.config.paths.assetPaths.js + '/**', ext: 'min.js'})), - .pipe(plugins.sourcemaps.init()) + .pipe(plugins.if(!sake.isBuildTask(), plugins.sourcemaps.init())) // transpile & minify, write sourcemaps .pipe(pipes.compileJs()) .pipe(gulp.dest(sake.config.paths.assetPaths.js)) @@ -72,7 +74,7 @@ module.exports = (gulp, plugins, sake) => { return Promise.resolve() } else { return gulp.src(sake.config.paths.assetPaths.blockSources) - .pipe(plugins.sourcemaps.init()) + .pipe(plugins.if(!sake.isBuildTask(), plugins.sourcemaps.init())) .pipe(webpack({ mode: 'production', entry: `${blockPath}/${blockSrc[0]}`, @@ -121,18 +123,17 @@ module.exports = (gulp, plugins, sake) => { cssPlugins.push(require('cssnano')({ zindex: false })) } + return gulp.src([ `${sake.config.paths.assetPaths.css}/**/*.scss`, `!${sake.config.paths.assetPaths.css}/**/mixins.scss` // don't compile any mixins by themselves ]) - .pipe(plugins.sourcemaps.init()) + .pipe(plugins.if(!sake.isBuildTask(), plugins.sourcemaps.init())) .pipe(sass({ outputStyle: 'expanded' })) .pipe(plugins.postcss(cssPlugins)) .pipe(plugins.rename({ suffix: '.min' })) - // ensure admin/ and frontend/ are removed from the source paths - // see https://www.npmjs.com/package/gulp-sourcemaps#alter-sources-property-on-sourcemaps - .pipe(plugins.sourcemaps.mapSources((sourcePath) => '../' + sourcePath)) - .pipe(plugins.sourcemaps.write('.', { includeContent: false })) + .pipe(plugins.if(!sake.isBuildTask(), plugins.sourcemaps.mapSources((sourcePath) => '../' + sourcePath))) + .pipe(plugins.if(!sake.isBuildTask(), plugins.sourcemaps.write('.', { includeContent: false }))) .pipe(gulp.dest(`${sake.config.paths.src}/${sake.config.paths.css}`)) .pipe(plugins.if(() => sake.isWatching && sake.config.tasks.watch.useBrowserSync, plugins.browserSync.stream({match: '**/*.css'}))) }) diff --git a/tasks/copy.js b/tasks/copy.js index e09b1a3..eb2c808 100644 --- a/tasks/copy.js +++ b/tasks/copy.js @@ -156,7 +156,6 @@ module.exports = (gulp, plugins, sake) => { // @link https://github.com/gulpjs/gulp/issues/2790 return gulp.src(paths, { base: sake.config.paths.src, allowEmpty: true, encoding: false }) .pipe(filter) - .pipe(plugins.replace(/\/\*# sourceMappingURL=.*?\*\/$/mg, '')) // remove source mapping references - TODO: consider skipping sourcemaps in compilers instead when running build/deploy tasks .pipe(plugins.replace('\n', '')) // remove an extra line added by libsass/node-sass .pipe(filter.restore) .pipe(gulp.dest(`${sake.config.paths.build}/${sake.config.plugin.id}`))