webpacker 4.0.0.rc.2 → 4.0.0.rc.3

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
  SHA256:
3
- metadata.gz: c29c9fa51a5fa2bbe873cb18849059726b5306f01c8bb1668009eba939a28c0e
4
- data.tar.gz: 20b8cb4c660e26d51e186d600ab98c1979cda90207bc63a65f7ce4f1fe749eed
3
+ metadata.gz: 7b4e8fa7b4d57022ab1e34dd1e5ea82e7aeabda41f11c158bdb8989c05fe1c0e
4
+ data.tar.gz: 0c9f6d2906a79d379d7f3f5fb097b338e740a39452d0bc2ac33dd3bd30eb3895
5
5
  SHA512:
6
- metadata.gz: c85de1b6b0b66bcb9a70d85ee1b4356a40ccce421193c94bc03df401ecf0ad9be84add25c190a59efd52659d3bddbcf0dcedd7edfc8ac274788d1a8d73e1f5eb
7
- data.tar.gz: e7dd4e624d4b21f003430e744a863c8bd8a84e8d5a5132a502fb4366ff748cc3ed05ba375dd2687fce06cfe257d263e56549736e8e6a162f4f4316f43d17cba1
6
+ metadata.gz: ee59500d81e622c90f0feddd8c026126eb1f1e14f0d37af880d826af56b60f74b5b3ee380397c36bbbd157de059e48d9f2552e6d72dcd295d310ee7ebaa01a42
7
+ data.tar.gz: 0fc59e37a1d3fa940ed3b9e5e8e15667c771a0571029142d507f5d0d3577fa70eb670115ccc6ea0540c0b2afa35c9c501049d204701a2a0389a9499e29f2699d
data/.travis.yml CHANGED
@@ -6,6 +6,7 @@ rvm:
6
6
  - 2.3.8
7
7
  - 2.4.5
8
8
  - 2.5.3
9
+ - 2.6.0
9
10
  - ruby-head
10
11
  gemfile:
11
12
  - gemfiles/Gemfile-rails.4.2.x
@@ -36,5 +37,9 @@ matrix:
36
37
  exclude:
37
38
  - rvm: 2.3.8
38
39
  gemfile: gemfiles/Gemfile-rails-edge
40
+ - rvm: 2.4.5
41
+ gemfile: gemfiles/Gemfile-rails-edge
42
+ - rvm: 2.5.3
43
+ gemfile: gemfiles/Gemfile-rails-edge
39
44
  - rvm: ruby-head
40
45
  gemfile: gemfiles/Gemfile-rails.4.2.x
data/CHANGELOG.md CHANGED
@@ -1,5 +1,37 @@
1
1
  **Please note that Webpacker 3.1.0 and 3.1.1 have some serious bugs so please consider using either 3.0.2 or 3.2.0**
2
2
 
