webpacker 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +6 -8
  3. data/CHANGELOG.md +64 -0
  4. data/Gemfile +8 -0
  5. data/Gemfile.lock +68 -1
  6. data/README.md +42 -25
  7. data/Rakefile +11 -0
  8. data/lib/install/angular.rb +1 -1
  9. data/lib/install/bin/webpack-dev-server.tt +2 -2
  10. data/lib/install/bin/webpack.tt +2 -9
  11. data/lib/install/bin/yarn.tt +3 -2
  12. data/lib/install/config/.babelrc +5 -0
  13. data/lib/install/config/loaders/core/assets.js +1 -1
  14. data/lib/install/config/loaders/core/babel.js +1 -6
  15. data/lib/install/config/loaders/core/erb.js +1 -1
  16. data/lib/install/config/loaders/core/sass.js +6 -1
  17. data/lib/install/config/loaders/installers/react.js +1 -7
  18. data/lib/install/config/loaders/installers/vue.js +4 -2
  19. data/lib/install/config/webpack/configuration.js +9 -4
  20. data/lib/install/config/webpack/development.js +1 -1
  21. data/lib/install/config/webpack/development.server.yml +17 -4
  22. data/lib/install/config/webpack/paths.yml +33 -19
  23. data/lib/install/config/webpack/production.js +2 -1
  24. data/lib/install/config/webpack/shared.js +13 -10
  25. data/lib/install/config/webpack/test.js +6 -0
  26. data/lib/install/examples/react/.babelrc +4 -1
  27. data/lib/install/examples/react/hello_react.jsx +2 -1
  28. data/lib/install/examples/vue/app.vue +1 -1
  29. data/lib/install/react.rb +20 -4
  30. data/lib/install/template.rb +21 -9
  31. data/lib/install/vue.rb +1 -1
  32. data/lib/tasks/installers.rake +2 -2
  33. data/lib/tasks/webpacker.rake +2 -0
  34. data/lib/tasks/webpacker/check_node.rake +19 -0
  35. data/lib/tasks/webpacker/check_yarn.rake +12 -0
  36. data/lib/tasks/webpacker/clobber.rake +17 -0
  37. data/lib/tasks/webpacker/compile.rake +15 -2
  38. data/lib/tasks/webpacker/install.rake +3 -3
  39. data/lib/tasks/webpacker/verify_install.rake +1 -1
  40. data/lib/webpacker.rb +5 -0
  41. data/lib/webpacker/configuration.rb +15 -6
  42. data/lib/webpacker/env.rb +27 -0
  43. data/lib/webpacker/manifest.rb +6 -1
  44. data/lib/webpacker/railtie.rb +6 -4
  45. data/lib/webpacker/version.rb +1 -1
  46. data/package.json +4 -1
  47. data/test/env_test.rb +16 -0
  48. data/test/webpacker_test.rb +14 -0
  49. data/webpacker.gemspec +2 -2
  50. metadata +18 -5
  51. data/lib/install/bin/webpack-watcher.tt +0 -10
@@ -1,7 +1,7 @@
1
1
  const { env, publicPath } = require('../configuration.js')
2
2
 
