import-loader
v1.0.1
Published
Webpack loader to import files at once with glob
Downloads
3,605
Maintainers
Readme
import-loader
Webpack loader to import files at once with glob
It's just a concept yet
Install
npm install --save-dev import-loader
or
yarn add --dev import-loader
How to use
Quick
import models from 'import?glob=models/*.js'
Use ?glob
var models = require('import-loader?glob=models/*.js')
// or
var models = require('import?glob=models/*.js')
// or
import models from 'import?glob=models/*.js'
It will be something like that:
var models ={
'models/User.js': require('models/User.js'),
'models/Pet.js': require('models/Pet.js'),
}
Use ?name
?name=basename
var models = require('import?glob=models/*.js&name=basename')
// Its will like that
var models = {
User: require('models/User.js'),
Pet: require('models/Pet.js'),
}
?name=path
var models = require('import?glob=models/*.js&name=path')
// Its will like that
var models ={
'models/User.js': require('models/User.js'),
'models/Pet.js': require('models/Pet.js'),
}
Use ?dir
and ?ext
If you have flat list of files in directory, you can drequire like that:
import models from 'import?dir=models'
// or
import models from 'import?dir=models&ext=.server.js'
// or
import components from 'import?dir=components&ext=jsx'
Notes
- By default
?ext=js
- In
?dir
mode by default?name=basename
(if you want path, you need?name=path
)
Use glob .pattern
file
You can write your glob pattern If you have a large pattern file
- Create a glob pattern file (example:
models.pattern
)
./models/*.js
- Use glob file
var models = require('import!./models.pattern');
Note: Pattern is relative to the file directory.
Other Examples
TODO
- source map