3
+ ## [4.0.0.rc.3] - 2019-01-17
4
+
5
+ ### Fixed
6
+ - Issue with javascript_pack_tag asset duplication [#1898](https://github.com/rails/webpacker/pull/1898)
7
+
8
+
9
+ ### Added
10
+ - `javascript_packs_with_chunks_tag` helper, which creates html tags
11
+ for a pack and all the dependent chunks, when using splitchunks.
12
+
13
+ ```erb
14
+ <%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
15
+
16
+ <script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
17
+ <script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
18
+ <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
19
+ <script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
20
+ <script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
21
+ ```
22
+
23
+ **Important:** Pass all your pack names when using `javascript_packs_with_chunks_tag`
24
+ helper otherwise you will get duplicated chunks on the page.
25
+
26
+ ```erb
27
+ <%# DO %>
28
+ <%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
29
+
30
+ <%# DON'T %>
31
+ <%= javascript_packs_with_chunks_tag 'calendar' %>
32
+ <%= javascript_packs_with_chunks_tag 'map' %>
33
+ ```
34
+
3
35
  ## [4.0.0.rc.2] - 2018-12-15
4
36
 
5
37
  ### Fixed
data/Gemfile.lock CHANGED
@@ -14,7 +14,7 @@ GIT
14
14
  PATH
15
15
  remote: .
16
16
  specs:
17
- webpacker (4.0.0.rc.2)
17
+ webpacker (4.0.0.rc.3)
18
18
  activesupport (>= 4.2)
19
19
  rack-proxy (>= 0.6.1)
20
20
  railties (>= 4.2)
@@ -156,4 +156,4 @@ DEPENDENCIES
156
156
  webpacker!
157
157
 
158
158
  BUNDLED WITH
159
- 1.17.1
159
+ 1.17.3
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2016-2018 David Heinemeier Hansson, Basecamp
1
+ Copyright (c) 2016-2019 David Heinemeier Hansson, Basecamp
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -142,6 +142,31 @@ can use the `asset_pack_path` helper:
142
142
  <img src="<%= asset_pack_path 'images/logo.svg' %>" />
143
143
  ```
144
144
 
145
+ If you are using new webpack 4 split chunks API, then consider using `javascript_packs_with_chunks_tag` helper, which creates html
146
+ tags for a pack and all the dependent chunks.
147
+
148
+ ```erb
149
+ <%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
150
+
151
+ <script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
152
+ <script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
153
+ <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
154
+ <script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
155
+ <script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
156
+ ```
157
+
158
+ **Important:** Pass all your pack names when using `javascript_packs_with_chunks_tag`
159
+ helper otherwise you will get duplicated chunks on the page.
160
+
161
+ ```erb
162
+ <%# DO %>
163
+ <%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
164
+
165
+ <%# DON'T %>
166
+ <%= javascript_packs_with_chunks_tag 'calendar' %>
167
+ <%= javascript_packs_with_chunks_tag 'map' %>
168
+ ```
169
+
145
170
  **Note:** In order for your styles or static assets files to be available in your view,
146
171
  you would need to link them in your "pack" or entry file.
147
172
 
@@ -269,7 +294,7 @@ You can run following commands to upgrade Webpacker to the latest stable version
269
294
  bundle update webpacker
270
295
  rails webpacker:binstubs
271
296
  yarn upgrade @rails/webpacker --latest
272
- yarn add webpack-dev-server@^2.11.1
297
+ yarn add webpack-dev-server@^3.1.14
273
298
 
274
299
  # Or to install a latest release (including pre-releases)
275
300
  yarn add @rails/webpacker@next
data/docs/engines.md ADDED
@@ -0,0 +1,155 @@
1
+ # Using in Rails engines
2
+
3
+ If the application UI consists of multiple frontend application, you'd probably like to isolate their building too (e.g. if you use different frameworks/versions). Hence we needed our webpack(-er) to be isolated too: separate `package.json`, dev server, compilation process.
4
+
5
+ You can do this by adding another Webpacker instance to your application.
6
+
7
+ This guide describes how to do that using [Rails engines](https://guides.rubyonrails.org/engines.html).
8
+
9
+
10
+ ## Step 1: create Rails engine.
11
+
12
+ First, you create a Rails engine (say, `MyEngine`). See the offical [Rails guide](https://guides.rubyonrails.org/engines.html).
13
+
14
+ ## Step 2: install Webpacker within the engine.
15
+
16
+ There is no built-in tasks to install Webpacker within the engine, thus you have to add all the require files manually (you can copy them from the main app):
17
+ - Add `config/webpacker.yml` and `config/webpack/*.js` files
18
+ - Add `bin/webpack` and `bin/webpack-dev-server` files
19
+ - Add `package.json` with required deps.
20
+
21
+
22
+ ## Step 3: configure Webpacker instance.
23
+
24
+ ```ruby
25
+ module MyEngine
26
+ ROOT_PATH = Pathname.new(File.join(__dir__, ".."))
27
+
28
+ class << self
29
+ def webpacker
30
+ @webpacker ||= ::Webpacker::Instance.new(
31
+ root_path: ROOT_PATH,
32
+ config_path: ROOT_PATH.join("config/webpacker.yml")
33
+ )
34
+ end
35
+ end
36
+ end
37
+ ```
38
+
39
+ ## Step 4: Configure dev server proxy.
40
+
41
+ ```ruby
42
+ module MyEngine
43
+ class Engine < ::Rails::Engine
44
+ initializer "webpacker.proxy" do |app|
45
+ insert_middleware = begin
46
+ MyEngine.webpacker.config.dev_server.present?
47
+ rescue
48
+ nil
49
+ end
50
+ next unless insert_middleware
51
+
52
+ app.middleware.insert_before(
53
+ 0, Webpacker::DevServerProxy, # "Webpacker::DevServerProxy" if Rails version < 5
54
+ ssl_verify_none: true,
55
+ webpacker: MyEngine.webpacker
56
+ )
57
+ end
58
+ end
59
+ end
60
+ ```
61
+
62
+ If you have multiple webpackers, you would probably want to run multiple dev servers at a time, and hence be able to configure their setting through env vars (e.g. within a `docker-compose.yml` file):
63
+
64
+ ```yml
65
+ # webpacker.yml
66
+ # ...
67
+ development:
68
+ # ...
69
+ dev_server:
70
+ env_prefix: "MY_ENGINE_WEBPACKER_DEV_SERVER"
71
+ # ...
72
+ ```
73
+
74
+ ## Step 5: configure helper.
75
+
76
+ ```ruby
77
+ require "webpacker/helper"
78
+
79
+ module MyEngine
80
+ module ApplicationHelper
81
+ include ::Webpacker::Helper
82
+
83
+ def current_webpacker_instance
84
+ MyEngine.webpacker
85
+ end
86
+ end
87
+ end
88
+ ```
89
+
90
+ Now you can use `stylesheet_pack_tag` and `javascript_pack_tag` from within your engine.
91
+
92
+ ## Step 6: rake tasks.
93
+
94
+ Add Rake task to compile assets in production (`rake my_engine:webpacker:compile`)
95
+
96
+ ```ruby
97
+ namespace :my_engine do
98
+ namespace :webpacker do
99
+ desc "Install deps with yarn"
100
+ task :yarn_install do
101
+ Dir.chdir(File.join(__dir__, "../..")) do
102
+ system "yarn install --no-progress --production"
103
+ end
104
+ end
105
+
106
+ desc "Compile JavaScript packs using webpack for production with digests"
107
+ task compile: [:yarn_install, :environment] do
108
+ Webpacker.with_node_env("production") do
109
+ if MyEngine.webpacker.commands.compile
110
+ # Successful compilation!
111
+ else
112
+ # Failed compilation
113
+ exit!
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ ```
120
+
121
+ ## Step 7: serving compiled packs.
122
+
123
+ There are two approaches on serving compiled assets.
124
+
125
+ ### Put engine's assets to the root app's public/ folder
126
+
127
+ You can serve engine's assets using the main app's static files server which serves files from `public/` folder.
128
+
129
+ For that you must configure your engine's webpacker to put compiled assets to the app's `public/` folder:
130
+
131
+ ```yml
132
+ # my_engine/config/webpacker.yml
133
+ default: &default
134
+ # ...
135
+ # public_root_path could be used to override the path to `public/` folder
136
+ # (relative to the engine root)
137
+ public_root_path: ../public
138
+ # use a different sub-folder name
139
+ public_output_path: my-engine-packs
140
+ ```
141
+
142
+ ### Use a separate middleware
143
+
144
+ To serve static assets from the engine's `public/` folder you must add a middleware and point it to your engine's webpacker output path:
145
+
146
+ ```ruby
147
+ # application.rb
148
+
149
+ config.middleware.use(
150
+ "Rack::Static",
151
+ urls: ["/my-engine-packs"], root: "my_engine/public"
152
+ )
153
+ ```
154
+
155
+ **NOTE:** in the example above we assume that your `public_output_path` is set to `my-engine-packs` in your engine's `webpacker.yml`.
data/docs/webpack.md CHANGED
@@ -267,32 +267,36 @@ For the full configuration options of SplitChunks, see the [Webpack documentatio
267
267
  // config/webpack/environment.js
268
268
  const WebpackAssetsManifest = require('webpack-assets-manifest');
269
269
 
270
- const splitChunks = {
271
- optimization: {
272
- splitChunks: {
273
- chunks: 'all'
274
- },
275
- },
276
- };
277
-
278
- environment.config.merge(splitChunks);
279
-
280
- // Should override the existing manifest plugin
281
- environment.plugins.insert(
282
- 'Manifest',
283
- new WebpackAssetsManifest({
284
- entrypoints: true, // default in rails is false
285
- writeToDisk: true, // rails defaults copied from webpacker
286
- publicPath: true // rails defaults copied from webpacker
287
- })
288
- )
270
+ // Enable the default config
271
+ environment.splitChunks()
272
+
273
+ // or using custom config
274
+ environment.splitChunks((config) => Object.assign({}, config, { optimization: { splitChunks: false }}))
289
275
  ```
290
276
 
291
- To use the `javascript_pack_tag` or the `stylesheet_pack_tag` with `SplitChunks` or `RuntimeChunks` you can refer to the packs as usual.
277
+ Then use, `javascript_packs_with_chunks_tag` helper to include all the transpiled
278
+ packs with the chunks in your view, which creates html tags for all the chunks.
292
279
 
293
280
  ```erb
294
- javascript_pack_tag "your-entrypoint-javascript-file"
295
- stylesheet_pack_tag "your-entrypoint-stylesheet-file"
281
+ <%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %>
282
+
283
+ <script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
284
+ <script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
285
+ <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
286
+ <script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
287
+ <script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
288
+ ```
289
+
290
+ **Important:** Pass all your pack names when using this helper otherwise you will
291
+ get duplicated chunks on the page.
292
+
293
+ ```erb
294
+ <%# DO %>
295
+ <%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
296
+
297
+ <%# DON'T %>
298
+ <%= javascript_packs_with_chunks_tag 'calendar' %>
299
+ <%= javascript_packs_with_chunks_tag 'map' %>
296
300
  ```
297
301
 
298
302
  For the old configuration with the CommonsChunkPlugin see below. **Note** that this functionality is deprecated in Webpack V4.
data/docs/yarn.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Yarn
2
2
 
3
- Webpacker by default uses `yarn` as package manager for node modules
3
+ Webpacker by default uses `yarn` as a package manager for `node_modules`
4
4
 
5
5
 
6
6
  ## Add a new npm module
@@ -10,3 +10,14 @@ To add any new JS module you can use `yarn`:
10
10
  ```bash
11
11
  yarn add bootstrap material-ui
12
12
  ```
13
+
14
+ ## Add an npm module to `devDependencies`
15
+ To add a new JS module that will only be available to local development:
16
+
17
+ ```bash
18
+ yarn add --dev browser-sync
19
+ ```
20
+
21
+ Be careful not to add any build or app related JS modules in this fashion. Adding JS modules to `devDependencies` [will block them from being installed in **any** production environment](https://yarnpkg.com/lang/en/docs/cli/install/#toc-yarn-install-production-true-false).
22
+
23
+ Docs from JS modules may instruct you to use `--dev` or `devDependencies`, but this is generally under the assumption that you are using a `node.js` workflow.
@@ -3,6 +3,7 @@
3
3
  default: &default
4
4
  source_path: app/javascript
5
5
  source_entry_path: packs
6
+ public_root_path: public
6
7
  public_output_path: packs
7
8
  cache_path: tmp/cache/webpacker
8
9
  check_yarn_integrity: false
@@ -71,7 +72,7 @@ development:
71
72
  headers:
72
73
  'Access-Control-Allow-Origin': '*'
73
74
  watch_options:
74
- ignored: /node_modules/
75
+ ignored: '**/node_modules/**'
75
76
 
76
77
 
77
78
  test:
@@ -10,7 +10,7 @@ module.exports = function(api) {
10
10
  'Please specify a valid `NODE_ENV` or ' +
11
11
  '`BABEL_ENV` environment variables. Valid values are "development", ' +
12
12
  '"test", and "production". Instead, received: ' +
13
- JSON.stringify(env) +
13
+ JSON.stringify(currentEnv) +
14
14
  '.'
15
15
  )
16
16
  }
@@ -10,7 +10,7 @@ class Webpacker::Compiler
10
10
  # Webpacker::Compiler.env['FRONTEND_API_KEY'] = 'your_secret_key'
11
11
  cattr_accessor(:env) { {} }
12
12
 
13
- delegate :config, :logger, to: :@webpacker
13
+ delegate :config, :logger, to: :webpacker
14
14
 
15
15
  def initialize(webpacker)
16
16
  @webpacker = webpacker
@@ -37,6 +37,8 @@ class Webpacker::Compiler
37
37
  end
38
38
 
39
39
  private
40
+ attr_reader :webpacker
41
+
40
42
  def last_compilation_digest
41
43
  compilation_digest_path.read if compilation_digest_path.exist? && config.public_manifest_path.exist?
42
44
  rescue Errno::ENOENT, Errno::ENOTDIR
@@ -56,7 +58,11 @@ class Webpacker::Compiler
56
58
  def run_webpack
57
59
  logger.info "Compiling…"
58
60
 
59
- stdout, sterr , status = Open3.capture3(webpack_env, "#{RbConfig.ruby} #{@webpacker.root_path}/bin/webpack")
61
+ stdout, sterr , status = Open3.capture3(
62
+ webpack_env,
63
+ "#{RbConfig.ruby} ./bin/webpack",
64
+ chdir: File.expand_path(config.root_path)
65
+ )
60
66
 
61
67
  if sterr == "" && status.success?
62
68
  logger.info "Compiled all packs in #{config.public_output_path}"
@@ -71,17 +77,19 @@ class Webpacker::Compiler
71
77
  def default_watched_paths
72
78
  [
73
79
  *config.resolved_paths_globbed,
74
- "#{config.source_path.relative_path_from(Rails.root)}/**/*",
80
+ "#{config.source_path.relative_path_from(config.root_path)}/**/*",
75
81
  "yarn.lock", "package.json",
76
82
  "config/webpack/**/*"
77
83
  ].freeze
78
84
  end
79
85
 
80
86
  def compilation_digest_path
81
- config.cache_path.join("last-compilation-digest-#{Webpacker.env}")
87
+ config.cache_path.join("last-compilation-digest-#{webpacker.env}")
82
88
  end
83
89
 
84
90
  def webpack_env
91
+ return env unless defined?(ActionController::Base)
92
+
85
93
  env.merge("WEBPACKER_ASSET_HOST" => ActionController::Base.helpers.compute_asset_host,
86
94
  "WEBPACKER_RELATIVE_URL_ROOT" => ActionController::Base.relative_url_root)
87
95
  end
@@ -36,7 +36,7 @@ class Webpacker::Configuration
36
36
  end
37
37
 
38
38
  def public_path
39
- root_path.join("public")
39
+ root_path.join(fetch(:public_root_path))
40
40
  end
41
41
 
42
42
  def public_output_path
@@ -1,4 +1,6 @@
1
1
  class Webpacker::DevServer
2
+ DEFAULT_ENV_PREFIX = "WEBPACKER_DEV_SERVER".freeze
3
+
2
4
  # Configure dev server connection timeout (in seconds), default: 0.01
3
5
  # Webpacker.dev_server.connect_timeout = 1
4
6
  cattr_accessor(:connect_timeout) { 0.01 }
