├── test ├── src │ ├── bar.md │ ├── foo.md │ ├── test.txt │ ├── a.js │ ├── b.js │ ├── baz.markdown │ └── c.js └── test.js ├── .npmignore ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── package.json ├── LICENSE ├── README.md └── index.js /test/src/bar.md: -------------------------------------------------------------------------------- 1 | bar 2 | -------------------------------------------------------------------------------- /test/src/foo.md: -------------------------------------------------------------------------------- 1 | foo 2 | -------------------------------------------------------------------------------- /test/src/test.txt: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /test/src/a.js: -------------------------------------------------------------------------------- 1 | var a = 'a'; 2 | -------------------------------------------------------------------------------- /test/src/b.js: -------------------------------------------------------------------------------- 1 | var b = 'b'; 2 | -------------------------------------------------------------------------------- /test/src/baz.markdown: -------------------------------------------------------------------------------- 1 | baz 2 | -------------------------------------------------------------------------------- /test/src/c.js: -------------------------------------------------------------------------------- 1 | var c = 'c'; 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Everything 2 | * 3 | 4 | !index.js 5 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | tags: 8 | - v*.*.* 9 | pull_request: 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v1 16 | - uses: actions/setup-node@v1 17 | with: 18 | node-version: '12.x' 19 | registry-url: 'https://registry.npmjs.org' 20 | - run: npm install 21 | - run: npm test 22 | - run: npm publish 23 | if: startsWith(github.ref, 'refs/tags/') 24 | env: 25 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "contents-loader", 3 | "version": "1.0.0", 4 | "description": "Webpack loader to import the contents of a directory.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "ava test/test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/erikdesjardins/contents-loader.git" 12 | }, 13 | "keywords": [ 14 | "webpack" 15 | ], 16 | "author": "Erik Desjardins", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/erikdesjardins/contents-loader/issues" 20 | }, 21 | "homepage": "https://github.com/erikdesjardins/contents-loader#readme", 22 | "peerDependencies": { 23 | "webpack": ">=2.2.0" 24 | }, 25 | "dependencies": { 26 | "loader-utils": "^1.0.2" 27 | }, 28 | "devDependencies": { 29 | "ava": "^0.18.2" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Erik Desjardins 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # contents-loader 2 | 3 | # ⚠ doesn't work ⚠ 4 | 5 | Webpack loader to import the contents of a directory. 6 | 7 | For a directory structure like: 8 | 9 | ``` 10 | └── abc 11 | ├── foo.js 12 | ├── bar.js 13 | ├── baz.js 14 | ├── somethingElse.txt 15 | └── styles.css 16 | ``` 17 | 18 | contents-loader?path=./abc/!` generates code like: 19 | 20 | ```js 21 | import * as _0 from "./abc/foo.js"; 22 | import * as _1 from "./abc/bar.js"; 23 | import * as _2 from "./abc/baz.js"; 24 | export default { "foo.js": _0, "bar.js": _1, "baz.js": _2 }; 25 | ``` 26 | 27 | If you're using vanilla Webpack, this loader has little advantage over the [built-in](https://webpack.js.org/guides/dependency-management/#require-context) `require.context('./abc', false, /\.js$/)`. If, however, you're using [`webpack-rollup-loader`](https://github.com/erikdesjardins/webpack-rollup-loader) or similar, `require.context` will only be processed by Webpack, resulting in suboptimal code generation. 28 | 29 | ## Installation 30 | 31 | `npm install --save-dev contents-loader` 32 | 33 | ## Usage 34 | 35 | ```js 36 | // trailing `!` is important 37 | import modules from 'contents-loader?path=./path/to/directory!'; 38 | 39 | // use the `match` option to specify which files to import 40 | // (defaults to `/\.js$/i`) 41 | import images from 'contents-loader?path=./path/to/images&match=\\.png$!'; 42 | ``` 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Erik Desjardins 3 | * See LICENSE file in root directory for full license. 4 | */ 5 | 6 | 'use strict'; 7 | 8 | var fs = require('fs'); 9 | var path = require('path'); 10 | var loaderUtils = require('loader-utils'); 11 | 12 | module.exports = function() {}; 13 | 14 | // https://github.com/webpack/webpack/blob/5abfeea64bcfe4f60f1c79eb1ba95686dc857faa/lib/NormalModuleFactory.js#L118 15 | // for using `pitching-loader?query!` (no resource) 16 | 17 | module.exports.pitch = function() { 18 | var callback = this.async(); 19 | 20 | var options = Object.assign({ match: /\.js$/i }, loaderUtils.getOptions(this)); 21 | var match = typeof options.match === 'string' ? new RegExp(options.match, 'i') : options.match; 22 | var context = path.join(this.context || this._module.issuer.context, options.path); 23 | 24 | // add context dependency so new files are picked up 25 | this.addContextDependency(context); 26 | 27 | fs.readdir(context, function(err, files) { 28 | if (err) return callback(err); 29 | 30 | var matchingFiles = files 31 | .filter(function(filename) { 32 | return match.test(filename); 33 | }) 34 | .map(function(filename, i) { 35 | return { 36 | name: filename, 37 | id: '_' + i 38 | }; 39 | }); 40 | 41 | var header = '/* generated by contents-loader */'; 42 | 43 | var importStatements = matchingFiles 44 | .map(function(info) { 45 | return 'import * as ' + info.id + ' from ' + JSON.stringify(path.join(context, info.name)) + ';'; 46 | }) 47 | .join('\n'); 48 | 49 | var exportStatement = 'export default { ' + matchingFiles 50 | .map(function(info) { 51 | return JSON.stringify(info.name) + ": " + info.id; 52 | }) 53 | .join(', ') + ' };'; 54 | 55 | callback(null, [header, importStatements, exportStatement].join('\n')); 56 | }); 57 | 58 | return ''; 59 | }; 60 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import test from 'ava'; 3 | 4 | import loader from '../index'; 5 | 6 | function runLoader(options) { 7 | return new Promise((resolve, reject) => { 8 | loader.pitch.call({ 9 | query: options, 10 | context: __dirname, 11 | async: () => (err, result) => err ? reject(err) : resolve(result), 12 | addContextDependency: () => {}, 13 | }); 14 | }); 15 | } 16 | 17 | test('invalid path', t => { 18 | t.throws(runLoader('?path=./notARealPath/'), /ENOENT/); 19 | }); 20 | 21 | test('adds a context dependency', async t => { 22 | t.plan(2); 23 | 24 | await new Promise(r => { 25 | loader.pitch.call({ 26 | query: '?path=./src/', 27 | context: __dirname, 28 | async: () => (err, result) => { t.truthy(result); r(); }, 29 | addContextDependency: path => t.is(path, './src/'), 30 | }); 31 | }); 32 | }); 33 | 34 | const header = '/* generated by contents-loader */'; 35 | 36 | test('path option - string', async t => { 37 | t.is( 38 | await runLoader('?path=./src/'), 39 | ` 40 | ${header} 41 | import * as _0 from "./src/a.js"; 42 | import * as _1 from "./src/b.js"; 43 | import * as _2 from "./src/c.js"; 44 | export default { "a.js": _0, "b.js": _1, "c.js": _2 }; 45 | `.trim() 46 | ); 47 | }); 48 | 49 | test('path option - object', async t => { 50 | t.is( 51 | await runLoader({ path: './src/' }), 52 | ` 53 | ${header} 54 | import * as _0 from "./src/a.js"; 55 | import * as _1 from "./src/b.js"; 56 | import * as _2 from "./src/c.js"; 57 | export default { "a.js": _0, "b.js": _1, "c.js": _2 }; 58 | `.trim() 59 | ); 60 | }); 61 | 62 | test('match option - string', async t => { 63 | t.is( 64 | await runLoader('?path=./src/&match=\\.txt$'), 65 | ` 66 | ${header} 67 | import * as _0 from "./src/test.txt"; 68 | export default { "test.txt": _0 }; 69 | `.trim() 70 | ); 71 | }); 72 | 73 | test('match option - regex', async t => { 74 | t.is( 75 | await runLoader({ path: './src/', match: /\.(md|markdown)$/ }), 76 | ` 77 | ${header} 78 | import * as _0 from "./src/bar.md"; 79 | import * as _1 from "./src/baz.markdown"; 80 | import * as _2 from "./src/foo.md"; 81 | export default { "bar.md": _0, "baz.markdown": _1, "foo.md": _2 }; 82 | `.trim() 83 | ); 84 | }); 85 | --------------------------------------------------------------------------------