webpacker 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +14 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +124 -0
  5. data/.travis.yml +24 -0
  6. data/Gemfile +1 -1
  7. data/Gemfile.lock +6 -6
  8. data/README.md +126 -19
  9. data/lib/install/angular.rb +18 -0
  10. data/lib/install/bin/webpack-dev-server.tt +23 -12
  11. data/lib/install/bin/webpack-watcher.tt +3 -0
  12. data/lib/install/bin/webpack.tt +33 -7
  13. data/lib/install/config/.postcssrc.yml +4 -0
  14. data/lib/install/config/loaders/core/assets.js +12 -0
  15. data/lib/install/config/loaders/core/babel.js +10 -0
  16. data/lib/install/config/loaders/core/coffee.js +4 -0
  17. data/lib/install/config/loaders/core/erb.js +9 -0
  18. data/lib/install/config/loaders/core/sass.js +9 -0
  19. data/lib/install/config/loaders/installers/angular.js +4 -0
  20. data/lib/install/config/loaders/installers/react.js +11 -0
  21. data/lib/install/config/loaders/installers/vue.js +10 -0
  22. data/lib/install/config/webpack/configuration.js +21 -0
  23. data/lib/install/config/webpack/development.js +16 -0
  24. data/lib/install/config/webpack/development.server.js +17 -0
  25. data/lib/install/config/webpack/development.server.yml +4 -0
  26. data/lib/install/config/webpack/paths.yml +19 -0
  27. data/lib/install/config/webpack/production.js +20 -0
  28. data/lib/install/config/webpack/shared.js +51 -0
  29. data/lib/install/{angular → examples/angular}/hello_angular.js +1 -1
  30. data/lib/install/{angular → examples/angular}/hello_angular/app/app.component.ts +0 -0
  31. data/lib/install/{angular → examples/angular}/hello_angular/app/app.module.ts +0 -0
  32. data/lib/install/{angular → examples/angular}/hello_angular/index.ts +0 -0
  33. data/lib/install/{angular → examples/angular}/hello_angular/polyfills.ts +0 -0
  34. data/lib/install/{angular → examples/angular}/tsconfig.json +0 -0
  35. data/lib/install/examples/react/.babelrc +3 -0
  36. data/lib/install/examples/react/hello_react.jsx +25 -0
  37. data/lib/install/examples/vue/app.vue +22 -0
  38. data/lib/install/examples/vue/hello_vue.js +19 -0
  39. data/lib/install/javascript/packs/application.js +1 -0
  40. data/lib/install/react.rb +15 -0
  41. data/lib/install/template.rb +20 -14
  42. data/lib/install/vue.rb +15 -0
  43. data/lib/tasks/installers.rake +21 -0
  44. data/lib/tasks/webpacker.rake +14 -107
  45. data/lib/tasks/webpacker/compile.rake +29 -0
  46. data/lib/tasks/webpacker/install.rake +12 -0
  47. data/lib/tasks/webpacker/verify_install.rake +16 -0
  48. data/lib/tasks/webpacker/yarn_install.rake +6 -0
  49. data/lib/webpacker.rb +1 -1
  50. data/lib/webpacker/configuration.rb +42 -0
  51. data/lib/webpacker/file_loader.rb +24 -0
  52. data/lib/webpacker/helper.rb +32 -3
  53. data/lib/webpacker/manifest.rb +29 -0
  54. data/lib/webpacker/railtie.rb +6 -16
  55. data/lib/webpacker/version.rb +3 -0
  56. data/package.json +28 -0
  57. data/webpacker.gemspec +15 -12
  58. data/yarn.lock +1014 -0
  59. metadata +50 -20
  60. data/lib/install/config/development.js +0 -24
  61. data/lib/install/config/production.js +0 -16
  62. data/lib/install/config/shared.js +0 -70
  63. data/lib/install/react/.babelrc +0 -3
  64. data/lib/install/react/hello_react.js +0 -16
  65. data/lib/webpacker/digests.rb +0 -42
  66. data/lib/webpacker/source.rb +0 -38
