Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate .yml files into webpacker.yml #403

Merged
merged 27 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
dc1e539
Consolidate yml config into one file webpacker.yml
gauravtiwari May 20, 2017
eaa5056
Remove enabled
gauravtiwari May 21, 2017
f03ed31
Change case and move extension out of paths
gauravtiwari May 21, 2017
c877358
Change output directory for test
gauravtiwari May 21, 2017
af4be41
Add back argv
gauravtiwari May 21, 2017
58c428d
Use 0.0.0.0 for cloud 9
gauravtiwari May 21, 2017
1702466
Remove manifest overide since we don't use different output
gauravtiwari May 21, 2017
099f080
Revert configuration.rb to use default path-per-key
gauravtiwari May 21, 2017
a60d569
Add https to dev server
gauravtiwari May 21, 2017
32f18cd
Move option up
gauravtiwari May 21, 2017
91c6c7b
Remove config and manifest option
gauravtiwari May 21, 2017
abc53ac
Remove reference to config_path
gauravtiwari May 21, 2017
37706a9
Flatten webpacker settings
gauravtiwari May 21, 2017
f76db0e
Fix extensions
gauravtiwari May 21, 2017
1140244
Update readme
gauravtiwari May 21, 2017
f992607
Update changelog
gauravtiwari May 21, 2017
5e363f3
Use 0.0.0.0 for host
gauravtiwari May 21, 2017
bb19d26
Fix typo
gauravtiwari May 21, 2017
6cdf5d0
Move dev_server to development env
gauravtiwari May 21, 2017
57e082c
Change wording
gauravtiwari May 21, 2017
3046503
Remove contentBase
gauravtiwari May 21, 2017
9d3288a
Fix typo
gauravtiwari May 21, 2017
bfab985
Update changelog and fix compile task
gauravtiwari May 22, 2017
c030cf9
Add a link break
gauravtiwari May 22, 2017
6b60766
Print stdout alongside stderr
gauravtiwari May 22, 2017
6cc0600
Use puts
gauravtiwari May 22, 2017
57e7ecf
Remove extra puts
gauravtiwari May 22, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
- New app: `rails new <app> --webpack=elm`
- Within an existing app: `rails webpacker:install:elm`

