README.md in webpacker-3.0.2 vs README.md in webpacker-3.1.0

- old
+ new

@@ -3,13 +3,13 @@ ![travis-ci status](https://api.travis-ci.org/rails/webpacker.svg?branch=master) [![node.js](https://img.shields.io/badge/node-%3E%3D%206.0.0-brightgreen.svg)](https://nodejs.org/en/) [![Gem](https://img.shields.io/gem/v/webpacker.svg)](https://github.com/rails/webpacker) Webpacker makes it easy to use the JavaScript pre-processor and bundler -[Webpack 3.x.x+](https://webpack.js.org/) +[webpack 3.x.x+](https://webpack.js.org/) to manage application-like JavaScript in Rails. It coexists with the asset pipeline, -as the primary purpose for Webpack is app-like JavaScript, not images, CSS, or +as the primary purpose for webpack is app-like JavaScript, not images, CSS, or even JavaScript Sprinkles (that all continues to live in app/assets). However, it is possible to use Webpacker for CSS, images and fonts assets as well, in which case you may not even need the asset pipeline. This is mostly relevant when exclusively using component-based JavaScript frameworks. @@ -18,14 +18,15 @@ ## Table of Contents - [Prerequisites](#prerequisites) - [Features](#features) - [Installation](#installation) - - [Upgrading](#upgrading) - [Usage](#usage) - [Development](#development) - - [Webpack configuration](#webpack-configuration) + - [webpack configuration](#webpack-configuration) + - [Upgrading](#upgrading) + - [Yarn Integrity](#yarn-integrity) - [Integrations](#integrations) - [React](#react) - [Angular with TypeScript](#angular-with-typescript) - [Vue](#vue) - [Elm](#elm) @@ -47,14 +48,14 @@ * Yarn 0.25.2+ ## Features -* [Webpack 3.x.x](https://webpack.js.org/) +* [webpack 3.x.x](https://webpack.js.org/) * ES6 with [babel](https://babeljs.io/) * Automatic code splitting using multiple entry points -* Stylesheets - SASS and CSS +* Stylesheets - Sass and CSS * Images and fonts * PostCSS - Auto-Prefixer * Asset compression, source-maps, and minification * CDN support * React, Angular, Elm and Vue support out-of-the-box @@ -80,11 +81,11 @@ # OR if you prefer to use master gem 'webpacker', git: 'https://github.com/rails/webpacker.git' ``` -and finally, run following to install webpacker: +and finally, run following to install Webpacker: ```bash bundle bundle exec rails webpacker:install @@ -106,11 +107,11 @@ │ └── application.css └── images: └── logo.svg ``` -You can then link the javascript pack in Rails view using `javascript_pack_tag` helper. +You can then link the JavaScript pack in Rails view using `javascript_pack_tag` helper. If you have styles imported in your pack file, you can link using `stylesheet_pack_tag`: ```erb <%= javascript_pack_tag 'application' %> <%= stylesheet_pack_tag 'application' %> @@ -157,11 +158,16 @@ 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 again. -You can use environment variables as options supported by [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environment variables will always take precedence over the ones already set in the configuration file. +You can use environment variables as options supported by +[webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the +form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environment +variables will always take precedence over the ones already set in the +configuration file, and that the _same_ environment variables must +be available to the `rails server` process. ```bash WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpack-dev-server ``` @@ -171,26 +177,49 @@ ```bash WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpack-dev-server ``` +**Note:** You need to allow webpack-dev-server host as allowed origin for `connect-src` if you are running your application in a restrict CSP environment like Rails 5.2+. This can be done in Rails 5.2+ for development environment in the CSP initializer `config/initializers/content_security_policy.rb` with a snippet like this: + +```ruby + p.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development? +``` + **Note:** Don't forget to prefix `ruby` when running these binstubs on windows -### Webpack configuration +### webpack configuration -See [docs/Webpack](docs/webpack.md) for modifying webpack configuration and loaders. +See [docs/webpack](docs/webpack.md) for modifying webpack configuration and loaders. ### Upgrading -You can run following commands to upgrade webpacker to the latest stable version, this involves upgrading the gem and npm module: +You can run following commands to upgrade Webpacker to the latest stable version, this involves upgrading the gem and related npm modules: ```bash bundle update webpacker yarn upgrade @rails/webpacker --latest +yarn upgrade webpack-dev-server --latest ``` +### Yarn Integrity + +By default in development, webpacker runs a yarn integrity check to ensure that all local npm packages are up-to-date. This is similar to what bundler does currently in Rails, but for JavaScript packages. If your system is out of date, then Rails will not initialize and you will be asked to upgrade your local npm packages by running `yarn install`. + +To turn off this option, you will need to override the default by adding a new config options to your Rails development environment configuration file (`config/environment/development.rb`): + +``` +config.webpacker.check_yarn_integrity = false +``` + +You may also turn on this option by adding the config option to any Rails environment configuration file: + +``` +config.webpacker.check_yarn_integrity = true +``` + ## Integrations Webpacker ships with basic out-of-the-box integration for React, Angular, Vue and Elm. You can see a list of available commands/tasks by running `bundle exec rails webpacker`: @@ -203,13 +232,13 @@ # Rails 5.1+ rails new myapp --webpack=react ``` (or run `bundle exec rails webpacker:install:react` in a existing Rails app already -setup with webpacker). +setup with Webpacker). -The installer will add all relevant dependencies using yarn, any changes +The installer will add all relevant dependencies using Yarn, any changes to the configuration files and an example React component to your project in `app/javascript/packs` so that you can experiment with React right away. ### Angular with TypeScript @@ -221,63 +250,93 @@ # Rails 5.1+ rails new myapp --webpack=angular ``` (or run `bundle exec rails webpacker:install:angular` on a Rails app already -setup with webpacker). +setup with Webpacker). -The installer will add TypeScript and Angular core libraries using yarn plus +The installer will add TypeScript and Angular core libraries using Yarn plus any changes to the configuration files. An example component is written in TypeScript will also be added to your project in `app/javascript` so that you can experiment with Angular right away. +By default Angular uses a JIT compiler for development environment, this +compiler is not compatible with restrictive CSP (Content Security +Policy) environments like Rails 5.2+. You can use Angular AOT compiler +in development with the [@ngtools/webpack](https://www.npmjs.com/package/@ngtools/webpack#usage) plugin. +Alternatively if you're using Rails 5.2+ you can enable `unsafe-eval` rule for +development environment, this can be done in the `config/initializers/content_security_policy.rb` +with the following configuration: + +```ruby + if Rails.env.development? + p.script_src :self, :https, :unsafe_eval + else + p.script_src :self, :https + end +``` + + ### Vue To use Webpacker with [Vue](https://vuejs.org/), create a new Rails 5.1+ app using `--webpack=vue` option: ```bash # Rails 5.1+ rails new myapp --webpack=vue ``` -(or run `bundle exec rails webpacker:install:vue` on a Rails app already setup with webpacker). +(or run `bundle exec rails webpacker:install:vue` on a Rails app already setup with Webpacker). -The installer will add Vue and required libraries using yarn plus +The installer will add Vue and required libraries using Yarn plus any changes to the configuration files. An example component will also be added to your project in `app/javascript` so that you can experiment Vue right away. +If you're using Rails 5.2+ you need to enable `unsafe-eval` rule for development environment, +this can be done in the `config/initializers/content_security_policy.rb` with the following +configuration: +```ruby + if Rails.env.development? + p.script_src :self, :https, :unsafe_eval + else + p.script_src :self, :https + end +``` +You can read more this in [Vue docs](https://vuejs.org/v2/guide/installation.html#CSP-environments). + + ### Elm To use Webpacker with [Elm](http://elm-lang.org), create a new Rails 5.1+ app using `--webpack=elm` option: ``` # Rails 5.1+ rails new myapp --webpack=elm ``` -(or run `bundle exec rails webpacker:install:elm` on a Rails app already setup with webpacker). +(or run `bundle exec rails webpacker:install:elm` on a Rails app already setup with Webpacker). The Elm library and core packages will be added via Yarn and Elm itself. An example `Main.elm` app will also be added to your project in `app/javascript` so that you can experiment with Elm right away. ## Paths -By default, webpacker ships with simple conventions for where the javascript -app files and compiled webpack bundles will go in your rails app, +By default, Webpacker ships with simple conventions for where the JavaScript +app files and compiled webpack bundles will go in your Rails app, but all these options are configurable from `config/webpacker.yml` file. -The configuration for what Webpack is supposed to compile by default rests +The configuration for what webpack is supposed to compile by default rests on the convention that every file in `app/javascript/packs/*`**(default)** or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration -is turned into their own output files (or entry points, as Webpack calls it). Therefore you don't want to put anything inside `packs` directory that you do want to be -an entry file. As a rule thumb, put all files your want to link in your views inside +is turned into their own output files (or entry points, as webpack calls it). Therefore you don't want to put anything inside `packs` directory that you do not want to be +an entry file. As a rule of thumb, put all files you want to link in your views inside "packs" directory and keep everything else under `app/javascript`. Suppose you want to change the source directory from `app/javascript` to `frontend` and output to `assets/packs`. This is how you would do it: @@ -301,11 +360,11 @@ If you have `hmr` turned to true, then the `stylesheet_pack_tag` generates no output, as you will want to configure your styles to be inlined in your JavaScript for hot reloading. During production and testing, the `stylesheet_pack_tag` will create the appropriate HTML tags. ### Resolved -If you are adding webpacker to an existing app that has most of the assets inside +If you are adding Webpacker to an existing app that has most of the assets inside `app/assets` or inside an engine and you want to share that with webpack modules then you can use `resolved_paths` option available in `config/webpacker.yml`, which lets you add additional paths webpack should lookup when resolving modules: @@ -328,24 +387,21 @@ ### Watched By default, the lazy compilation is cached until a file is changed under tracked paths. You can configure the paths tracked -by adding new paths to `watched_paths` array, much like rails `autoload_paths`: +by adding new paths to `watched_paths` array, much like Rails `autoload_paths`: ```rb # config/initializers/webpacker.rb # or config/application.rb Webpacker::Compiler.watched_paths << 'bower_components' ``` ## Deployment -Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using sprockets you -can manually trigger `NODE_ENV=production bundle exec rails webpacker:compile` -during your app deploy. - +Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using Sprockets `webpacker:compile` is automatically aliased to `assets:precompile`. Remember to set NODE_ENV environment variable to production during deployment or when running the rake task. ## Docs You can find more detailed guides under [docs](./docs).