sastranababan

webpack.config

Mar 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import path from "path"
  2.  
  3. import webpack from "webpack"
  4. import ExtractTextPlugin from "extract-text-webpack-plugin"
  5. import { phenomicLoader } from "phenomic"
  6. import PhenomicLoaderFeedWebpackPlugin
  7.   from "phenomic/lib/loader-feed-webpack-plugin"
  8. import PhenomicLoaderSitemapWebpackPlugin
  9.   from "phenomic/lib/loader-sitemap-webpack-plugin"
  10.  
  11. import pkg from "./package.json"
  12.  
  13. export default (config = {}) => {
  14.  
  15.   // hot loading for postcss config
  16.   // until this is officially supported
  17.   // https://github.com/postcss/postcss-loader/issues/66
  18.   const postcssPluginFile = require.resolve("./postcss.config.js")
  19.   const postcssPlugins = (webpackInstance) => {
  20.     webpackInstance.addDependency(postcssPluginFile)
  21.     delete require.cache[postcssPluginFile]
  22.     return require(postcssPluginFile)(config)
  23.   }
  24.  
  25.   return {
  26.     ...config.dev && {
  27.       devtool: "#cheap-module-eval-source-map",
  28.     },
  29.     module: {
  30.       noParse: /\.min\.js/,
  31.       // webpack 1
  32.       // loaders: [
  33.       // webpack 2
  34.       rules: [
  35.         // *.md => consumed via phenomic special webpack loader
  36.         // allow to generate collection and rss feed.
  37.         {
  38.           // phenomic requirement
  39.           test: /\.(md|markdown)$/,
  40.           loader: phenomicLoader,
  41.           query: {
  42.             context: path.join(__dirname, config.source),
  43.             // plugins: [
  44.             //   ...require("phenomic/lib/loader-preset-markdown").default
  45.             // ]
  46.             // see https://phenomic.io/docs/usage/plugins/
  47.           },
  48.         },
  49.  
  50.         // *.json => like in node, return json
  51.         // (not handled by webpack by default)
  52.         /*
  53.         {
  54.           test: /\.json$/,
  55.           loader: "json-loader",
  56.         },
  57.         */
  58.  
  59.         // *.js => babel + eslint
  60.         {
  61.           test: /\.jsx?$/,
  62.           include: [
  63.             path.resolve(__dirname, "scripts"),
  64.             path.resolve(__dirname, "src"),
  65.           ],
  66.           loaders: [
  67.             "babel-loader?cacheDirectory",
  68.             "eslint-loader" + (config.dev ? "?emitWarning" : ""),
  69.           ],
  70.         },
  71.  
  72.         // ! \\
  73.         // by default *.css files are considered as CSS Modules
  74.         // And *.global.css are considered as global (normal) CSS
  75.  
  76.         // *.css => CSS Modules
  77.         {
  78.           test: /\.css$/,
  79.           exclude: /\.global\.css$/,
  80.           include: path.resolve(__dirname, "src"),
  81.           // webpack 1
  82.           /*
  83.           loader: ExtractTextPlugin.extract(
  84.             "style-loader",
  85.             [ `css-loader?modules&localIdentName=${
  86.               config.production
  87.               ? "[hash:base64:5]"
  88.               : "[path][name]--[local]--[hash:base64:5]"
  89.               }`,
  90.               "postcss-loader",
  91.             ].join("!"),
  92.           ),
  93.           */
  94.           // webpack 2
  95.           use: ExtractTextPlugin.extract({
  96.             fallback: "style-loader",
  97.             use: [
  98.               {
  99.                 loader: "css-loader",
  100.                 query: {
  101.                   modules: true,
  102.                   localIdentName: (
  103.                     config.production
  104.                     ? "[hash:base64:5]"
  105.                     : "[path][name]--[local]--[hash:base64:5]"
  106.                   ),
  107.                 },
  108.               },
  109.               {
  110.                 loader: "postcss-loader",
  111.                 // query for postcss can't be used right now
  112.                 // https://github.com/postcss/postcss-loader/issues/99
  113.                 // meanwhile, see webpack.LoaderOptionsPlugin in plugins list
  114.                 // query: { plugins: postcssPlugins },
  115.               },
  116.             ],
  117.           }),
  118.         },
  119.         // *.global.css => global (normal) css
  120.         {
  121.           test: /\.global\.css$/,
  122.           include: path.resolve(__dirname, "src"),
  123.           // webpack 1
  124.           /*
  125.           loader: ExtractTextPlugin.extract(
  126.             "style-loader",
  127.             [ "css-loader", "postcss-loader" ].join("!"),
  128.           ),
  129.           */
  130.           // webpack 2
  131.           use: ExtractTextPlugin.extract({
  132.             fallback: "style-loader",
  133.             use: [
  134.               "css-loader",
  135.               {
  136.                 loader: "postcss-loader",
  137.                 // query for postcss can't be used right now
  138.                 // https://github.com/postcss/postcss-loader/issues/99
  139.                 // meanwhile, see webpack.LoaderOptionsPlugin in plugins list
  140.                 // query: { plugins: postcssPlugins },
  141.               },
  142.             ],
  143.           }),
  144.         },
  145.         // ! \\
  146.         // If you want global CSS only, just remove the 2 sections above
  147.         // and use the following one
  148.         // ! \\ If you want global CSS for node_modules only, just uncomment
  149.         // this section and the `include` part
  150.         // // webpack 1
  151.         /*
  152.         {
  153.           test: /\.css$/,
  154.           // depending on your need, you might need to scope node_modules
  155.           // for global CSS if you want to keep CSS Modules by default
  156.           // for your own CSS. If so, uncomment the line below
  157.           // include: path.resolve(__dirname, "node_modules"),
  158.           loader: ExtractTextPlugin.extract(
  159.             "style-loader",
  160.             [
  161.               "css-loader",
  162.               "postcss-loader",
  163.             ].join("!")
  164.           ),
  165.         },
  166.         */
  167.         // // webpack 2
  168.         /*
  169.         {
  170.           test: /\.css$/,
  171.           // depending on your need, you might need to scope node_modules
  172.           // for global CSS if you want to keep CSS Modules by default
  173.           // for your own CSS. If so, uncomment the line below
  174.           // include: path.resolve(__dirname, "node_modules"),
  175.           loader: ExtractTextPlugin.extract({
  176.             fallbackLoader: "style-loader",
  177.             loader: [
  178.               "css-loader",
  179.               {
  180.                 loader: "postcss-loader",
  181.                 query: { "plugins": postcssPlugins },
  182.               },
  183.             ]
  184.           }),
  185.         },
  186.         */
  187.         // ! \\ if you want to use Sass or LESS, you can add sass-loader or
  188.         // less-loader after postcss-loader (or replacing it).
  189.         // ! \\ You will also need to adjust the file extension
  190.         // and to run the following command
  191.         //
  192.         // Sass: `npm install --save-dev node-sass sass-loader`
  193.         // https://github.com/jtangelder/sass-loader
  194.         //
  195.         // LESS: npm install --save-dev less less-loader
  196.         // https://github.com/webpack/less-loader
  197.  
  198.         // copy assets and return generated path in js
  199.         {
  200.           test: /\.(html|ico|jpe?g|png|gif|eot|otf|webp|ttf|woff|woff2)$/,
  201.           use: {
  202.             loader: "file-loader",
  203.             query: {
  204.               name: "[path][name].[hash].[ext]",
  205.               context: path.join(__dirname, config.source),
  206.             },
  207.           },
  208.         },
  209.  
  210.         // svg as raw string to be inlined
  211.         {
  212.           test: /\.svg$/,
  213.           use: "raw-loader",
  214.         },
  215.       ],
  216.     },
  217.  
  218.     // webpack 1
  219.     /*
  220.     postcss: postcssPlugins,
  221.     */
  222.  
  223.     plugins: [
  224.       // webpack 2
  225.       //
  226.       // You should be able to remove the block below when the following
  227.       // issue has been correctly handled (and postcss-loader supports
  228.       // "plugins" option directly in query, see postcss-loader usage above)
  229.       // https://github.com/postcss/postcss-loader/issues/99
  230.       new webpack.LoaderOptionsPlugin({
  231.         test: /\.css$/,
  232.         options: {
  233.           postcss: postcssPlugins,
  234.           // required to avoid issue css-loader?modules
  235.           // this is normally the default value, but when we use
  236.           // LoaderOptionsPlugin, we must specify it again, otherwise,
  237.           // context is missing (and css modules names can be broken)!
  238.           context: __dirname,
  239.         },
  240.       }),
  241.  
  242.       new PhenomicLoaderFeedWebpackPlugin({
  243.         // here you define generic metadata for your feed
  244.         feedsOptions: {
  245.           title: pkg.name,
  246.           site_url: pkg.homepage,
  247.         },
  248.         feeds: {
  249.           // here we define one feed, but you can generate multiple, based
  250.           // on different filters
  251.           "feed.xml": {
  252.             collectionOptions: {
  253.               filter: { layout: "Post" },
  254.               sort: "date",
  255.               reverse: true,
  256.               limit: 20,
  257.             },
  258.           },
  259.         },
  260.       }),
  261.  
  262.       new PhenomicLoaderSitemapWebpackPlugin({
  263.         site_url: pkg.homepage,
  264.       }),
  265.  
  266.       // webpack 1
  267.       /*
  268.       new ExtractTextPlugin("[name].[hash].css", { disable: config.dev }),
  269.       */
  270.       // webpack 2
  271.       new ExtractTextPlugin({
  272.         filename: "[name].[hash].css",
  273.         disable: config.dev,
  274.       }),
  275.  
  276.       ...config.production && [
  277.         // webpack 2
  278.         // DedupePlugin does not work correctly with Webpack 2, yet ;)
  279.         // https://github.com/webpack/webpack/issues/2644
  280.         /*
  281.         new webpack.optimize.DedupePlugin(),
  282.         */
  283.         new webpack.optimize.UglifyJsPlugin(
  284.           { compress: { warnings: false } }
  285.         ),
  286.       ],
  287.     ],
  288.  
  289.     output: {
  290.       path: path.join(__dirname, config.destination),
  291.       publicPath: config.baseUrl.pathname,
  292.       filename: "[name].[hash].js",
  293.     },
  294.  
  295.     // webpack 1
  296.     /*
  297.     resolve: {
  298.       extensions: [ ".js", ".jsx", ".json", "" ],
  299.       root: [ path.join(__dirname, "node_modules") ],
  300.     },
  301.     resolveLoader: { root: [ path.join(__dirname, "node_modules") ] },
  302.     */
  303.     // webpack 2
  304.     resolve: {
  305.       extensions: [ ".js", ".jsx", ".json" ],
  306.       modules: [
  307.         "node_modules",
  308.         path.resolve(__dirname, "src"),
  309.       ],
  310.     },
  311.   }
  312. }
Add Comment
Please, Sign In to add comment