@@ -49,9 +51,13 @@ class Webpacker::DevServer
49
51
  fetch(:pretty)
50
52
  end
51
53
 
54
+ def env_prefix
55
+ config.dev_server.fetch(:env_prefix, DEFAULT_ENV_PREFIX)
56
+ end
57
+
52
58
  private
53
59
  def fetch(key)
54
- ENV["WEBPACKER_DEV_SERVER_#{key.upcase}"] || config.dev_server.fetch(key, defaults[key])
60
+ ENV["#{env_prefix}_#{key.upcase}"] || config.dev_server.fetch(key, defaults[key])
55
61
  end
56
62
 
57
63
  def defaults
@@ -1,18 +1,25 @@
1
1
  require "rack/proxy"
2
2
 
3
3
  class Webpacker::DevServerProxy < Rack::Proxy
4
+ delegate :config, :dev_server, to: :@webpacker
5
+
6
+ def initialize(app = nil, opts = {})
7
+ @webpacker = opts.delete(:webpacker) || Webpacker.instance
8
+ super
9
+ end
10
+
4
11
  def rewrite_response(response)
5
12
  _status, headers, _body = response
6
13
  headers.delete "transfer-encoding"
7
- headers.delete "content-length" if Webpacker.dev_server.running? && Webpacker.dev_server.https?
14
+ headers.delete "content-length" if dev_server.running? && dev_server.https?
8
15
  response
