Przeglądaj źródła

during construct the webpack settings for production

Yuki Takei 9 lat temu
rodzic
commit
569f1ce5dd
4 zmienionych plików z 105 dodań i 62 usunięć
  1. 0 61
      config/webpack.plugin.js
  2. 102 0
      config/webpack.prod.js
  3. 1 1
      lib/views/layout/layout.html
  4. 2 0
      package.json

+ 0 - 61
config/webpack.plugin.js

@@ -1,61 +0,0 @@
-var path = require('path');
-var webpack = require('webpack');
-var UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');
-
-var ManifestPlugin = require('webpack-manifest-plugin');
-
-var config = {
-  entry: {
-    plugin: './resource/js/plugin.js',
-  },
-  output: {
-    path: path.join(__dirname + "/public/js"),
-    filename: "[name].[hash].js"
-  },
-  resolve: {
-    modules: [
-      './node_modules', './plugin/node_modules',
-    ],
-  },
-  module: {
-    rules: [
-      {
-        test: /.jsx?$/,
-        exclude: /node_modules/,
-        use: [{
-          loader: 'babel-loader',
-        }]
-      },
-      {
-        test: /\.css$/,
-        use: ['style-loader', 'css-loader'],
-      },
-      {
-        test: /\.scss$/,
-        use: ['style-loader', 'css-loader', 'sass-loader'],
-      },
-    ]
-  },
-  plugins: []
-};
-
-if (process.env && process.env.NODE_ENV !== 'development') {
-  config.plugins = [
-    new webpack.DefinePlugin({
-      'process.env':{
-        'NODE_ENV': JSON.stringify('production')
-      }
-    }),
-    new UglifyJsPlugin({
-      compress:{
-        warnings: false
-      }
-    }),
-  ];
-}
-
-config.plugins.push(new ManifestPlugin({
-  fileName: 'manifest-plugin.json'
-}));
-
-module.exports = config;

+ 102 - 0
config/webpack.prod.js

@@ -0,0 +1,102 @@
+/**
+ * @author: Yuki Takei <yuki@weseek.co.jp>
+ */
+
+const helpers = require('./helpers');
+const webpackMerge = require('webpack-merge'); // used to merge webpack configs
+const commonConfig = require('./webpack.common.js'); // the settings that are common to prod and dev
+
+/**
+ * Webpack Plugins
+ */
+const ExtractTextPlugin = require('extract-text-webpack-plugin');
+const IgnorePlugin = require('webpack/lib/IgnorePlugin');
+const UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');
+const OptimizeJsPlugin = require('optimize-js-plugin');
+
+/**
+ * Webpack Constants
+ */
+const ENV = process.env.NODE_ENV = process.env.ENV = 'production';
+const HOST = process.env.HOST || 'localhost';
+const PORT = process.env.PORT || 3000;
+
+module.exports = function (env) {
+  return webpackMerge(commonConfig({ env: ENV }), {
+    devtool: 'source-map',
+    output: {
+      path: helpers.root('public/js'),
+      filename: '[name].[chunkhash].bundle.js',
+      sourceMapFilename: '[name].[chunkhash].bundle.map',
+      chunkFilename: '[id].[chunkhash].chunk.js'
+    },
+    module: {
+      rules: [
+        /*
+         * to string and css loader support for *.css files (from Angular components)
+         * Returns file content as string
+         */
+        {
+          test: /\.css$/,
+          use: ['to-string-loader', 'css-loader'],
+          include: [helpers.root('resource')]
+        },
+        /*
+         * to string and sass loader support for *.scss files (from Angular components)
+         * Returns compiled css content as string
+         */
+        {
+          test: /\.scss$/,
+          use: ['to-string-loader', 'css-loader', 'sass-loader'],
+          include: [helpers.root('resource')]
+        },
+      ]
+    },
+    plugins: [
+
+      new OptimizeJsPlugin({
+        sourceMap: false
+      }),
+
+      new UglifyJsPlugin({
+        // beautify: true, //debug
+        // mangle: false, //debug
+        // dead_code: false, //debug
+        // unused: false, //debug
+        // deadCode: false, //debug
+        // compress: {
+        //   screw_ie8: true,
+        //   keep_fnames: true,
+        //   drop_debugger: false,
+        //   dead_code: false,
+        //   unused: false
+        // }, // debug
+        // comments: true, //debug
+
+
+        beautify: false, //prod
+        output: {
+          comments: false
+        }, //prod
+        mangle: {
+          screw_ie8: true
+        }, //prod
+        compress: {
+          screw_ie8: true,
+          warnings: false,
+          conditionals: true,
+          unused: true,
+          comparisons: true,
+          sequences: true,
+          dead_code: true,
+          evaluate: true,
+          if_return: true,
+          join_vars: true,
+          negate_iife: false // we need this for lazy v8
+        },
+      }),
+
+    ],
+
+  });
+}

+ 1 - 1
lib/views/layout/layout.html

@@ -23,11 +23,11 @@
   <link rel="icon" type="image/png" href="/android-chrome-192x192.png" sizes="192x192">
 
   {% if env === 'development' %}
-    <script src="{{ webpack_asset('style').js }}"></script>
     <script src="{{ webpack_asset('dev').js }}" async></script>
     <script src="/js/dll/vendor.dll.js" defer></script>
   {% endif %}
 
+  <script src="{{ webpack_asset('style').js }}"></script>
   <script src="{{ webpack_asset('commons').js }}" defer></script>
   {% if config.crowi['plugin:isEnabledPlugins'] %}
     <script src="{{ webpack_asset('plugin').js }}" defer></script>

+ 2 - 0
package.json

@@ -106,6 +106,7 @@
     "css-loader": "^0.27.1",
     "express-webpack-assets": "0.0.2",
     "mocha": "~2.2.0",
+    "optimize-js-plugin": "0.0.4",
     "proxyquire": "~1.4.0",
     "reload": "^1.1.1",
     "rimraf": "^2.6.1",
@@ -113,6 +114,7 @@
     "sinon": "~1.14.0",
     "sinon-chai": "~2.7.0",
     "style-loader": "^0.13.2",
+    "to-string-loader": "^1.1.5",
     "webpack": "2.2.0",
     "webpack-dev-server": "2.4.1",
     "webpack-dll-bundles-plugin": "^1.0.0-beta.5",