'use strict';

var gulp = require('gulp'),
    webpack = require('webpack-stream'),
    gulpIgnore = require('gulp-ignore'),
    ExtractTextPlugin = require('extract-text-webpack-plugin'),
    path = require('path'),
    _ = require('underscore'),
    webpackConfigJS = require('../../webpack.config.js'),
    webpackConfigCSS = require('../../webpack.css.config.js');

module.exports = {
    /**
     * Packages a JavaScript file using Webpack.
     */
    packageJavaScript: function(options) {
        var sourceExtension = path.extname(options.source),
            targetExtension = '.js',
            baseName = path.basename(options.source, sourceExtension),
            packageConfig = _.extend(
                {},
                webpackConfigJS,
                {
                    output: _.extend(
                        {},
                        webpackConfigJS.output,
                        {
                            filename: baseName + targetExtension,
                            chunkFilename: baseName + '-[id]' + targetExtension
                        }
                    ),
                    plugins: [
                        new ExtractTextPlugin(baseName + targetExtension)
                    ]
                }
            );
        return gulp.src(options.source)
            .pipe(webpack(packageConfig))
            .pipe(gulp.dest(options.targetDirectory));
    },

    /**
     * Packages a CSS file using Webpack.
     */
    packageCss: function(options) {
        var sourceExtension = path.extname(options.source),
            targetExtension = '.css',
            baseName = path.basename(options.source, sourceExtension),
            targetPath = '../../' + options.targetDirectory + '/' + baseName + targetExtension,
            targetFile = path.resolve(__dirname, targetPath),
            patternLibraryPath = options.patternLibraryPath,
            packageConfig = _.extend(
                {},
                webpackConfigCSS,
                {
                    entry: {},
                    output: {
                        filename: targetFile,
                        chunkFilename: baseName + '-[id]' + targetExtension
                    }
                }
            );
        if (patternLibraryPath) {
            packageConfig.sassLoader.data = '$pattern-library-path: \'' + patternLibraryPath + '\' !default;';
        }
        packageConfig.entry[baseName + targetExtension] = path.resolve(__dirname, '../../' + options.source);
        return gulp.src('')
            .pipe(webpack(packageConfig))
            .pipe(gulpIgnore.exclude('**/*.js'))  // Exclude JavaScript files that are generated
            .pipe(gulp.dest(options.targetDirectory));
    }
};

@@ -243,131 +243,10 @@
 re')
-,%0A    webpackConfigJS = require('../../webpack.config.js'),%0A    webpackConfigCSS = require('../../webpack.css.config.js')
 ;%0A
+
 %0Amod
@@ -369,35 +369,328 @@
 ions) %7B%0A        
-var
+// @TODO: All requires should be done at global scope so they can be statically analyzed by module tools.%0A        // Doing so here breaks our Webpack configuration, for some reason.%0A        var webpackConfig = require('../../webpack.config.js'),  // eslint-disable-line global-require%0A           
  sourceExtension
@@ -917,18 +917,16 @@
 ckConfig
-JS
 ,%0A      
@@ -1044,10 +1044,8 @@
 nfig
-JS
 .out
@@ -1677,19 +1677,316 @@
         
-var
+// @TODO: All requires should be done at global scope so they can be statically analyzed by module tools.%0A        // Doing so here breaks our Webpack configuration, for some reason.%0A        var webpackConfig = require('../../webpack.css.config.js'),  // eslint-disable-line global-require%0A           
  sourceE
@@ -2404,32 +2404,32 @@
             %7B%7D,%0A
+
                 
@@ -2441,19 +2441,16 @@
 ckConfig
-CSS
 ,%0A      

Fix previewing issues (#346)