Skip to content

Commit

Permalink
adds the first version
Browse files Browse the repository at this point in the history
  • Loading branch information
gjbkz committed Apr 8, 2017
1 parent 74c766c commit f8bed8b
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
npm-debug.log
coverage
18 changes: 18 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Copyright 2017 Kei Ito ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 changes: 45 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const path = require('path');
const promisify = require('j1/promisify');
const console = require('j1/console').create('globImport');
const {createFilter} = require('rollup-pluginutils');
const glob = promisify(require('glob'));

function getPseudoName(importeePatten) {
return importeePatten
.split(path.sep)
.join('_')
.replace(/[*.]/g, (char) => {
return char.codePointAt(0).toString(16);
});
}

function globImport({include, exclude, debug}) {
const filter = createFilter(include, exclude);
const codes = {};
return {
resolveId: function (importee, importer = '') {
if (!filter(importee) || importee.indexOf('*') < 0) {
return null;
}
const dir = path.dirname(importer);
return glob(path.join(dir, importee))
.then((files) => {
const code = files
.map((file) => {
return `import './${path.relative(dir, file)}'`;
}).join('\n');
const id = path.join(dir, getPseudoName(importee));
if (debug) {
console.info(`${importee}\n${code}`);
}
codes[id] = code;
return id;
});
},
load: (id) => {
return codes[id];
}
};
}

module.exports = globImport;
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "rollup-plugin-glob-import",
"version": "0.0.1",
"description": "Enables import '../*/test.js'",
"license": "MIT",
"repository": "kei-ito/rollup-plugin-glob-import",
"author": {
"name": "Kei Ito",
"email": "[email protected]",
"url": "https://github.com/kei-ito"
},
"keywords": [
"rollup",
"glob"
],
"engines": {
"node": ">=6"
},
"scripts": {
"test": "mocha ./test/*/test.js",
"test_coverage": "istanbul cover _mocha ./test/*/test.js"
},
"dependencies": {
"glob": "^7.1.1",
"j1": "^0.0.25",
"rollup-pluginutils": "^2.0.1"
},
"devDependencies": {
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"rollup": "^0.41.4"
}
}
1 change: 1 addition & 0 deletions test/001/deps/001.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('001');
1 change: 1 addition & 0 deletions test/001/deps/002.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('002');
1 change: 1 addition & 0 deletions test/001/deps/003.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('003');
1 change: 1 addition & 0 deletions test/001/src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './deps/*.js';
19 changes: 19 additions & 0 deletions test/001/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const path = require('path');
const assert = require('assert');
const {rollup} = require('rollup');
const globImport = require('../..');

it('should parse *', async function () {
const bundle = await rollup({
entry: path.join(__dirname, 'src.js'),
plugins: [
globImport({debug: true})
]
});
const {code} = bundle.generate({format: 'es'});
assert.deepEqual(code.trim().split(/\s+/), [
'console.log(\'001\');',
'console.log(\'002\');',
'console.log(\'003\');'
]);
});

0 comments on commit f8bed8b

Please sign in to comment.