Advertisement
sastranababan

webpack.config.js

Mar 24th, 2017
105
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.  
  35.       rules: [
  36.  
  37.         // *.md => consumed via phenomic special webpack loader
  38.         // allow to generate collection and rss feed.
  39.         {
  40.           // phenomic requirement
  41.           test: /\.(md|markdown)$/,
  42.           loader: phenomicLoader,
  43.           query: {
  44.             context: path.join(__dirname, config.source),
  45.             // plugins: [
  46.             //   ...require("phenomic/lib/loader-preset-markdown").default
  47.             // ]
  48.             // see https://phenomic.io/docs/usage/plugins/
  49.           },
  50.         },
  51.  
  52.         // *.json => like in node, return json
  53.         // (not handled by webpack by default)
  54.         {
  55.           test: /\.json$/,
  56.           loader: "json-loader",
  57.         },
  58.  
  59.         // *.js => babel + eslint
  60.         {
  61.           test: /\.js$/,
  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.           /*
  84.           loader: ExtractTextPlugin.extract(
  85.             "style-loader",
  86.             [ `css-loader?modules&localIdentName=${
  87.               config.production
  88.               ? "[hash:base64:5]"
  89.               : "[path][name]--[local]--[hash:base64:5]"
  90.               }`,
  91.               "postcss-loader",
  92.             ].join("!"),
  93.           ),
  94.           */
  95.           // webpack 2
  96.  
  97.           loader: ExtractTextPlugin.extract({
  98.             fallbackLoader: "style-loader",
  99.             loader: [
  100.               {
  101.                 loader: "css-loader",
  102.                 query: {
  103.                   modules: true,
  104.                   localIdentName: (
  105.                     config.production
  106.                     ? "[hash:base64:5]"
  107.                     : "[path][name]--[local]--[hash:base64:5]"
  108.                   ),
  109.                 },
  110.               },
  111.               {
  112.                 loader: "postcss-loader",
  113.                 // query for postcss can't be used right now
  114.                 // https://github.com/postcss/postcss-loader/issues/99
  115.                 // meanwhile, see webpack.LoaderOptionsPlugin in plugins list
  116.                 // query: { plugins: postcssPlugins },
  117.               },
  118.             ],
  119.           }),
  120.  
  121.         },
  122.         // *.global.css => global (normal) css
  123.         {
  124.           test: /\.global\.css$/,
  125.           include: path.resolve(__dirname, "src"),
  126.           // webpack 1
  127.           /*
  128.           loader: ExtractTextPlugin.extract(
  129.             "style-loader",
  130.             [ "css-loader", "postcss-loader" ].join("!"),
  131.           ),
  132.           */
  133.           // webpack 2
  134.  
  135.           loader: ExtractTextPlugin.extract({
  136.             fallbackLoader: "style-loader",
  137.             loader: [
  138.               "css-loader",
  139.               {
  140.                 loader: "postcss-loader",
  141.                 // query for postcss can't be used right now
  142.                 // https://github.com/postcss/postcss-loader/issues/99
  143.                 // meanwhile, see webpack.LoaderOptionsPlugin in plugins list
  144.                 // query: { plugins: postcssPlugins },
  145.               },
  146.             ],
  147.           }),
  148.  
  149.         },
  150.         // ! \\
  151.         // If you want global CSS only, just remove the 2 sections above
  152.         // and use the following one
  153.         // ! \\ If you want global CSS for node_modules only, just uncomment
  154.         // this section and the `include` part
  155.         // // webpack 1
  156.         /*
  157.         {
  158.           test: /\.css$/,
  159.           // depending on your need, you might need to scope node_modules
  160.           // for global CSS if you want to keep CSS Modules by default
  161.           // for your own CSS. If so, uncomment the line below
  162.           // include: path.resolve(__dirname, "node_modules"),
  163.           loader: ExtractTextPlugin.extract(
  164.             "style-loader",
  165.             [
  166.               "css-loader",
  167.               "postcss-loader",
  168.             ].join("!")
  169.           ),
  170.         },
  171.         */
  172.         // // webpack 2
  173.  
  174.         {
  175.           test: /\.css$/,
  176.           // depending on your need, you might need to scope node_modules
  177.           // for global CSS if you want to keep CSS Modules by default
  178.           // for your own CSS. If so, uncomment the line below
  179.           // include: path.resolve(__dirname, "node_modules"),
  180.           loader: ExtractTextPlugin.extract({
  181.             fallbackLoader: "style-loader",
  182.             loader: [
  183.               "css-loader",
  184.               {
  185.                 loader: "postcss-loader",
  186.                 query: { "plugins": postcssPlugins },
  187.               },
  188.             ]
  189.           }),
  190.         },
  191.  
  192.         // ! \\ if you want to use Sass or LESS, you can add sass-loader or
  193.         // less-loader after postcss-loader (or replacing it).
  194.         // ! \\ You will also need to adjust the file extension
  195.         // and to run the following command
  196.         //
  197.         // Sass: `npm install --save-dev node-sass sass-loader`
  198.         // https://github.com/jtangelder/sass-loader
  199.         //
  200.         // LESS: npm install --save-dev less less-loader
  201.         // https://github.com/webpack/less-loader
  202.  
  203.         // copy assets and return generated path in js
  204.         {
  205.           test: /\.(html|ico|jpe?g|png|gif|eot|otf|webp|ttf|woff|woff2)$/,
  206.           loader: "file-loader",
  207.           query: {
  208.             name: "[path][name].[hash].[ext]",
  209.             context: path.join(__dirname, config.source),
  210.           },
  211.         },
  212.  
  213.         // svg as raw string to be inlined
  214.         {
  215.           test: /\.svg$/,
  216.           loader: "raw-loader",
  217.         },
  218.       ],
  219.     },
  220.  
  221.     // webpack 1
  222.     //postcss: postcssPlugins,
  223.  
  224.     plugins: [
  225.       // webpack 2
  226.  
  227.       // You should be able to remove the block below when the following
  228.       // issue has been correctly handled (and postcss-loader supports
  229.       // "plugins" option directly in query, see postcss-loader usage above)
  230.       // https://github.com/postcss/postcss-loader/issues/99
  231.       new webpack.LoaderOptionsPlugin({
  232.         test: /\.css$/,
  233.         options: {
  234.           postcss: postcssPlugins,
  235.           // required to avoid issue css-loader?modules
  236.           // this is normally the default value, but when we use
  237.           // LoaderOptionsPlugin, we must specify it again, otherwise,
  238.           // context is missing (and css modules names can be broken)!
  239.           context: __dirname,
  240.         },
  241.       }),
  242.  
  243.  
  244.       new PhenomicLoaderFeedWebpackPlugin({
  245.         // here you define generic metadata for your feed
  246.         feedsOptions: {
  247.           title: pkg.name,
  248.           site_url: pkg.homepage,
  249.         },
  250.         feeds: {
  251.           // here we define one feed, but you can generate multiple, based
  252.           // on different filters
  253.           "feed.xml": {
  254.             collectionOptions: {
  255.               filter: { layout: "Post" },
  256.               sort: "date",
  257.               reverse: true,
  258.               limit: 20,
  259.             },
  260.           },
  261.         },
  262.       }),
  263.  
  264.       new PhenomicLoaderSitemapWebpackPlugin({
  265.         site_url: pkg.homepage,
  266.       }),
  267.  
  268.       // webpack 1
  269.       //new ExtractTextPlugin("[name].[hash].css", { disable: config.dev }),
  270.       // webpack 2
  271.  
  272.       new ExtractTextPlugin({
  273.         filename: "[name].[hash].css",
  274.         disable: config.dev,
  275.       }),
  276.  
  277.  
  278.       ...config.production && [
  279.         // webpack 2
  280.         // DedupePlugin does not work correctly with Webpack 2, yet ;)
  281.         // https://github.com/webpack/webpack/issues/2644
  282.         new webpack.optimize.DedupePlugin(),
  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", ".json", "" ],
  299.       root: [ path.join(__dirname, "node_modules") ],
  300.     },
  301.     resolveLoader: { root: [ path.join(__dirname, "node_modules") ] },
  302.     */
  303.     // webpack 2
  304.  
  305.     resolve: { extensions: [ ".js", ".json" ] },
  306.  
  307.   }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement