Skip to content

Commit 5de0fbc

Browse files
author
David Heinemeier Hansson
authored
Follow app/javascript/application.js convention from Rails 7 (#3156)
* Follow app/javascript/application.js convention from Rails 7 So it remains compatible with other bundler setups, like esbuild-rails. * Drop the idea of default dependencies We can add them via Rails 7 installers * Ensure entrypoints are only available at the top level * Cut words * Remove test * Use Ruby target for Rails 7 * Match Rails 7 requirement of minimum Ruby version 2.7
1 parent 9a4aa0f commit 5de0fbc

File tree

10 files changed

+23
-40
lines changed

10 files changed

+23
-40
lines changed

.github/workflows/ruby.yml

-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ jobs:
1616
os: [ubuntu-latest]
1717
node: [14]
1818
ruby: [
19-
2.4,
20-
2.5,
21-
2.6,
2219
2.7,
2320
3.0
2421
]

README.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Webpacker makes it easy to use the JavaScript pre-processor and bundler
1212
[Webpack v5](https://webpack.js.org/)
1313
to manage application-like JavaScript in Rails. It can coexist with the asset pipeline,
14-
leaving Webpack responsible solely for app-like JavaScript, or it can be used exclusively, making it also responsible for images, fronts, and CSS as well.
14+
leaving Webpack responsible solely for app-like JavaScript, or it can be used exclusively, making it also responsible for images, fronts, and CSS.
1515

1616
**NOTE:** The master branch now hosts the code for v6.x.x. Please refer to [5-x-stable](https://github.com/rails/webpacker/tree/5-x-stable) branch for 5.x documentation.
1717

@@ -78,10 +78,10 @@ leaving Webpack responsible solely for app-like JavaScript, or it can be used ex
7878

7979
## Installation
8080

81-
You can configure a new Rails application with Webpacker right from the start using the `--webpack` option:
81+
You can configure a new Rails application with Webpacker right from the start using the `-j webpack` option:
8282

8383
```bash
84-
rails new myapp --webpack
84+
rails new myapp -j webpack
8585
```
8686

8787
Or you can add it later by changing your `Gemfile`:
@@ -113,11 +113,10 @@ yarn install
113113
Once installed, you can start writing modern ES6-flavored JavaScript apps right away:
114114

115115
```yml
116-
app/packs:
117-
├── entrypoints:
118-
# Only Webpack entry files here
119-
│ └── application.js
120-
│ └── application.css
116+
app/javascript:
117+
# Only Webpack entry files here
118+
└── application.js
119+
└── application.css
121120
└── src:
122121
│ └── my_component.js
123122
└── stylesheets:

docs/v6_upgrade.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ This means you have to configure integration with frameworks yourself, but webpa
1212

1313
## How to upgrade to Webpacker 6
1414

15-
1. If your `source_path` is `app/javascript`, rename it to `app/packs`
16-
2. If your `source_entry_path` is `packs`, rename it to `entrypoints`
17-
3. Rename `config/webpack` to `config/webpack_old`
18-
4. Rename `config/webpacker.yml` to `config/webpacker_old.yml`
19-
5. Uninstall the current version of `webpack-dev-server`: `yarn remove webpack-dev-server`
20-
6. Remove .browserslistrc from the root of your Rails app
21-
7. Upgrade the Webpacker Ruby gem and NPM package
15+
1. Move your `app/javascript/packs/application.js` to `app/javascript/application.js`
16+
2. Rename `config/webpack` to `config/webpack_old`
17+
3. Rename `config/webpacker.yml` to `config/webpacker_old.yml`
18+
4. Uninstall the current version of `webpack-dev-server`: `yarn remove webpack-dev-server`
19+
5. Remove .browserslistrc from the root of your Rails app
20+
6. Upgrade the Webpacker Ruby gem and NPM package
2221

2322
Note: [Check the releases page to verify the latest version](https://github.com/rails/webpacker/releases), and make sure to install identical version numbers of webpacker gem and `@rails/webpacker` npm package. (Gems use a period and packages use a dot between the main version number and the beta version.)
2423

@@ -41,7 +40,7 @@ Example going to a specific version:
4140
bundle exec rails webpacker:install
4241
```
4342

44-
7. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` or `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932). If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/packs/entrypoints/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
43+
7. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` or `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932). If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/javascript/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
4544
8. If you are using any integrations like `css`, `React` or `TypeScript`. Please see https://github.com/rails/webpacker#integrations section on how they work in v6.
4645
9. Copy over any custom webpack config from `config/webpack_old`. Common code previously called 'environment' should be changed to 'base', and import `environment` changed to `webpackConfig`.
4746

Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
/* eslint no-console:0 */
22
// This file is automatically compiled by Webpack, along with any other files
33
// present in this directory. You're encouraged to place your actual application logic in
4-
// a relevant structure within app/packs and only use these pack files to reference
4+
// a relevant structure within app/javascript and only use these pack files to reference
55
// that code so it'll be compiled.
66
//
77
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
88
// layout file, like app/views/layouts/application.html.erb
99

10-
// Uncomment to copy all static images under ../images to the output folder and reference
10+
// Uncomment to copy all static images under ./images to the output folder and reference
1111
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
1212
// or the `imagePath` JavaScript helper below.
1313
//
14-
// const images = require.context('../images', true)
14+
// const images = require.context('./images', true)
1515
// const imagePath = (name) => images(name, true)
16-
17-
// Activate Active Storage
18-
// import * as ActiveStorage from "@rails/activestorage"
19-
// ActiveStorage.start()

lib/install/config/webpacker.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Note: You must restart bin/webpack-dev-server for changes to take effect
22

33
default: &default
4-
source_path: app/packs
5-
source_entry_path: entrypoints
4+
source_path: app/javascript
5+
source_entry_path: /
66
public_root_path: public
77
public_output_path: packs
88
cache_path: tmp/webpacker

lib/install/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
"name": "app",
33
"private": true,
44
"dependencies": {
5-
"@rails/activestorage": "latest",
6-
"@rails/actioncable": "latest"
75
},
86
"version": "0.1.0",
97
"babel": {

lib/install/template.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
say "The packs app source directory already exists"
1010
else
1111
say "Creating packs app source directory"
12-
directory "#{__dir__}/packs", Webpacker.config.source_path
12+
empty_directory "app/javascript"
13+
copy_file "#{__dir__}/application.js", "app/javascript/application.js"
1314
end
1415

1516
apply "#{__dir__}/binstubs.rb"

package/__tests__/development.js

-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ describe('Development environment', () => {
1919

2020
expect(webpackConfig.output.path).toEqual(resolve('public', 'packs'))
2121
expect(webpackConfig.output.publicPath).toEqual('/packs/')
22-
expect(webpackConfig).toMatchObject({
23-
devServer: {
24-
host: 'localhost',
25-
port: 3035,
26-
hot: false
27-
}
28-
})
2922
})
3023

3124
test('should use development config and environment if WEBPACK_SERVE', () => {

package/environments/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const getEntryObject = () => {
1616
const entries = {}
1717
const rootPath = join(config.source_path, config.source_entry_path)
1818

19-
globSync(`${rootPath}/**/*.*`).forEach((path) => {
19+
globSync(`${rootPath}/*.*`).forEach((path) => {
2020
const namespace = relative(join(rootPath), dirname(path))
2121
const name = join(namespace, basename(path, extname(path)))
2222
let assetPaths = resolve(path)

webpacker.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
1515
"changelog_uri" => "https://github.com/rails/webpacker/blob/v#{Webpacker::VERSION}/CHANGELOG.md"
1616
}
1717

18-
s.required_ruby_version = ">= 2.4.0"
18+
s.required_ruby_version = ">= 2.7.0"
1919

2020
s.add_dependency "activesupport", ">= 5.2"
2121
s.add_dependency "railties", ">= 5.2"

0 commit comments

Comments
 (0)