Giter VIP home page Giter VIP logo

python-webpack's Introduction

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-service

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

var webpackService = require('webpack-service');

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

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

python-webpack's People

Contributors

markfinger avatar maxpoletaev avatar matthewryanscott avatar

Watchers

Rocky Meza avatar Joshua Pokotilow avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.