Skip to content

Webpack loader to automatically require any resources related to the required one

License

Notifications You must be signed in to change notification settings

bgannonPL/baggage-loader

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

"Baggage" loader for webpack

npm travis climate peer deps gratipay

Automatically require any resources related to the required one. See example below.

Documentation: Using loaders.

Install

$ npm i -S baggage-loader

Usage

Imagine that you have project structure like this and you're using webpack:

components/
├── component-1/
│   ├── script.js
│   ├── styles.css
│   └── template.html
├── component-2/
│   ├── script.js
│   └── template.html
└── component-3/
    ├── script.js
    └── styles.css

and in each of component's script.js you're doing something like this:

var template = require('./template.html');
require('./styles.css');

var html = template({ foo: 'bar' });

Now you have to stop and give it to baggage-loader, so:

module: {
    preLoaders: [ {
        test: /\/components\/.+script\.js$/,
        // baggage?file=var&file-without-var&…
        loader: 'baggage?template.html=template&styles.css'
    } ]
}

will become the necessary requires with variables declarations if corresponding files exists:

// injected by preloader at the top of script.js
var template = require('./template.html');
require('./styles.css');

// your code
var html = template({ foo: 'bar' };

Even more, there are placeholders [dir], [Dir], [file] and [File], so you can use them in various tricky ways both with file and var:

alert/
├── view.js
├── templateAlert.html
└── alertViewStyles.css
loader: 'baggage?template[Dir].html=[file]Template&[dir][File]Styles.css'
var viewTemplate = require('./templateAlert.html');
require('./alertViewStyles.css');

About

Webpack loader to automatically require any resources related to the required one

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%