From f0056675938562a17964b2c3ab00531817bf939e Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 10 Jan 2017 22:14:11 +0000 Subject: [PATCH] Update static build --- plugins/static_build.py | 67 +++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/plugins/static_build.py b/plugins/static_build.py index ace1520..f8f1682 100644 --- a/plugins/static_build.py +++ b/plugins/static_build.py @@ -1,28 +1,63 @@ from pelican import signals import os -import sh +import subprocess +import logging + +logger = logging.getLogger(__file__) NODE_PRODUCTION = os.environ.get('NODE_ENV') == 'production' + +def flatten_list(array): + res = [] + for el in array: + if isinstance(el, (list, tuple)): + res.extend(flatten_list(el)) + continue + res.append(el) + return res + + +def run_command(detail, args, use_system=False): + logger.info(detail + '...') + if use_system: + exit_code = os.system(' '.join(flatten_list(args))) + if exit_code: + exit(exit_code) + else: + subprocess.run(flatten_list(args), check=True) + + +def node_bin(exec): + return os.path.join('node_modules', '.bin', exec) + + def static_build(*args, **kwargs): if NODE_PRODUCTION: - print('Building Production...') + logger.info('Building Production...') + UGLIFY_ARGS = ['--compress', '--screw-ie8', '--define', '--stats', '--keep-fnames'] + else: + UGLIFY_ARGS = [] - sh.mkdir('-p', 'theme/static/build/js/lib') - os.system('uglifyjs node_modules/bootstrap-sass/assets/javascripts/bootstrap.js --compress --screw-ie8 --define --stats --keep-fnames -o theme/static/build/js/lib/bootstrap.js') - os.system('uglifyjs theme/static/build/js/lib/* --compress --screw-ie8 --define --stats --keep-fnames -o theme/static/build/js/libs.js') - sh.rm('-rf', 'theme/static/build/js/lib') - os.system('uglifyjs node_modules/jquery/dist/jquery.js --compress --screw-ie8 --define --stats --keep-fnames -o theme/static/build/js/jquery.js') - os.system('browserify -t [ babelify --presets [ es2015 react ] ] theme/static/src/js/app.js -o theme/static/build/js/app.js') - os.system('uglifyjs theme/static/build/js/app.js --compress --screw-ie8 --define --stats --keep-fnames -o theme/static/build/js/app.js') - print('JS built!') + run_command('Building Bootstrap', [node_bin('uglifyjs'), 'node_modules/bootstrap-sass/assets/javascripts/bootstrap.js', UGLIFY_ARGS , '-o', 'theme/static/build/js/bootstrap.js']) + run_command('Building jQuery', [node_bin('uglifyjs'), 'node_modules/jquery/dist/jquery.js', UGLIFY_ARGS, '-o', 'theme/static/build/js/jquery.js']) + run_command('Building Application', [ + node_bin('browserify'), + '-t', '[', 'babelify', '--presets', '[', 'es2015', 'react', ']', ']', + 'theme/static/src/js/app.js', + '-o', 'theme/static/build/js/app.js' + ]) + run_command('Compressing Application', [node_bin('uglifyjs'), 'theme/static/build/js/app.js', UGLIFY_ARGS, '-o', 'theme/static/build/js/app.js']) + + logger.info('JS built!') + + run_command('Building Pygments Style', ['pygmentize', '-S', 'github', '-f', 'html', '-a', '.highlight', '>', 'theme/static/src/scss/pygment.css'], True) + run_command('Building Styles', [node_bin('node-sass'), 'theme/static/src/scss/index.scss', 'theme/static/build/css/index.css', '--source-map-embed']) + run_command('Prefixing Styles', [node_bin('postcss'), '-u', 'autoprefixer', '-o', 'theme/static/build/css/index.css', 'theme/static/build/css/index.css']) + run_command('Compressing Styles', [node_bin('cleancss'), '-d', '--s0', '-o', 'theme/static/build/css/index.css', 'theme/static/build/css/index.css']) + + logger.info('SCSS Built!') - os.system('pygmentize -S github -f html -a .highlight > theme/static/src/scss/pygment.css') - os.system('npm run build-scss') - os.system('node-sass theme/static/src/scss/index.scss theme/static/build/css/index.css --source-map-embed') - os.system('postcss -u autoprefixer -o theme/static/build/css/index.css theme/static/build/css/index.css') - os.system('cleancss -d --s0 -o theme/static/build/css/index.css theme/static/build/css/index.css') - print('SCSS Built!') def register(): signals.static_generator_init.connect(static_build)