3
3
  module.exports = {
4
- test: /\.(jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,
4
+ test: /\.(jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,
5
5
  use: [{
6
6
  loader: 'file-loader',
7
7
  options: {
@@ -1,10 +1,5 @@
1
1
  module.exports = {
2
2
  test: /\.js(\.erb)?$/,
3
3
  exclude: /node_modules/,
4
- loader: 'babel-loader',
5
- options: {
6
- presets: [
7
- ['env', { modules: false }]
8
- ]
9
- }
4
+ loader: 'babel-loader'
10
5
  }
@@ -4,6 +4,6 @@ module.exports = {
4
4
  exclude: /node_modules/,
5
5
  loader: 'rails-erb-loader',
6
6
  options: {
7
- runner: 'DISABLE_SPRING=1 bin/rails runner'
7
+ runner: 'bin/rails runner'
8
8
  }
9
9
  }
@@ -1,9 +1,14 @@
1
1
  const ExtractTextPlugin = require('extract-text-webpack-plugin')
2
+ const { env } = require('../configuration.js')
2
3
 
3
4
  module.exports = {
4
5
  test: /\.(scss|sass|css)$/i,
5
6
  use: ExtractTextPlugin.extract({
6
7
  fallback: 'style-loader',
7
- use: ['css-loader', 'postcss-loader', 'sass-loader']
8
+ use: [
9
+ { loader: 'css-loader', options: { minimize: env.NODE_ENV === 'production' } },
10
+ 'postcss-loader',
11
+ 'sass-loader'
12
+ ]
8
13
  })
9
14
  }
@@ -1,11 +1,5 @@
1
1
  module.exports = {
2
2
  test: /\.(js|jsx)?(\.erb)?$/,
3
3
  exclude: /node_modules/,
4
- loader: 'babel-loader',
5
- options: {
6
- presets: [
7
- 'react',
8
- ['env', { modules: false }]
9
- ]
10
- }
4
+ loader: 'babel-loader'
11
5
  }
@@ -3,8 +3,10 @@ module.exports = {
3
3
  loader: 'vue-loader',
4
4
  options: {
5
5
  loaders: {
6
- scss: 'vue-style-loader!css-loader!sass-loader',
7
- sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
6
+ js: 'babel-loader',
7
+ file: 'file-loader',
8
+ scss: 'vue-style-loader!css-loader!postcss-loader!sass-loader',
9
+ sass: 'vue-style-loader!css-loader!postcss-loader!sass-loader?indentedSyntax'
8
10
  }
9
11
  }
10
12
  }
@@ -7,15 +7,20 @@ const { readFileSync } = require('fs')
7
7
 
8
8
  const configPath = resolve('config', 'webpack')
9
9
  const loadersDir = join(__dirname, 'loaders')
10
- const paths = safeLoad(readFileSync(join(configPath, 'paths.yml'), 'utf8'))
11
- const devServer = safeLoad(readFileSync(join(configPath, 'development.server.yml'), 'utf8'))
12
- const publicPath = env.NODE_ENV !== 'production' && devServer.enabled ?
13
- `http://${devServer.host}:${devServer.port}/` : `/${paths.entry}/`
10
+ const paths = safeLoad(readFileSync(join(configPath, 'paths.yml'), 'utf8'))[env.NODE_ENV]
11
+ const devServer = safeLoad(readFileSync(join(configPath, 'development.server.yml'), 'utf8'))[env.NODE_ENV]
12
+
13
+ // Compute public path based on environment and ASSET_HOST in production
14
+ const ifHasCDN = env.ASSET_HOST !== undefined && env.NODE_ENV === 'production'
15
+ const devServerUrl = `http://${devServer.host}:${devServer.port}/${paths.entry}/`
16
+ const publicUrl = ifHasCDN ? `${env.ASSET_HOST}/${paths.entry}/` : `/${paths.entry}/`
17
+ const publicPath = env.NODE_ENV !== 'production' ? devServerUrl : publicUrl
14
18
 
15
19
  module.exports = {
16
20
  devServer,
17
21
  env,
18
22
  paths,
19
23
  loadersDir,
24
+ publicUrl,
20
25
  publicPath
21
26
  }
@@ -1,4 +1,4 @@
1
- // Note: You must restart bin/webpack-watcher for changes to take effect
1
+ // Note: You must restart bin/webpack-dev-server for changes to take effect
2
2
 
3
3
  const merge = require('webpack-merge')
4
4
  const sharedConfig = require('./shared.js')
@@ -1,4 +1,17 @@
1
- # Restart webpack-dev-server if you make changes here
2
- enabled: true
3
- host: localhost
4
- port: 8080
1
+ # Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ default: &default
4
+ enabled: true
5
+ host: localhost
6
+ port: 8080
7
+
8
+ development:
9
+ <<: *default
10
+
11
+ test:
12
+ <<: *default
13
+ enabled: false
14
+
15
+ production:
16
+ <<: *default
17
+ enabled: false
@@ -1,19 +1,33 @@
1
- # Restart webpack-watcher or webpack-dev-server if you make changes here
2
- config: config/webpack
3
- entry: packs
4
- output: public
5
- node_modules: node_modules
6
- source: app/javascript
7
- extensions:
8
- - .coffee
9
- - .js
10
- - .jsx
11
- - .ts
12
- - .vue
13
- - .sass
14
- - .scss
15
- - .css
16
- - .png
17
- - .svg
18
- - .gif
19
- - .jpeg
1
+ # Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ default: &default
4
+ config: config/webpack
5
+ entry: packs
6
+ output: public
7
+ manifest: manifest.json
8
+ node_modules: node_modules
9
+ source: app/javascript
10
+ extensions:
11
+ - .coffee
12
+ - .js
13
+ - .jsx
14
+ - .ts
15
+ - .vue
16
+ - .sass
17
+ - .scss
18
+ - .css
19
+ - .png
20
+ - .svg
21
+ - .gif
22
+ - .jpeg
23
+ - .jpg
24
+
25
+ development:
26
+ <<: *default
27
+
28
+ test:
29
+ <<: *default
30
+ manifest: manifest-test.json
31
+
32
+ production:
33
+ <<: *default
@@ -1,5 +1,6 @@
1
+ // Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
1
3
  /* eslint global-require: 0 */
2
- // Note: You must run bin/webpack for changes to take effect
3
4
 
4
5
  const webpack = require('webpack')
5
6
  const merge = require('webpack-merge')
@@ -1,40 +1,43 @@
1
- // Note: You must restart bin/webpack-watcher for changes to take effect
1
+ // Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
2
3
  /* eslint global-require: 0 */
3
4
  /* eslint import/no-dynamic-require: 0 */
4
5
 
5
6
  const webpack = require('webpack')
6
- const { basename, join, resolve } = require('path')
7
+ const { basename, dirname, join, relative, resolve } = require('path')
7
8
  const { sync } = require('glob')
8
- const { readdirSync } = require('fs')
9
9
  const ExtractTextPlugin = require('extract-text-webpack-plugin')
10
10
  const ManifestPlugin = require('webpack-manifest-plugin')
11
11
  const extname = require('path-complete-extname')
12
12
  const { env, paths, publicPath, loadersDir } = require('./configuration.js')
13
13
 
14
- const extensionGlob = `*{${paths.extensions.join(',')}}*`
14
+ const extensionGlob = `**/*{${paths.extensions.join(',')}}*`
15
15
  const packPaths = sync(join(paths.source, paths.entry, extensionGlob))
16
16
 
17
17
  module.exports = {
18
18
  entry: packPaths.reduce(
19
19
  (map, entry) => {
20
20
  const localMap = map
21
- localMap[basename(entry, extname(entry))] = resolve(entry)
21
+ const namespace = relative(join(paths.source, paths.entry), dirname(entry))
22
+ localMap[join(namespace, basename(entry, extname(entry)))] = resolve(entry)
22
23
  return localMap
23
24
  }, {}
24
25
  ),
25
26
 
26
- output: { filename: '[name].js', path: resolve(paths.output, paths.entry) },
27
+ output: {
28
+ filename: '[name].js',
29
+ path: resolve(paths.output, paths.entry),
30
+ publicPath
31
+ },
27
32
 
28
33
  module: {
29
- rules: readdirSync(loadersDir).map(file => (
30
- require(join(loadersDir, file))
31
- ))
34
+ rules: sync(join(loadersDir, '*.js')).map(loader => require(loader))
32
35
  },
33
36
 
34
37
  plugins: [
35
38
  new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(env))),
36
39
  new ExtractTextPlugin(env.NODE_ENV === 'production' ? '[name]-[hash].css' : '[name].css'),
37
- new ManifestPlugin({ fileName: 'manifest.json', publicPath, writeToFileEmit: true })
40
+ new ManifestPlugin({ fileName: paths.manifest, publicPath, writeToFileEmit: true })
38
41
  ],
39
42
 
40
43
  resolve: {
@@ -0,0 +1,6 @@
1
+ // Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ const merge = require('webpack-merge')
4
+ const sharedConfig = require('./shared.js')
5
+
6
+ module.exports = merge(sharedConfig, {})
@@ -1,3 +1,6 @@
1
1
  {
2
- "presets": ["env", "react"]
2
+ "presets": [
3
+ ["env", { "modules": false } ],
4
+ "react"
5
+ ]
3
6
  }
@@ -4,6 +4,7 @@
4
4
 
5
5
  import React from 'react'
6
6
  import ReactDOM from 'react-dom'
7
+ import PropTypes from 'prop-types'
7
8
 
8
9
  const Hello = props => (
9
10
  <div>Hello {props.name}!</div>
@@ -14,7 +15,7 @@ Hello.defaultProps = {
14
15
  }
15
16
 
16
17
  Hello.propTypes = {
17
- name: React.PropTypes.string
18
+ name: PropTypes.string
18
19
  }
19
20
 
20
21
  document.addEventListener('DOMContentLoaded', () => {
@@ -5,7 +5,7 @@
5
5
  </template>
6
6
 
7
7
  <script>
8
- module.exports = {
8
+ export default {
9
9
  data: function () {
10
10
  return {
11
11
  message: "Hello Vue!"
@@ -1,15 +1,31 @@
1
1
  require "webpacker/configuration"
2
2
 
3
+ babelrc = Rails.root.join(".babelrc")
4
+
5
+ if File.exist?(babelrc)
6
+ react_babelrc = JSON.parse(File.read(babelrc))
7
+ react_babelrc["presets"] ||= []
8
+
9
+ unless react_babelrc["presets"].include?("react")
10
+ react_babelrc["presets"].push("react")
11
+ puts "Copying react preset to your .babelrc file"
12
+
13
+ File.open(babelrc, "w") do |f|
14
+ f.puts JSON.pretty_generate(react_babelrc)
15
+ end
16
+ end
17
+ else
18
+ puts "Copying .babelrc to app root directory"
19
+ copy_file "#{__dir__}/examples/react/.babelrc", ".babelrc"
20
+ end
21
+
3
22
  puts "Copying react loader to #{Webpacker::Configuration.config_path}/loaders"
4
23
  copy_file "#{__dir__}/config/loaders/installers/react.js", "config/webpack/loaders/react.js"
5
24
 
6
- puts "Copying .babelrc to app root directory"
7
- copy_file "#{__dir__}/examples/react/.babelrc", ".babelrc"
8
-
9
25
  puts "Copying react example entry file to #{Webpacker::Configuration.entry_path}"
10
26
  copy_file "#{__dir__}/examples/react/hello_react.jsx", "#{Webpacker::Configuration.entry_path}/hello_react.jsx"
11
27
 
12
28
  puts "Installing all react dependencies"
13
- run "./bin/yarn add react react-dom babel-preset-react"
29
+ run "./bin/yarn add react react-dom babel-preset-react prop-types"
14
30
 
15
31
  puts "Webpacker now supports react.js 🎉"
@@ -1,24 +1,36 @@
1
1
  # Install webpacker
2
- puts "Creating javascript app source directory"
3
- directory "#{__dir__}/javascript", "app/javascript"
4
-
5
- puts "Copying binstubs"
6
- directory "#{__dir__}/bin", "bin"
7
- chmod "bin", 0755 & ~File.umask, verbose: false
8
-
9
2
  puts "Copying webpack core config and loaders"
10
3
  directory "#{__dir__}/config/webpack", "config/webpack"
11
4
  directory "#{__dir__}/config/loaders/core", "config/webpack/loaders"
12
5
  copy_file "#{__dir__}/config/.postcssrc.yml", ".postcssrc.yml"
13
6
 
14
- append_to_file ".gitignore", <<-EOS
7
+ puts "Copying .babelrc to app root directory"
8
+ copy_file "#{__dir__}/config/.babelrc", ".babelrc"
9
+
10
+ puts "Creating javascript app source directory"
11
+ directory "#{__dir__}/javascript", "#{Webpacker::Configuration.source}"
12
+
13
+ puts "Copying binstubs"
14
+ template "#{__dir__}/bin/webpack-dev-server", "bin/webpack-dev-server"
15
+ template "#{__dir__}/bin/webpack", "bin/webpack"
16
+
17
+ if !File.exist?("bin/yarn")
18
+ puts "Copying yarn"
19
+ template "#{__dir__}/bin/yarn", "bin/yarn"
20
+ end
21
+
22
+ chmod "bin", 0755 & ~File.umask, verbose: false
23
+
24
+ if File.exists?(".gitignore")
25
+ append_to_file ".gitignore", <<-EOS
15
26
  /public/packs
16
27
  /node_modules
17
28
  EOS
29
+ end
18
30
 
19
31
  puts "Installing all JavaScript dependencies"
20
32
  run "./bin/yarn add webpack webpack-merge js-yaml path-complete-extname " \
21
- "webpack-manifest-plugin babel-loader coffee-loader coffee-script " \
33
+ "webpack-manifest-plugin babel-loader@7.x coffee-loader coffee-script " \
22
34
  "babel-core babel-preset-env compression-webpack-plugin rails-erb-loader glob " \
23
35
  "extract-text-webpack-plugin node-sass file-loader sass-loader css-loader style-loader " \
24
36
  "postcss-loader autoprefixer postcss-smart-import precss"
@@ -10,6 +10,6 @@ puts "Copying vue app file to #{Webpacker::Configuration.entry_path}"
10
10
  copy_file "#{__dir__}/examples/vue/app.vue", "#{Webpacker::Configuration.entry_path}/app.vue"
11
11
 
12
12
  puts "Installing all vue dependencies"
13
- run "./bin/yarn add vue vue-loader vue-template-compiler sass-loader node-sass css-loader"
13
+ run "./bin/yarn add vue vue-loader vue-template-compiler"
14
14
 
15
15
  puts "Webpacker now supports vue.js 🎉"
@@ -11,9 +11,9 @@ namespace :webpacker do
11
11
  task task_name => ["webpacker:verify_install"] do
12
12
  template = File.expand_path("../install/#{task_name}.rb", __dir__)
13
13
  if Rails::VERSION::MAJOR >= 5
14
- exec "./bin/rails app:template LOCATION=#{template}"
14
+ exec "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{template}"
15
15
  else
16
- exec "./bin/rake rails:template LOCATION=#{template}"
16
+ exec "#{RbConfig.ruby} ./bin/rake rails:template LOCATION=#{template}"
17
17
  end
18
18
  end
19
19
  end
@@ -1,6 +1,8 @@
1
1
  tasks = {
2
2
  "webpacker:install" => "Installs and setup webpack with yarn",
3
3
  "webpacker:compile" => "Compiles webpack bundles based on environment",
4
+ "webpacker:check_node" => "Verifies if Node.js is installed",
5
+ "webpacker:check_yarn" => "Verifies if yarn is installed",
4
6
  "webpacker:verify_install" => "Verifies if webpacker is installed",
5
7
  "webpacker:yarn_install" => "Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn",
6
8
  "webpacker:install:react" => "Installs and setup example react component",
@@ -0,0 +1,19 @@
1
+ namespace :webpacker do
2
+ desc "Verifies if Node.js is installed"
3
+ task :check_node do
4
+ begin
5
+ node_version = `node -v`
6
+ required_node_version = "6.4"
7
+
8
+ raise Errno::ENOENT if node_version.blank?
9
+ if Gem::Version.new(node_version.strip.tr("v", "")) < Gem::Version.new(required_node_version)
10
+ puts "Webpacker requires Node.js >= v#{required_node_version} and you are using #{node_version}"
11
+ puts "Please upgrade Node.js https://nodejs.org/en/download/"
12
+ puts "Exiting!" && exit!
13
+ end
14
+ rescue Errno::ENOENT
15
+ puts "Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/"
16
+ puts "Exiting!" && exit!
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ namespace :webpacker do
2
+ desc "Verifies if yarn is installed"
3
+ task :check_yarn do
4
+ begin
5
+ version = `yarn --version`
6
+ raise Errno::ENOENT if version.blank?
7
+ rescue Errno::ENOENT
8
+ puts "Webpacker requires yarn. Please download and install Yarn https://yarnpkg.com/lang/en/docs/install/"
9
+ puts "Exiting!" && exit!
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require "webpacker/configuration"
2
+
3
+ namespace :webpacker do
4
+ desc "Remove the webpack compiled output directory"
5
+ task clobber: ["webpacker:verify_install", :environment] do
6
+ packs_path = Webpacker::Configuration.packs_path
7
+ FileUtils.rm_r(packs_path) if File.exist?(packs_path)
8
+ puts "Removed webpack output path directory #{packs_path}"
9
+ end
10
+ end
11
+
12
+ # Run clobber if the assets:clobber is run
13
+ if Rake::Task.task_defined?("assets:clobber")
14
+ Rake::Task["assets:clobber"].enhance do
15
+ Rake::Task["webpacker:clobber"].invoke
16
+ end
17
+ end