Skip to content

Commit

Permalink
webpack-1.2 - update all files to webpack 1.2 defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
himdel committed Aug 3, 2017
1 parent f5f3ce2 commit c61195b
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 76 deletions.
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
["env", { "modules": false } ]
]
}
11 changes: 2 additions & 9 deletions bin/webpack
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@ NODE_ENV = ENV["NODE_ENV"]

APP_PATH = File.expand_path("../", __dir__)
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]

NODE_MODULES_PATH = File.join(APP_PATH.shellescape, paths["node_modules"])
WEBPACK_CONFIG_PATH = File.join(APP_PATH.shellescape, paths["config"])

if NODE_ENV == "development" && dev_server["enabled"]
puts "Warning: webpack-dev-server is currently enabled in #{DEV_SERVER_CONFIG_PATH}. " \
"Disable to serve assets directly from public/packs directory"
end
rescue Errno::ENOENT, NoMethodError
puts "Configuration not found in config/webpack/paths.yml or config/webpack/development.server.yml."
puts "Configuration not found in config/webpack/paths.yml"
puts "Please run bundle exec rails webpacker:install to install webpacker"
exit!
end
Expand Down
4 changes: 2 additions & 2 deletions bin/webpack-dev-server
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ENV["NODE_ENV"] ||= RAILS_ENV
NODE_ENV = ENV["NODE_ENV"]

APP_PATH = File.expand_path("../", __dir__)
CONFIG_PATH = File.join(APP_PATH, "config/webpack/paths.yml")
CONFIG_PATH = File.join(APP_PATH, "config/webpack/paths.yml")[NODE_ENV]

begin
paths = YAML.load(File.read(CONFIG_PATH))
Expand All @@ -29,5 +29,5 @@ end

Dir.chdir(APP_PATH) do
exec "NODE_PATH=#{NODE_MODULES_PATH} #{WEBPACK_BIN} --progress --color " \
"--config #{DEV_SERVER_CONFIG}"
"--config #{DEV_SERVER_CONFIG} #{ARGV.join(" ")}"
end
10 changes: 0 additions & 10 deletions bin/webpack-watcher

This file was deleted.

13 changes: 9 additions & 4 deletions config/webpack/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ const { execSync } = require('child_process')

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 publicPath = env.NODE_ENV !== 'production' && devServer.enabled ?
`http://${devServer.host}:${devServer.port}/` : `/${paths.entry}/`
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' ? devServerUrl : publicUrl

// override paths.output to use Rails.root
const outputPrefix = execSync('rake webpacker:output', { encoding: 'utf8' }).trim();
Expand All @@ -22,5 +26,6 @@ module.exports = {
env,
paths,
loadersDir,
publicUrl,
publicPath
}
2 changes: 1 addition & 1 deletion config/webpack/development.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Note: You must restart bin/webpack-watcher for changes to take effect
// Note: You must restart bin/webpack-dev-server for changes to take effect

const merge = require('webpack-merge')
const sharedConfig = require('./shared.js')
Expand Down
21 changes: 17 additions & 4 deletions config/webpack/development.server.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# Restart webpack-dev-server if you make changes here
enabled: true
host: localhost
port: 8080
# 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
2 changes: 1 addition & 1 deletion config/webpack/loaders/assets.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { env, publicPath } = require('../configuration.js')

module.exports = {
test: /\.(jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,
test: /\.(jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,
use: [{
loader: 'file-loader',
options: {
Expand Down
7 changes: 1 addition & 6 deletions config/webpack/loaders/babel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
module.exports = {
test: /\.js(\.erb)?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [
['env', { modules: false }]
]
}
loader: 'babel-loader'
}
2 changes: 1 addition & 1 deletion config/webpack/loaders/erb.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module.exports = {
exclude: /node_modules/,
loader: 'rails-erb-loader',
options: {
runner: 'DISABLE_SPRING=1 bin/rails runner'
runner: 'bin/rails runner'
}
}
7 changes: 6 additions & 1 deletion config/webpack/loaders/sass.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const { env } = require('../configuration.js')

module.exports = {
test: /\.(scss|sass|css)$/i,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader', 'postcss-loader', 'sass-loader']
use: [
{ loader: 'css-loader', options: { minimize: env.NODE_ENV === 'production' } },
'postcss-loader',
'sass-loader'
]
})
}
52 changes: 33 additions & 19 deletions config/webpack/paths.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
# 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
# Note: You must restart bin/webpack-dev-server for changes to take effect

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
- .jpg

development:
<<: *default

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

production:
<<: *default
3 changes: 2 additions & 1 deletion config/webpack/production.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Note: You must restart bin/webpack-dev-server for changes to take effect

/* eslint global-require: 0 */
// Note: You must run bin/webpack for changes to take effect

const webpack = require('webpack')
const merge = require('webpack-merge')
Expand Down
23 changes: 13 additions & 10 deletions config/webpack/shared.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
// Note: You must restart bin/webpack-watcher for changes to take effect
// Note: You must restart bin/webpack-dev-server for changes to take effect

/* eslint global-require: 0 */
/* eslint import/no-dynamic-require: 0 */

const webpack = require('webpack')
const { basename, join, resolve } = require('path')
const { basename, dirname, join, relative, resolve } = require('path')
const { sync } = require('glob')
const { readdirSync } = require('fs')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const ManifestPlugin = require('webpack-manifest-plugin')
const extname = require('path-complete-extname')
const { env, paths, publicPath, loadersDir } = require('./configuration.js')

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

module.exports = {
entry: packPaths.reduce(
(map, entry) => {
const localMap = map
localMap[basename(entry, extname(entry))] = resolve(entry)
const namespace = relative(join(paths.source, paths.entry), dirname(entry))
localMap[join(namespace, basename(entry, extname(entry)))] = resolve(entry)
return localMap
}, {}
),

output: { filename: '[name].js', path: resolve(paths.output, paths.entry) },
output: {
filename: '[name].js',
path: resolve(paths.output, paths.entry),
publicPath
},

module: {
rules: readdirSync(loadersDir).map(file => (
require(join(loadersDir, file))
))
rules: sync(join(loadersDir, '*.js')).map(loader => require(loader))
},

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 config/webpack/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Note: You must restart bin/webpack-dev-server for changes to take effect

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

module.exports = merge(sharedConfig, {})
6 changes: 0 additions & 6 deletions lib/tasks/manageiq/ui_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ namespace :webpack do
system("bin/webpack-dev-server") || abort("\n== webpack-dev-server failed ==")
end
end

task :watcher do
Dir.chdir ManageIQ::UI::Classic::Engine.root do
system("bin/webpack-watcher") || abort("\n== webpack-watcher failed ==")
end
end
end

# needed by config/webpack/configuration.js
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"autoprefixer": "~6.7.7",
"babel-core": "~6.24.1",
"babel-eslint": "~6.0.4",
"babel-loader": "~6.4.1",
"babel-loader": "~7.0",
"babel-preset-env": "~1.4.0",
"coffee-loader": "~0.7.3",
"coffee-script": "~1.12.5",
Expand Down

0 comments on commit c61195b

Please sign in to comment.