Skip to main content

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) [![Code Coverage](https://codecov.io/gh/lektor/lektor-webpack-support/branch/master/graph/badge.svg)](https://codecov.io/gh/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

lektor-webpack-support-0.3.tar.gz (4.3 kB view details)

Uploaded Source

Built Distributions

lektor_webpack_support-0.3-py2.py3-none-any.whl (3.2 kB view details)

Uploaded Python 2 Python 3

lektor_webpack_support-0.3-py2.7.egg (3.4 kB view details)

Uploaded Source

File details

Details for the file lektor-webpack-support-0.3.tar.gz.

File metadata

File hashes

Hashes for lektor-webpack-support-0.3.tar.gz
Algorithm Hash digest
SHA256 24b9106136dbfbc7dbdc008a7fe99e0696fbdee480a1e46b0343b66bcd842d4f
MD5 4acff4e8ba91da1e968147d6ea31df1d
BLAKE2b-256 eaeef21d05d3bd85dd900981f2b1bafefab75e45ce14f92b0460cb00756103a1

See more details on using hashes here.

Provenance

File details

Details for the file lektor_webpack_support-0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for lektor_webpack_support-0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3c83d0e810ff719a4f7567282c29be671f5ac16fbf1fdc88588ff2956ad9f032
MD5 62c8d043fd0ba494741bbac094dd2bfa
BLAKE2b-256 4784c7322c3875744937e12bcd9baa44168b1c603dd38e73ecc66dc6a286b634

See more details on using hashes here.

Provenance

File details

Details for the file lektor_webpack_support-0.3-py2.7.egg.

File metadata

File hashes

Hashes for lektor_webpack_support-0.3-py2.7.egg
Algorithm Hash digest
SHA256 096065145419442da1c62ed123bc9ec7207ab9844c11638c29e120a2bfc309f7
MD5 e36673d1be46b31479e75f8ce51783e4
BLAKE2b-256 8bccac976943633763dc7beba1bc08c9158fdfeb6c06091b50beadc00205273b

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page