9
16
  end
10
17
 
11
18
  def perform_request(env)
12
- if env["PATH_INFO"].start_with?("/#{public_output_uri_path}") && Webpacker.dev_server.running?
13
- env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = env["HTTP_X_FORWARDED_SERVER"] = Webpacker.dev_server.host_with_port
14
- env["HTTP_X_FORWARDED_PROTO"] = env["HTTP_X_FORWARDED_SCHEME"] = Webpacker.dev_server.protocol
15
- unless Webpacker.dev_server.https?
19
+ if env["PATH_INFO"].start_with?("/#{public_output_uri_path}") && dev_server.running?
20
+ env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = env["HTTP_X_FORWARDED_SERVER"] = dev_server.host_with_port
21
+ env["HTTP_X_FORWARDED_PROTO"] = env["HTTP_X_FORWARDED_SCHEME"] = dev_server.protocol
22
+ unless dev_server.https?
16
23
  env["HTTPS"] = env["HTTP_X_FORWARDED_SSL"] = "off"
17
24
  end
18
25
  env["SCRIPT_NAME"] = ""
@@ -25,6 +32,6 @@ class Webpacker::DevServerProxy < Rack::Proxy
25
32
 
26
33
  private
27
34
  def public_output_uri_path
28
- Webpacker.config.public_output_path.relative_path_from(Webpacker.config.public_path)
35
+ config.public_output_path.relative_path_from(config.public_path)
29
36
  end
30
37
  end
@@ -1,6 +1,13 @@
1
1
  module Webpacker::Helper
2
+ # Returns current Webpacker instance.
3
+ # Could be overriden to use multiple Webpacker
4
+ # configurations within the same app (e.g. with engines)
5
+ def current_webpacker_instance
6
+ Webpacker.instance
7
+ end
8
+
2
9
  # Computes the relative path for a given Webpacker asset.
3
- # Return relative path using manifest.json and passes it to asset_path helper
10
+ # Return relative path using manifest.json and passes it to asset_path helper.
4
11
  # This will use asset_path internally, so most of their behaviors will be the same.
5
12
  #
6
13
  # Example:
@@ -11,13 +18,13 @@ module Webpacker::Helper
11
18
  # # When extract_css is true in webpacker.yml or the file is not a css:
12
19
  # <%= asset_pack_path 'calendar.css' %> # => "/packs/calendar-1016838bab065ae1e122.css"
13
20
  def asset_pack_path(name, **options)
14
- if Webpacker.config.extract_css? || !stylesheet?(name)
15
- asset_path(Webpacker.manifest.lookup!(name), **options)
21
+ if current_webpacker_instance.config.extract_css? || !stylesheet?(name)
22
+ asset_path(current_webpacker_instance.manifest.lookup!(name), **options)
16
23
  end
17
24
  end
18
25
 
19
26
  # Computes the absolute path for a given Webpacker asset.
20
- # Return absolute path using manifest.json and passes it to asset_url helper
27
+ # Return absolute path using manifest.json and passes it to asset_url helper.
21
28
  # This will use asset_url internally, so most of their behaviors will be the same.
22
29
  #
23
30
  # Example:
@@ -28,8 +35,8 @@ module Webpacker::Helper
28
35
  # # When extract_css is true in webpacker.yml or the file is not a css:
29
36
  # <%= asset_pack_url 'calendar.css' %> # => "http://example.com/packs/calendar-1016838bab065ae1e122.css"
30
37
  def asset_pack_url(name, **options)
31
- if Webpacker.config.extract_css? || !stylesheet?(name)
32
- asset_url(Webpacker.manifest.lookup!(name), **options)
38
+ if current_webpacker_instance.config.extract_css? || !stylesheet?(name)
39
+ asset_url(current_webpacker_instance.manifest.lookup!(name), **options)
33
40
  end
34
41
  end
35
42
 
@@ -40,7 +47,7 @@ module Webpacker::Helper
40
47
  # <%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
41
48
  # <img alt='Edit Entry' src='/packs/application-k344a6d59eef8632c9d1.png' width='16' height='10' />
42
49
  def image_pack_tag(name, **options)
43
- image_tag(asset_path(Webpacker.manifest.lookup!(name)), **options)
50
+ image_tag(asset_path(current_webpacker_instance.manifest.lookup!(name)), **options)
44
51
  end
45
52
 
46
53
  # Creates a script tag that references the named pack file, as compiled by webpack per the entries list
@@ -52,7 +59,29 @@ module Webpacker::Helper
52
59
  # <%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
53
60
  # <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
54
61
  def javascript_pack_tag(*names, **options)