- Support for custom `output` paths independent of `entry` in `paths.yml`. `output` is also now relative to `public/`. - [#397](https://github.com/rails/webpacker/pull/397)
- Support for custom `public_output_path` paths independent of `source_entry_path` in `config/webpacker.yml`. `output` is also now relative to `public/`. - [#397](https://github.com/rails/webpacker/pull/397)

Before (compile to `public/packs`):
```yaml
entry: packs
output: public
source_entry_path: packs
public_output_path: packs
```
After (compile to `public/sweet/js`):
```yaml
entry: packs
output: sweet/js
source_entry_path: packs
public_output_path: sweet/js
```
- Consolidate `paths.yml` and `development.server.yml` into one file - `config/webpacker.yml` - [#403](https://github.com/rails/webpacker/pull/403)

## [1.2] - 2017-04-27
Some of the changes made requires you to run below commands to install new changes.
Expand Down
27 changes: 11 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,27 @@ and
## Advanced Configuration

By default, webpacker offers simple conventions for where the webpack configs, javascript app files and compiled webpack bundles will go in your rails app,
but all these options are configurable from `config/webpack/paths.yml` file.
but all these options are configurable from `config/webpacker.yml` file.

```yml
# config/webpack/paths.yml
source: app/javascript
entry: packs
output: public
config: config/webpack
node_modules: node_modules
# config/webpacker.yml
source_path: app/assets/javascript
source_entry_path: entries
public_output_path: entries
```

*Note:* Behind the scenes, webpacker will use same `entry` directory name inside `output`
directory to emit bundles. For ex, `public/packs`

Similary, you can also control and configure `webpack-dev-server` settings from
`config/webpack/development.server.yml` file
`config/webpacker.yml` file

```yml
# config/webpack/development.server.yml
enabled: true
host: localhost
port: 8080
# config/webpacker.yml
dev_server_host: localhost
dev_server_port: 8080
dev_server_https: false
```

By default, `webpack-dev-server` uses `output` option specified in
`paths.yml` as `contentBase`.
`webpacker.yml` as `contentBase`.

## Linking to static assets

Expand Down
2 changes: 1 addition & 1 deletion lib/install/angular.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "webpacker/configuration"

puts "Copying angular loader to #{Webpacker::Configuration.config_path}/loaders"
puts "Copying angular loader to config/webpack/loaders"
copy_file "#{__dir__}/config/loaders/installers/angular.js", "config/webpack/loaders/angular.js"

puts "Copying angular example entry file to #{Webpacker::Configuration.entry_path}"
Expand Down
30 changes: 19 additions & 11 deletions lib/install/bin/webpack-dev-server.tt
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,32 @@ RAILS_ENV = ENV["RAILS_ENV"]
ENV["NODE_ENV"] ||= RAILS_ENV
NODE_ENV = ENV["NODE_ENV"]

APP_PATH = File.expand_path("../", __dir__)
APP_PATH = File.expand_path("../", __dir__)
CONFIG_FILE = File.join(APP_PATH, "config/webpacker.yml")
NODE_MODULES_PATH = File.join(APP_PATH, "node_modules")
WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/development.js")

def args(key)
index = ARGV.index(key)
index ? ARGV[index + 1] : nil
end

begin
config = YAML.load_file(CONFIG_FILE)[NODE_ENV]

DEV_SERVER_HOST = "http#{"s" if args('--https') || config["dev_server_https"]}://#{args('--host') || config["dev_server_host"]}:#{args('--port') || config["dev_server_port"]}"

def load_yaml_config(config_file)
YAML.load_file(File.join(APP_PATH, config_file))[NODE_ENV]
rescue Errno::ENOENT, NoMethodError
puts "Configuration not found in #{config_file}."
puts "Configuration not found in #{CONFIG_FILE}."
puts "Please run bundle exec rails webpacker:install to install webpacker"
exit!
end

paths = load_yaml_config("config/webpack/paths.yml")
NODE_MODULES_PATH = File.join(APP_PATH, paths["node_modules"])
WEBPACK_CONFIG = File.join(APP_PATH, paths["config"], "development.server.js")

dev_server = load_yaml_config("config/webpack/development.server.yml")
DEV_SERVER_HOST = "http#{"s" if dev_server["https"]}://#{dev_server["host"]}:#{dev_server["port"]}"
newenv = {
"NODE_PATH" => NODE_MODULES_PATH.shellescape,
"ASSET_HOST" => DEV_SERVER_HOST.shellescape
}.freeze

newenv = { "NODE_PATH" => NODE_MODULES_PATH.shellescape, "ASSET_HOST" => DEV_SERVER_HOST.shellescape }
cmdline = ["yarn", "run", "webpack-dev-server", "--", "--progress", "--color", "--config", WEBPACK_CONFIG] + ARGV

Dir.chdir(APP_PATH) do
Expand Down
16 changes: 6 additions & 10 deletions lib/install/bin/webpack.tt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@ RAILS_ENV = ENV["RAILS_ENV"]
ENV["NODE_ENV"] ||= RAILS_ENV
NODE_ENV = ENV["NODE_ENV"]

APP_PATH = File.expand_path("../", __dir__)
APP_PATH = File.expand_path("../", __dir__)
NODE_MODULES_PATH = File.join(APP_PATH, "node_modules")
WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/#{NODE_ENV}.js")

def load_yaml_config(config_file)
YAML.load_file(File.join(APP_PATH, config_file))[NODE_ENV]
rescue Errno::ENOENT, NoMethodError
puts "Configuration not found in #{config_file}."
unless File.exist?(WEBPACK_CONFIG)
puts "Webpack configuration not found."
puts "Please run bundle exec rails webpacker:install to install webpacker"
exit!
end

paths = load_yaml_config("config/webpack/paths.yml")
NODE_MODULES_PATH = File.join(APP_PATH, paths["node_modules"])
WEBPACK_CONFIG = File.join(APP_PATH, paths["config"], "#{NODE_ENV}.js")

newenv = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }
newenv = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }
cmdline = ["yarn", "run", "webpack", "--", "--config", WEBPACK_CONFIG] + ARGV

Dir.chdir(APP_PATH) do
Expand Down
14 changes: 6 additions & 8 deletions lib/install/config/webpack/configuration.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Common configuration for webpacker loaded from config/webpack/paths.yml
// Common configuration for webpacker loaded from config/webpacker.yml

const { join, resolve } = require('path')
const { env } = require('process')
const { safeLoad } = require('js-yaml')
const { readFileSync } = require('fs')

const configPath = resolve('config', 'webpack')
const configPath = resolve('config', 'webpacker.yml')
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]
const settings = safeLoad(readFileSync(configPath), 'utf8')[env.NODE_ENV]

