Guest User

Untitled

a guest
Dec 11th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. import path = require("path");
  2. import * as webpack from "webpack";
  3. import {getCommonBuildOptions, getEnvironments} from "./BuildOptions";
  4. import {HotModuleReplacementPlugin, LoaderOptionsPlugin, NamedModulesPlugin} from "webpack";
  5. import CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
  6. const {BundleAnalyzerPlugin} = require("webpack-bundle-analyzer");
  7. const ExtractTextPlugin = require("extract-text-webpack-plugin");
  8. const HtmlWebpackPlugin = require("webpack-html-plugin");
  9.  
  10. const fileLoader = {
  11. loader: "file-loader",
  12. options: {
  13. name: "assets/[hash].[ext]"
  14. }
  15. };
  16.  
  17. const imageCompressionLoader = {
  18. loader: "image-webpack-loader",
  19. options: {
  20. mozjpeg: {progressive: true, quality: 65},
  21. optipng: {enabled: false},
  22. pngquant: {quality: "65-90", speed: 4},
  23. gifsicle: {interlaced: false},
  24. webp: {quality: 75}
  25. }
  26. };
  27.  
  28. const flowRemoveTypes = require("flow-remove-types");
  29. const loaderUtils = require("loader-utils");
  30.  
  31. function flowRemoveTypesLoader (source: any) {
  32. this.cacheable();
  33.  
  34. const options = Object.assign({}, loaderUtils.getOptions(this));
  35. const removed = flowRemoveTypes(source, options).toString();
  36.  
  37. console.log(removed);
  38.  
  39. return removed;
  40. }
  41.  
  42. // NOTE webpack requires a default export
  43. // noinspection TsLint
  44. export default function webpackConfig (env: any = {}): webpack.Configuration {
  45. const {ocast, node, ...additionalOptions} = env;
  46. const {ocastEnv, nodeEnv} = getEnvironments(ocast, node);
  47. const options = {
  48. ...getCommonBuildOptions(ocastEnv, nodeEnv),
  49. ...additionalOptions
  50. };
  51.  
  52. const extensions = [".ts", ".tsx", ".js", ".jsx"];
  53. return {
  54. entry: compact([
  55. //path.join(__dirname, "app", "src", "polyfills", "index.js"),
  56. options.hmr && "react-hot-loader/patch",
  57. path.join(__dirname, "app", "src", "client.js")
  58. ]),
  59. output: {
  60. path: path.join(__dirname, options.outputFolder),
  61. filename: "app.js"
  62. },
  63. cache: options.cache,
  64. devtool: options.sourceMaps ? "source-map" : undefined,
  65. resolve: {
  66. extensions,
  67. modules: [
  68. path.join(__dirname, "app"),
  69. "node_modules"
  70. ],
  71. alias: {
  72. aphrodite: "aphrodite/no-important",
  73. src: path.join(__dirname, "app", "src")
  74. }
  75. },
  76. plugins: compact([
  77. new HtmlWebpackPlugin({filename: "index.html"}),
  78. new webpack.DefinePlugin({
  79. "process.env": {
  80. NODE_ENV: JSON.stringify(nodeEnv),
  81. OCAST_CONFIG: JSON.stringify(ocastEnv)
  82. }
  83. }),
  84. new ExtractTextPlugin("styles.css"),
  85. //new webpack.NormalModuleReplacementPlugin(/^react?$/, require.resolve("react")),
  86. //new BundleAnalyzerPlugin({analyzerMode: "static"}),
  87. new CommonsChunkPlugin({
  88. name: "vendor",
  89. filename: "vendor.js",
  90. minChunks: module => module.context && module.context.indexOf("node_modules") >= 0
  91. }),
  92.  
  93. options.hmr && new NamedModulesPlugin(),
  94. options.hmr && new HotModuleReplacementPlugin(),
  95. options.debug && new LoaderOptionsPlugin({debug: true})
  96. ]),
  97. module: {
  98. rules: [
  99. {
  100. test: /\.jsx?/,
  101. exclude: /node_modules/,
  102. use: compact([
  103. options.hmr && "react-hot-loader/webpack",
  104. {
  105. loader: "babel-loader",
  106. options: {
  107. presets: ["flow", "react", "stage-0"]
  108. }
  109. }
  110. ])
  111. },
  112. {
  113. test: /\.tsx?/,
  114. exclude: /node_modules/,
  115. use: compact([
  116. options.hmr && "react-hot-loader/webpack",
  117. "ts-loader"
  118. ])
  119. },
  120. {
  121. test: /\.(png|jpe?g)$/,
  122. exclude: /node_modules/,
  123. use: compact([
  124. fileLoader,
  125. options.compress && imageCompressionLoader
  126. ])
  127. },
  128. {
  129. test: /\.json$/,
  130. use: "json-loader"
  131. },
  132. {
  133. test: /\.css$/,
  134. use: ExtractTextPlugin.extract({
  135. fallback: "style-loader",
  136. use: "css-loader"
  137. })
  138. },
  139. {
  140. test: /\.(ttf|eot|woff|woff2|svg)$/,
  141. use: fileLoader
  142. }
  143. ]
  144. },
  145. devServer: {
  146. hot: options.hmr
  147. }
  148. };
  149. };
  150.  
  151. function compact<T> (array: T[]) {
  152. return array.filter(item => item);
  153. }
Add Comment
Please, Sign In to add comment