webpacker 0.2 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8718b611e0688ca042fc8c4e76cbac4037eb7ef8
4
- data.tar.gz: 22a7a34b3326f4cb4da111d84875a0e5493a9794
3
+ metadata.gz: d08bb5dd0f0a4b9fea13b49223f8c8ab56bc9269
4
+ data.tar.gz: 51fef7706f57d3a3676334251e89886b5690290a
5
5
  SHA512:
6
- metadata.gz: 2faf426f4d370a0107f5fc865125c127ab2ef280dddb037d89214798f776b9cd2b29534c98059ac30e283a4946586f88176086b42e343ae6bfc489abfb3b1353
7
- data.tar.gz: 9c4b9245412e474a8c248d3857c49fee082ca41d9663d30f8cf2ff474b061804136b0b3327961b6d6d918087eae2767a8d96b32a977a72f661cef6a10084df0b
6
+ metadata.gz: a86074c5ea9ddcee8b3fe380ddad6fe8b07fe5ddaf6cc0cc593d473fb16faf0df74f2373e8983ed171b702cb0ba047025bab07c41b54a2a52d2703e89d6f6417
7
+ data.tar.gz: 99dfc2b3a748c8cac918423b22a765f24ae339d7960210ea2db66b9f84b40c3f958ff4b527939cf41125b48207a367f51892f3d7d7616e6145e9c16301f89a64
@@ -1,55 +1,55 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- webpacker (0.1)
5
- activesupport (>= 3.0.0, < 5.1)
4
+ webpacker (0.2)
5
+ activesupport (>= 5.0)
6
6
  multi_json (~> 1.2)
7
- railties (~> 5)
7
+ railties (>= 5.0)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- actionpack (5.0.0.1)
13
- actionview (= 5.0.0.1)
14
- activesupport (= 5.0.0.1)
12
+ actionpack (5.0.1)
13
+ actionview (= 5.0.1)
14
+ activesupport (= 5.0.1)
15
15
  rack (~> 2.0)
16
16
  rack-test (~> 0.6.3)
17
17
  rails-dom-testing (~> 2.0)
18
18
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
19
- actionview (5.0.0.1)
20
- activesupport (= 5.0.0.1)
19
+ actionview (5.0.1)
20
+ activesupport (= 5.0.1)
21
21
  builder (~> 3.1)
22
22
  erubis (~> 2.7.0)
23
23
  rails-dom-testing (~> 2.0)
24
24
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
25
- activesupport (5.0.0.1)
25
+ activesupport (5.0.1)
26
26
  concurrent-ruby (~> 1.0, >= 1.0.2)
27
27
  i18n (~> 0.7)
28
28
  minitest (~> 5.1)
29
29
  tzinfo (~> 1.1)
30
- builder (3.2.2)
31
- concurrent-ruby (1.0.2)
30
+ builder (3.2.3)
31
+ concurrent-ruby (1.0.4)
32
32
  erubis (2.7.0)
33
- i18n (0.7.0)
33
+ i18n (0.8.0)
34
34
  loofah (2.0.3)
35
35
  nokogiri (>= 1.5.9)
36
36
  method_source (0.8.2)
37
37
  mini_portile2 (2.1.0)
38
38
  minitest (5.10.1)
39
39
  multi_json (1.12.1)
40
- nokogiri (1.6.8.1)
40
+ nokogiri (1.7.0.1)
41
41
  mini_portile2 (~> 2.1.0)
42
42
  rack (2.0.1)
43
43
  rack-test (0.6.3)
44
44
  rack (>= 1.0)
45
- rails-dom-testing (2.0.1)
45
+ rails-dom-testing (2.0.2)
46
46
  activesupport (>= 4.2.0, < 6.0)
47
- nokogiri (~> 1.6.0)
47
+ nokogiri (~> 1.6)
48
48
  rails-html-sanitizer (1.0.3)
49
49
  loofah (~> 2.0)
50
- railties (5.0.0.1)
51
- actionpack (= 5.0.0.1)
52
- activesupport (= 5.0.0.1)
50
+ railties (5.0.1)
51
+ actionpack (= 5.0.1)
52
+ activesupport (= 5.0.1)
53
53
  method_source
54
54
  rake (>= 0.8.7)
55
55
  thor (>= 0.18.1, < 2.0)
@@ -67,4 +67,4 @@ DEPENDENCIES
67
67
  webpacker!
68
68
 
69
69
  BUNDLED WITH
70
- 1.12.1
70
+ 1.14.3
data/README.md CHANGED
@@ -1,33 +1,46 @@
1
1
  # Webpacker
2
2
 
3
- Webpacker makes it easy to use the JavaScript preprocessor and bundler [Webpack](http://webpack.github.io)
3
+ Webpacker makes it easy to use the JavaScript preprocessor and bundler [Webpack](https://webpack.github.io)
4
4
  to manage application-like JavaScript in Rails. It coexists with the asset pipeline,
5
5
  as the purpose is only to use Webpack for app-like JavaScript, not images, css, or
6
6
  even JavaScript Sprinkles (that all continues to live in app/assets).
7
7
 
8
- It's designed to work with Rails 5.1+ and makes use of the [Yarn](https://yarnpkg.com/) dependency management
8
+ It's designed to work with Rails 5.1+ and makes use of the [Yarn](https://yarnpkg.com) dependency management
9
9
  that's been made default from that version forward.
10
- It's also currently compatible with Rails 5.0 stable but there's absolutely no warranty
11
- it will still be in the future.
10
+
11
+ ## Installation
12
+
13
+ Webpacker is currently compatible with Rails 5.0 stable, but there's no guarantee it will still be
14
+ in the future.
15
+
12
16
  You can either make use of Webpacker during setup of a new application with `--webpack`
13
17
  or you can add the gem and run `bin/rails webpacker:install` in an existing application.
14
18
 
19
+ As the rubygems version isn't promised to be kept up to date until the release of Rails 5.1, you may want to include the gem directly from GitHub:
20
+
21
+ ```ruby
22
+ gem 'webpacker', github: 'rails/webpacker'
23
+ ```
15
24
 
16
25
  ## Binstubs
17
26
 
18
27
  Webpacker ships with three binstubs: `./bin/webpack`, `./bin/webpack-watcher` and `./bin/webpack-dev-server`.
19
28
  They're thin wrappers around the standard webpack.js executable, just to ensure that the right configuration
20
- file is loaded and the node_modules from vendor are used.
29
+ file is loaded.
30
+
31
+
32
+ A binstub is also created to install your npm dependencies,
33
+ and can be called via `./bin/yarn`.
21
34
 
22
35
  In development, you'll need to run `./bin/webpack-watcher` in a separate terminal from
23
36
  `./bin/rails server` to have your `app/javascript/packs/*.js` files compiled as you make changes.
24
37
  If you'd rather not have to run the two processes separately by hand, you can use
25
- [Foreman](http://ddollar.github.io/foreman/).
38
+ [Foreman](https://ddollar.github.io/foreman).
26
39
 
27
40
  Alternatively, you can run `./bin/webpack-dev-server`. This will launch a
28
41
  [Webpack Dev Server](https://webpack.github.io/docs/webpack-dev-server.html) listening on http://localhost:8080/
29
42
  serving your pack files. It will recompile your files as you make changes. You also need to set
30
- `config.x.webpacker[:dev_server_host]` in your `config/environment/development.rb` to tell Webpacker to load
43
+ `config.x.webpacker[:dev_server_host]` in your `config/environments/development.rb` to tell Webpacker to load
31
44
  your packs from the Webpack Dev Server. This setup allows you to leverage advanced Webpack features, such
32
45
  as [Hot Module Replacement](https://webpack.github.io/docs/hot-module-replacement-with-webpack.html).
33
46
 
@@ -72,6 +85,19 @@ will invoke the production configuration, which includes digesting. The `javascr
72
85
  method will automatically insert the correct digest when run in production mode. Just like the asset
73
86
  pipeline does it.
74
87
 
88
+ ## Linking to sprockets assets
89
+
90
+ It's possible to link to assets that have been precompiled by sprockets. Add the `.erb` extension
91
+ to your javascript file, then you can use Sprockets' asset helpers:
92
+
93
+ ```
94
+ // app/javascript/my_pack/example.js.erb
95
+
96
+ <% helpers = ActionController::Base.helpers %>
97
+ var railsImagePath = "<%= helpers.image_path('rails.png') %>";
98
+ ```
99
+
100
+ This is enabled by the `rails-erb-loader` loader rule in `config/webpack/shared.js`.
75
101
 
76
102
  ## Ready for React
77
103
 
@@ -79,13 +105,16 @@ To use Webpacker with React, just create a new app with `rails new myapp --webpa
79
105
  will be added via yarn and changes to the configuration files made. Now you can create JSX files and
80
106
  have them properly compiled automatically.
81
107
 
108
+ ## Ready for Angular with TypeScript
109
+
110
+ To use Webpacker with Angular, just create a new app with `rails new myapp --webpack=angular` (or run `rails webpacker:install:angular` on a Rails 5.1 app already setup with webpack). TypeScript support and the Angular core libraries will be added via yarn and changes to the configuration files made. An example component written in TypeScript is also added to your project in `app/javascript` so that you can experiment Angular right away.
82
111
 
83
- ## Work left to do
112
+ ## Wishlist
84
113
 
85
- - Make asset pipeline digests readable from webpack, so you can reference images etc
114
+ - Improve process for linking to assets compiled by sprockets - shouldn't need to specify
115
+ ` <% helpers = ActionController::Base.helpers %>` at the beginning of each file
86
116
  - Consider chunking setup
87
117
  - Consider on-demand compiling with digests when digesting=true
88
- - I'm sure a ton of other shit
89
118
 
90
119
  ## License
91
- Webpacker is released under the [MIT License](http://www.opensource.org/licenses/MIT).
120
+ Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,7 @@
1
+ // Run this Angular example by adding the following HTML markup to your view:
2
+ //
3
+ // <hello-angular>Loading...</hello-angular>
4
+ //
5
+ // <%= javascript_pack_tag 'hello_angular' %>
6
+
7
+ require("hello_angular")
@@ -0,0 +1,9 @@
1
+ import { Component } from '@angular/core';
2
+
3
+ @Component({
4
+ selector: 'hello-angular',
5
+ template: `<h1>Hello {{name}}</h1>`
6
+ })
7
+ export class AppComponent {
8
+ name = 'Angular!';
9
+ }
@@ -0,0 +1,16 @@
1
+ import { BrowserModule } from '@angular/platform-browser';
2
+ import { NgModule } from '@angular/core';
3
+
4
+ import { AppComponent } from './app.component';
5
+
6
+ @NgModule({
7
+ declarations: [
8
+ AppComponent
9
+ ],
10
+ imports: [
11
+ BrowserModule
12
+ ],
13
+ providers: [],
14
+ bootstrap: [AppComponent]
15
+ })
16
+ export class AppModule { }
@@ -0,0 +1,6 @@
1
+ import './polyfills.ts';
2
+
3
+ import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
4
+ import { AppModule } from './app/app.module';
5
+
6
+ platformBrowserDynamic().bootstrapModule(AppModule);
@@ -0,0 +1,19 @@
1
+ // This file includes polyfills needed by Angular and is loaded before
2
+ // the app. You can add your own extra polyfills to this file.
3
+ import 'core-js/es6/symbol';
4
+ import 'core-js/es6/object';
5
+ import 'core-js/es6/function';
6
+ import 'core-js/es6/parse-int';
7
+ import 'core-js/es6/parse-float';
8
+ import 'core-js/es6/number';
9
+ import 'core-js/es6/math';
10
+ import 'core-js/es6/string';
11
+ import 'core-js/es6/date';
12
+ import 'core-js/es6/array';
13
+ import 'core-js/es6/regexp';
14
+ import 'core-js/es6/map';
15
+ import 'core-js/es6/set';
16
+ import 'core-js/es6/reflect';
17
+
18
+ import 'core-js/es7/reflect';
19
+ import 'zone.js/dist/zone';
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": false,
4
+ "emitDecoratorMetadata": true,
5
+ "experimentalDecorators": true,
6
+ "lib": ["es6", "dom"],
7
+ "module": "es6",
8
+ "moduleResolution": "node",
9
+ "sourceMap": true,
10
+ "target": "es5"
11
+ },
12
+ "exclude": [
13
+ "**/*.spec.ts",
14
+ "node_modules",
15
+ "public"
16
+ ],
17
+ "compileOnSave": false
18
+ }
@@ -1,14 +1,13 @@
1
1
  <%= shebang %>
2
2
 
3
- RAILS_ENV = ENV['RAILS_ENV'] || 'development'
4
- WEBPACK_ENV = ENV['WEBPACK_ENV'] || RAILS_ENV
3
+ RAILS_ENV = ENV['RAILS_ENV'] || 'development'
4
+ NODE_ENV = ENV['NODE_ENV'] || RAILS_ENV
5
5
 
6
- APP_PATH = File.expand_path('../', __dir__)
7
- VENDOR_PATH = File.expand_path('../vendor', __dir__)
6
+ APP_PATH = File.expand_path('../', __dir__)
8
7
 
9
- SET_NODE_PATH = "NODE_PATH=#{VENDOR_PATH}/node_modules"
10
- WEBPACKER_BIN = "./node_modules/.bin/webpack-dev-server"
11
- WEBPACK_CONFIG = "#{APP_PATH}/config/webpack/#{WEBPACK_ENV}.js"
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"
12
11
 
13
12
  # Warn the user if the configuration is not set
14
13
  RAILS_ENV_CONFIG = File.join("config", "environments", "#{RAILS_ENV}.rb")
@@ -18,6 +17,6 @@ unless File.foreach(File.join(APP_PATH, RAILS_ENV_CONFIG)).detect { |line| line.
18
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"
19
18
  end
20
19
 
21
- Dir.chdir(VENDOR_PATH) do
22
- exec "#{SET_NODE_PATH} #{WEBPACKER_BIN} --config #{WEBPACK_CONFIG} --content-base public/packs #{ARGV.join(" ")}"
20
+ Dir.chdir(APP_PATH) do
21
+ exec "#{SET_NODE_PATH} #{WEBPACKER_BIN} --config #{WEBPACK_CONFIG} --content-base #{APP_PATH}/public/packs #{ARGV.join(" ")}"
23
22
  end
@@ -1,15 +1,14 @@
1
1
  <%= shebang %>
2
2
 
3
- RAILS_ENV = ENV['RAILS_ENV'] || 'development'
4
- WEBPACK_ENV = ENV['WEBPACK_ENV'] || RAILS_ENV
3
+ RAILS_ENV = ENV['RAILS_ENV'] || 'development'
4
+ NODE_ENV = ENV['NODE_ENV'] || RAILS_ENV
5
5
 
6
- APP_PATH = File.expand_path('../', __dir__)
7
- VENDOR_PATH = File.expand_path('../vendor', __dir__)
6
+ APP_PATH = File.expand_path('../', __dir__)
8
7
 
9
- SET_NODE_PATH = "NODE_PATH=#{VENDOR_PATH}/node_modules"
8
+ SET_NODE_PATH = "NODE_PATH=#{APP_PATH}/node_modules"
10
9
  WEBPACK_BIN = "./node_modules/webpack/bin/webpack.js"
11
- WEBPACK_CONFIG = "#{APP_PATH}/config/webpack/#{WEBPACK_ENV}.js"
10
+ WEBPACK_CONFIG = "#{APP_PATH}/config/webpack/#{NODE_ENV}.js"
12
11
 
13
- Dir.chdir(VENDOR_PATH) do
12
+ Dir.chdir(APP_PATH) do
14
13
  exec "#{SET_NODE_PATH} #{WEBPACK_BIN} --config #{WEBPACK_CONFIG} #{ARGV.join(" ")}"
15
14
  end
@@ -1,5 +1,5 @@
1
1
  <%= shebang %>
2
- VENDOR_PATH = File.expand_path('../vendor', __dir__)
2
+ VENDOR_PATH = File.expand_path('..', __dir__)
3
3
  Dir.chdir(VENDOR_PATH) do
4
4
  begin
5
5
  exec "yarnpkg #{ARGV.join(" ")}"
@@ -1,12 +1,11 @@
1
1
  // Note: You must restart bin/webpack-watcher for changes to take effect
2
2
 
3
- var path = require('path')
4
3
  var webpack = require('webpack')
5
4
  var merge = require('webpack-merge')
6
5
 
7
- var config = require('./shared.js')
6
+ var sharedConfig = require('./shared.js')
8
7
 
9
- module.exports = merge(config, {
8
+ module.exports = merge(sharedConfig.config, {
10
9
  devtool: 'sourcemap',
11
10
 
12
11
  stats: {
@@ -1,13 +1,12 @@
1
1
  // Note: You must restart bin/webpack-watcher for changes to take effect
2
2
 
3
- var path = require('path')
4
3
  var webpack = require('webpack')
5
4
  var merge = require('webpack-merge')
6
5
 
7
- var config = require('./shared.js')
6
+ var sharedConfig = require('./shared.js')
8
7
 
9
- module.exports = merge(config, {
10
- output: { filename: "[name]-[hash].js" },
8
+ module.exports = merge(sharedConfig.config, {
9
+ output: { filename: '[name]-[hash].js' },
11
10
 
12
11
  plugins: [
13
12
  new webpack.LoaderOptionsPlugin({
@@ -1,19 +1,26 @@
1
1
  // Note: You must restart bin/webpack-watcher for changes to take effect
2
2
 
3
+ var webpack = require('webpack')
3
4
  var path = require('path')
5
+ var process = require('process')
4
6
  var glob = require('glob')
5
7
  var extname = require('path-complete-extname')
8
+ var distDir = process.env.WEBPACK_DIST_DIR
6
9
 
7
- module.exports = {
8
- entry: glob.sync(path.join('..', 'app', 'javascript', 'packs', '*.js*')).reduce(
10
+ if(distDir === undefined) {
11
+ distDir = 'packs'
12
+ }
13
+
14
+ config = {
15
+ entry: glob.sync(path.join('app', 'javascript', 'packs', '*.js*')).reduce(
9
16
  function(map, entry) {
10
17
  var basename = path.basename(entry, extname(entry))
11
- map[basename] = entry
18
+ map[basename] = path.resolve(entry)
12
19
  return map
13
20
  }, {}
14
21
  ),
15
22
 
16
- output: { filename: '[name].js', path: path.resolve('..', 'public', 'packs') },
23
+ output: { filename: '[name].js', path: path.resolve('public', distDir) },
17
24
 
18
25
  module: {
19
26
  rules: [
@@ -29,27 +36,35 @@ module.exports = {
29
36
  }
30
37
  },
31
38
  {
32
- test: /\.erb$/,
39
+ test: /.erb$/,
33
40
  enforce: 'pre',
41
+ exclude: /node_modules/,
34
42
  loader: 'rails-erb-loader',
35
43
  options: {
36
- runner: '../bin/rails runner'
44
+ runner: 'DISABLE_SPRING=1 bin/rails runner'
37
45
  }
38
46
  },
39
47
  ]
40
48
  },
41
49
 
42
- plugins: [],
50
+ plugins: [
51
+ new webpack.EnvironmentPlugin(Object.keys(process.env))
52
+ ],
43
53
 
44
54
  resolve: {
45
55
  extensions: [ '.js', '.coffee' ],
46
56
  modules: [
47
- path.resolve('../app/javascript'),
48
- path.resolve('../vendor/node_modules')
57
+ path.resolve('app/javascript'),
58
+ path.resolve('node_modules')
49
59
  ]
50
60
  },
51
61
 
52
62
  resolveLoader: {
53
- modules: [ path.resolve('../vendor/node_modules') ]
63
+ modules: [ path.resolve('node_modules') ]
54
64
  }
55
65
  }
66
+
67
+ module.exports = {
68
+ distDir: distDir,
69
+ config: config
70
+ }
@@ -1,9 +1,9 @@
1
- // This file is will automatically compiled by Webpack, along with any other files
1
+ // This file is automatically compiled by Webpack, along with any other files
2
2
  // present in this directory. You're encouraged to place your actual application logic in
3
- // a relevant structure within app/javascript, and only use these pack files to reference
4
- // that code, so it'll be compiled.
3
+ // a relevant structure within app/javascript and only use these pack files to reference
4
+ // that code so it'll be compiled.
5
5
  //
6
6
  // To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
7
7
  // layout file, like app/views/layouts/application.html.erb
8
8
 
9
- console.log("Hello World from Webpacker")
9
+ console.log('Hello World from Webpacker')
@@ -0,0 +1,3 @@
1
+ {
2
+ "presets": ["es2015", "react"]
3
+ }
@@ -1,17 +1,16 @@
1
- INSTALL_PATH = File.dirname(__FILE__)
1
+ directory "#{__dir__}/javascript", 'app/javascript'
2
2
 
3
- directory "#{INSTALL_PATH}/javascript", 'app/javascript'
4
-
5
- directory "#{INSTALL_PATH}/bin", 'bin'
3
+ directory "#{__dir__}/bin", 'bin'
6
4
  chmod 'bin', 0755 & ~File.umask, verbose: false
7
5
 
8
- directory "#{INSTALL_PATH}/config", 'config/webpack'
6
+ directory "#{__dir__}/config", 'config/webpack'
9
7
 
10
8
  append_to_file '.gitignore', <<-EOS
11
9
  /public/packs
10
+ /node_modules
12
11
  EOS
13
12
 
14
- run './bin/yarn add --dev webpack@beta webpack-merge webpack-dev-server@beta path-complete-extname babel-loader babel-core babel-preset-latest coffee-loader coffee-script rails-erb-loader'
13
+ run './bin/yarn add --dev webpack webpack-merge webpack-dev-server path-complete-extname babel-loader babel-core babel-preset-latest coffee-loader coffee-script rails-erb-loader glob'
15
14
 
16
15
  environment \
17
16
  "# Make javascript_pack_tag lookup digest hash to enable long-term caching\n" +
@@ -1,18 +1,27 @@
1
- PACKS_PATH = Rails.root.join('public/packs')
2
- PACK_DIGESTS_PATH = PACKS_PATH.join('digests.json')
3
-
4
- WEBPACKER_APP_TEMPLATE_PATH = File.expand_path('../install/template.rb', File.dirname(__FILE__))
1
+ WEBPACKER_APP_TEMPLATE_PATH = File.expand_path('../install/template.rb', __dir__)
2
+ REGEX_MAP = /\A.*\.map\z/
5
3
 
6
4
  namespace :webpacker do
7
5
  desc "Compile javascript packs using webpack for production with digests"
8
- task :compile do
9
- webpack_digests_json = JSON.parse(`WEBPACK_ENV=production ./bin/webpack --json`)['assetsByChunkName'].to_json
6
+ task :compile => :environment do
7
+ dist_dir = Rails.application.config.x.webpacker[:packs_dist_dir]
8
+ result = `WEBPACK_DIST_DIR=#{dist_dir} NODE_ENV=production ./bin/webpack --json`
9
+
10
+ exit! $?.exitstatus unless $?.success?
11
+
12
+ webpack_digests = JSON.parse(result)['assetsByChunkName'].each_with_object({}) do |(chunk, file), h|
13
+ h[chunk] = file.is_a?(Array) ? file.find {|f| REGEX_MAP !~ f } : file
14
+ end.to_json
10
15
 
11
- FileUtils.mkdir_p(PACKS_PATH)
12
- File.open(PACK_DIGESTS_PATH, 'w+') { |file| file.write webpack_digests_json }
16
+ digests_path = Rails.application.config.x.webpacker[:digests_path]
17
+ packs_path = Rails.root.join('public', dist_dir) || File.dirname(digests_path)
18
+ packs_digests_path = digests_path || Rails.root.join(packs_path, 'digests.json')
13
19
 
14
- puts "Compiled digests for all packs in #{PACK_DIGESTS_PATH}: "
15
- puts webpack_digests_json
20
+ FileUtils.mkdir_p(packs_path)
21
+ File.open(packs_digests_path, 'w+') { |file| file.write webpack_digests }
22
+
23
+ puts "Compiled digests for all packs in #{packs_digests_path}: "
24
+ puts webpack_digests
16
25
  end
17
26
 
18
27
  desc "Install webpacker in this application"
@@ -42,16 +51,58 @@ namespace :webpacker do
42
51
 
43
52
  File.write config_path, config
44
53
 
54
+ puts "Copying .babelrc to project directory"
55
+ FileUtils.copy File.expand_path('../install/react/.babelrc', __dir__),
56
+ Rails.root
57
+
45
58
  puts "Copying react example to app/javascript/packs/hello_react.js"
46
- FileUtils.copy File.expand_path('../install/react/hello_react.js', File.dirname(__FILE__)),
59
+ FileUtils.copy File.expand_path('../install/react/hello_react.js', __dir__),
47
60
  Rails.root.join('app/javascript/packs/hello_react.js')
48
61
 
49
62
  exec './bin/yarn add --dev babel-preset-react && ./bin/yarn add react react-dom'
50
63
  end
64
+
65
+ desc "Install everything needed for Angular"
66
+ task :angular do
67
+ config_path = Rails.root.join('config/webpack/shared.js')
68
+ config = File.read(config_path)
69
+
70
+ if config.include?('ts-loader')
71
+ puts "The configuration file already has a reference to ts-loader, skipping the test rule..."
72
+ else
73
+ puts "Adding a loader rule to include ts-loader for .ts files in #{config_path}..."
74
+ config.gsub!(/rules:(\s*\[)(\s*\{)/, "rules:\\1\\2 test: /\.ts$/, loader: 'ts-loader' },\\2")
75
+ end
76
+
77
+ if config =~ /["'].ts["']/
78
+ puts "The configuration file already has a reference to .ts extension, skipping the addition of this extension to the list..."
79
+ else
80
+ puts "Adding '.ts' in loader extensions in #{config_path}..."
81
+ config.gsub!(/extensions:(.*')(\s*\])/, "extensions:\\1, '.ts'\\2")
82
+ end
83
+
84
+ File.write config_path, config
85
+
86
+ puts "Copying Angular example to app/javascript/packs/hello_angular.js"
87
+ FileUtils.copy File.expand_path('../install/angular/hello_angular.js', __dir__),
88
+ Rails.root.join('app/javascript/packs/hello_angular.js')
89
+
90
+ puts "Copying Angular Hello app to app/javascript/hello_angular"
91
+ FileUtils.copy_entry File.expand_path('../install/angular/hello_angular', __dir__),
92
+ Rails.root.join('app/javascript/hello_angular')
93
+
94
+ puts "Copying tsconfig.json to the Rails root directory"
95
+ FileUtils.copy File.expand_path('../install/angular/tsconfig.json', __dir__),
96
+ Rails.root.join('tsconfig.json')
97
+
98
+ exec './bin/yarn add --dev typescript ts-loader && ./bin/yarn add "core-js zone.js rxjs @angular/core @angular/common @angular/compiler @angular/platform-browser @angular/platform-browser-dynamic"'
99
+ end
51
100
  end
52
101
  end
53
102
 
54
103
  # Compile packs after we've compiled all other assets during precompilation
55
- Rake::Task['assets:precompile'].enhance do
56
- Rake::Task['webpacker:compile'].invoke
104
+ if Rake::Task.task_defined?('assets:precompile')
105
+ Rake::Task['assets:precompile'].enhance do
106
+ Rake::Task['webpacker:compile'].invoke
107
+ end
57
108
  end
@@ -3,6 +3,8 @@
3
3
  # "calendar-1016838bab065ae1e314.js". These digested filenames are what enables you to long-term
4
4
  # cache things in production.
5
5
  class Webpacker::Digests
6
+ class DigestError < StandardError; end
7
+
6
8
  class_attribute :instance
7
9
 
8
10
  class << self
@@ -12,16 +14,16 @@ class Webpacker::Digests
12
14
 
13
15
  def lookup(name)
14
16
  if instance
15
- instance.lookup(name)
17
+ instance.lookup(name).presence || raise(DigestError.new("Can't find #{name} in #{instance.inspect}"))
16
18
  else
17
- raise "Webpacker::Digests.load(path) must be called first"
19
+ raise DigestError.new("Webpacker::Digests.load(path) must be called first")
18
20
  end
19
21
  end
20
22
  end
21
23
 
22
24
  def initialize(path)
23
- @path = path
24
- load
25
+ @path = path
26
+ @digests = load
25
27
  end
26
28
 
27
29
  def lookup(name)
@@ -30,11 +32,11 @@ class Webpacker::Digests
30
32
 
31
33
  private
32
34
  def load
33
- begin
34
- @digests = JSON.parse(File.read(@path))
35
- rescue Errno::ENOENT
36
- Rails.logger.error \
37
- "Missing digests file at #{@path}! You must first compile the packs via rails webpacker:compile"
35
+ if File.exist?(@path)
36
+ JSON.parse(File.read(@path))
37
+ else
38
+ Rails.logger.info "Didn't find any digests file at #{@path}. You must first compile the packs via rails webpacker:compile"
39
+ {}
38
40
  end
39
41
  end
40
42
  end
@@ -4,15 +4,23 @@ require 'webpacker/helper'
4
4
  require 'webpacker/digests'
5
5
 
6
6
  class Webpacker::Engine < ::Rails::Engine
7
- initializer :webpacker do
7
+ initializer :webpacker do |app|
8
8
  ActiveSupport.on_load :action_controller do
9
9
  ActionController::Base.helper Webpacker::Helper
10
10
  end
11
11
 
12
- if Rails.configuration.x.webpacker[:digesting]
12
+ app.config.x.webpacker[:packs_dist_dir] ||= 'packs'
13
+ app.config.x.webpacker[:packs_dist_path] ||= \
14
+ "/#{app.config.x.webpacker[:packs_dist_dir]}"
15
+
16
+ if app.config.x.webpacker[:digesting]
17
+ app.config.x.webpacker[:digests_path] ||= \
18
+ Rails.root.join('public',
19
+ app.config.x.webpacker[:packs_dist_dir],
20
+ 'digests.json')
21
+
13
22
  Webpacker::Digests.load \
14
- Rails.application.config.x.webpacker[:digests_path] ||
15
- Rails.root.join('public/packs/digests.json')
23
+ app.config.x.webpacker[:digests_path]
16
24
  end
17
25
  end
18
26
  end
@@ -11,9 +11,9 @@ class Webpacker::Source
11
11
  if config[:dev_server_host].present?
12
12
  "#{config[:dev_server_host]}/#{filename}"
13
13
  elsif config[:digesting]
14
- "/packs/#{digested_filename}"
14
+ File.join(dist_path, digested_filename)
15
15
  else
16
- "/packs/#{filename}"
16
+ File.join(dist_path, filename)
17
17
  end
18
18
  end
19
19
 
@@ -28,6 +28,10 @@ class Webpacker::Source
28
28
  Webpacker::Digests.lookup(name)
29
29
  end
30
30
 
31
+ def dist_path
32
+ config[:packs_dist_path]
33
+ end
34
+
31
35
  def filename
32
36
  "#{name}.js"
33
37
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'webpacker'
3
- s.version = '0.2'
3
+ s.version = '1.0'
4
4
  s.authors = 'David Heinemeier Hansson'
5
5
  s.email = 'david@basecamp.com'
6
6
  s.summary = 'Use Webpack to manage app-like JavaScript modules in Rails'
@@ -9,9 +9,9 @@ Gem::Specification.new do |s|
9
9
 
10
10
  s.required_ruby_version = '>= 1.9.3'
11
11
 
12
- s.add_dependency 'activesupport', '>= 3.0.0', '< 5.1'
12
+ s.add_dependency 'activesupport', '>= 5.0'
13
13
  s.add_dependency 'multi_json', '~> 1.2'
14
- s.add_dependency 'railties', '~> 5'
14
+ s.add_dependency 'railties', '>= 5.0'
15
15
 
16
16
  s.add_development_dependency 'bundler', '~> 1.12'
17
17
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpacker
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '1.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-17 00:00:00.000000000 Z
11
+ date: 2017-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,20 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '5.1'
19
+ version: '5.0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: 3.0.0
30
- - - "<"
31
- - !ruby/object:Gem::Version
32
- version: '5.1'
26
+ version: '5.0'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: multi_json
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -48,16 +42,16 @@ dependencies:
48
42
  name: railties
49
43
  requirement: !ruby/object:Gem::Requirement
50
44
  requirements:
51
- - - "~>"
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
- version: '5'
47
+ version: '5.0'
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
51
  requirements:
58
- - - "~>"
52
+ - - ">="
59
53
  - !ruby/object:Gem::Version
60
- version: '5'
54
+ version: '5.0'
61
55
  - !ruby/object:Gem::Dependency
62
56
  name: bundler
63
57
  requirement: !ruby/object:Gem::Requirement
@@ -84,6 +78,12 @@ files:
84
78
  - MIT-LICENSE
85
79
  - README.md
86
80
  - Rakefile
81
+ - lib/install/angular/hello_angular.js
82
+ - lib/install/angular/hello_angular/app/app.component.ts
83
+ - lib/install/angular/hello_angular/app/app.module.ts
84
+ - lib/install/angular/hello_angular/index.ts
85
+ - lib/install/angular/hello_angular/polyfills.ts
86
+ - lib/install/angular/tsconfig.json
87
87
  - lib/install/bin/webpack-dev-server.tt
88
88
  - lib/install/bin/webpack-watcher.tt
89
89
  - lib/install/bin/webpack.tt
@@ -92,6 +92,7 @@ files:
92
92
  - lib/install/config/production.js
93
93
  - lib/install/config/shared.js
94
94
  - lib/install/javascript/packs/application.js
95
+ - lib/install/react/.babelrc
95
96
  - lib/install/react/hello_react.js
96
97
  - lib/install/template.rb
97
98
  - lib/tasks/webpacker.rake
@@ -121,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
122
  version: '0'
122
123
  requirements: []
123
124
  rubyforge_project:
124
- rubygems_version: 2.5.2
125
+ rubygems_version: 2.6.10
125
126
  signing_key:
126
127
  specification_version: 4
127
128
  summary: Use Webpack to manage app-like JavaScript modules in Rails