webpack
"Baggage" loader forAutomatically require any resources related to the required one. See example below.
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 = ;; var html = ;
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.jsvar template = ;; // your codevar html =
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 = ;;