Commit ba8780a5 authored by Asanka Anthony's avatar Asanka Anthony

add separate webpack config for production

remove duplicate dependency
parent 0350f980
'use strict';
// Load modules ---------------------------------------
var fs = require('fs');
var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var log = require('gulplog');
var webserver = require('gulp-webserver');
var clean = require('gulp-clean');
var runsequence = require('run-sequence');
var terser = require('gulp-terser');
var watch = require('gulp-watch');
var htmlmin = require('gulp-htmlmin');
var replaceTemplate = require('gulp-replace-template');
// Configuration settings ---------------------------------------
var config = {
base : {
src : './src/',
dist : './dist/'
},
paths : {
assets : 'assets/',
scripts : 'scripts/',
views : 'views/'
},
serverport : 8080,
tersercfg : { mangle: { toplevel: true } }
};
// Tasks ---------------------------------------
// Default: All you need to start developing
gulp.task('default', function(){
// run in sequence, first build. when done start webserver and watch
//return runsequence('copy', ['javascript', 'html', 'css']);
return runsequence('build');
});
// Build: copy and bundle needed files
gulp.task('build', function() {
return runsequence('copy', ['javascript', 'html', 'css']);
});
// Clean: empty release folder, delete tmp files
gulp.task('clean', function() {
return gulp.src(config.base.dist, {read: false})
.pipe(clean()); // del dist folder
});
// Copy: copy needed folder/files to release folder
gulp.task('copy', ['clean'], function() {
gulp.src([
config.base.src + "**/*",
"!" + config.base.src + "scripts", // do not copy scripts folder
"!" + config.base.src + "scripts/**",
"!" + config.base.src + "index.html" // do not copy index because its minified
]
, {read: true})
.pipe(gulp.dest(config.base.dist));
});
// whatch for changes
gulp.task('watch', function () {
// Endless stream mode
return watch([
config.base.src + "**/*",
"!" + config.base.src + "scripts", // do not copy scripts folder
"!" + config.base.src + "scripts/**"
], { ignoreInitial: false })
.pipe(gulp.dest(config.base.dist));
});
// Release: make a release version
gulp.task('release', ['build'], function() {
// todo: automated versioning, see:
// https://github.com/gulpjs/gulp/blob/v3.9.1/docs/recipes/automate-release-workflow.md
});
// FILE CONVERSION
// Minify html files and do template merging
gulp.task('html', () => {
gulp.src(config.base.src + config.paths.views + '*.tpl')
// template merging
// .pipe( replaceTemplate({
// '<include:_headernav.tpl>' : fs.readFileSync("src/views/_headernav.tpl", "utf8"),
// repKeyB: "TEST",
// repKeyC: "WORLD!"
// }) )
// view files minification
.pipe(htmlmin({ collapseWhitespace: true, minifyCSS : true, minifyJS : true, removeComments : true }))
// place in dest/views folder
.pipe(gulp.dest(config.base.dist + config.paths.views));
// index file minification
return gulp.src(config.base.src + 'index.html')
.pipe(htmlmin({ collapseWhitespace: true, minifyCSS : true, minifyJS : true, removeComments : true }))
.pipe(gulp.dest(config.base.dist));
});
// Bundle and minify javascript files
gulp.task('javascript', function () {
// app.js
// set up the browserify instance on a task basis
var b = browserify({
entries: config.base.src + 'scripts/app.js',
debug: false
});
b.bundle()
.pipe(source('app.js'))
.pipe(buffer())
// .pipe(sourcemaps.init({loadMaps: true}))
// // Add transformation tasks to the pipeline here.
// .pipe(terser(config.tersercfg))
// .on('error', log.error)
// .pipe(sourcemaps.write('./'))
//.pipe(gulp.dest(config.base.src + 'assets/js/')) // a copy to src
.pipe(gulp.dest(config.base.dist + 'assets/js/'));
// worker.js
var c = browserify({
entries: config.base.src + 'scripts/worker.js',
debug: false
});
return c.bundle()
.pipe(source('worker.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
// Add transformation tasks to the pipeline here.
.pipe(terser(config.tersercfg))
.on('error', log.error)
.pipe(sourcemaps.write('./'))
//.pipe(gulp.dest(config.base.src + 'assets/js/')) // a copy to src
.pipe(gulp.dest(config.base.dist + 'assets/js/'));
});
// Bundle and minify CSS files
gulp.task('css', function () {
return gulp.src(config.base.src + "assets/css/**", {read: true})
.pipe(gulp.dest(config.base.dist + "assets/css/"));
});
// ADDITIONAL SERVICES
// Webserver: run local dev webserver
gulp.task('webserver', function() {
gulp.src(config.base.dist)
.pipe(webserver({
host : config.serverhost,
port : config.serverport,
https : true,
livereload : true,
directoryListing: false,
open : 'index.html',
}));
});
......@@ -829,22 +829,6 @@
"regexpu-core": "^4.5.4"
}
},
"@babel/polyfill": {
"version": "7.4.4",
"resolved": "https://registry.npmjs.org/@babel/polyfill/-/polyfill-7.4.4.tgz",
"integrity": "sha512-WlthFLfhQQhh+A2Gn5NSFl0Huxz36x86Jn+E9OW7ibK8edKPq+KLy4apM1yDpQ8kJOVi1OVjpP4vSDLdrI04dg==",
"requires": {
"core-js": "^2.6.5",
"regenerator-runtime": "^0.13.2"
},
"dependencies": {
"core-js": {
"version": "2.6.5",
"resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.5.tgz",
"integrity": "sha512-klh/kDpwX8hryYL14M9w/xei6vrv6sE8gTHDG7/T/+SEovB/G4ejwcfE/CBzO6Edsu+OETZMZ3wcX/EjUkrl5A=="
}
}
},
"@babel/preset-env": {
"version": "7.4.4",
"resolved": "https://registry.npmjs.org/@babel/preset-env/-/preset-env-7.4.4.tgz",
......@@ -4212,7 +4196,8 @@
"ansi-regex": {
"version": "2.1.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"aproba": {
"version": "1.2.0",
......@@ -4627,7 +4612,8 @@
"safe-buffer": {
"version": "5.1.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"safer-buffer": {
"version": "2.1.2",
......@@ -4683,6 +4669,7 @@
"version": "3.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
......@@ -4726,12 +4713,14 @@
"wrappy": {
"version": "1.0.2",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"yallist": {
"version": "3.0.2",
"bundled": true,
"dev": true
"dev": true,
"optional": true
}
}
},
......@@ -8395,11 +8384,6 @@
"regenerate": "^1.4.0"
}
},
"regenerator-runtime": {
"version": "0.13.2",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.2.tgz",
"integrity": "sha512-S/TQAZJO+D3m9xeN1WTI8dLKBBiRgXBlTJvbWjCThHWZj9EvHK70Ff50/tYj2J/fvBY6JtFVwRuazHN2E7M9BA=="
},
"regenerator-transform": {
"version": "0.13.4",
"resolved": "https://registry.npmjs.org/regenerator-transform/-/regenerator-transform-0.13.4.tgz",
......@@ -10596,12 +10580,14 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
......@@ -10621,7 +10607,8 @@
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"console-control-strings": {
"version": "1.1.0",
......@@ -10769,6 +10756,7 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
......
......@@ -8,8 +8,9 @@
"url": "https://gitlab.com/schluss/schluss.app.git"
},
"scripts": {
"build": "webpack",
"dev": "webpack-dev-server --https --hot --progress --colors --open"
"dev-build": "webpack --config webpack.common.js",
"start": "webpack-dev-server --https --hot --progress --colors --open --config webpack.prod.js",
"build": "webpack --config webpack.prod.js"
},
"author": "Bob Hageman <bob@schluss.org>",
"license": "UNKNOWN",
......@@ -19,7 +20,6 @@
"homepage": "https://www.schluss.org",
"private": true,
"dependencies": {
"@babel/polyfill": "^7.4.4",
"babel-polyfill": "^6.26.0",
"cryptico": "^1.0.2",
"crypto-js": "^3.1.9-1",
......
/*
* SplitChunksPlugin is enabled by default and replaced
* deprecated CommonsChunkPlugin. It automatically identifies modules which
* should be splitted of chunk by heuristics using module duplication count and
* module category (i. e. node_modules). And splits the chunks…
*
* It is safe to remove "splitChunks" from the generated configuration
* and was added as an educational example.
*
* https://webpack.js.org/plugins/split-chunks-plugin/
*
*/
/*
* We've enabled UglifyJSPlugin for you! This minifies your app
* in order to load faster and run less javascript.
*
* https://github.com/webpack-contrib/uglifyjs-webpack-plugin
*
*/
const webpack = require('webpack');
const path = require('path');
const htmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
entry: {
app: ['babel-polyfill', './src/scripts/app.js','sammy'],
worker: ['babel-polyfill', './src/scripts/worker.js'],
},
output: {
filename: '[name].js',
globalObject: 'this'
},
devtool: "cheap-module-eval-source-map",
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader?cacheDirectory',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /\.(scss|css)$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader'
},
{
loader: 'sass-loader'
}
]
}
]
},
mode: 'development',
optimization: {
minimizer: [
new TerserPlugin({
cache: true,
parallel: 4,//Enable multi-process parallel running and set number of concurrent runs.
sourceMap: true, // Must be set to true if using source-maps in production
terserOptions: {
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
}
}),
],
},
plugins: [
new htmlWebpackPlugin({
inject: false,
template:path.join(__dirname,'src','index.html')
}),
// copy static assets to dist folder
new CopyWebpackPlugin([{
from: path.join(__dirname, 'src'),
to: path.join(__dirname, 'dist'),
ignore: ['scripts/**/*'],
}
])
],
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment