From 158987a82b605b534bcdc9468efca2602313c791 Mon Sep 17 00:00:00 2001 From: Gaurav Tiwari Date: Mon, 22 May 2017 17:23:08 +0100 Subject: [PATCH] Consolidate .yml files into webpacker.yml (#403) * Consolidate yml config into one file webpacker.yml Setup eager_load Minor cleanup Remove behind the scenes Update template installer Use filename Install webpack loader as dependency Fix spacing Add watchtoptions Remove hot Remove ARGV for dev server Put back colors Oops use nil Use cheap-module-source-map Minor cleanup Refactor Change to default_settings * Remove enabled * Change case and move extension out of paths * Change output directory for test * Add back argv * Use 0.0.0.0 for cloud 9 * Remove manifest overide since we don't use different output * Revert configuration.rb to use default path-per-key Fix typo * Add https to dev server * Move option up * Remove config and manifest option * Remove reference to config_path * Flatten webpacker settings * Fix extensions * Update readme * Update changelog * Use 0.0.0.0 for host * Fix typo * Move dev_server to development env * Change wording * Remove contentBase * Fix typo * Update changelog and fix compile task * Add a link break * Print stdout alongside stderr * Use puts * Remove extra puts --- CHANGELOG.md | 34 ++++++++++++++--- README.md | 29 ++++++-------- lib/install/angular.rb | 2 +- lib/install/bin/webpack-dev-server.tt | 30 +++++++++------ lib/install/bin/webpack.tt | 16 +++----- lib/install/config/webpack/configuration.js | 14 +++---- lib/install/config/webpack/development.js | 18 ++++++++- .../config/webpack/development.server.js | 17 --------- .../config/webpack/development.server.yml | 17 --------- lib/install/config/webpack/paths.yml | 33 ---------------- lib/install/config/webpack/shared.js | 18 ++++----- lib/install/config/webpacker.yml | 38 +++++++++++++++++++ lib/install/elm.rb | 10 ++--- lib/install/react.rb | 2 +- lib/install/template.rb | 14 +++++-- lib/install/vue.rb | 2 +- lib/tasks/webpacker/compile.rake | 24 +++++++----- lib/tasks/webpacker/verify_install.rake | 2 +- lib/webpacker/configuration.rb | 24 +++++------- lib/webpacker/env.rb | 4 +- test/configuration_test.rb | 7 +--- test/env_test.rb | 2 +- test/webpacker_test.rb | 1 + 23 files changed, 184 insertions(+), 174 deletions(-) delete mode 100644 lib/install/config/webpack/development.server.js delete mode 100644 lib/install/config/webpack/development.server.yml delete mode 100644 lib/install/config/webpack/paths.yml create mode 100644 lib/install/config/webpacker.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 27b1d5381..768da276a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## [Unreleased] + ### Fixed - Update `.babelrc` to fix compilation issues - [#306](https://github.com/rails/webpacker/issues/306) @@ -7,23 +9,45 @@ - Incorrect asset host when running `webpacker:compile` or `bin/webpack` in development mode - [#397](https://github.com/rails/webpacker/pull/397) +- Update `webpacker:compile` task to use `stdout` and `stderr` for better logging - [#395](https://github.com/rails/webpacker/issues/395) + +- ARGV support for `webpack-dev-server` - [#286](https://github.com/rails/webpacker/issues/286) + ### 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` -- 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 ``` + +- `https` option to use `https` mode, particularly on platforms like - https://community.c9.io/t/running-a-rails-app/1615 or locally - [#176](https://github.com/rails/webpacker/issues/176) + + +#### Breaking Change + +- Consolidate and flatten `paths.yml` and `development.server.yml` config into one file - `config/webpacker.yml` - [#403](https://github.com/rails/webpacker/pull/403). This is a breaking change and requires you to re-install webpacker and cleanup old configuration files. + + ```bash + bundle update webpacker + bundle exec rails webpacker:install + + # Remove old/unused configuration files + rm config/paths.yml + rm config/development.server.yml + rm config/development.server.js + ``` + ## [1.2] - 2017-04-27 Some of the changes made requires you to run below commands to install new changes. diff --git a/README.md b/README.md index 9ed33ee39..b469a4530 100644 --- a/README.md +++ b/README.md @@ -101,33 +101,26 @@ 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: 0.0.0.0 + port: 8080 + https: false ``` -By default, `webpack-dev-server` uses `output` option specified in -`paths.yml` as `contentBase`. - ## Linking to static assets Static assets like images, fonts and stylesheets support is enabled out-of-box so, you can link them into your javascript app code and have them compiled automatically. diff --git a/lib/install/angular.rb b/lib/install/angular.rb index 7d2dd7964..09112d028 100644 --- a/lib/install/angular.rb +++ b/lib/install/angular.rb @@ -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}" diff --git a/lib/install/bin/webpack-dev-server.tt b/lib/install/bin/webpack-dev-server.tt index 3a736fc96..58e1faad2 100644 --- a/lib/install/bin/webpack-dev-server.tt +++ b/lib/install/bin/webpack-dev-server.tt @@ -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 + dev_server = YAML.load_file(CONFIG_FILE)["development"]["dev_server"] + + DEV_SERVER_HOST = "http#{"s" if args('--https') || dev_server["https"]}://#{args('--host') || dev_server["host"]}:#{args('--port') || 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 "Webpack dev_server 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 diff --git a/lib/install/bin/webpack.tt b/lib/install/bin/webpack.tt index 09371db1a..751da1318 100644 --- a/lib/install/bin/webpack.tt +++ b/lib/install/bin/webpack.tt @@ -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 diff --git a/lib/install/config/webpack/configuration.js b/lib/install/config/webpack/configuration.js index 4f55505a8..0d669ff0a 100644 --- a/lib/install/config/webpack/configuration.js +++ b/lib/install/config/webpack/configuration.js @@ -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(/\/*$/, '') @@ -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 } diff --git a/lib/install/config/webpack/development.js b/lib/install/config/webpack/development.js index 7dfa2df11..87772d84f 100644 --- a/lib/install/config/webpack/development.js +++ b/lib/install/config/webpack/development.js @@ -2,9 +2,10 @@ const merge = require('webpack-merge') const sharedConfig = require('./shared.js') +const { settings, output } = require('./configuration.js') module.exports = merge(sharedConfig, { - devtool: 'sourcemap', + devtool: 'cheap-eval-source-map', stats: { errorDetails: true @@ -12,5 +13,20 @@ module.exports = merge(sharedConfig, { 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/ + } } }) diff --git a/lib/install/config/webpack/development.server.js b/lib/install/config/webpack/development.server.js deleted file mode 100644 index 08dfff1e4..000000000 --- a/lib/install/config/webpack/development.server.js +++ /dev/null @@ -1,17 +0,0 @@ -// Note: You must restart bin/webpack-dev-server for changes to take effect - -const merge = require('webpack-merge') -const devConfig = require('./development.js') -const { devServer, output } = require('./configuration.js') - -module.exports = merge(devConfig, { - devServer: { - host: devServer.host, - port: devServer.port, - contentBase: output.path, - publicPath: output.publicPath, - compress: true, - headers: { 'Access-Control-Allow-Origin': '*' }, - historyApiFallback: true - } -}) diff --git a/lib/install/config/webpack/development.server.yml b/lib/install/config/webpack/development.server.yml deleted file mode 100644 index ee588a888..000000000 --- a/lib/install/config/webpack/development.server.yml +++ /dev/null @@ -1,17 +0,0 @@ -# Note: You must restart bin/webpack-dev-server for changes to take effect - -default: &default - enabled: true - host: localhost - port: 8080 - -development: - <<: *default - -test: - <<: *default - enabled: false - -production: - <<: *default - enabled: false diff --git a/lib/install/config/webpack/paths.yml b/lib/install/config/webpack/paths.yml deleted file mode 100644 index 5c3550ccf..000000000 --- a/lib/install/config/webpack/paths.yml +++ /dev/null @@ -1,33 +0,0 @@ -# Note: You must restart bin/webpack-dev-server for changes to take effect - -default: &default # ~ = Rails.root - source: app/javascript # ~/:source - entry: packs # ~/:source/:entry - output: packs # ~/public/:output - manifest: manifest.json # ~/public/:output/:manifest - config: config/webpack # ~/:config - node_modules: node_modules # ~/:node_modules - extensions: - - .coffee - - .js - - .jsx - - .ts - - .vue - - .sass - - .scss - - .css - - .png - - .svg - - .gif - - .jpeg - - .jpg - -development: - <<: *default - -test: - <<: *default - manifest: manifest-test.json - -production: - <<: *default diff --git a/lib/install/config/webpack/shared.js b/lib/install/config/webpack/shared.js index 53a0b5895..d901cc6da 100644 --- a/lib/install/config/webpack/shared.js +++ b/lib/install/config/webpack/shared.js @@ -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 }, {} @@ -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'] } } diff --git a/lib/install/config/webpacker.yml b/lib/install/config/webpacker.yml new file mode 100644 index 000000000..28f2b0221 --- /dev/null +++ b/lib/install/config/webpacker.yml @@ -0,0 +1,38 @@ +# Note: You must restart bin/webpack-dev-server for changes to take effect + +default: &default + 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 + + dev_server: + host: 0.0.0.0 + port: 8080 + https: false + +test: + <<: *default + + public_output_path: packs-test + +production: + <<: *default diff --git a/lib/install/elm.rb b/lib/install/elm.rb index 9bbf68f79..033254436 100644 --- a/lib/install/elm.rb +++ b/lib/install/elm.rb @@ -1,8 +1,8 @@ require "webpacker/configuration" -puts "Copying elm loader to #{Webpacker::Configuration.config_path}/loaders" +puts "Copying elm loader to config/webpack/loaders" copy_file "#{__dir__}/config/loaders/installers/elm.js", - "#{Webpacker::Configuration.config_path}/loaders/elm.js" + "config/webpack/loaders/elm.js" puts "Copying elm example entry file to #{Webpacker::Configuration.entry_path}" copy_file "#{__dir__}/examples/elm/Main.elm", "#{Webpacker::Configuration.entry_path}/Main.elm" @@ -12,15 +12,15 @@ "#{Webpacker::Configuration.entry_path}/hello_elm.js" puts "Installing all elm dependencies" -run "yarn add elm" -run "yarn add --dev elm-hot-loader elm-webpack-loader" +run "yarn add elm elm-webpack-loader" +run "yarn add --dev elm-hot-loader" run "yarn run elm package install -- --yes" puts "Updating Webpack paths to include Elm file extension" insert_into_file Webpacker::Configuration.file_path, " - .elm\n", after: /extensions:\n/ puts "Updating elm source location" -source_path = File.join Webpacker::Configuration.source, Webpacker::Configuration.paths.fetch(:entry, "packs") +source_path = File.join(Webpacker::Configuration.source, Webpacker::Configuration.fetch(:source_entry_path)) gsub_file "elm-package.json", /\"\.\"\n/, %("#{source_path}"\n) puts "Updating .gitignore to include elm-stuff folder" diff --git a/lib/install/react.rb b/lib/install/react.rb index c6e2dc374..9e16414de 100644 --- a/lib/install/react.rb +++ b/lib/install/react.rb @@ -19,7 +19,7 @@ copy_file "#{__dir__}/examples/react/.babelrc", ".babelrc" end -puts "Copying react loader to #{Webpacker::Configuration.config_path}/loaders" +puts "Copying react loader to config/webpack/loaders" copy_file "#{__dir__}/config/loaders/installers/react.js", "config/webpack/loaders/react.js" puts "Copying react example entry file to #{Webpacker::Configuration.entry_path}" diff --git a/lib/install/template.rb b/lib/install/template.rb index 2d65b3e4c..73fd6487b 100644 --- a/lib/install/template.rb +++ b/lib/install/template.rb @@ -1,11 +1,19 @@ # Install webpacker +copy_file "#{__dir__}/config/webpacker.yml", "config/webpacker.yml" + puts "Copying webpack core config and loaders" directory "#{__dir__}/config/webpack", "config/webpack" directory "#{__dir__}/config/loaders/core", "config/webpack/loaders" -copy_file "#{__dir__}/config/.postcssrc.yml", ".postcssrc.yml" -puts "Copying .babelrc to app root directory" -copy_file "#{__dir__}/config/.babelrc", ".babelrc" +unless File.exist?(".postcssrc.yml") + puts "Copying .postcssrc.yml to app root directory" + copy_file "#{__dir__}/config/.postcssrc.yml", ".postcssrc.yml" +end + +unless File.exist?(".babelrc") + puts "Copying .babelrc to app root directory" + copy_file "#{__dir__}/config/.babelrc", ".babelrc" +end puts "Creating javascript app source directory" directory "#{__dir__}/javascript", "#{Webpacker::Configuration.source}" diff --git a/lib/install/vue.rb b/lib/install/vue.rb index 71adb8c41..24f3c7ef7 100644 --- a/lib/install/vue.rb +++ b/lib/install/vue.rb @@ -1,6 +1,6 @@ require "webpacker/configuration" -puts "Copying vue loader to #{Webpacker::Configuration.config_path}/loaders" +puts "Copying vue loader to config/webpack/loaders" copy_file "#{__dir__}/config/loaders/installers/vue.js", "config/webpack/loaders/vue.js" puts "Copying the example entry file to #{Webpacker::Configuration.entry_path}" diff --git a/lib/tasks/webpacker/compile.rake b/lib/tasks/webpacker/compile.rake index 1ed5632d6..0e04d51b3 100644 --- a/lib/tasks/webpacker/compile.rake +++ b/lib/tasks/webpacker/compile.rake @@ -1,22 +1,26 @@ +require "open3" require "webpacker/env" require "webpacker/configuration" -REGEX_MAP = /\A.*\.map\z/ namespace :webpacker do desc "Compile javascript packs using webpack for production with digests" task compile: ["webpacker:verify_install", :environment] do - puts "Compiling webpacker assets 🎉" + puts "[Webpacker] Compiling assets 🎉" + asset_host = ActionController::Base.helpers.compute_asset_host - asset_env = asset_host ? "ASSET_HOST=#{asset_host}" : "" - result = `#{asset_env} NODE_ENV=#{Webpacker.env} ./bin/webpack --json` + env = { "NODE_ENV" => Webpacker.env, "ASSET_HOST" => asset_host }.freeze - unless $?.success? - puts JSON.parse(result)["errors"] - exit! $?.exitstatus - end + stdout_str, stderr_str, status = Open3.capture3(env, "./bin/webpack") - puts "Compiled digests for all packs in #{Webpacker::Configuration.entry_path}: " - puts JSON.parse(File.read(Webpacker::Configuration.manifest_path)) + if status.success? + $stdout.puts "\e[32m[Webpacker] Compiled digests for all packs in #{Webpacker::Configuration.entry_path}:\e[0m" + $stdout.puts "\e[32m#{JSON.parse(File.read(Webpacker::Configuration.manifest_path))}\e[0m" + else + $stdout.puts "[Webpacker] Compilation Failed" + $stdout.puts "\e[31m#{stdout_str}\e[0m" + $stderr.puts "\e[31m#{stderr_str}\e[0m" + exit! + end end end diff --git a/lib/tasks/webpacker/verify_install.rake b/lib/tasks/webpacker/verify_install.rake index ff7896254..b6d80b446 100644 --- a/lib/tasks/webpacker/verify_install.rake +++ b/lib/tasks/webpacker/verify_install.rake @@ -7,7 +7,7 @@ namespace :webpacker do puts "Webpacker is installed 🎉 🍰" puts "Using #{Webpacker::Configuration.file_path} file for setting up webpack paths" else - puts "Configuration config/webpack/paths.yml file not found. \n"\ + puts "Configuration config/webpacker.yml file not found. \n"\ "Make sure webpacker:install is run successfully before " \ "running dependent tasks" exit! diff --git a/lib/webpacker/configuration.rb b/lib/webpacker/configuration.rb index ad330ef2f..70c5c4be9 100644 --- a/lib/webpacker/configuration.rb +++ b/lib/webpacker/configuration.rb @@ -1,19 +1,19 @@ -# Loads webpacker configuration from config/webpack/paths.yml +# Loads webpacker configuration from config/webpacker.yml require "webpacker/file_loader" class Webpacker::Configuration < Webpacker::FileLoader class << self def entry_path - source_path.join(fetch(:entry)) + source_path.join(fetch(:source_entry_path)) end def output_path - public_path.join(fetch(:output)) + public_path.join(fetch(:public_output_path)) end def manifest_path - output_path.join(fetch(:manifest)) + output_path.join("manifest.json") end def source_path @@ -24,12 +24,8 @@ def public_path Rails.root.join("public") end - def config_path - Rails.root.join(fetch(:config)) - end - def file_path(root: Rails.root) - root.join("config/webpack/paths.yml") + root.join("config/webpacker.yml") end def default_file_path @@ -37,21 +33,21 @@ def default_file_path end def source - fetch(:source) + fetch(:source_path) end def fetch(key) - paths.fetch(key, default_paths[key]) + data.fetch(key, defaults[key]) end - def paths + def data load if Webpacker.env.development? raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Configuration.load must be called first") unless instance instance.data end - def default_paths - @default_paths ||= HashWithIndifferentAccess.new(YAML.load(default_file_path.read)["default"]) + def defaults + @defaults ||= HashWithIndifferentAccess.new(YAML.load(default_file_path.read)["default"]) end end diff --git a/lib/webpacker/env.rb b/lib/webpacker/env.rb index 147def958..f7a385e37 100644 --- a/lib/webpacker/env.rb +++ b/lib/webpacker/env.rb @@ -1,4 +1,4 @@ -# Singleton registry for determining NODE_ENV from config/webpack/paths.yml +# Singleton registry for determining NODE_ENV from config/webpacker.yml require "webpacker/file_loader" class Webpacker::Env < Webpacker::FileLoader @@ -9,7 +9,7 @@ def current end def file_path - Rails.root.join("config", "webpack", "paths.yml") + Rails.root.join("config", "webpacker.yml") end end diff --git a/test/configuration_test.rb b/test/configuration_test.rb index f3bc51356..133353076 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -1,18 +1,13 @@ require "webpacker_test" class ConfigurationTest < Minitest::Test - def test_config_path - config_path = File.join(File.dirname(__FILE__), "test_app/config", "webpack").to_s - assert_equal Webpacker::Configuration.config_path.to_s, config_path - end - def test_entry_path entry_path = File.join(File.dirname(__FILE__), "test_app/app/javascript", "packs").to_s assert_equal Webpacker::Configuration.entry_path.to_s, entry_path end def test_file_path - file_path = File.join(File.dirname(__FILE__), "test_app/config", "webpack", "paths.yml").to_s + file_path = File.join(File.dirname(__FILE__), "test_app/config", "webpacker.yml").to_s assert_equal Webpacker::Configuration.file_path.to_s, file_path end diff --git a/test/env_test.rb b/test/env_test.rb index abc102f44..ae058ddf5 100644 --- a/test/env_test.rb +++ b/test/env_test.rb @@ -8,7 +8,7 @@ def test_current_env end def test_file_path - correct_path = File.join(File.dirname(__FILE__), "test_app/config", "webpack", "paths.yml").to_s + correct_path = File.join(File.dirname(__FILE__), "test_app/config", "webpacker.yml").to_s assert_equal Webpacker::Env.file_path.to_s, correct_path end end diff --git a/test/webpacker_test.rb b/test/webpacker_test.rb index 18e6cee58..f05871147 100644 --- a/test/webpacker_test.rb +++ b/test/webpacker_test.rb @@ -8,6 +8,7 @@ module TestApp class Application < ::Rails::Application config.root = File.join(File.dirname(__FILE__), "test_app") + config.eager_load = true end end