@@ -1,22 +1,33 @@
1
1
  <%= shebang %>
2
+ $stdout.sync = true
2
3
 
3
- RAILS_ENV = ENV['RAILS_ENV'] || 'development'
4
- NODE_ENV = ENV['NODE_ENV'] || RAILS_ENV
4
+ require "shellwords"
5
+ require "yaml"
5
6
 
6
- APP_PATH = File.expand_path('../', __dir__)
7
+ ENV["RAILS_ENV"] ||= "development"
8
+ RAILS_ENV = ENV["RAILS_ENV"]
7
9
 
8
- SET_NODE_PATH = "NODE_PATH=#{APP_PATH}/node_modules"
9
- WEBPACKER_BIN = "./node_modules/.bin/webpack-dev-server"
10
- WEBPACK_CONFIG = "#{APP_PATH}/config/webpack/#{NODE_ENV}.js"
10
+ ENV["NODE_ENV"] ||= RAILS_ENV
11
+ NODE_ENV = ENV["NODE_ENV"]
11
12
 
12
- # Warn the user if the configuration is not set
13
- RAILS_ENV_CONFIG = File.join("config", "environments", "#{RAILS_ENV}.rb")
13
+ APP_PATH = File.expand_path("../", __dir__)
14
+ CONFIG_PATH = File.join(APP_PATH, "config/webpack/paths.yml")
14
15
 
