Adds support for webpack to Lektor
Project description
# lektor-webpack-support
[![Build Status](https://travis-ci.org/lektor/lektor-webpack-support.svg)](https://travis-ci.org/lektor/lektor-webpack-support)
This is a plugin for Lektor that adds support for webpack to projects. When
enabled it can build a webpack project from the `webpack/` folder into the
asset folder automatically when the server (or build process) is run with
the `-f webpack` flag.
## Enabling the Plugin
To enable the plugin add this to your project file, run this command while
sitting in your Lektor project directory:
```bash
lektor plugins add lektor-webpack-support
```
## Creating a Webpack Project
Next you need to create a webpack project in the `webpack/` folder. You
will need a `package.json` as well as a `webpack.config.js`
### `webpack/package.json`
This file instructs `npm` which packages we will need. All we need for a
start is to create an almost empty file:
```json
{
"private": true
}
```
Now we can npm install the rest:
```
$ npm install --save-dev webpack babel-core node-sass babel-loader sass-loader css-loader url-loader style-loader file-loader extract-text-webpack-plugin
```
This will install webpack itself together with babel and sass as well as
a bunch of loaders we need for getting all that configured. Because we
created a `package.json` before and we use `--save-dev` the dependencies
will be remembered in the `package.json` file.
### `webpack/webpack.config.js`
Next up is the webpack config file. Here we will go with a very basic
setup that's good enough to cover most things you will encounter. The
idea is to build the files from `webpack/scss` and `webpack/js` into
`assets/static/gen` so that we can use it even if we do not have webpack
installed for as long as someone else ran it before.
```javascript
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var options = {
entry: {
'app': './js/main.js',
'styles': './scss/main.scss'
},
output: {
path: path.dirname(__dirname) + '/assets/static/gen',
filename: '[name].js'
},
devtool: '#cheap-module-source-map',
resolve: {
modulesDirectories: ['node_modules'],
extensions: ['', '.js']
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader')
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
},
{
test: /\.woff2?$|\.ttf$|\.eot$|\.svg$|\.png|\.jpe?g\|\.gif$/,
loader: 'file'
}
]
},
plugins: [
new ExtractTextPlugin('styles.css', {
allChunks: true
}),
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.DedupePlugin()
]
};
module.exports = options;
```
## Creating the App
Now we can start building our app. We configured at least two files
in webpack: `js/main.js` and `scss/main.scss`. Those are the entry
points we need to have. You can create them as empty files in
`webpack/js/main.js` and `webpack/scss/main.scss`.
## Running the Server
Now you're ready to go. When you run `lektor server` webpack will not
run, instead you need to now run it as `lektor server -f webpack` which
will enable the webpack build. Webpack automatically builds your files
into `assets/static/gen` and this is where Lektor will then pick up the
files. This is done so that you can ship the webpack generated assets
to others that do not have webpack installed which simplifies using a
Lektor website that uses webpack.
## Manual Builds
To manually trigger a build that also invokes webpack you can use
`lektor build -f webpack`.
## Including The Files
Now you need to include the files in your template. This will do it:
```html
<link rel="stylesheet" href="{{ '/static/gen/styles.css'|asseturl }}">
<script type=text/javascript src="{{ '/static/gen/app.js'|asseturl }}" charset="utf-8"></script>
```
[![Build Status](https://travis-ci.org/lektor/lektor-webpack-support.svg)](https://travis-ci.org/lektor/lektor-webpack-support)
This is a plugin for Lektor that adds support for webpack to projects. When
enabled it can build a webpack project from the `webpack/` folder into the
asset folder automatically when the server (or build process) is run with
the `-f webpack` flag.
## Enabling the Plugin
To enable the plugin add this to your project file, run this command while
sitting in your Lektor project directory:
```bash
lektor plugins add lektor-webpack-support
```
## Creating a Webpack Project
Next you need to create a webpack project in the `webpack/` folder. You
will need a `package.json` as well as a `webpack.config.js`
### `webpack/package.json`
This file instructs `npm` which packages we will need. All we need for a
start is to create an almost empty file:
```json
{
"private": true
}
```
Now we can npm install the rest:
```
$ npm install --save-dev webpack babel-core node-sass babel-loader sass-loader css-loader url-loader style-loader file-loader extract-text-webpack-plugin
```
This will install webpack itself together with babel and sass as well as
a bunch of loaders we need for getting all that configured. Because we
created a `package.json` before and we use `--save-dev` the dependencies
will be remembered in the `package.json` file.
### `webpack/webpack.config.js`
Next up is the webpack config file. Here we will go with a very basic
setup that's good enough to cover most things you will encounter. The
idea is to build the files from `webpack/scss` and `webpack/js` into
`assets/static/gen` so that we can use it even if we do not have webpack
installed for as long as someone else ran it before.
```javascript
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var options = {
entry: {
'app': './js/main.js',
'styles': './scss/main.scss'
},
output: {
path: path.dirname(__dirname) + '/assets/static/gen',
filename: '[name].js'
},
devtool: '#cheap-module-source-map',
resolve: {
modulesDirectories: ['node_modules'],
extensions: ['', '.js']
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader')
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
},
{
test: /\.woff2?$|\.ttf$|\.eot$|\.svg$|\.png|\.jpe?g\|\.gif$/,
loader: 'file'
}
]
},
plugins: [
new ExtractTextPlugin('styles.css', {
allChunks: true
}),
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.DedupePlugin()
]
};
module.exports = options;
```
## Creating the App
Now we can start building our app. We configured at least two files
in webpack: `js/main.js` and `scss/main.scss`. Those are the entry
points we need to have. You can create them as empty files in
`webpack/js/main.js` and `webpack/scss/main.scss`.
## Running the Server
Now you're ready to go. When you run `lektor server` webpack will not
run, instead you need to now run it as `lektor server -f webpack` which
will enable the webpack build. Webpack automatically builds your files
into `assets/static/gen` and this is where Lektor will then pick up the
files. This is done so that you can ship the webpack generated assets
to others that do not have webpack installed which simplifies using a
Lektor website that uses webpack.
## Manual Builds
To manually trigger a build that also invokes webpack you can use
`lektor build -f webpack`.
## Including The Files
Now you need to include the files in your template. This will do it:
```html
<link rel="stylesheet" href="{{ '/static/gen/styles.css'|asseturl }}">
<script type=text/javascript src="{{ '/static/gen/app.js'|asseturl }}" charset="utf-8"></script>
```
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file lektor-webpack-support-0.2.tar.gz
.
File metadata
- Download URL: lektor-webpack-support-0.2.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b3879bee2091d7f127f341b94b86eca7352591543f4a817cf8439ee319c17aa |
|
MD5 | 5159e502decab6c9675c6501a0e9a2d8 |
|
BLAKE2b-256 | 72c928e30486a703c4f049a790306e05b5a43d4a1a79694b30c37ba6abffc404 |
Provenance
File details
Details for the file lektor_webpack_support-0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: lektor_webpack_support-0.2-py2.py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e3f5821042420b1eb0e44c4372eafe2e0f945fefcc27ec12d7da9c781949175 |
|
MD5 | 92975270684d11ceebc347e6cec6e28d |
|
BLAKE2b-256 | c7236228e762c3180f86e28dc69f4d1bfbf8f96ddde92fa8ca2f0c6c18ae67fe |
Provenance
File details
Details for the file lektor_webpack_support-0.2-py2.7.egg
.
File metadata
- Download URL: lektor_webpack_support-0.2-py2.7.egg
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a413a824287862f5922e0935c6b7478e4d1662baa425789c51dd488ed0a2ec97 |
|
MD5 | b7890f9add8cb39e2197f1df6c6f3138 |
|
BLAKE2b-256 | 67a31c57497d17cd5d0c71f20c4c7027528ef236a00bc307ebcca3a3bb9d4b60 |