Skip to content

Commit

Permalink
Vertical expansion mode
Browse files Browse the repository at this point in the history
Instead of horizontal expansions with commas, which is supported only in
Sass. Vertical expansions (duplicate line with different content of
quoted string) work for a wider number of cases.
  • Loading branch information
Aintaer committed Jul 11, 2015
1 parent 0c9a9b2 commit 3088c1f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
52 changes: 30 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
{
"name": "import-glob-loader",
"version": "1.0.0",
"description": "Globbing preloader for Webpack",
"main": "index.js",
"scripts": {
"prepublish": "babel src/index.js -o index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"webpack",
"loader",
"glob"
],
"author": "Alex Lee <[email protected]>",
"license": "ISC",
"dependencies": {
"glob": "^5.0.13",
"loader-utils": "^0.2.10"
},
"devDependencies": {
"babel": "^5.6.14"
}
"name": "import-glob-loader",
"version": "1.0.0",
"description": "Globbing preloader for Webpack",
"main": "index.js",
"scripts": {
"prepublish": "babel src/index.js -o index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"webpack",
"loader",
"glob"
],
"author": "Alex Lee <[email protected]>",
"license": "ISC",
"dependencies": {
"glob": "^5.0.13",
"loader-utils": "^0.2.10"
},
"devDependencies": {
"babel": "^5.6.14"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Aintaer/import-glob-loader.git"
},
"bugs": {
"url": "https://github.com/Aintaer/import-glob-loader/issues"
},
"homepage": "https://github.com/Aintaer/import-glob-loader#readme"
}
22 changes: 15 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@ export default function importGlob(source) {
options.nodir = typeof options.nodir !== 'undefined' ? options.nodir : true;
options.cwd = this.context;

let { test = "import", delimiter = ', ' } = options;
let { test = "import", delimiter = '\n' } = options;
const qualifier = new RegExp(`^.*\\b${test}\\b(.*)$`, 'gm');

function expandGlob(match, quote, content) {
if (!glob.hasMagic(content)) return match;
function expandGlob(result) {
if (!result) return;
const [match, quote, content] = result;
const offset = result.index;
const line = result.input;

if (!glob.hasMagic(content)) return;

let pre = line.slice(0, offset),
post = line.slice(offset + match.length);

return glob.sync(content, options)
.map(filename => `${quote}${filename}${quote}`)
.map(filename => `${pre}${quote}${filename}${quote}${post}`)
.join(delimiter);
}

const quotedString = /(['"])(.*?)\1/g;
const quotedString = /(['"])(.*?)\1/;
function expandLine(line, payload) {
if (!payload) return line;
return line.replace(quotedString, expandGlob);
if (!(payload && payload.trim())) return line;
return expandGlob(quotedString.exec(line)) || line;
}

return source.replace(qualifier, expandLine);
Expand Down

0 comments on commit 3088c1f

Please sign in to comment.