function removeOuterSlashes(string) {
return string.replace(/^\/*/, '').replace(/\/*$/, '')
Expand All @@ -24,14 +23,13 @@ function formatPublicPath(host = '', path = '') {
}

const output = {
path: resolve('public', paths.output),
publicPath: formatPublicPath(env.ASSET_HOST, paths.output)
path: resolve('public', settings.public_output_path),
publicPath: formatPublicPath(env.ASSET_HOST, settings.public_output_path)
}

module.exports = {
devServer,
settings,
env,
paths,
loadersDir,
output
}
18 changes: 17 additions & 1 deletion lib/install/config/webpack/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@

const merge = require('webpack-merge')
const sharedConfig = require('./shared.js')
const { settings, output } = require('./configuration.js')

module.exports = merge(sharedConfig, {
devtool: 'sourcemap',
devtool: 'cheap-module-source-map',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a necessary change or just better source map option?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommended source map option in development


stats: {
errorDetails: true
},

output: {
pathinfo: true
},

devServer: {
clientLogLevel: 'none',
https: settings.dev_server_https,
host: settings.dev_server_host,
port: settings.dev_server_port,
contentBase: output.path,
publicPath: output.publicPath,
compress: true,
headers: { 'Access-Control-Allow-Origin': '*' },
historyApiFallback: true,
watchOptions: {
ignored: /node_modules/
}
}
})
17 changes: 0 additions & 17 deletions lib/install/config/webpack/development.server.js

This file was deleted.

17 changes: 0 additions & 17 deletions lib/install/config/webpack/development.server.yml

This file was deleted.

33 changes: 0 additions & 33 deletions lib/install/config/webpack/paths.yml

This file was deleted.

18 changes: 9 additions & 9 deletions lib/install/config/webpack/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ const { sync } = require('glob')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const ManifestPlugin = require('webpack-manifest-plugin')
const extname = require('path-complete-extname')
const { env, paths, output, loadersDir } = require('./configuration.js')
const { env, settings, output, loadersDir } = require('./configuration.js')

const extensionGlob = `**/*{${paths.extensions.join(',')}}*`
const packPaths = sync(join(paths.source, paths.entry, extensionGlob))
const extensionGlob = `**/*{${settings.extensions.join(',')}}*`
const entryPath = join(settings.source_path, settings.source_entry_path)
const packPaths = sync(join(entryPath, extensionGlob))

module.exports = {
entry: packPaths.reduce(
(map, entry) => {
const localMap = map
const namespace = relative(join(paths.source, paths.entry), dirname(entry))
const namespace = relative(join(entryPath), dirname(entry))
localMap[join(namespace, basename(entry, extname(entry)))] = resolve(entry)
return localMap
}, {}
Expand All @@ -38,21 +39,20 @@ module.exports = {
new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(env))),
new ExtractTextPlugin(env.NODE_ENV === 'production' ? '[name]-[hash].css' : '[name].css'),
new ManifestPlugin({
fileName: paths.manifest,
publicPath: output.publicPath,
writeToFileEmit: true
})
],

resolve: {
extensions: paths.extensions,
extensions: settings.extensions,
modules: [
resolve(paths.source),
resolve(paths.node_modules)
resolve(settings.source_path),
'node_modules'
]
},

resolveLoader: {
modules: [paths.node_modules]
modules: ['node_modules']
}
}
41 changes: 41 additions & 0 deletions lib/install/config/webpacker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
dev_server_host: localhost
dev_server_port: 8080
dev_server_https: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more.. Maybe we should move these options to the development config only since bin/webpack-dev-server exclusive loads config/webpack/development.js (which is the only config file containing devServer).

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  ...
  
development:
  <<: *default

  dev_server:
    host: 0.0.0.0
    port: 8080
    https: false
...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless someone wanna override it in other environment - cloud9? I guess then we would enter into the same problem we discussed earlier right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never used cloud9, but I gather that the issue in #176 in wasn't about using a different environment, just configuring the host and port. It looks like apps on cloud9 run in development mode: https://community.c9.io/t/running-a-rails-app/1615

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me neither 😄 Alright, makes sense so lets move the dev server to development only.


source_path: app/javascript
source_entry_path: packs
public_output_path: packs

extensions:
- .coffee
- .erb
- .js
- .jsx
- .ts
- .vue
- .sass
- .scss
- .css
- .png
- .svg
- .gif
- .jpeg
- .jpg

development:
<<: *default

test:
<<: *default

public_output_path: packs-test

production:
<<: *default

dev_server_host: 0.0.0.0
dev_server_port: 8080
dev_server_https: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My vote is to remove these alternate dev server settings from production. People can add them if they're using the dev server in production (for some reason). Also, if 0.0.0.0 works more broadly, let's use it in the default settings instead of localhost.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍

Loading