Skip to content

Converts Sprockets/Rails directives, in both JS and CSS files, into modules recognizable by Webpack.

Notifications You must be signed in to change notification settings

timmfin/sprockets-directive-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sprockets-style directive loader for webpack

Converts Sprockets/Rails directives—things like //= require <path>—into modules recognizable by Webpack. Works on JS, CoffeeScript, CSS, and Sass files.

Caveats

  1. This loader assumes that every JS file you //= require is a complete, valid JavaScript file that can run on its own. In Rails, you could concatenate together files that were each not valid JavaScript syntax (for example, three files like this: function sillySplitFunction() {, console.log('in silly function');, } // end silly function).
  2. Related to #1, your JS files will no longer be run in the global context. They will wrapped inside functions like Webpack does with all modules. So if you were relying on your top-level JS variables getting set globally (or on the window), that will no longer work.
  3. This loader doesn't do automatic extension conversion for you. Sprockets/Rails would allow you to refer to //= require file.coffee or //= require file.js, but in this loader you'll need to refer to exactly the extension that is on the file system (at the point the loader runs?).

Note it may be possible to fix some or all of the above, but this is only version 0.x.y and I'm trying the simplest approach first.

Usage

Documentation: Using loaders

Examples

{
  // ...

  module: {
    // Needs to be a pre-loader so that it is run before files are converted to JS
    preLoaders: [
      {
        test: /\.js$/,
        loader:  "sprockets-directive-loader"
      }, {
        test: /\.css$/,
        loader: "sprockets-directive-loader"
      }
    ],

    // ...
  }
}

About

Converts Sprockets/Rails directives, in both JS and CSS files, into modules recognizable by Webpack.

Resources

Stars

Watchers

Forks

Packages

No packages published