From 2fd3112bf2625325d66e2caeb2f0f17b64022759 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Sat, 6 May 2017 13:49:15 +0100 Subject: [PATCH 1/9] Add url parsing for asset host Fix asset host duplication Revert change in shared.js Return if no host is provided Remove https Remove asset host Change function name --- lib/install/config/loaders/core/assets.js | 14 ++++++++++++-- lib/install/config/webpack/configuration.js | 9 +++------ lib/install/template.rb | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/install/config/loaders/core/assets.js b/lib/install/config/loaders/core/assets.js index 595f073fc..3b7e4f3c0 100644 --- a/lib/install/config/loaders/core/assets.js +++ b/lib/install/config/loaders/core/assets.js @@ -1,11 +1,21 @@ -const { env, publicPath } = require('../configuration.js') +const Url = require('url-parse') +const { env, paths, publicPath } = require('../configuration.js') + +// Compute path for internal pack assets if ASSET_HOST is supplied +const computeAssetPath = (host) => { + if (!host) { return `/${paths.entry}/` } + const { slashes, href } = new Url(host) + if (slashes) { return `${href}/${paths.entry}/` } + if (href) { return `//${href}/${paths.entry}/` } + return `/${paths.entry}/` +} module.exports = { test: /\.(jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i, use: [{ loader: 'file-loader', options: { - publicPath, + publicPath: env.NODE_ENV === 'production' ? computeAssetPath(env.ASSET_HOST) : publicPath, name: env.NODE_ENV === 'production' ? '[name]-[hash].[ext]' : '[name].[ext]' } }] diff --git a/lib/install/config/webpack/configuration.js b/lib/install/config/webpack/configuration.js index 904af8229..f89d5db28 100644 --- a/lib/install/config/webpack/configuration.js +++ b/lib/install/config/webpack/configuration.js @@ -10,17 +10,14 @@ const loadersDir = join(__dirname, 'loaders') const paths = safeLoad(readFileSync(join(configPath, 'paths.yml'), 'utf8'))[env.NODE_ENV] const devServer = safeLoad(readFileSync(join(configPath, 'development.server.yml'), 'utf8'))[env.NODE_ENV] -// Compute public path based on environment and ASSET_HOST in production -const ifHasCDN = env.ASSET_HOST !== undefined && env.NODE_ENV === 'production' -const devServerUrl = `http://${devServer.host}:${devServer.port}/${paths.entry}/` -const publicUrl = ifHasCDN ? `${env.ASSET_HOST}/${paths.entry}/` : `/${paths.entry}/` -const publicPath = env.NODE_ENV !== 'production' && devServer.enabled ? devServerUrl : publicUrl +// Compute public path based on environment +const publicPath = env.NODE_ENV !== 'production' && devServer.enabled ? + `//${devServer.host}:${devServer.port}/${paths.entry}/` : `/${paths.entry}/` module.exports = { devServer, env, paths, loadersDir, - publicUrl, publicPath } diff --git a/lib/install/template.rb b/lib/install/template.rb index fa600347a..83dd837d9 100644 --- a/lib/install/template.rb +++ b/lib/install/template.rb @@ -27,7 +27,7 @@ "webpack-manifest-plugin babel-loader@7.x coffee-loader coffee-script " \ "babel-core babel-preset-env compression-webpack-plugin rails-erb-loader glob " \ "extract-text-webpack-plugin node-sass file-loader sass-loader css-loader style-loader " \ -"postcss-loader autoprefixer postcss-smart-import precss" +"postcss-loader autoprefixer postcss-smart-import precss url-parse" puts "Installing dev server for live reloading" run "yarn add --dev webpack-dev-server" From 1bee939acd6adc9800579e133d84e3f8ad8d2cc6 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Sun, 7 May 2017 09:58:05 +0100 Subject: [PATCH 2/9] Update changelog --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d9273a7a..7c0fa0cd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,13 @@ -- Added [Elm](http://elm-lang.org) support. You can now add Elm support via the following methods: +## [Unreleased] + +### Added + +- [Elm](http://elm-lang.org) support. You can now add Elm support via the following methods: - New app: `rails new --webpack=elm` - Within an existing app: `rails webpacker:install:elm` +- Fix asset host duplication - [#320](https://github.com/rails/webpacker/issues/320) + ## [1.2] - 2017-04-27 Some of the changes made requires you to run below commands to install new changes. From abb60de3c60ea1fc724fc53e3291cf522bfb6658 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Sun, 7 May 2017 18:49:53 +0100 Subject: [PATCH 3/9] Extract asset options --- lib/install/config/loaders/core/assets.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/install/config/loaders/core/assets.js b/lib/install/config/loaders/core/assets.js index 3b7e4f3c0..b21c9000d 100644 --- a/lib/install/config/loaders/core/assets.js +++ b/lib/install/config/loaders/core/assets.js @@ -10,13 +10,18 @@ const computeAssetPath = (host) => { return `/${paths.entry}/` } +// Get asset options based on NODE_ENV +const assetOptions = () => { + if (env.NODE_ENV === 'production') { + return { publicPath: computeAssetPath(env.ASSET_HOST), name: '[name]-[hash].[ext]' } + } + return { publicPath, name: '[name].[ext]' } +} + module.exports = { test: /\.(jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i, use: [{ loader: 'file-loader', - options: { - publicPath: env.NODE_ENV === 'production' ? computeAssetPath(env.ASSET_HOST) : publicPath, - name: env.NODE_ENV === 'production' ? '[name]-[hash].[ext]' : '[name].[ext]' - } + options: assetOptions(env) }] } From 858348d19618a70bb74c306984f01e466e050340 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Sun, 7 May 2017 18:51:18 +0100 Subject: [PATCH 4/9] Remove param --- lib/install/config/loaders/core/assets.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/install/config/loaders/core/assets.js b/lib/install/config/loaders/core/assets.js index b21c9000d..522edf02f 100644 --- a/lib/install/config/loaders/core/assets.js +++ b/lib/install/config/loaders/core/assets.js @@ -22,6 +22,6 @@ module.exports = { test: /\.(jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i, use: [{ loader: 'file-loader', - options: assetOptions(env) + options: assetOptions() }] } From badb9fa78ae4e63a23dc2d822252fb68b090c992 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Sun, 7 May 2017 19:07:20 +0100 Subject: [PATCH 5/9] Destructure into two function --- lib/install/config/loaders/core/assets.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/install/config/loaders/core/assets.js b/lib/install/config/loaders/core/assets.js index 522edf02f..dff6a5db1 100644 --- a/lib/install/config/loaders/core/assets.js +++ b/lib/install/config/loaders/core/assets.js @@ -1,13 +1,16 @@ const Url = require('url-parse') const { env, paths, publicPath } = require('../configuration.js') +// Make sure host has slashes +const ensureSlashes = ({ host, slashes }) => { + if (slashes) { return `${host}/${paths.entry}/` } + return `//${host}/${paths.entry}/` +} + // Compute path for internal pack assets if ASSET_HOST is supplied const computeAssetPath = (host) => { if (!host) { return `/${paths.entry}/` } - const { slashes, href } = new Url(host) - if (slashes) { return `${href}/${paths.entry}/` } - if (href) { return `//${href}/${paths.entry}/` } - return `/${paths.entry}/` + return ensureSlashes(new Url(host)) } // Get asset options based on NODE_ENV From fa96db8b584784566f4db9502977751b71701285 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Sun, 7 May 2017 19:10:10 +0100 Subject: [PATCH 6/9] Make function re-usable --- lib/install/config/loaders/core/assets.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/install/config/loaders/core/assets.js b/lib/install/config/loaders/core/assets.js index dff6a5db1..71843d37c 100644 --- a/lib/install/config/loaders/core/assets.js +++ b/lib/install/config/loaders/core/assets.js @@ -14,8 +14,8 @@ const computeAssetPath = (host) => { } // Get asset options based on NODE_ENV -const assetOptions = () => { - if (env.NODE_ENV === 'production') { +const assetOptions = (nodeENV) => { + if (nodeENV === 'production') { return { publicPath: computeAssetPath(env.ASSET_HOST), name: '[name]-[hash].[ext]' } } return { publicPath, name: '[name].[ext]' } @@ -25,6 +25,6 @@ module.exports = { test: /\.(jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i, use: [{ loader: 'file-loader', - options: assetOptions() + options: assetOptions(env.NODE_ENV) }] } From 32b959fd92e747572d3e41aa7769cf73027979df Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Mon, 8 May 2017 21:28:44 +0100 Subject: [PATCH 7/9] Change to href --- lib/install/config/loaders/core/assets.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/install/config/loaders/core/assets.js b/lib/install/config/loaders/core/assets.js index 71843d37c..01ab44d7b 100644 --- a/lib/install/config/loaders/core/assets.js +++ b/lib/install/config/loaders/core/assets.js @@ -2,9 +2,9 @@ const Url = require('url-parse') const { env, paths, publicPath } = require('../configuration.js') // Make sure host has slashes -const ensureSlashes = ({ host, slashes }) => { - if (slashes) { return `${host}/${paths.entry}/` } - return `//${host}/${paths.entry}/` +const ensureSlashes = ({ href, slashes }) => { + if (slashes) { return `${href}/${paths.entry}/` } + return `//${href}/${paths.entry}/` } // Compute path for internal pack assets if ASSET_HOST is supplied From 9bd7b00c04624ee39c828aaf60fb182642f4c034 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Wed, 10 May 2017 19:10:51 +0100 Subject: [PATCH 8/9] Add explicit return --- lib/install/config/loaders/core/assets.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/install/config/loaders/core/assets.js b/lib/install/config/loaders/core/assets.js index 01ab44d7b..c78a1f35d 100644 --- a/lib/install/config/loaders/core/assets.js +++ b/lib/install/config/loaders/core/assets.js @@ -1,24 +1,33 @@ +/* eslint no-else-return: 0 */ + const Url = require('url-parse') const { env, paths, publicPath } = require('../configuration.js') // Make sure host has slashes const ensureSlashes = ({ href, slashes }) => { - if (slashes) { return `${href}/${paths.entry}/` } - return `//${href}/${paths.entry}/` + if (slashes) { + return `${href}/${paths.entry}/` + } else { + return `//${href}/${paths.entry}/` + } } // Compute path for internal pack assets if ASSET_HOST is supplied const computeAssetPath = (host) => { - if (!host) { return `/${paths.entry}/` } - return ensureSlashes(new Url(host)) + if (!host) { + return `/${paths.entry}/` + } else { + return ensureSlashes(new Url(host)) + } } // Get asset options based on NODE_ENV const assetOptions = (nodeENV) => { if (nodeENV === 'production') { return { publicPath: computeAssetPath(env.ASSET_HOST), name: '[name]-[hash].[ext]' } + } else { + return { publicPath, name: '[name].[ext]' } } - return { publicPath, name: '[name].[ext]' } } module.exports = { From 586dc465102753faa26e730812cf792850d497cb Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Tue, 16 May 2017 23:06:09 +0100 Subject: [PATCH 9/9] Remove duplicate --- lib/install/template.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/install/template.rb b/lib/install/template.rb index f0fd10590..73a154fe6 100644 --- a/lib/install/template.rb +++ b/lib/install/template.rb @@ -27,7 +27,6 @@ "webpack-manifest-plugin babel-loader@7.x coffee-loader coffee-script " \ "babel-core babel-preset-env babel-polyfill compression-webpack-plugin rails-erb-loader glob " \ "extract-text-webpack-plugin node-sass file-loader sass-loader css-loader style-loader " \ -"postcss-loader autoprefixer postcss-smart-import precss url-parse" "postcss-loader autoprefixer postcss-smart-import precss resolve-url-loader url-parse" puts "Installing dev server for live reloading"