Advertisement
Guest User

Untitled

a guest
Sep 19th, 2020
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'; // eslint-disable-line
  2.  
  3. const webpack = require('webpack');
  4. const merge = require('webpack-merge');
  5. const CleanPlugin = require('clean-webpack-plugin');
  6. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  7. const StyleLintPlugin = require('stylelint-webpack-plugin');
  8. const CopyGlobsPlugin = require('copy-globs-webpack-plugin');
  9. const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
  10.  
  11. const desire = require('./util/desire');
  12. const config = require('./config');
  13.  
  14. const assetsFilenames = (config.enabled.cacheBusting) ? config.cacheBusting : '[name]';
  15.  
  16. let webpackConfig = {
  17.   context: config.paths.assets,
  18.   entry: config.entry,
  19.   devtool: (config.enabled.sourceMaps ? '#source-map' : undefined),
  20.   output: {
  21.     path: config.paths.dist,
  22.     publicPath: config.publicPath,
  23.     filename: `scripts/${assetsFilenames}.js`,
  24.   },
  25.   stats: {
  26.     hash: false,
  27.     version: false,
  28.     timings: false,
  29.     children: false,
  30.     errors: false,
  31.     errorDetails: false,
  32.     warnings: false,
  33.     chunks: false,
  34.     modules: false,
  35.     reasons: false,
  36.     source: false,
  37.     publicPath: false,
  38.   },
  39.   module: {
  40.     rules: [
  41.       {
  42.         enforce: 'pre',
  43.         test: /\.js$/,
  44.         include: config.paths.assets,
  45.         use: 'eslint',
  46.       },
  47.       {
  48.         enforce: 'pre',
  49.         test: /\.(js|s?[ca]ss)$/,
  50.         include: config.paths.assets,
  51.         loader: 'import-glob',
  52.       },
  53.       {
  54.         test: /\.js$/,
  55.         exclude: [/node_modules(?![/|\\](bootstrap|foundation-sites))/],
  56.         use: [
  57.           { loader: 'cache' },
  58.           { loader: 'buble', options: { objectAssign: 'Object.assign' } },
  59.         ],
  60.       },
  61.       {
  62.         test: /\.css$/,
  63.         include: config.paths.assets,
  64.         use: ExtractTextPlugin.extract({
  65.           fallback: 'style',
  66.           use: [
  67.             { loader: 'cache' },
  68.             { loader: 'css', options: { sourceMap: config.enabled.sourceMaps } },
  69.             {
  70.               loader: 'postcss', options: {
  71.                 config: { path: __dirname, ctx: config },
  72.                 sourceMap: config.enabled.sourceMaps,
  73.               },
  74.             },
  75.           ],
  76.         }),
  77.       },
  78.       {
  79.         test: /\.scss$/,
  80.         include: config.paths.assets,
  81.         use: ExtractTextPlugin.extract({
  82.           fallback: 'style',
  83.           use: [
  84.             { loader: 'cache' },
  85.             { loader: 'css', options: { sourceMap: config.enabled.sourceMaps } },
  86.             {
  87.               loader: 'postcss', options: {
  88.                 config: { path: __dirname, ctx: config },
  89.                 sourceMap: config.enabled.sourceMaps,
  90.               },
  91.             },
  92.             { loader: 'resolve-url', options: { sourceMap: config.enabled.sourceMaps } },
  93.             {
  94.               loader: 'sass', options: {
  95.                 sourceMap: config.enabled.sourceMaps,
  96.                 sourceComments: true,
  97.               },
  98.             },
  99.           ],
  100.         }),
  101.       },
  102.       {
  103.         test: /\.(ttf|otf|eot|woff2?|png|jpe?g|gif|svg|ico)$/,
  104.         include: config.paths.assets,
  105.         loader: 'url',
  106.         options: {
  107.           limit: 4096,
  108.           name: `[path]${assetsFilenames}.[ext]`,
  109.         },
  110.       },
  111.       {
  112.         test: /\.(ttf|otf|eot|woff2?|png|jpe?g|gif|svg|ico)$/,
  113.         include: /node_modules/,
  114.         loader: 'url',
  115.         options: {
  116.           limit: 4096,
  117.           outputPath: 'vendor/',
  118.           name: `${config.cacheBusting}.[ext]`,
  119.         },
  120.       },
  121.     ],
  122.   },
  123.   resolve: {
  124.     modules: [
  125.       config.paths.assets,
  126.       'node_modules',
  127.     ],
  128.     enforceExtension: false,
  129.   },
  130.   resolveLoader: {
  131.     moduleExtensions: ['-loader'],
  132.   },
  133.   externals: {
  134.     //jquery: 'jQuery',
  135.   },
  136.   plugins: [
  137.     new CleanPlugin([config.paths.dist], {
  138.       root: config.paths.root,
  139.       verbose: false,
  140.     }),
  141.     /**
  142.      * It would be nice to switch to copy-webpack-plugin, but
  143.      * unfortunately it doesn't provide a reliable way of
  144.      * tracking the before/after file names
  145.      */
  146.     new CopyGlobsPlugin({
  147.       pattern: config.copy,
  148.       output: `[path]${assetsFilenames}.[ext]`,
  149.       manifest: config.manifest,
  150.     }),
  151.     new ExtractTextPlugin({
  152.       filename: `styles/${assetsFilenames}.css`,
  153.       allChunks: true,
  154.       disable: (config.enabled.watcher),
  155.     }),
  156.     new webpack.ProvidePlugin({
  157.       $: 'jquery',
  158.       jQuery: 'jquery',
  159.       'window.jQuery': 'jquery',
  160.       Popper: 'popper.js/dist/umd/popper.js',
  161.     }),
  162.     new webpack.LoaderOptionsPlugin({
  163.       minimize: config.enabled.optimize,
  164.       debug: config.enabled.watcher,
  165.       stats: { colors: true },
  166.     }),
  167.     new webpack.LoaderOptionsPlugin({
  168.       test: /\.s?css$/,
  169.       options: {
  170.         output: { path: config.paths.dist },
  171.         context: config.paths.assets,
  172.       },
  173.     }),
  174.     new webpack.LoaderOptionsPlugin({
  175.       test: /\.js$/,
  176.       options: {
  177.         eslint: { failOnWarning: false, failOnError: true },
  178.       },
  179.     }),
  180.     new StyleLintPlugin({
  181.       failOnError: !config.enabled.watcher,
  182.       syntax: 'scss',
  183.     }),
  184.     new FriendlyErrorsWebpackPlugin(),
  185.   ],
  186. };
  187.  
  188. /* eslint-disable global-require */ /** Let's only load dependencies as needed */
  189.  
  190. if (config.enabled.optimize) {
  191.   webpackConfig = merge(webpackConfig, require('./webpack.config.optimize'));
  192. }
  193.  
  194. if (config.env.production) {
  195.   webpackConfig.plugins.push(new webpack.NoEmitOnErrorsPlugin());
  196. }
  197.  
  198. if (config.enabled.cacheBusting) {
  199.   const WebpackAssetsManifest = require('webpack-assets-manifest');
  200.  
  201.   webpackConfig.plugins.push(
  202.     new WebpackAssetsManifest({
  203.       output: 'assets.json',
  204.       space: 2,
  205.       writeToDisk: false,
  206.       assets: config.manifest,
  207.       replacer: require('./util/assetManifestsFormatter'),
  208.     })
  209.   );
  210. }
  211.  
  212. if (config.enabled.watcher) {
  213.   webpackConfig.entry = require('./util/addHotMiddleware')(webpackConfig.entry);
  214.   webpackConfig = merge(webpackConfig, require('./webpack.config.watch'));
  215. }
  216.  
  217. /**
  218.  * During installation via sage-installer (i.e. composer create-project) some
  219.  * presets may generate a preset specific config (webpack.config.preset.js) to
  220.  * override some of the default options set here. We use webpack-merge to merge
  221.  * them in. If you need to modify Sage's default webpack config, we recommend
  222.  * that you modify this file directly, instead of creating your own preset
  223.  * file, as there are limitations to using webpack-merge which can hinder your
  224.  * ability to change certain options.
  225.  */
  226. module.exports = merge.smartStrategy({
  227.   'module.loaders': 'replace',
  228. })(webpackConfig, desire(`${__dirname}/webpack.config.preset`));
  229.  
Advertisement
RAW Paste Data Copied
Advertisement