Skip to content

infrastructure-wixproducts/python-webpack

 
 

Repository files navigation

python-webpack

Build Status

Python bindings to webpack.

Bundles your assets so that they can be reused on the clientside. Watches your files for changes and rebuilds the bundle whenever they change.

Just point webpack at your config files and plug the rendered elements into your front end.

from webpack.compiler import webpack

bundle = webpack('/path/to/webpack.config.js')

# Returns a string containing <script> and <link> elements 
# pointing to the bundle's assets
bundle.render()

Documentation

Installation

Webpack depends on js-host to provide interoperability with JavaScript. Complete its quick start before continuing.

Install webpack's JS dependencies

npm install --save webpack webpack-wrapper@0.2.2

Add webpack-wrapper to the functions definition of your host.config.js file

var webpackWrapper = require('webpack-wrapper');

module.exports = {
  functions: {
    // ...
    webpack: webpackWrapper
  }
};

And install python-webpack

pip install webpack

Settings

If you are using this library in a Django project, please refer to the settings for Django projects section of the documentation.

Settings can be defined by calling webpack.conf.settings.configure with keyword arguments matching the setting that you want to define. For example

from webpack.conf import settings

DEBUG = True

settings.configure(
    BUNDLE_ROOT='/path/to/your/projects/static_root',
    BUNDLE_URL='/static/',
    WATCH_CONFIG_FILES=DEBUG,
    WATCH_SOURCE_FILES=DEBUG,
)

BUNDLE_ROOT

An absolute path to the root directory that you use for static assets.

For example, '/path/to/your/projects/static_root'.

This setting must be defined.

Default: None

BUNDLE_URL

The root url that your static assets are served from.

For example, '/static/.

This setting must be defined.

Default: None

OUTPUT_DIR

The directory in BUNDLE_ROOT which webpack will output any generated bundles or config files.

Default: 'webpack'

BUNDLE_DIR

The directory into which bundles are placed in the OUTPUT_DIR.

Default: 'bundles'

CONFIG_DIR

The directory into which bundles are placed in the OUTPUT_DIR.

Default: 'config_files'

WATCH_CONFIG_FILES

A boolean flag which indicates that file watchers should be set to watch config files and rebuild the resulting bundle whenever it changes. Set this to True in development environments.

Bundles are rebuilt in the background. If webpack is still rebuilding when a request comes in, it will block until the build has completed.

Default: False

WATCH_SOURCE_FILES

A boolean flag which indicates that file watchers should be set to watch the bundle's source files and rebuild the bundle whenever it changes. Set this to True in development environments.

Bundles are rebuilt in the background. If webpack is still rebuilding when a request comes in, it will block until the build has completed.

Default: False

WATCH_DELAY

The number of milliseconds before any changes to your source files will trigger the bundle to be rebuilt.

Default: 200

Settings for Django projects

The following configuration should be placed in your settings files to enable webpack to function seamlessly in a Django project.

Add 'webpack' to your INSTALLED_APPS

INSTALLED_APPS = (
    # ...
    'webpack',
)

Add 'webpack.django_integration.WebpackFinder' to your STATICFILES_FINDERS

STATICFILES_FINDERS = (
    # ...
    'webpack.django_integration.WebpackFinder',
)

Configure webpack to respect your project's configuration

WEBPACK = {
    'BUNDLE_ROOT': STATIC_ROOT,
    'BUNDLE_URL': STATIC_URL,
    'WATCH_CONFIG_FILES': DEBUG,
    'WATCH_SOURCE_FILES': DEBUG,
}

When used in a Django project, Webpack allows you to specify paths to config files which will be resolved with Django's file finders.

For example, webpack('my_app/webpack.config.js') could match a file within an app's static directory, such as my_app/static/my_app/webpack.config.js.

Usage

from webpack.compiler import webpack

bundle = webpack('/path/to/webpack.config.js')

# Returns a string containing <script> and <link> elements pointing 
# to the generated assets
bundle.render()

# An object providing information about the compilation process
bundle.output

# Returns the paths and urls to the generated assets
bundle.get_assets()

# Returns absolute paths to the generated assets on your filesystem
bundle.get_paths()

# Returns urls pointing to the generated assets
bundle.get_urls()

# Returns a string matching the `library` property of your config file
bundle.get_library()

A helper is provided for configuring your bundle's output path: the substring [bundle_dir] will be replaced with an absolute path generated by joining your BUNDLE_ROOT, WEBPACK_DIR and BUNDLE_DIR settings.

module.exports = {
  // ...
  output: {
    path: '[bundle_dir]',
    // ..
  }
};

Running the tests

pip install -r requirements.txt
cd tests
npm install
cd ..
python runtests.py

About

Build and watch webpack bundles

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 87.0%
  • JavaScript 12.6%
  • HTML 0.4%