55
- javascript_include_tag(*sources_from_pack_manifest(names, type: :javascript), **options)
62
+ javascript_include_tag(*sources_from_manifest_entries(names, type: :javascript), **options)
63
+ end
64
+
65
+ # Creates script tags that references the chunks from entrypoints when using split chunks API,
66
+ # as compiled by webpack per the entries list in config/webpack/shared.js.
67
+ # By default, this list is auto-generated to match everything in
68
+ # app/javascript/packs/*.js and all the dependent chunks. In production mode, the digested reference is automatically looked up.
69
+ # See: https://webpack.js.org/plugins/split-chunks-plugin/
70
+ # Example:
71
+ #
72
+ # <%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %> # =>
73
+ # <script src="/packs/vendor-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
74
+ # <script src="/packs/calendar~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
75
+ # <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
76
+ # <script src="/packs/map~runtime-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
77
+ # <script src="/packs/map-16838bab065ae1e314.js" data-turbolinks-track="reload"></script>
78
+ # DO:
79
+ # <%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
80
+ # DON'T:
81
+ # <%= javascript_packs_with_chunks_tag 'calendar' %>
82
+ # <%= javascript_packs_with_chunks_tag 'map' %>
83
+ def javascript_packs_with_chunks_tag(*names, **options)
84
+ javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), **options)
56
85
  end
57
86
 
58
87
  # Creates a link tag that references the named pack file, as compiled by webpack per the entries list
@@ -72,8 +101,8 @@ module Webpacker::Helper
72
101
  # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
73
102
  # <link rel="stylesheet" media="screen" href="/packs/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />
74
103
  def stylesheet_pack_tag(*names, **options)
75
- if Webpacker.config.extract_css?
76
- stylesheet_link_tag(*sources_from_pack_manifest(names, type: :stylesheet), **options)
104
+ if current_webpacker_instance.config.extract_css?
105
+ stylesheet_link_tag(*sources_from_manifest_entries(names, type: :stylesheet), **options)
77
106
  end
78
107
  end
79
108
 
@@ -82,7 +111,11 @@ module Webpacker::Helper
82
111
  File.extname(name) == ".css"
83
112
  end
84
113
 
85
- def sources_from_pack_manifest(names, type:)
86
- names.map { |name| Webpacker.manifest.lookup!(name, type: type) }.flatten
114
+ def sources_from_manifest_entries(names, type:)
115
+ names.map { |name| current_webpacker_instance.manifest.lookup!(name, type: type) }.flatten
116
+ end
117
+
118
+ def sources_from_manifest_entrypoints(names, type:)
119
+ names.map { |name| current_webpacker_instance.manifest.lookup_pack_with_chunks!(name, type: type) }.flatten.uniq
87
120
  end
88
121
  end
@@ -18,27 +18,33 @@ class Webpacker::Manifest
18
18
  @data = load
19
19
  end
20
20
 
21
+ def lookup_pack_with_chunks(name, pack_type = {})
22
+ compile if compiling?
23
+
24
+ manifest_pack_type = manifest_type(pack_type[:type])
25
+ manifest_pack_name = manifest_name(name, manifest_pack_type)
26
+ find("entrypoints")[manifest_pack_name][manifest_pack_type]
27
+ rescue NoMethodError
28
+ nil
29
+ end
30
+
31
+ def lookup_pack_with_chunks!(name, pack_type = {})
32
+ lookup_pack_with_chunks(name, pack_type) || handle_missing_entry(name)
33
+ end
34
+
35
+ # Computes the relative path for a given Webpacker asset using manifest.json.
36
+ # If no asset is found, returns nil.
37
+ #
38
+ # Example:
39
+ #
40
+ # Webpacker.manifest.lookup('calendar.js') # => "/packs/calendar-1016838bab065ae1e122.js"
21
41
  def lookup(name, pack_type = {})
22
42
  compile if compiling?
23
43
 
24
- # When using SplitChunks or RuntimeChunks the manifest hash will contain
25
- # an extra object called "entrypoints". When the entrypoints key is not
26
- # present in the manifest, or the name is not found in the entrypoints hash,
27
- # it will raise a NoMethodError. If this happens, we should try to lookup
28
- # a single instance of the pack based on the given name.
29
- begin
30
- manifest_pack_type = manifest_type(pack_type[:type])
31
- manifest_pack_name = manifest_name(name, manifest_pack_type)
32
-
33
- # Lookup the pack in the entrypoints of the manifest
34
- find("entrypoints")[manifest_pack_name][manifest_pack_type]
35
- rescue NoMethodError
36
-
37
- # Lookup a single instance of the pack
38
- find(full_pack_name(name, pack_type[:type]))
39
- end
44
+ find(full_pack_name(name, pack_type[:type]))
40
45
  end
41
46
 
47
+ # Like lookup, except that if no asset is found, raises a Webpacker::Manifest::MissingEntryError.
42
48
  def lookup!(name, pack_type = {})
43
49
  lookup(name, pack_type) || handle_missing_entry(name)
44
50
  end
@@ -1,4 +1,4 @@
1
1
  module Webpacker
2
2
  # Change the version in package.json too, please!
3
- VERSION = "4.0.0.rc.2".freeze
3
+ VERSION = "4.0.0.rc.3".freeze
4
4
  end
@@ -20,6 +20,21 @@ describe('DevServer', () => {
20
20
  expect(devServer.port).toEqual('5000')
21
21
  })
22
22
 
