1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/guides/source/webpacker.md

224 lines
16 KiB
Markdown
Raw Normal View History

Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
Webpacker
=========
This guide will show you how to install and use Webpacker to package JavaScript, CSS, and other assets for the client-side of your Rails application.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
After reading this guide, you will know:
* What Webpacker does and why it is different from Sprockets.
* How to install Webpacker and integrate it with your framework of choice.
* How to use Webpacker for JavaScript assets.
* How to use Webpacker for CSS assets.
* How to use Webpacker for static assets.
* How to deploy a site that uses Webpacker.
* How to use Webpacker in alternate Rails contexts, such as engines or Docker containers.
--------------------------------------------------------------
What Is Webpacker?
------------------
Webpacker is a Rails wrapper around the [webpack](https://webpack.js.org) build system that provides a standard webpack configuration and reasonable defaults.
### What is webpack?
The goal of webpack, or any front-end build system, is to allow you to write your front-end code in a way that is convenient for developers and then package that code in a way that is convenient for browsers. With webpack, you can manage JavaScript, CSS, and static assets like images or fonts. Webpack will allow you to write your code, reference other code in your application, transform your code, and combine your code into easily downloadable packs.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
See the [webpack documentation](https://webpack.js.org) for information.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
### How is Webpacker Different from Sprockets?
2021-01-18 10:14:53 -05:00
Rails also ships with Sprockets, an asset-packaging tool whose features overlap with Webpacker. Both tools will compile your JavaScript into browser-friendly files, and minify and fingerprint them in production. Both tools allow you to incrementally change files in development.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
Sprockets, which was designed to be used with Rails, is somewhat simpler to integrate. In particular, code can be added to Sprockets via a Ruby gem. However, webpack is better at integrating with more current JavaScript tools and NPM packages, and allows for a wider range of integration. New Rails apps are configured to use webpack for JavaScript and Sprockets for CSS, although you can do CSS in webpack.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
You should choose webpacker over Sprockets on a new project if you want to use NPM packages, and if you want access to the most current JavaScript features and tools. You should choose Sprockets over Webpacker for legacy applications where migration might be costly, if you want to integrate using Gems, or if you have a very small amount of code to package.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
If you are familiar with Sprockets, the following guide might give you some idea of how to translate. Please note that each tool has a slightly different structure, and the concepts don't directly map onto each other.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
|Task | Sprockets | Webpacker |
|------------------|----------------------|-------------------|
|Attach JavaScript |javascript_include_tag|javascript_pack_tag|
|Attach CSS |stylesheet_link_tag |stylesheet_pack_tag|
|Link to an image |image_url |image_pack_tag |
|Link to an asset |asset_url |asset_pack_tag |
|Require a script |//= require |import or require |
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
Installing Webpacker
--------------------
To use Webpacker, you must install the Yarn package manager, version 1.x or up, and you must have Node.js installed, version 10.13.0 and up.
2021-01-18 10:14:53 -05:00
NOTE: Webpacker depends on NPM and Yarn. NPM, the Node package manager registry, is the primary repository for publishing and downloading open-source JavaScript projects, both for Node.js and browser runtimes. It is analogous to rubygems.org for Ruby gems. Yarn is a command-line utility that enables installation and management of JavaScript dependencies, much like Bundler does for Ruby.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
Webpacker is installed by default in Rails 6.0 and up. In some older versions, you can install it with a new project by adding `--webpack` to the `rails new` command. In an existing project, webpacker can be added by running `bin/rails webpacker:install`. This installation command creates following local files:
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
|File |Location |Explanation |
|------------------------|------------------------|----------------------------------------------------------------------------------------------------|
|Javascript Folder | `app/javascript` |A place for your front-end source |
|Webpacker Configuration | `config/webpacker.yml` |Configure the Webpacker gem |
|Babel Configuration | `babel.config.js` |Configuration for the [Babel](https://babeljs.io) JavaScript Compiler |
|PostCSS Configuration | `postcss.config.js` |Configuration for the [PostCSS](https://postcss.org) CSS Post-Processor |
|Browserlist | `.browserslistrc` |[Browserlist](https://github.com/browserslist/browserslist) manages target browsers configuration |
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
The installation also calls the `yarn` package manager, creates a `package.json` file with a basic set of packages listed, and uses Yarn to install these dependencies.
### Integrating Frameworks with Webpacker
Webpacker also contains support for many popular JavaScript frameworks and tools. Typically, these are installed either when the application is created with something like `rails new myapp --webpack=<framework_name>` or with a separate command line task, like `rails webpacker:install:<framework_name>`.
These integrations typically install the set of NPM packages needed to get started with the framework or tool, a "hello world" page to show that it works, and any other webpack loaders or transformations needed to compile the tool. The supported frameworks and tools are:
INFO. It's possible to install frameworks not included in this list. These are basic integrations of popular choices.
|Framework |Install command |Description |
|------------------|----------------------------------------|--------------------------------------------------|
|Angular |`bin/rails webpacker:install:angular` |Sets up Angular and Typescript |
|CoffeeScript |`bin/rails webpacker:install:coffee` |Sets up CoffeeScript |
|Elm |`bin/rails webpacker:install:elm` |Sets up Elm |
|ERB |`bin/rails webpacker:install:erb` |Sets up ERB support on your Javascript files |
|React |`bin/rails webpacker:install:react` |Sets up ReactJS |
|Stimulus |`bin/rails webpacker:install:stimulus` |Sets up StimulusJS |
|Svelte |`bin/rails webpacker:install:svelte` |Sets up Svelte JS |
|TypeScript |`bin/rails webpacker:install:typescript`|Sets up Typescript for your project using Babel's TypeScript support|
|Vue |`bin/rails webpacker:install:vue` |Sets up VueJS |
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
2021-01-18 22:21:41 -05:00
For more information about the existing integrations, see https://github.com/rails/webpacker#integrations
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
Usage
-----
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
### Using Webpacker for JavaScript
With Webpacker installed, by default any JavaScript file in the `app/javascripts/packs` directory will get compiled to its own pack file.
So if you have a file called `app/javascript/packs/application.js`, Webpacker will create a pack called `application`, and you can add it to your Rails application with the code `<%= javascript_pack_tag "application" %>`. With that in place, in development, Rails will re-compile the `application.js` file every time it changes and you load a page that uses that pack. Typically, the file in the actual `packs` directory will be a manifest that mostly loads other files, but it can also have arbitrary JavaScript code.
The default pack created for you by Webpacker will link to Rails default JavaScript packages if they have been included in the project:
```
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
Rails.start()
Turbolinks.start()
ActiveStorage.start()
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
```
You'll need to include a pack that requires these packages to use them in your Rails application.
It is important to note that only webpack entry files should be placed in the `app/javascript/packs` directory; webpack will create a separate dependency graph for each entry point so a large number of packs will increase compilation overhead. The rest of your asset source code should live outside this directory though Webpacker does not place any restrictions or make any suggestions on how to structure your source code. Here is an example:
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
```sh
app/javascript:
├── packs:
│ # only webpack entry files here
│ └── application.js
│ └── application.css
└── src:
│ └── my_component.js
└── stylesheets:
│ └── my_styles.css
└── images:
└── logo.svg
```
Typically the pack file itself is largely a manifest that uses `import` or `require` to load the necessary files and may also do some initialization.
If you want to change these directories, you can adjust the `source_path` (default `app/javascript`) and `source_entry_path` (default `packs`) in the `configuration/webpacker.yml` file.
Within source files, `import` statements are resolved relative to the file doing the import, so `import Bar from "./foo"` finds a `foo.js` file in the same directory as the current file, while `import Bar from "../src/foo"` finds a file in a sibling directory named `src`.
### Using Webpacker for CSS
Out of the box, Webpacker supports CSS and SCSS using the PostCSS processor.
To include CSS code in your packs, first include your CSS files in your top-level pack file as though it was a JavaScript file. So if your CSS top-level manifest is in `app/javascript/styles/styles.scss`, you can import it with `import styles/styles`. This tells webpack to include your CSS file in the download. To actually load it in the page, include `<%= stylesheet_pack_tag "application" %>` in the view, where the `application` is the same pack name that you were using.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
If you are using a CSS framework, you can add it to Webpacker by following the instructions to load the framework as an NPM module using `yarn`, typically `yarn add <framework>`. The framework should have instructions on importing it into a CSS or SCSS file.
### Using Webpacker for Static Assets
The default Webpacker [configuration](https://github.com/rails/webpacker/blob/master/lib/install/config/webpacker.yml#L21) should work out of the box for static assets.
The configuration includes several image and font file format extensions, allowing webpack to include them in the generated `manifest.json` file.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
2021-01-18 10:14:53 -05:00
With webpack, static assets can be imported directly in JavaScript files. The imported value represents the URL to the asset. For example:
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
```javascript
import myImageUrl from '../images/my-image.jpg'
// ...
let myImage = new Image();
myImage.src = myImageUrl;
myImage.alt = "I'm a Webpacker-bundled image";
document.body.appendChild(myImage);
```
To reference Webpacker static assets from a Rails view, the assets need to be explicitly required from Webpacker-bundled JavaScript files. Unlike Sprockets, Webpacker does not import your static assets by default. The default `app/javascript/packs/application.js` file has a template for importing files from a given directory, which you can uncomment for every directory you want to have static files in. The directories are relative to `app/javascript`. The template uses the directory `images`, but you can use anything in `app/javascript`:
```
const images = require.context("../images", true)
const imagePath = name => images(name, true)
```
Static assets will be output into a directory under `public/packs/media`. For example, an image located and imported at `app/javascript/images/my-image.jpg` will be output at `public/packs/media/images/my-image-abcd1234.jpg`. To render an image tag for this image in a Rails view, use `image_pack_tag 'media/images/my-image.jpg`.
The Webpacker ActionView helpers for static assets correspond to asset pipeline helpers according to the following table:
|ActionView helper | Webpacker helper |
|------------------|------------------|
|favicon_link_tag |favicon_pack_tag |
|image_tag |image_pack_tag |
Also the generic helper `asset_pack_path` takes the local location of a file and returns its webpacker location for use in Rails views.
You can also access the image by directly referencing the file from a CSS file in `app/javascript`.
### Webpacker in Rails Engines
2021-01-18 10:14:53 -05:00
As of Webpacker version 5, Webpacker is not "engine-aware," which means Webpacker does not have feature-parity with Sprockets when it comes to using within Rails engines. The [Webpacker engine guides](https://github.com/rails/webpacker/blob/master/docs/engines.md) provide some detailed workarounds to add Webpacker-support and developing locally against an engine with Webpacker.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
Gem authors of Rails engines who wish to support consumers using Webpacker are encouraged to distribute frontend assets as an NPM package in addition to the gem itself and provide instructions (or an installer) to demonstrate how host apps should integrate. A good example of this approach is [Alchemy CMS](https://github.com/AlchemyCMS/alchemy_cms).
### Hot Module Replacement (HMR)
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
Webpacker out-of-the-box supports HMR with webpack-dev-server and you can toggle it by setting dev_server/hmr option inside webpacker.yml.
Check out [webpack's documentation on DevServer](https://webpack.js.org/configuration/dev-server/#devserver-hot) for more information.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
To support HMR with React you would need to add react-hot-loader. Check out [React Hot Loader's _Getting Started_ guide](https://gaearon.github.io/react-hot-loader/getstarted/).
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
Don't forget to disable HMR if you are not running webpack-dev-server otherwise you will get "not found error" for stylesheets.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
Webpacker in Different Environments
-----------------------------------
Webpacker has three environments by default `development`, `test`, and `production`. You can add additional environment configurations in the `webpacker.yml` file and set different defaults for each environment, Webpacker will also load the file `config/webpack/<environment>.js` for additional environment setup.
## Running Webpacker in Development
Webpacker ships with two binstub files to run in development: `./bin/webpack` and `./bin/webpack-dev-server`. Both are thin wrappers around the standard `webpack.js` and `webpack-dev-server.js` executables and ensure that the right configuration files and environmental variables are loaded based on your environment.
2021-01-28 02:02:46 -05:00
By default, Webpacker compiles automatically on demand in development when a Rails page loads. This means that you don't have to run any separate processes, and compilation errors will be logged to the standard Rails log. You can change this by changing to `compile: false` in the `config/webpacker.yml` file. Running `bin/webpack` will force compilation of your packs.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run `./bin/webpack-dev-server` or `ruby ./bin/webpack-dev-server`. This process will watch for changes in the `app/javascript/packs/*.js` files and automatically recompile and reload the browser to match.
Windows users will need to run these commands in a terminal separate from `bundle exec rails server`.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
2021-01-18 10:14:53 -05:00
Once you start this development server, Webpacker will automatically start proxying all webpack asset requests to this server. When you stop the server, it'll revert to on-demand compilation.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
The [Webpacker Documentation](https://github.com/rails/webpacker) gives information on environment variables you can use to control `webpack-dev-server`. See additional notes in the [rails/webpacker docs on the webpack-dev-server usage](https://github.com/rails/webpacker#development).
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
### Deploying Webpacker
Webpacker adds a `webpacker:compile` task to the `assets:precompile` rake task, so any existing deploy pipeline that was using `assets:precompile` should work. The compile task will compile the packs and place them in `public/packs`.
Add a Webpacker guide [ci skip] (#40817) * Initial upload * Moving toward a first pass * Add Tables I added tables for the configuration files/folders that are automatically generated. I also added a table for the specific integrations. This way we can add basic links to the specific integration providing more detailed information about the integration. * Add static assets section * Bring over rails/webpacker development docs * Respond to comments * Update * Update webpacker_guide.md I think `include` should be `import` and would like to emphasis `import` over `require` in an effort to promote ES module syntax. * Expand on image docs I thought some additional details are needed to clarify image usage, especially from a Rails view. * Update webpacker_guide.md Add details for Webpacker + Docker section * Update webpacker_guide.md Make important distinction about pack files. Add example directory layout. * Reword sentence * Add section for Rails engines * Rename webpacker guide * Remove Docker section * Add link to additional documentation * Rework section formatting according to Rails guidelines * Add link to webpack documentation * Add note about NPM and Yarn * Update guides/source/webpacker.md Fix typo Co-authored-by: Karl Entwistle <me@karlentwistle.com> Co-authored-by: Noel Rappin <noel.rappin@joinroot.com> Co-authored-by: Niklas Häusele <niklas.haeusele@gmail.com> Co-authored-by: Tasos Latsas <tlatsas2000@gmail.com> Co-authored-by: Noel Rappin <noelrappin@gmail.com> Co-authored-by: Karl Entwistle <me@karlentwistle.com>
2021-01-18 05:40:18 -05:00
Additional Documentation
------------------------
For more information on advanced topics, such as using Webpacker with popular frameworks, consult the [Webpacker Documentation](https://github.com/rails/webpacker).