Skip to content

Commit

Permalink
Scope webpack config through env (#191)
Browse files Browse the repository at this point in the history
* Scope webpack config through env

* Disable dev server in test and production

* Add test config
  • Loading branch information
gauravtiwari authored and dhh committed Mar 27, 2017
1 parent 3c488f9 commit 7adaf8d
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/install/bin/webpack-dev-server.tt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ APP_PATH = File.expand_path("../", __dir__)
CONFIG_PATH = File.join(APP_PATH, "config/webpack/paths.yml")

begin
paths = YAML.load(File.read(CONFIG_PATH))
paths = YAML.load(File.read(CONFIG_PATH))[NODE_ENV]

NODE_MODULES_PATH = File.join(APP_PATH.shellescape, paths["node_modules"])
WEBPACK_CONFIG_PATH = File.join(APP_PATH.shellescape, paths["config"])
Expand Down
4 changes: 2 additions & 2 deletions lib/install/bin/webpack.tt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ CONFIG_PATH = File.join(APP_PATH, "config/webpack/paths.yml")
DEV_SERVER_CONFIG_PATH = File.join(APP_PATH, "config/webpack/development.server.yml")

begin
paths = YAML.load(File.read(CONFIG_PATH))
dev_server = YAML.load(File.read(DEV_SERVER_CONFIG_PATH))
paths = YAML.load(File.read(CONFIG_PATH))[NODE_ENV]
dev_server = YAML.load(File.read(DEV_SERVER_CONFIG_PATH))[NODE_ENV]

NODE_MODULES_PATH = File.join(APP_PATH.shellescape, paths["node_modules"])
WEBPACK_CONFIG_PATH = File.join(APP_PATH.shellescape, paths["config"])
Expand Down
4 changes: 2 additions & 2 deletions lib/install/config/webpack/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const { readFileSync } = require('fs')

const configPath = resolve('config', 'webpack')
const loadersDir = join(__dirname, 'loaders')
const paths = safeLoad(readFileSync(join(configPath, 'paths.yml'), 'utf8'))
const devServer = safeLoad(readFileSync(join(configPath, 'development.server.yml'), 'utf8'))
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]
const publicPath = env.NODE_ENV !== 'production' && devServer.enabled ?
`http://${devServer.host}:${devServer.port}/` : `/${paths.entry}/`

Expand Down
18 changes: 15 additions & 3 deletions lib/install/config/webpack/development.server.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
# Restart webpack-dev-server if you make changes here
enabled: true
host: localhost
port: 8080
default: &default
enabled: true
host: localhost
port: 8080

development:
<<: *default

test:
<<: *default
enabled: false

production:
<<: *default
enabled: false
48 changes: 30 additions & 18 deletions lib/install/config/webpack/paths.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
# Restart webpack-watcher or webpack-dev-server if you make changes here
config: config/webpack
entry: packs
output: public
node_modules: node_modules
source: app/javascript
extensions:
- .coffee
- .js
- .jsx
- .ts
- .vue
- .sass
- .scss
- .css
- .png
- .svg
- .gif
- .jpeg
default: &default
config: config/webpack
entry: packs
output: public
manifest: manifest.json
node_modules: node_modules
source: app/javascript
extensions:
- .coffee
- .js
- .jsx
- .ts
- .vue
- .sass
- .scss
- .css
- .png
- .svg
- .gif
- .jpeg

development:
<<: *default

test:
<<: *default
manifest: manifest-test.json

production:
<<: *default
2 changes: 1 addition & 1 deletion lib/install/config/webpack/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
plugins: [
new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(env))),
new ExtractTextPlugin(env.NODE_ENV === 'production' ? '[name]-[hash].css' : '[name].css'),
new ManifestPlugin({ fileName: 'manifest.json', publicPath, writeToFileEmit: true })
new ManifestPlugin({ fileName: paths.manifest, publicPath, writeToFileEmit: true })
],

resolve: {
Expand Down
6 changes: 6 additions & 0 deletions lib/install/config/webpack/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Note: You must restart bin/webpack-watcher for changes to take effect

const merge = require('webpack-merge')
const sharedConfig = require('./shared.js')

module.exports = merge(sharedConfig, {})
4 changes: 2 additions & 2 deletions lib/webpacker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def file_path
end

def manifest_path
Rails.root.join(output_path, "manifest.json")
Rails.root.join(output_path, paths.fetch(:manifest, "manifest.json"))
end

def output_path
Expand All @@ -37,6 +37,6 @@ def source_path
private
def load
return super unless File.exist?(@path)
HashWithIndifferentAccess.new(YAML.load(File.read(@path)))
HashWithIndifferentAccess.new(YAML.load(File.read(@path))[Rails.env])
end
end

0 comments on commit 7adaf8d

Please sign in to comment.