23
+ test('with custom env prefix', () => {
24
+ const config = require('../config')
25
+ config.dev_server.env_prefix = 'TEST_WEBPACKER_DEV_SERVER_'
26
+
27
+ process.env.NODE_ENV = 'development'
28
+ process.env.RAILS_ENV = 'development'
29
+ process.env.TEST_WEBPACKER_DEV_SERVER_HOST = '0.0.0.0'
30
+ process.env.TEST_WEBPACKER_DEV_SERVER_PORT = 5000
31
+
32
+ const devServer = require('../dev_server')
33
+ expect(devServer).toBeDefined()
34
+ expect(devServer.host).toEqual('0.0.0.0')
35
+ expect(devServer.port).toEqual('5000')
36
+ })
37
+
23
38
  test('with NODE_ENV and RAILS_ENV set to production', () => {
24
39
  process.env.RAILS_ENV = 'production'
25
40
  process.env.NODE_ENV = 'production'
data/package/config.js CHANGED
@@ -19,7 +19,7 @@ const app = safeLoad(readFileSync(configPath), 'utf8')[railsEnv]
19
19
  if (isArray(app.extensions) && app.extensions.length) delete defaults.extensions
20
20
 
21
21
  const config = deepMerge(defaults, app)
22
- config.outputPath = resolve('public', config.public_output_path)
22
+ config.outputPath = resolve(config.public_root_path, config.public_output_path)
23
23
 
24
24
  // Ensure that the publicPath includes our asset host so dynamic imports
25
25
  // (code-splitting chunks and static assets) load from the CDN instead of a relative path.
@@ -9,8 +9,10 @@ const fetch = (key) => {
9
9
  const devServerConfig = config.dev_server
10
10
 
11
11
  if (devServerConfig) {
12
+ const envPrefix = config.dev_server.env_prefix || 'WEBPACKER_DEV_SERVER_'
13
+
12
14
  Object.keys(devServerConfig).forEach((key) => {
13
- const envValue = fetch(`WEBPACKER_DEV_SERVER_${key.toUpperCase().replace(/_/g, '')}`)
15
+ const envValue = fetch(`${envPrefix}${key.toUpperCase().replace(/_/g, '')}`)
14
16
  if (envValue !== undefined) devServerConfig[key] = envValue
15
17
  })
16
18
  }
@@ -9,7 +9,6 @@ module.exports = class extends Base {
9
9
 
10
10
  if (devServer.hmr) {
11
11
  this.plugins.append('HotModuleReplacement', new webpack.HotModuleReplacementPlugin())
12
- this.plugins.append('NamedModules', new webpack.NamedModulesPlugin())
13
12
  this.config.output.filename = '[name]-[hash].js'
14
13
  }
15
14
 
@@ -5,6 +5,7 @@ const { nodeEnv } = require('../env')
5
5
  // Compile standard ES features for JS in node_modules with Babel.
6
6
  module.exports = {
7
7
  test: /\.(js|mjs)$/,
8
+ include: /node_modules/,
8
9
  exclude: /@babel(?:\/|\\{1,2})runtime/,
9
10
  use: [
10
11
  {
@@ -1,3 +1,5 @@
1
+ const { stringify } = require('flatted/cjs')
2
+
1
3
  const isObject = value => typeof value === 'object'
2
4
  && value !== null
3
5
  && (value.length === undefined || value.length === null)
@@ -17,7 +19,7 @@ const isStrPath = (key) => {
17
19
 
18
20
  const isArray = value => Array.isArray(value)
19
21
 
20
- const isEqual = (target, source) => JSON.stringify(target) === JSON.stringify(source)
22
+ const isEqual = (target, source) => stringify(target) === stringify(source)
21
23
 
22
24
  const canMerge = value => isObject(value) || isArray(value)
23
25
 
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rails/webpacker",
3
- "version": "4.0.0-rc.2",
3
+ "version": "4.0.0-rc.3",
4
4
  "description": "Use webpack to manage app-like JavaScript modules in Rails",
5
5
  "main": "package/index.js",
6
6
  "files": [
@@ -29,6 +29,7 @@
29
29
  "compression-webpack-plugin": "^2.0.0",
30
30
  "css-loader": "^2.0.1",
31
31
  "file-loader": "^2.0.0",
32
+ "flatted": "^2.0.0",
32
33
  "glob": "^7.1.3",
33
34
  "js-yaml": "^3.12.0",
34
35
  "mini-css-extract-plugin": "^0.5.0",
@@ -22,6 +22,15 @@ class ConfigurationTest < Webpacker::Test
22
22
  def test_public_output_path
23
23
  public_output_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs").to_s
24
24
  assert_equal @config.public_output_path.to_s, public_output_path
25
+
26
+ @config = Webpacker::Configuration.new(
27
+ root_path: @config.root_path,
28
+ config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_public_root.yml", __dir__)),
29
+ env: "production"
30
+ )
31
+
32
+ public_output_path = File.expand_path File.join(File.dirname(__FILE__), "public/packs").to_s
33
+ assert_equal @config.public_output_path.to_s, public_output_path
25
34
  end
26
35
 
27
36
  def test_public_manifest_path
data/test/helper_test.rb CHANGED
@@ -64,7 +64,7 @@ class HelperTest < ActionView::TestCase
64
64
  %(<script src="/packs/vendors~application~bootstrap-c20632e7baf2c81200d3.chunk.js"></script>\n) +
65
65
  %(<script src="/packs/vendors~application-e55f2aae30c07fb6d82a.chunk.js"></script>\n) +
66
66
  %(<script src="/packs/application-k344a6d59eef8632c9d1.js"></script>),
67
- javascript_pack_tag("application")
67
+ javascript_packs_with_chunks_tag("application")
68
68
  end
69
69
 
70
70
  def test_stylesheet_pack_tag
@@ -22,6 +22,10 @@ class ManifestTest < Minitest::Test
22
22
  assert_nil Webpacker.manifest.lookup("foo.js")
23
23
  end
24
24
 
25
+ def test_lookup_chunks_nil
26
+ assert_nil Webpacker.manifest.lookup_pack_with_chunks("foo.js")
27
+ end
28
+
25
29
  def test_lookup_success
26
30
  assert_equal Webpacker.manifest.lookup("bootstrap.js"), "/packs/bootstrap-300631c4f0e0f9c865bc.js"
27
31
  end
@@ -33,6 +37,6 @@ class ManifestTest < Minitest::Test
33
37
  "/packs/application-k344a6d59eef8632c9d1.js"
34
38
  ]
35
39
 
36
- assert_equal Webpacker.manifest.lookup!("application", type: :javascript), application_entrypoints
40
+ assert_equal Webpacker.manifest.lookup_pack_with_chunks!("application", type: :javascript), application_entrypoints
37
41
  end
38
42
  end
@@ -3,6 +3,7 @@
3
3
  default: &default
4
4
  source_path: app/javascript
5
5
  source_entry_path: packs
6
+ public_root_path: public
6
7
  public_output_path: packs
7
8
  cache_path: tmp/cache/webpacker
8
9
  webpack_compile_output: false
@@ -0,0 +1,19 @@
1
+ # Note: You must restart bin/webpack-dev-server for changes to take effect
2
+
3
+ default: &default
4
+ public_root_path: ../public
5
+
6
+ development:
7
+ <<: *default
8
+ compile: true
9
+
10
+ test:
11
+ <<: *default
12
+ compile: true
13
+ public_output_path: packs-test
14
+
15
+ production:
16
+ <<: *default
17
+ compile: false
18
+ extract_css: true
19
+ cache_manifest: true
data/yarn.lock CHANGED
@@ -3053,6 +3053,11 @@ flat-cache@^1.2.1:
3053
3053
  rimraf "~2.6.2"
3054
3054
  write "^0.2.1"
3055
3055
 
3056
+ flatted@^2.0.0:
3057
+ version "2.0.0"
3058
+ resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.0.tgz#55122b6536ea496b4b44893ee2608141d10d9916"
3059
+ integrity sha512-R+H8IZclI8AAkSBRQJLVOsxwAoHd6WC40b4QTNWIjzAa6BXOBfQcM587MXDTVPeYaopFNWHUFLx7eNmHDSxMWg==
3060
+
3056
3061
  flatten@^1.0.2:
3057
3062
  version "1.0.2"
3058
3063
  resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpacker
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0.rc.2
4
+ version: 4.0.0.rc.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-12-15 00:00:00.000000000 Z
12
+ date: 2019-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -93,6 +93,7 @@ files:
93
93
  - docs/css.md
94
94
  - docs/deployment.md
95
95
  - docs/docker.md
96
+ - docs/engines.md
96
97
  - docs/env.md
97
98
  - docs/es6.md
98
99
  - docs/folder-structure.md
@@ -238,6 +239,7 @@ files:
238
239
  - test/test_app/config/environment.rb
239
240
  - test/test_app/config/webpack/development.js
240
241
  - test/test_app/config/webpacker.yml
242
+ - test/test_app/config/webpacker_public_root.yml
241
243
  - test/test_app/package.json
242
244
  - test/test_app/public/packs/manifest.json
243
245
  - test/test_app/yarn.lock
@@ -250,8 +252,8 @@ homepage: https://github.com/rails/webpacker
250
252
  licenses:
251
253
  - MIT
252
254
  metadata:
253
- source_code_uri: https://github.com/rails/webpacker/tree/v4.0.0.rc.2
254
- changelog_uri: https://github.com/rails/webpacker/blob/v4.0.0.rc.2/CHANGELOG.md
255
+ source_code_uri: https://github.com/rails/webpacker/tree/v4.0.0.rc.3
256
+ changelog_uri: https://github.com/rails/webpacker/blob/v4.0.0.rc.3/CHANGELOG.md
255
257
  post_install_message:
256
258
  rdoc_options: []
257
259
  require_paths:
@@ -291,6 +293,7 @@ test_files:
291
293
  - test/test_app/config/environment.rb
292
294
  - test/test_app/config/webpack/development.js
293
295
  - test/test_app/config/webpacker.yml
296
+ - test/test_app/config/webpacker_public_root.yml
294
297
  - test/test_app/package.json
295
298
  - test/test_app/public/packs/manifest.json
296
299
  - test/test_app/yarn.lock