This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
3,464 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/Gemfile.lock | ||
/lib/generators/npm_pipeline/*/vendor | ||
/lib/generators/npm_pipeline/*/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Webpack integration | ||
|
||
Run this to set your project up with a simple Webpack config. | ||
|
||
```sh | ||
./bin/rails generate npm_pipeline:webpack | ||
``` | ||
|
||
This configuration includes: | ||
|
||
- [babel-loader](https://www.npmjs.com/package/babel-loader) for evergreen `.js` support | ||
- [postcss-loader](https://www.npmjs.com/package/postcss-loader) with: | ||
- [autoprefixer](https://www.npmjs.com/package/autoprefixer) | ||
- [postcss-asset-url-rails](https://www.npmjs.com/package/postcss-asset-url-rails) for `image-url()` support | ||
- [sass-loader](https://www.npmjs.com/package/sass-loader) for Sass support | ||
- [extract-text-webpack-plugin](https://www.npmjs.com/package/extract-text-webpack-plugin) for `.css` file support | ||
|
||
## Manual setup | ||
|
||
If you don't want to use the generator, here's what it does. | ||
|
||
#### package.json | ||
|
||
> Set up `webpack` (et al) and some basic Webpack plugins. | ||
```sh | ||
npm init --yes | ||
npm install --save-dev --save-exact webpack style-loader css-loader extract-text-webpack-plugin | ||
``` | ||
|
||
You also need to set the `start` and `build` scripts. | ||
|
||
```js | ||
/* package.json */ | ||
"scripts": { | ||
"start": "webpack --progress --colors --watch", | ||
"build": "webpack --progress --colors" | ||
} | ||
``` | ||
|
||
_See:_ [sample package.json](../lib/generators/npm_pipeline/webpack/package.json) | ||
|
||
#### webpack.config.js | ||
|
||
> Set it up to watch source files in `app/webpack`, then put built files into `vendor/assets`. | ||
_See:_ [sample webpack.config.js](../lib/generators/npm_pipeline/webpack/webpack.config.js), | ||
[simplified webpack.config.js](../lib/generators/npm_pipeline/webpack/simple-webpack.config.js) | ||
|
||
#### .gitignore | ||
|
||
> Set it up to ignore Webpack's built files. | ||
``` | ||
/node_modules | ||
/vendor/assets/stylesheets/webpack | ||
/vendor/assets/javascripts/webpack | ||
``` | ||
|
||
#### app/assets/stylesheets/application.css | ||
|
||
> Set it up to include Webpack's built files. This will load from `vendor/assets/stylesheets/webpack/app.css`, as built by webpack. | ||
```css | ||
/* | ||
*= require webpack/app | ||
*/ | ||
``` | ||
|
||
#### app/assets/javascripts/application.js | ||
|
||
> Set it up to include Webpack's built files. This will load from `vendor/assets/javascripts/webpack/app.js`, as built by webpack. | ||
```css | ||
//= require webpack/app | ||
``` | ||
|
||
#### app/webpack/ | ||
|
||
> Put your source files into `app/webpack`. For instance: | ||
|
||
* `app/webpack/app.js` | ||
|
||
```js | ||
alert('it works!') | ||
``` | ||
|
||
Be sure to point the proper `entry` points in the Webpack config. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// http://stackoverflow.com/a/30652110/873870 | ||
function requireAll (r) { r.keys().forEach(r) } | ||
|
||
requireAll(require.context('./components/', true, /\.scss$/)) |
1 change: 1 addition & 0 deletions
1
lib/generators/npm_pipeline/webpack/app/webpack/css/components/example.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
html:before { content: 'Webpack assets added successfully! See app/webpack/css/.'; display: block; padding: 20px; background: #ffc; color: #111; position: fixed; top: 16px; right: 16px; max-width: 600px; z-index: 10000; font-family: sans-serif; font-size: 14px; line-height: 1.6; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// http://stackoverflow.com/a/30652110/873870 | ||
function requireAll (r) { r.keys().forEach(r) } | ||
|
||
requireAll(require.context('./behaviors/', true, /\.js$/)) |
1 change: 1 addition & 0 deletions
1
lib/generators/npm_pipeline/webpack/app/webpack/js/behaviors/example.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alert('Webpack works!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
lib/generators/npm_pipeline/webpack/simple-webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Simple Webpack config. | ||
* This has no Babel or PostCSS support. | ||
*/ | ||
|
||
var webpack = require('webpack') | ||
var join = require('path').join | ||
var ExtractTextPlugin = require('extract-text-webpack-plugin') | ||
|
||
module.exports = { | ||
context: __dirname, | ||
entry: { | ||
// JavaScript | ||
'javascripts/webpack/app': ['./app/webpack/js/app.js'], | ||
|
||
// Stylesheets | ||
'stylesheets/webpack/app': ['./app/webpack/css/app/app.js'] | ||
}, | ||
|
||
output: { | ||
path: join(__dirname, 'vendor/assets'), | ||
filename: '[name].js', | ||
}, | ||
|
||
module: { | ||
loaders: [ | ||
{ | ||
test: /\.css$/, | ||
loader: ExtractTextPlugin.extract('style-loader', 'css-loader?-url&sourceMap') | ||
} | ||
], | ||
}, | ||
|
||
resolve: { | ||
extensions: ['', '.js'] | ||
}, | ||
|
||
plugins: [ | ||
new ExtractTextPlugin('[name].css.erb') | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.