From 01e8c7683a5f66eda7e050896a383099d6ea14e3 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 22 Jul 2020 20:28:33 +0300 Subject: [PATCH] refactor: change function arguments of the `import` option (#1124) BREAKING CHANGE: function arguments of the `import` option were changed, it is now `funciton(url, media, resourcePath) {}` --- README.md | 6 ++---- src/plugins/postcss-import-parser.js | 2 +- src/utils.js | 4 ++-- test/import-option.test.js | 12 +++++++++--- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index daec7159..7dcaae67 100644 --- a/README.md +++ b/README.md @@ -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; } diff --git a/src/plugins/postcss-import-parser.js b/src/plugins/postcss-import-parser.js index 71dc394c..297c70cf 100644 --- a/src/plugins/postcss-import-parser.js +++ b/src/plugins/postcss-import-parser.js @@ -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; } diff --git a/src/utils.js b/src/utils.js index 506aac47..6441cc7f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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; diff --git a/test/import-option.test.js b/test/import-option.test.js index 6a3d4198..6a154754 100644 --- a/test/import-option.test.js +++ b/test/import-option.test.js @@ -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; }