15
- # Look into the environment file for a non-commented variable declaration
16
- unless File.foreach(File.join(APP_PATH, RAILS_ENV_CONFIG)).detect { |line| line.match(/^\s*[^#]*config\.x\.webpacker\[\:dev_server_host\].*=/) }
17
- puts "Warning: if you want to use webpack-dev-server, you need to tell Webpacker to serve asset packs from it. Please set config.x.webpacker[:dev_server_host] in #{RAILS_ENV_CONFIG}.\n\n"
16
+ begin
17
+ paths = YAML.load(File.read(CONFIG_PATH))
18
+
19
+ NODE_MODULES_PATH = File.join(APP_PATH.shellescape, paths["node_modules"])
20
+ WEBPACK_CONFIG_PATH = File.join(APP_PATH.shellescape, paths["config"])
21
+
22
+ WEBPACK_BIN = "#{NODE_MODULES_PATH}/.bin/webpack-dev-server"
23
+ DEV_SERVER_CONFIG = "#{WEBPACK_CONFIG_PATH}/development.server.js"
24
+ rescue Errno::ENOENT, NoMethodError
25
+ puts "Configuration not found in config/webpacker/paths.yml."
26
+ puts "Please run bundle exec rails webpacker:install to install webpacker"
27
+ exit!
18
28
  end
19
29
 
20
30
  Dir.chdir(APP_PATH) do
21
- exec "#{SET_NODE_PATH} #{WEBPACKER_BIN} --config #{WEBPACK_CONFIG} --content-base #{APP_PATH}/public/packs #{ARGV.join(" ")}"
31
+ exec "NODE_PATH=#{NODE_MODULES_PATH} #{WEBPACK_BIN} --progress --color " \
32
+ "--config #{DEV_SERVER_CONFIG}"
22
33
  end
@@ -1,5 +1,8 @@
1
1
  <%= shebang %>
2
2
 
3
+ ENV['RAILS_ENV'] ||= 'development'
4
+ ENV['NODE_ENV'] ||= ENV['RAILS_ENV']
5
+
3
6
  BIN_PATH = File.expand_path('.', __dir__)
4
7
 
5
8
  Dir.chdir(BIN_PATH) do
@@ -1,14 +1,40 @@
1
1
  <%= shebang %>
2
+ $stdout.sync = true
2
3
 
3
- RAILS_ENV = ENV['RAILS_ENV'] || 'development'
4
- NODE_ENV = ENV['NODE_ENV'] || RAILS_ENV
4
+ require "shellwords"
5
+ require "yaml"
5
6
 
6
- APP_PATH = File.expand_path('../', __dir__)
7
+ ENV["RAILS_ENV"] ||= "development"
8
+ RAILS_ENV = ENV["RAILS_ENV"]
7
9
 
8
- SET_NODE_PATH = "NODE_PATH=#{APP_PATH}/node_modules"
9
- WEBPACK_BIN = "./node_modules/webpack/bin/webpack.js"
10
- WEBPACK_CONFIG = "#{APP_PATH}/config/webpack/#{NODE_ENV}.js"
10
+ ENV["NODE_ENV"] ||= RAILS_ENV
11
+ NODE_ENV = ENV["NODE_ENV"]
12
+
13
+ APP_PATH = File.expand_path("../", __dir__)
14
+ CONFIG_PATH = File.join(APP_PATH, "config/webpack/paths.yml")
15
+ DEV_SERVER_CONFIG_PATH = File.join(APP_PATH, "config/webpack/development.server.yml")
16
+
17
+ begin
18
+ paths = YAML.load(File.read(CONFIG_PATH))
19
+ dev_server = YAML.load(File.read(DEV_SERVER_CONFIG_PATH))
20
+
21
+ NODE_MODULES_PATH = File.join(APP_PATH.shellescape, paths["node_modules"])
22
+ WEBPACK_CONFIG_PATH = File.join(APP_PATH.shellescape, paths["config"])
23
+
24
+ if NODE_ENV == "development" && dev_server["enabled"]
25
+ puts "Warning: webpack-dev-server is currently enabled in #{DEV_SERVER_CONFIG_PATH}. " \
26
+ "Disable to serve assets directly from public/packs directory"
27
+ end
28
+ rescue Errno::ENOENT, NoMethodError
29
+ puts "Configuration not found in config/webpack/paths.yml or config/webpack/development.server.yml."
30
+ puts "Please run bundle exec rails webpacker:install to install webpacker"
31
+ exit!
32
+ end
33
+
34
+ WEBPACK_BIN = "#{NODE_MODULES_PATH}/.bin/webpack"
35
+ WEBPACK_CONFIG = "#{WEBPACK_CONFIG_PATH}/#{NODE_ENV}.js"
11
36
 
12
37
  Dir.chdir(APP_PATH) do
13
- exec "#{SET_NODE_PATH} #{WEBPACK_BIN} --config #{WEBPACK_CONFIG} #{ARGV.join(" ")}"
38
+ exec "NODE_PATH=#{NODE_MODULES_PATH} #{WEBPACK_BIN} --config #{WEBPACK_CONFIG}" \
39
+ " #{ARGV.join(" ")}"
14
40
  end
@@ -0,0 +1,4 @@
1
+ plugins:
2
+ postcss-smart-import: {}
3
+ precss: {}
4
+ autoprefixer: {}
@@ -0,0 +1,12 @@
1
+ const { env, publicPath } = require('../configuration.js')
2
+
3
+ module.exports = {
4
+ test: /\.(jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,
5
+ use: [{
6
+ loader: 'file-loader',
7
+ options: {
8
+ publicPath,
9
+ name: env.NODE_ENV === 'production' ? '[name]-[hash].[ext]' : '[name].[ext]'
10
+ }
11
+ }]
12
+ }
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ test: /\.js(\.erb)?$/,
3
+ exclude: /node_modules/,
4
+ loader: 'babel-loader',
5
+ options: {
6
+ presets: [
7
+ ['env', { modules: false }]
8
+ ]
9
+ }
10
+ }
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ test: /\.coffee(\.erb)?$/,
3
+ loader: 'coffee-loader'
4
+ }
@@ -0,0 +1,9 @@
1
+ module.exports = {
2
+ test: /\.erb$/,
3
+ enforce: 'pre',
4
+ exclude: /node_modules/,
5
+ loader: 'rails-erb-loader',
6
+ options: {
7
+ runner: 'DISABLE_SPRING=1 bin/rails runner'
8
+ }
9
+ }
@@ -0,0 +1,9 @@
1
+ const ExtractTextPlugin = require('extract-text-webpack-plugin')
2
+
3
+ module.exports = {
4
+ test: /\.(scss|sass|css)$/i,
5
+ use: ExtractTextPlugin.extract({
6
+ fallback: 'style-loader',
7
+ use: ['css-loader', 'postcss-loader', 'sass-loader']
8
+ })
9
+ }
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ test: /.ts$/,
3
+ loader: 'ts-loader'
4
+ }
@@ -0,0 +1,11 @@
1
+ module.exports = {
2
+ test: /\.(js|jsx)?(\.erb)?$/,
3
+ exclude: /node_modules/,
4
+ loader: 'babel-loader',
5
+ options: {
6
+ presets: [
7
+ 'react',
8
+ ['env', { modules: false }]
9
+ ]
10
+ }
11
+ }
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ test: /.vue$/,
3
+ loader: 'vue-loader',
4
+ options: {
5
+ loaders: {
6
+ scss: 'vue-style-loader!css-loader!sass-loader',
7
+ sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
8
+ }
9
+ }
10
+ }
@@ -0,0 +1,21 @@
1
+ // Common configuration for webpacker loaded from config/webpack/paths.yml
2
+
3
+ const { join, resolve } = require('path')
4
+ const { env } = require('process')
5
+ const { safeLoad } = require('js-yaml')
6
+ const { readFileSync } = require('fs')
7
+
8
+ const configPath = resolve('config', 'webpack')
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}/`
14
+
15
+ module.exports = {
16
+ devServer,
17
+ env,
18
+ paths,
19
+ loadersDir,
20
+ publicPath
21
+ }
@@ -0,0 +1,16 @@
1
+ // Note: You must restart bin/webpack-watcher for changes to take effect
2
+
3
+ const merge = require('webpack-merge')
4
+ const sharedConfig = require('./shared.js')
5
+
6
+ module.exports = merge(sharedConfig, {
7
+ devtool: 'sourcemap',
8
+
9
+ stats: {
10
+ errorDetails: true
11
+ },
12
+
13
+ output: {
14
+ pathinfo: true
15
+ }
16
+ })
@@ -0,0 +1,17 @@
1
+ // Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ const { resolve } = require('path')
4
+ const merge = require('webpack-merge')
5
+ const devConfig = require('./development.js')
6
+ const { devServer, publicPath, paths } = require('./configuration.js')
7
+
8
+ module.exports = merge(devConfig, {
9
+ devServer: {
10
+ host: devServer.host,
11
+ port: devServer.port,
12
+ compress: true,
13
+ historyApiFallback: true,
14
+ contentBase: resolve(paths.output, paths.entry),
15
+ publicPath
16
+ }
17
+ })
@@ -0,0 +1,4 @@
1
+ # Restart webpack-dev-server if you make changes here
2
+ enabled: true
3
+ host: localhost
4
+ port: 8080
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,20 @@
1
+ /* eslint global-require: 0 */
2
+ // Note: You must run bin/webpack for changes to take effect
3
+
4
+ const webpack = require('webpack')
5
+ const merge = require('webpack-merge')
6
+ const CompressionPlugin = require('compression-webpack-plugin')
7
+ const sharedConfig = require('./shared.js')
8
+
9
+ module.exports = merge(sharedConfig, {
10
+ output: { filename: '[name]-[chunkhash].js' },
11
+
12
+ plugins: [
13
+ new webpack.optimize.UglifyJsPlugin(),
14
+ new CompressionPlugin({
15
+ asset: '[path].gz[query]',
16
+ algorithm: 'gzip',
17
+ test: /\.(js|css|svg|eot|ttf|woff|woff2)$/
18
+ })
19
+ ]
20
+ })
@@ -0,0 +1,51 @@
1
+ // Note: You must restart bin/webpack-watcher for changes to take effect
2
+ /* eslint global-require: 0 */
3
+ /* eslint import/no-dynamic-require: 0 */
4
+
5
+ const webpack = require('webpack')
6
+ const { basename, join, resolve } = require('path')
7
+ const { sync } = require('glob')
8
+ const { readdirSync } = require('fs')
9
+ const ExtractTextPlugin = require('extract-text-webpack-plugin')
10
+ const ManifestPlugin = require('webpack-manifest-plugin')
11
+ const extname = require('path-complete-extname')
12
+ const { env, paths, publicPath, loadersDir } = require('./configuration.js')
13
+
14
+ const extensionGlob = `*{${paths.extensions.join(',')}}*`
15
+ const packPaths = sync(join(paths.source, paths.entry, extensionGlob))
16
+
17
+ module.exports = {
18
+ entry: packPaths.reduce(
19
+ (map, entry) => {
20
+ const localMap = map
21
+ localMap[basename(entry, extname(entry))] = resolve(entry)
22
+ return localMap
23
+ }, {}
24
+ ),
25
+
26
+ output: { filename: '[name].js', path: resolve(paths.output, paths.entry) },
27
+
28
+ module: {
29
+ rules: readdirSync(loadersDir).map(file => (
30
+ require(join(loadersDir, file))
31
+ ))
32
+ },
33
+
34
+ plugins: [
35
+ new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(env))),
36
+ new ExtractTextPlugin(env.NODE_ENV === 'production' ? '[name]-[hash].css' : '[name].css'),
37
+ new ManifestPlugin({ fileName: 'manifest.json', publicPath, writeToFileEmit: true })
38
+ ],
39
+
40
+ resolve: {
41
+ extensions: paths.extensions,
42
+ modules: [
43
+ resolve(paths.source),
44
+ resolve(paths.node_modules)
45
+ ]
46
+ },
47
+
48
+ resolveLoader: {
49
+ modules: [paths.node_modules]
50
+ }
51
+ }
@@ -4,4 +4,4 @@
4
4
  //
5
5
  // <%= javascript_pack_tag 'hello_angular' %>
6
6
 
7
- require("hello_angular")
7
+ require('../hello_angular')
@@ -0,0 +1,3 @@
1
+ {
2
+ "presets": ["env", "react"]
3
+ }
@@ -0,0 +1,25 @@
1
+ // Run this example by adding <%= javascript_pack_tag 'hello_react' %> to the head of your layout file,
2
+ // like app/views/layouts/application.html.erb. All it does is render <div>Hello React</div> at the bottom
3
+ // of the page.
4
+
5
+ import React from 'react'
6
+ import ReactDOM from 'react-dom'
7
+
8
+ const Hello = props => (
9
+ <div>Hello {props.name}!</div>
10
+ )
11
+
12
+ Hello.defaultProps = {
13
+ name: 'David'
14
+ }
15
+
16
+ Hello.propTypes = {
17
+ name: React.PropTypes.string
18
+ }
19
+
20
+ document.addEventListener('DOMContentLoaded', () => {
21
+ ReactDOM.render(
22
+ <Hello name="React" />,
23
+ document.body.appendChild(document.createElement('div')),
24
+ )
25
+ })
@@ -0,0 +1,22 @@
1
+ <template>
2
+ <div id="app">
3
+ <p>{{ message }}</p>
4
+ </div>
5
+ </template>
6
+
7
+ <script>
8
+ module.exports = {
9
+ data: function () {
10
+ return {
11
+ message: "Hello Vue!"
12
+ }
13
+ }
14
+ }
15
+ </script>
16
+
17
+ <style scoped>
18
+ p {
19
+ font-size: 2em;
20
+ text-align: center;
21
+ }
22
+ </style>
@@ -0,0 +1,19 @@
1
+ /* eslint no-console: 0 */
2
+ // Run this example by adding <%= javascript_pack_tag 'hello_vue' %>
3
+ // to the head of your layout file,
4
+ // like app/views/layouts/application.html.erb.
5
+ // All it does is render <div>Hello Vue</div> at the bottom of the page.
6
+
7
+ import Vue from 'vue/dist/vue.esm'
8
+ import App from './app.vue'
9
+
10
+ document.addEventListener('DOMContentLoaded', () => {
11
+ document.body.appendChild(document.createElement('hello'))
12
+ const app = new Vue({
13
+ el: 'hello',
14
+ template: '<App/>',
15
+ components: { App }
16
+ })
17
+
18
+ console.log(app)
19
+ })