/* eslint-env node, es6 */

'use strict';

const
  path = require('path'),
  SRC_PATH = path.resolve(__dirname, 'src'),
  BUILD = process.env.NODE_ENV === 'production',
  LIMIT = process.env.EDITION === 'limit',
  SYNC = process.env.SYNC === 'yes', // Enable "sync-mode support"
  BABEL_RULE = {
    loader: 'babel-loader',
    options: {
      presets: ['es2015'],
      plugins: ['add-module-exports']
    }
  },

  LIMIT_TAGS = ['FACE'],
  BASE_NAME = 'plain-overlay',
  ENTRY_PATH = path.resolve(SRC_PATH, `${BASE_NAME}.js`),
  preProc = require('pre-proc');

module.exports = [
  {
    resource: {and: [SRC_PATH, /\.js$/]},
    use: [
      BABEL_RULE,
      {
        loader: 'skeleton-loader',
        options: {
          procedure: function(content) {
            if (LIMIT) { content = preProc.removeTag(LIMIT_TAGS, content); }
            if (SYNC) { content = preProc.removeTag('DISABLE-SYNC', content); }

            if (!BUILD && this.resourcePath === ENTRY_PATH) {
              // Save the source code after preProc has been applied.
              const destPath = path.resolve(SRC_PATH,
                `${BASE_NAME}${LIMIT ? '-limit' : ''}${SYNC ? '-sync' : ''}.proc.js`);
              require('fs').writeFileSync(destPath,
                '/*\n    DON\'T MANUALLY EDIT THIS FILE\n*/\n\n' +
                preProc.removeTag('DEBUG', content));
              console.log(`Output: ${destPath}`);
            }

            return BUILD ? preProc.removeTag('DEBUG', content) : content;
          }
        }
      }
    ]
  },
  {
    resource: {and: [SRC_PATH, /\.scss$/]},
    use: [
      {
        loader: 'skeleton-loader',
        options: {
          procedure: content => (content + '').replace(/\n/g, ''), // for node-sass bug?
          toCode: true
        }
      },
      {
        loader: 'sass-loader',
        options: {
          includePaths: process.env.SASS_PATHS.split('\n'),
          outputStyle: 'compressed'
        }
      },
      BUILD ? {
        loader: 'pre-proc-loader',
        options: {removeTag: {tag: 'DEBUG'}}
      } : null,
      LIMIT ? {
        loader: 'pre-proc-loader',
        options: {removeTag: {tag: LIMIT_TAGS}}
      } : null
    ].filter(loader => !!loader)
  },
  {
    test: path.resolve(SRC_PATH, 'face.html'),
    use: [
      'htmlclean-loader',
      {
        loader: 'pre-proc-loader',
        options: {pickTag: {}}
      }
    ]
  }
];

@@ -525,43 +525,10 @@
 js%60)
-,%0A  preProc = require('pre-proc')
 ;%0A
+
 %0Amod
@@ -619,24 +619,206 @@
 _RULE,%0A     
+ BUILD ? %7B%0A        loader: 'pre-proc-loader',%0A        options: %7B%0A          removeTag: %7Btag: %5B'DEBUG'%5D.concat(LIMIT ? LIMIT_TAGS : %5B%5D, SYNC ? 'DISABLE-SYNC' : %5B%5D)%7D%0A        %7D%0A      %7D :
  %7B%0A        l
@@ -899,24 +899,73 @@
 (content) %7B%0A
+            const preProc = require('pre-proc');%0A
             
@@ -1109,17 +1109,16 @@
 ent); %7D%0A
-%0A
         
@@ -1129,18 +1129,8 @@
 if (
-!BUILD && 
 this
@@ -1609,17 +1609,16 @@
       %7D%0A
-%0A
         
@@ -1631,54 +1631,8 @@
 turn
- BUILD ? preProc.removeTag('DEBUG', content) :
  con

Change build script