Skip to content

Commit

Permalink
actually skip loader if didn't pass test
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Jan 10, 2018
1 parent f138f7f commit 4c3688d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ Use a loader, currently built-in loaders are:
- `stylus` (TODO)
- `less` (TODO)

They are executed from right to left.

### loaders

Type: `Loader[]`
Expand Down
10 changes: 9 additions & 1 deletion src/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ export default class Loaders {
id,
sourceMap
}
return v => loader.process.call(loaderContext, v)
return v => {
// Only process if it's postcss loader
// Or passed `test`
if (name === 'postcss' || loader.test.test(id)) {
return loader.process.call(loaderContext, v)
}
// Otherwise directly return input value
return v
}
}), { code, map })
}

Expand Down
40 changes: 40 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,46 @@ console.log(css, css$1);
"
`;

exports[`skip-loader: js code 1`] = `
"'use strict';
function __$styleInject(css, ref) {
if ( ref === void 0 ) ref = {};
var insertAt = ref.insertAt;
if (!css) { return }
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild);
} else {
head.appendChild(style);
}
} else {
head.appendChild(style);
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}
var css = \\"body {\\\\n color: red;\\\\n}\\\\n\\";
__$$styleInject(css);
var css$1 = \\".bar {\\\\n color: red;\\\\n}\\\\n\\";
__$$styleInject(css$1);
console.log(css, css$1);
"
`;

exports[`sourcemap:inline: js code 1`] = `
"'use strict';
Expand Down
17 changes: 17 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,20 @@ snapshot({
]
}
})

snapshot({
title: 'skip-loader',
input: 'simple/index.js',
options: {
use: ['loader'],
loaders: [
{
name: 'loader',
test: /\.random$/,
process() {
return 'lol'
}
}
]
}
})

0 comments on commit 4c3688d

Please sign in to comment.