Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change function arguments of the import option #1124

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,11 @@ module.exports = {
test: /\.css$/i,
loader: 'css-loader',
options: {
import: (parsedImport, resourcePath) => {
// parsedImport.url - url of `@import`
// parsedImport.media - media query of `@import`
import: (url, media, resourcePath) => {
// resourcePath - path to css file

// Don't handle `style.css` import
if (parsedImport.url.includes('style.css')) {
if (url.includes('style.css')) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/postcss-import-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default postcss.plugin(pluginName, (options) => async (css, result) => {
media = valueParser.stringify(mediaNodes).trim().toLowerCase();
}

if (options.filter && !options.filter({ url: normalizedUrl, media })) {
if (options.filter && !options.filter(normalizedUrl, media)) {
// eslint-disable-next-line no-continue
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ function requestify(url, rootContext) {
}

function getFilter(filter, resourcePath) {
return (item) => {
return (...args) => {
if (typeof filter === 'function') {
return filter(item, resourcePath);
return filter(...args, resourcePath);
}

return true;
Expand Down
12 changes: 9 additions & 3 deletions test/import-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ describe('"import" option', () => {

it('should work when "Function"', async () => {
const compiler = getCompiler('./import/import.js', {
import: (parsedImport, resourcePath) => {
expect(typeof resourcePath === 'string').toBe(true);
import: (url, media, resourcePath) => {
expect(url).toBeDefined();

if (url === 'test-nested-media.css') {
expect(media).toBeDefined();
}

expect(resourcePath).toBeDefined();

// Don't handle `test.css`
if (parsedImport.url.includes('test.css')) {
if (url.includes('test.css')) {
return false;
}

Expand Down