From 96e7c459f19937c9d739fb000f8decf052b0e125 Mon Sep 17 00:00:00 2001 From: daneryl Date: Thu, 24 Aug 2017 11:23:11 +0200 Subject: [PATCH] webpack config for dev does not hash the assets --- app/api/config/database.js | 2 +- app/api/config/elasticIndexes.js | 2 +- app/api/config/ports.js | 2 +- server.js | 8 +- webpack.config.js | 2 +- webpack.production.config.js | 3 +- webpack/config.js | 217 ++++++++++++++++--------------- 7 files changed, 127 insertions(+), 109 deletions(-) diff --git a/app/api/config/database.js b/app/api/config/database.js index 5cc37e15cd..667dd8bac9 100644 --- a/app/api/config/database.js +++ b/app/api/config/database.js @@ -4,5 +4,5 @@ export default { demo: 'mongodb://localhost/uwazi_demo', development: DBHOST ? `mongodb://${DBHOST}/${DATABASE_NAME}` : 'mongodb://localhost/uwazi_development', testing: DBHOST ? `mongodb://${DBHOST}/${DATABASE_NAME}` : 'mongodb://localhost/uwazi_testing', - production: `mongodb://localhost/${DATABASE_NAME}` + production: DBHOST ? `mongodb://${DBHOST}/${DATABASE_NAME}` : 'mongodb://localhost/uwazi_development' }; diff --git a/app/api/config/elasticIndexes.js b/app/api/config/elasticIndexes.js index d89953f535..41239d015d 100644 --- a/app/api/config/elasticIndexes.js +++ b/app/api/config/elasticIndexes.js @@ -1,7 +1,7 @@ const INDEX_NAME = process.env.INDEX_NAME; export default { - production: INDEX_NAME ? INDEX_NAME : 'uwazi', + production: INDEX_NAME ? INDEX_NAME : 'uwazi_development', testing: INDEX_NAME ? INDEX_NAME : 'testing', development: INDEX_NAME ? INDEX_NAME : 'uwazi_development', demo: INDEX_NAME ? INDEX_NAME : 'uwazi_demo' diff --git a/app/api/config/ports.js b/app/api/config/ports.js index 88d4ab199c..a91429d39d 100644 --- a/app/api/config/ports.js +++ b/app/api/config/ports.js @@ -1,6 +1,6 @@ const PORT = process.env.PORT; export default { - production: PORT ? PORT : 3001, + production: PORT ? PORT : 3000, development: PORT ? PORT : 3000, demo: PORT ? PORT : 4000 }; diff --git a/server.js b/server.js index 38f9a6c03d..98936035d6 100644 --- a/server.js +++ b/server.js @@ -18,7 +18,13 @@ var error_handling_middleware = require('./app/api/utils/error_handling_middlewa app.use(error_handling_middleware); app.use(compression()); var oneYear = 31557600; -app.use(express.static(path.resolve(__dirname, 'dist'), {maxage: oneYear})); + +var maxage = 0; +if (app.get('env') === 'production') { + maxage = oneYear; +} + +app.use(express.static(path.resolve(__dirname, 'dist'), {maxage: maxage})); app.use('/uploaded_documents', express.static(path.resolve(__dirname, 'uploaded_documents'))); app.use('/public', express.static(path.resolve(__dirname, 'public'))); app.use('/flag-images', express.static(path.resolve(__dirname, 'node_modules/react-flags/vendor/flags'))); diff --git a/webpack.config.js b/webpack.config.js index df47ea15c3..f0ce82ba38 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,7 +1,7 @@ /* eslint-disable */ 'use strict'; -var config = require('./webpack/config'); +var config = require('./webpack/config')(); config.context = __dirname; diff --git a/webpack.production.config.js b/webpack.production.config.js index 30219715b5..9224c50fc7 100644 --- a/webpack.production.config.js +++ b/webpack.production.config.js @@ -6,7 +6,8 @@ var webpack = require('webpack'); var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin'); var CompressionPlugin = require('compression-webpack-plugin'); -var config = require('./webpack/config'); +var production = true; +var config = require('./webpack/config')(production); config.devtool = 'cheap-module-source-map'; config.context = __dirname; diff --git a/webpack/config.js b/webpack/config.js index c5f5d6004e..f973a4ad4b 100644 --- a/webpack/config.js +++ b/webpack/config.js @@ -8,111 +8,122 @@ var CleanPlugin = require('./CleanPlugin'); var rootPath = __dirname + '/../'; -const CoreCss = new ExtractTextPlugin('styles.[contenthash].css'); var AssetsPlugin = require('assets-webpack-plugin') var assetsPluginInstance = new AssetsPlugin({path: path.join(rootPath + '/dist/')}) -module.exports = { - context: rootPath, - devtool: '#eval-source-map', - entry: { - main: path.join(rootPath, 'app/react/index.js'), - nprogress: path.join(rootPath, 'node_modules/nprogress/nprogress.js'), - 'pdf.worker': path.join(rootPath, 'node_modules/pdfjs-dist/build/pdf.worker.entry'), - }, - output: { - path: path.join(rootPath, '/dist/'), - publicPath: '/', - filename: '[name].[chunkhash].js' - }, - resolveLoader: { - modules: ['node_modules', __dirname + '/webpackLoaders'], - extensions: ['.js', '.json'], - mainFields: ['loader', 'main'] - }, - module: { - rules: [ - { - test: /world-countries/, - loader: 'country-loader' - }, - { - test: /\.js$/, - loader: 'babel-loader?cacheDirectory', - include: path.join(rootPath, 'app'), - exclude: /node_modules/ - }, - { - test: /\.scss$/, - loader: CoreCss.extract({ - fallback: 'style-loader', - use: 'css-loader?sourceMap!sass-loader?outputStyle=expanded&sourceMap=true&sourceMapContents=true' - }), - include: [ - path.join(rootPath, 'app'), - path.join(rootPath, 'node_modules/react-widgets/lib/scss/') - ] - }, - { - test: /\.css$/, - loader: CoreCss.extract({ - fallback: 'style-loader', - use: 'css-loader?sourceMap' - }), - include: [ - path.join(rootPath, 'app'), - path.join(rootPath, 'node_modules/react-datepicker/dist/'), - path.join(rootPath, 'node_modules/bootstrap/dist/'), - path.join(rootPath, 'node_modules/nprogress/'), - path.join(rootPath, 'node_modules/font-awesome/css/'), - path.join(rootPath, 'node_modules/pdfjs-dist/web'), - ] - }, - { - test: /\.(jpe?g|png|eot|woff|woff2|ttf|gif|svg)(\?.*)?$/i, - loaders: ['url-loader', 'img-loader'], - include: [ - path.join(rootPath, 'public'), - path.join(rootPath, 'app'), - path.join(rootPath, 'node_modules/react-widgets/lib/fonts/'), - path.join(rootPath, 'node_modules/font-awesome/fonts/'), - path.join(rootPath, 'node_modules/react-widgets/lib/img/'), - path.join(rootPath, 'node_modules/pdfjs-dist/web/images/'), - path.join(rootPath, 'node_modules/pdfjs-dist/web/images/'), - path.join(rootPath, 'node_modules/bootstrap/dist/fonts/') - ] - }, - { - test:/\.json$/i, - loaders: ['json-loader'], - include: [ - path.join(rootPath, 'app') - ] - } - ] - }, - plugins: [ - new CleanPlugin(__dirname + '/../dist/'), - new webpack.optimize.CommonsChunkPlugin({ - name: 'vendor', - chunks: ["main"], - minChunks: ({ resource }) => { - if (/pdfjs/.test(resource)) { - return false; - } +module.exports = function(production) { + var stylesName = 'styles.css'; + var jsChunkHashName = ''; + + if (production) { + stylesName = 'styles.[contenthash].css'; + jsChunkHashName = '.[chunkhash]'; + } - if (/.js$/.test(resource)) { - return /node_modules/.test(resource) + const CoreCss = new ExtractTextPlugin(stylesName); + return { + context: rootPath, + devtool: '#eval-source-map', + entry: { + main: path.join(rootPath, 'app/react/index.js'), + nprogress: path.join(rootPath, 'node_modules/nprogress/nprogress.js'), + 'pdf.worker': path.join(rootPath, 'node_modules/pdfjs-dist/build/pdf.worker.entry'), + }, + output: { + path: path.join(rootPath, '/dist/'), + publicPath: '/', + filename: '[name]'+jsChunkHashName+'.js' + }, + resolveLoader: { + modules: ['node_modules', __dirname + '/webpackLoaders'], + extensions: ['.js', '.json'], + mainFields: ['loader', 'main'] + }, + module: { + rules: [ + { + test: /world-countries/, + loader: 'country-loader' + }, + { + test: /\.js$/, + loader: 'babel-loader?cacheDirectory', + include: path.join(rootPath, 'app'), + exclude: /node_modules/ + }, + { + test: /\.scss$/, + loader: CoreCss.extract({ + fallback: 'style-loader', + use: 'css-loader?sourceMap!sass-loader?outputStyle=expanded&sourceMap=true&sourceMapContents=true' + }), + include: [ + path.join(rootPath, 'app'), + path.join(rootPath, 'node_modules/react-widgets/lib/scss/') + ] + }, + { + test: /\.css$/, + loader: CoreCss.extract({ + fallback: 'style-loader', + use: 'css-loader?sourceMap' + }), + include: [ + path.join(rootPath, 'app'), + path.join(rootPath, 'node_modules/react-datepicker/dist/'), + path.join(rootPath, 'node_modules/bootstrap/dist/'), + path.join(rootPath, 'node_modules/nprogress/'), + path.join(rootPath, 'node_modules/font-awesome/css/'), + path.join(rootPath, 'node_modules/pdfjs-dist/web'), + ] + }, + { + test: /\.(jpe?g|png|eot|woff|woff2|ttf|gif|svg)(\?.*)?$/i, + loaders: ['url-loader', 'img-loader'], + include: [ + path.join(rootPath, 'public'), + path.join(rootPath, 'app'), + path.join(rootPath, 'node_modules/react-widgets/lib/fonts/'), + path.join(rootPath, 'node_modules/font-awesome/fonts/'), + path.join(rootPath, 'node_modules/react-widgets/lib/img/'), + path.join(rootPath, 'node_modules/pdfjs-dist/web/images/'), + path.join(rootPath, 'node_modules/pdfjs-dist/web/images/'), + path.join(rootPath, 'node_modules/bootstrap/dist/fonts/') + ] + }, + { + test:/\.json$/i, + loaders: ['json-loader'], + include: [ + path.join(rootPath, 'app') + ] } - }, - }), - new webpack.optimize.CommonsChunkPlugin({ - name: 'manifest', - chunks: ["main", "vendor", "nprogress", "pdf.worker"], - minChunks: Infinity - }), - CoreCss, - new webpack.optimize.ModuleConcatenationPlugin(), - assetsPluginInstance - ] -}; + ] + }, + plugins: [ + new CleanPlugin(__dirname + '/../dist/'), + new webpack.optimize.CommonsChunkPlugin({ + name: 'vendor', + chunks: ["main"], + minChunks: ({ resource }) => { + if (/pdfjs/.test(resource)) { + return false; + } + + if (/.js$/.test(resource)) { + return /node_modules/.test(resource) + } + }, + }), + new webpack.optimize.CommonsChunkPlugin({ + name: 'manifest', + chunks: ["main", "vendor", "nprogress", "pdf.worker"], + minChunks: Infinity + }), + CoreCss, + new webpack.optimize.ModuleConcatenationPlugin(), + assetsPluginInstance + ] + }; +} +