Advertisement
Guest User

Webpack conf

a guest
May 27th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. var webpack = require('webpack');
  3. var path = require('path');
  4. var CopyWebpackPlugin = require('copy-webpack-plugin');
  5. var glob_copy_webpack_plugin_1 = require('../plugins/glob-copy-webpack-plugin');
  6. var package_chunk_sort_1 = require('../utilities/package-chunk-sort');
  7. var base_href_webpack_1 = require('@angular-cli/base-href-webpack');
  8. var webpack_build_utils_1 = require('./webpack-build-utils');
  9. var autoprefixer = require('autoprefixer');
  10. var ProgressPlugin = require('webpack/lib/ProgressPlugin');
  11. var HtmlWebpackPlugin = require('html-webpack-plugin');
  12. var ExtractTextPlugin = require('extract-text-webpack-plugin');
  13. var SilentError = require('silent-error');
  14. /**
  15.  * Enumerate loaders and their dependencies from this file to let the dependency validator
  16.  * know they are used.
  17.  *
  18.  * require('source-map-loader')
  19.  * require('raw-loader')
  20.  * require('script-loader')
  21.  * require('json-loader')
  22.  * require('url-loader')
  23.  * require('file-loader')
  24.  */
  25. function getWebpackCommonConfig(projectRoot, environment, appConfig, baseHref, sourcemap, vendorChunk, verbose, progress, outputHashing) {
  26.     var appRoot = path.resolve(projectRoot, appConfig.root);
  27.     var nodeModules = path.resolve(projectRoot, 'node_modules');
  28.     var extraPlugins = [];
  29.     var extraRules = [];
  30.     var entryPoints = {};
  31.     // figure out which are the lazy loaded entry points
  32.     var lazyChunks = webpack_build_utils_1.lazyChunksFilter(webpack_build_utils_1.extraEntryParser(appConfig.scripts, appRoot, 'scripts').concat(webpack_build_utils_1.extraEntryParser(appConfig.styles, appRoot, 'styles')));
  33.     if (appConfig.main) {
  34.         entryPoints['main'] = [path.resolve(appRoot, appConfig.main)];
  35.     }
  36.     // determine hashing format
  37.     var hashFormat = webpack_build_utils_1.getOutputHashFormat(outputHashing);
  38.     // process global scripts
  39.     if (appConfig.scripts.length > 0) {
  40.         var globalScripts = webpack_build_utils_1.extraEntryParser(appConfig.scripts, appRoot, 'scripts');
  41.         // add script entry points
  42.         globalScripts.forEach(function (script) {
  43.             return entryPoints[script.entry]
  44.                 ? entryPoints[script.entry].push(script.path)
  45.                 : entryPoints[script.entry] = [script.path];
  46.         });
  47.         // load global scripts using script-loader
  48.         extraRules.push({
  49.             include: globalScripts.map(function (script) { return script.path; }), test: /\.js$/, loader: 'script-loader'
  50.         });
  51.     }
  52.     if (vendorChunk) {
  53.         extraPlugins.push(new webpack.optimize.CommonsChunkPlugin({
  54.             name: 'vendor',
  55.             chunks: ['main'],
  56.             minChunks: function (module) { return module.userRequest && module.userRequest.startsWith(nodeModules); }
  57.         }));
  58.     }
  59.     // process environment file replacement
  60.     if (appConfig.environments) {
  61.         if (!('source' in appConfig.environments)) {
  62.             throw new SilentError("Environment configuration does not contain \"source\" entry.");
  63.         }
  64.         if (!(environment in appConfig.environments)) {
  65.             throw new SilentError("Environment \"" + environment + "\" does not exist.");
  66.         }
  67.         extraPlugins.push(new webpack.NormalModuleReplacementPlugin(
  68.         // This plugin is responsible for swapping the environment files.
  69.         // Since it takes a RegExp as first parameter, we need to escape the path.
  70.         // See https://webpack.github.io/docs/list-of-plugins.html#normalmodulereplacementplugin
  71.         new RegExp(path.resolve(appRoot, appConfig.environments['source'])
  72.             .replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')), path.resolve(appRoot, appConfig.environments[environment])));
  73.     }
  74.     // process asset entries
  75.     if (appConfig.assets) {
  76.         extraPlugins.push(new glob_copy_webpack_plugin_1.GlobCopyWebpackPlugin({
  77.             patterns: appConfig.assets,
  78.             globOptions: { cwd: appRoot, dot: true, ignore: '**/.gitkeep' }
  79.         }));
  80.     }
  81.     if (progress) {
  82.         extraPlugins.push(new ProgressPlugin({ profile: verbose, colors: true }));
  83.     }
  84.     return {
  85.         target: 'electron-renderer',
  86.         devtool: sourcemap ? 'source-map' : false,
  87.         resolve: {
  88.             extensions: ['.ts', '.js'],
  89.             modules: [nodeModules],
  90.         },
  91.         resolveLoader: {
  92.             modules: [nodeModules]
  93.         },
  94.         context: projectRoot,
  95.         entry: entryPoints,
  96.         output: {
  97.             path: path.resolve(projectRoot, appConfig.outDir),
  98.             publicPath: appConfig.deployUrl,
  99.             filename: "[name]" + hashFormat.chunk + ".bundle.js",
  100.             sourceMapFilename: "[name]" + hashFormat.chunk + ".bundle.map",
  101.             chunkFilename: "[id]" + hashFormat.chunk + ".chunk.js"
  102.         },
  103.         module: {
  104.             rules: [
  105.                 { enforce: 'pre', test: /\.js$/, loader: 'source-map-loader', exclude: [nodeModules] },
  106.                 { test: /\.json$/, loader: 'json-loader' },
  107.                 { test: /\.html$/, loader: 'raw-loader' },
  108.                 { test: /\.(eot|svg)$/, loader: "file-loader?name=[name]" + hashFormat.file + ".[ext]" },
  109.                 {
  110.                     test: /\.(jpg|png|gif|otf|ttf|woff|woff2)$/,
  111.                     loader: "url-loader?name=[name]" + hashFormat.file + ".[ext]&limit=10000"
  112.                 }
  113.             ].concat(extraRules)
  114.         },
  115.         plugins: [
  116.             new HtmlWebpackPlugin({
  117.                 template: path.resolve(appRoot, appConfig.index),
  118.                 filename: path.resolve(appConfig.outDir, appConfig.index),
  119.                 chunksSortMode: package_chunk_sort_1.packageChunkSort(['inline', 'styles', 'scripts', 'vendor', 'main']),
  120.                 excludeChunks: lazyChunks,
  121.                 xhtml: true
  122.             }),
  123.             new base_href_webpack_1.BaseHrefWebpackPlugin({
  124.                 baseHref: baseHref
  125.             }),
  126.             // inserted lines
  127.             new CopyWebpackPlugin([{
  128.                 context: path.resolve(appRoot),
  129.                 from: "entry.js"
  130.             }]),
  131.             new webpack.optimize.CommonsChunkPlugin({
  132.                 minChunks: Infinity,
  133.                 name: 'inline'
  134.             })
  135.         ].concat(extraPlugins),
  136.         node: {
  137.             fs: 'empty',
  138.             global: true,
  139.             crypto: 'empty',
  140.             tls: 'empty',
  141.             net: 'empty',
  142.             process: true,
  143.             module: false,
  144.             clearImmediate: false,
  145.             setImmediate: false
  146.         }
  147.     };
  148. }
  149. exports.getWebpackCommonConfig = getWebpackCommonConfig;
  150. //# sourceMappingURL=/Users/hans/Sources/angular-cli/packages/angular-cli/models/webpack-build-common.js.map
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement