webpacker 0.2 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +19 -19
- data/README.md +40 -11
- data/lib/install/angular/hello_angular.js +7 -0
- data/lib/install/angular/hello_angular/app/app.component.ts +9 -0
- data/lib/install/angular/hello_angular/app/app.module.ts +16 -0
- data/lib/install/angular/hello_angular/index.ts +6 -0
- data/lib/install/angular/hello_angular/polyfills.ts +19 -0
- data/lib/install/angular/tsconfig.json +18 -0
- data/lib/install/bin/webpack-dev-server.tt +8 -9
- data/lib/install/bin/webpack.tt +6 -7
- data/lib/install/bin/yarn.tt +1 -1
- data/lib/install/config/development.js +2 -3
- data/lib/install/config/production.js +3 -4
- data/lib/install/config/shared.js +25 -10
- data/lib/install/javascript/packs/application.js +4 -4
- data/lib/install/react/.babelrc +3 -0
- data/lib/install/template.rb +5 -6
- data/lib/tasks/webpacker.rake +64 -13
- data/lib/webpacker/digests.rb +11 -9
- data/lib/webpacker/railtie.rb +12 -4
- data/lib/webpacker/source.rb +6 -2
- data/webpacker.gemspec +3 -3
- metadata +16 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d08bb5dd0f0a4b9fea13b49223f8c8ab56bc9269
|
4
|
+
data.tar.gz: 51fef7706f57d3a3676334251e89886b5690290a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a86074c5ea9ddcee8b3fe380ddad6fe8b07fe5ddaf6cc0cc593d473fb16faf0df74f2373e8983ed171b702cb0ba047025bab07c41b54a2a52d2703e89d6f6417
|
7
|
+
data.tar.gz: 99dfc2b3a748c8cac918423b22a765f24ae339d7960210ea2db66b9f84b40c3f958ff4b527939cf41125b48207a367f51892f3d7d7616e6145e9c16301f89a64
|
data/Gemfile.lock
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
webpacker (0.
|
5
|
-
activesupport (>=
|
4
|
+
webpacker (0.2)
|
5
|
+
activesupport (>= 5.0)
|
6
6
|
multi_json (~> 1.2)
|
7
|
-
railties (
|
7
|
+
railties (>= 5.0)
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
actionpack (5.0.
|
13
|
-
actionview (= 5.0.
|
14
|
-
activesupport (= 5.0.
|
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.
|
20
|
-
activesupport (= 5.0.
|
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.
|
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.
|
31
|
-
concurrent-ruby (1.0.
|
30
|
+
builder (3.2.3)
|
31
|
+
concurrent-ruby (1.0.4)
|
32
32
|
erubis (2.7.0)
|
33
|
-
i18n (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.
|
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.
|
45
|
+
rails-dom-testing (2.0.2)
|
46
46
|
activesupport (>= 4.2.0, < 6.0)
|
47
|
-
nokogiri (~> 1.6
|
47
|
+
nokogiri (~> 1.6)
|
48
48
|
rails-html-sanitizer (1.0.3)
|
49
49
|
loofah (~> 2.0)
|
50
|
-
railties (5.0.
|
51
|
-
actionpack (= 5.0.
|
52
|
-
activesupport (= 5.0.
|
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.
|
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](
|
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
|
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
|
-
|
11
|
-
|
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
|
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](
|
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/
|
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
|
-
##
|
112
|
+
## Wishlist
|
84
113
|
|
85
|
-
-
|
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](
|
120
|
+
Webpacker is released under the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -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,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
|
4
|
-
|
3
|
+
RAILS_ENV = ENV['RAILS_ENV'] || 'development'
|
4
|
+
NODE_ENV = ENV['NODE_ENV'] || RAILS_ENV
|
5
5
|
|
6
|
-
APP_PATH
|
7
|
-
VENDOR_PATH = File.expand_path('../vendor', __dir__)
|
6
|
+
APP_PATH = File.expand_path('../', __dir__)
|
8
7
|
|
9
|
-
SET_NODE_PATH = "NODE_PATH=#{
|
10
|
-
WEBPACKER_BIN
|
11
|
-
WEBPACK_CONFIG = "#{APP_PATH}/config/webpack/#{
|
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(
|
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
|
data/lib/install/bin/webpack.tt
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
<%= shebang %>
|
2
2
|
|
3
|
-
RAILS_ENV
|
4
|
-
|
3
|
+
RAILS_ENV = ENV['RAILS_ENV'] || 'development'
|
4
|
+
NODE_ENV = ENV['NODE_ENV'] || RAILS_ENV
|
5
5
|
|
6
|
-
APP_PATH
|
7
|
-
VENDOR_PATH = File.expand_path('../vendor', __dir__)
|
6
|
+
APP_PATH = File.expand_path('../', __dir__)
|
8
7
|
|
9
|
-
SET_NODE_PATH = "NODE_PATH=#{
|
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/#{
|
10
|
+
WEBPACK_CONFIG = "#{APP_PATH}/config/webpack/#{NODE_ENV}.js"
|
12
11
|
|
13
|
-
Dir.chdir(
|
12
|
+
Dir.chdir(APP_PATH) do
|
14
13
|
exec "#{SET_NODE_PATH} #{WEBPACK_BIN} --config #{WEBPACK_CONFIG} #{ARGV.join(" ")}"
|
15
14
|
end
|
data/lib/install/bin/yarn.tt
CHANGED
@@ -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
|
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
|
6
|
+
var sharedConfig = require('./shared.js')
|
8
7
|
|
9
|
-
module.exports = merge(config, {
|
10
|
-
output: { filename:
|
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
|
-
|
8
|
-
|
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('
|
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:
|
39
|
+
test: /.erb$/,
|
33
40
|
enforce: 'pre',
|
41
|
+
exclude: /node_modules/,
|
34
42
|
loader: 'rails-erb-loader',
|
35
43
|
options: {
|
36
|
-
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('
|
48
|
-
path.resolve('
|
57
|
+
path.resolve('app/javascript'),
|
58
|
+
path.resolve('node_modules')
|
49
59
|
]
|
50
60
|
},
|
51
61
|
|
52
62
|
resolveLoader: {
|
53
|
-
modules: [ path.resolve('
|
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
|
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
|
4
|
-
// that code
|
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(
|
9
|
+
console.log('Hello World from Webpacker')
|
data/lib/install/template.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
|
-
|
1
|
+
directory "#{__dir__}/javascript", 'app/javascript'
|
2
2
|
|
3
|
-
directory "#{
|
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 "#{
|
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
|
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" +
|
data/lib/tasks/webpacker.rake
CHANGED
@@ -1,18 +1,27 @@
|
|
1
|
-
|
2
|
-
|
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
|
-
|
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
|
-
|
12
|
-
|
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
|
-
|
15
|
-
|
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',
|
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
|
56
|
-
Rake::Task['
|
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
|
data/lib/webpacker/digests.rb
CHANGED
@@ -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
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
Rails.logger.
|
37
|
-
|
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
|
data/lib/webpacker/railtie.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
15
|
-
Rails.root.join('public/packs/digests.json')
|
23
|
+
app.config.x.webpacker[:digests_path]
|
16
24
|
end
|
17
25
|
end
|
18
26
|
end
|
data/lib/webpacker/source.rb
CHANGED
@@ -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
|
-
|
14
|
+
File.join(dist_path, digested_filename)
|
15
15
|
else
|
16
|
-
|
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
|
data/webpacker.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'webpacker'
|
3
|
-
s.version = '0
|
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', '>=
|
12
|
+
s.add_dependency 'activesupport', '>= 5.0'
|
13
13
|
s.add_dependency 'multi_json', '~> 1.2'
|
14
|
-
s.add_dependency 'railties', '
|
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
|
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-
|
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:
|
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:
|
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.
|
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
|