How to replace rails asset pipeline with webpack?

25
How to replace Rails Asset Pipeline with Webpack? Tomasz Bąk [email protected]

Transcript of How to replace rails asset pipeline with webpack?

Page 1: How to replace rails asset pipeline with webpack?

How to replace Rails Asset Pipeline with Webpack?

Tomasz Bą[email protected]

Page 2: How to replace rails asset pipeline with webpack?

Why replacing Rails Asset Pipeline with Webpack?

● fast rebuilds (rebuild is done in background, page can be live updated with Hot Module Replacement)

● runs any JavaScript “variant”: ES6, ES7, TypeScript etc.● runs tasks like: autoprefixer, eslint/tslint, SVG icons

concatenation etc.

Page 3: How to replace rails asset pipeline with webpack?

gem 'webpack-rails'layouts/application.html.erb

<html>

<head>

<title>Rails Webpack</title>

</head>

<body>

<div id="app"></div>

<%= javascript_include_tag *webpack_asset_paths('vendor') %>

<%= javascript_include_tag *webpack_asset_paths('styles') %>

<%= javascript_include_tag *webpack_asset_paths('app') %>

</body>

</html>

Page 4: How to replace rails asset pipeline with webpack?

gem 'webpack-rails'frontend/webpack.config.js

if (isDev) {

config.devtool = 'cheap-module-eval-source-map';

config.devServer = {

port: 3808,

headers: { 'Access-Control-Allow-Origin': '*' },

stats: 'minimal'

};

config.output.publicPath = `//localhost:3808/webpack/`;

}

Page 5: How to replace rails asset pipeline with webpack?

npm start / npm run build:dev

Page 6: How to replace rails asset pipeline with webpack?

npm run build:prod

Page 7: How to replace rails asset pipeline with webpack?

html-webpack-pluginfrontend/src/index.html → public/index.html

<html>

<head>

<title>Webpack</title>

</head>

<body>

<div id="app"></div>

{% for (var chunk in o.htmlWebpackPlugin.files.chunks) { %}

<script src="{%=o.htmlWebpackPlugin.files.chunks[chunk].entry %}"></script>

{% } %}

</body>

</html>

Page 8: How to replace rails asset pipeline with webpack?

Rails Webpack Examplehttps://github.com/tb/rails-webpack-example

Page 9: How to replace rails asset pipeline with webpack?

How to structurewebpack.config.js?

Tomasz Bą[email protected]

Page 10: How to replace rails asset pipeline with webpack?

Webpack config requires per env configurations

Possible approaches:

● webpack.config.js

● webpack.config.base.js + webpack.config.js

● webpack.config.base.js + webpack.config.js +

webpack.config.[env].js

Page 11: How to replace rails asset pipeline with webpack?

webpack.config.jsconst webpack = require('webpack');

const path = require('path');

const autoprefixer = require('autoprefixer');

const StatsPlugin = require('stats-webpack-plugin');

const nodeEnv = process.env.NODE_ENV || process.env.RAILS_ENV || 'development';

const isTest = nodeEnv == 'test';

const isDev = nodeEnv == 'development';

const isProd = nodeEnv == 'production' || nodeEnv == 'staging';

Page 12: How to replace rails asset pipeline with webpack?

webpack.config.jsconfig = { context: __dirname, entry: { vendor: './src/vendor.js', styles: './styles/main.scss', app: './src/bootstrap.jsx' },

output: { filename: '[name].js', path: '../public/webpack', publicPath: '/webpack/', },

module: { preLoaders: [ { test: /\.jsx?$/, loader: 'eslint?parser=babel-eslint', exclude: /node_modules/ } ], Loaders: [ { test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/, }, // ...};

Page 13: How to replace rails asset pipeline with webpack?

if (isDev) {

config.devtool = 'cheap-module-eval-source-map';

config.devServer = {

port: 3808,

headers: { 'Access-Control-Allow-Origin': '*' },

stats: 'minimal'

};

config.output.publicPath = `//localhost:3808/webpack/`;

}

Page 14: How to replace rails asset pipeline with webpack?

if (isProd) {

config.output.filename = '[name]-[hash].js';

config.plugins = config.plugins.concat([

new webpack.optimize.UglifyJsPlugin({

compressor: { warnings: false },

sourceMap: false

}),

]);

}

Page 15: How to replace rails asset pipeline with webpack?

if (isTest) {

config.devtool = 'inline-source-map';

// http://airbnb.io/enzyme/docs/guides/webpack.html

config.externals = Object.assign({}, config.externals, {

'react/addons': true,

'react/lib/ExecutionEnvironment': true,

'react/lib/ReactContext': true

});

}

Page 16: How to replace rails asset pipeline with webpack?

if (!isTest) {

config.plugins = config.plugins.concat([

new webpack.optimize.DedupePlugin(),

new webpack.optimize.OccurenceOrderPlugin(),

new webpack.optimize.CommonsChunkPlugin({

name: 'vendor',

filename: `vendor${isProd ? '-[hash]' : ''}.js`,

minChunks: Infinity,

}),

]);

}

Page 17: How to replace rails asset pipeline with webpack?

Build “scripts” in package.json"scripts": {

"clean": "rm -rf ../public/webpack",

"start": "npm run build:dev",

"build:dev": "NODE_ENV=development webpack-dev-server --hot --inline",

"build:prod": "npm run clean && NODE_ENV=production webpack",

"test": "NODE_ENV=test karma start",

"test:ci": "NODE_ENV=test karma start --singleRun"

},

Page 18: How to replace rails asset pipeline with webpack?

Rails Webpack Examplehttps://github.com/tb/rails-webpack-example

Page 19: How to replace rails asset pipeline with webpack?

What is NPM equivalent of ".ruby-version"?

Tomasz Bą[email protected]

Page 20: How to replace rails asset pipeline with webpack?

Node Version Manager - Simple bash script to manage multiple node.js versionshttps://github.com/creationix/nvm

.nvmrc

4.4

Page 21: How to replace rails asset pipeline with webpack?

Node Version Manager - Simple bash script to manage multiple node.js versionshttps://github.com/creationix/nvm

.nvmrc

4.4

$ node -v

v0.10.38

MacOS X default,many npm packages won’t work

Page 22: How to replace rails asset pipeline with webpack?

Node Version Manager - Simple bash script to manage multiple node.js versionshttps://github.com/creationix/nvm

.nvmrc

4.4

$ node -v

v0.10.38

$ cd frontend

Found '.nvmrc' with version <4.4>

Now using node v4.4.3 (npm v3.10.2)

$ node -v

v4.4.3

MacOS X default,many npm packages won’t work

Page 23: How to replace rails asset pipeline with webpack?

What is NPM equivalent of "Gamefile.lock"?

Tomasz Bą[email protected]

Page 24: How to replace rails asset pipeline with webpack?

https://devcenter.heroku.com/articles/node-best-practices#use-a-smart-npmrc

.npmrc

save=true

save-exact=true

package.json

"dependencies": {

"autoprefixer": "^6.3.2",

"babel": "~6.5.0",

"autoprefixer": "6.3.7",

"babel": "6.5.2",

Page 25: How to replace rails asset pipeline with webpack?

npm-check-updates