Skip to content

Commit

Permalink
refactor: change function arguments of the import option (#1124)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: function arguments of the `import` option were changed, it is now `funciton(url, media, resourcePath) {}`
  • Loading branch information
evilebottnawi authored Jul 22, 2020
1 parent c153fe6 commit 01e8c76
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
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

0 comments on commit 01e8c76

Please sign in to comment.