Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat(url): add url() filter support (options.url)
  • Loading branch information
michael-ciniawsky committed Jan 8, 2018
commit 16e5130ccbadc7a61154d0e9e2f013ca992a2578
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default `
}
`
```

#### `{Boolean}`

To disable `url()` resolving by `css-loader` set the option to `false`
Expand All @@ -88,6 +89,32 @@ To disable `url()` resolving by `css-loader` set the option to `false`
}
```

#### `{RegExp}`
Copy link

@stereokai stereokai Jan 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few words about what these options do would be smashing!
@d3viant0ne @michael-ciniawsky

Copy link
Member Author

@michael-ciniawsky michael-ciniawsky Jan 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah yeah :) I intend to add appropriate documentation before this goes anywhere, it hasn't even been signed-off by other maintainers nor released yet... 😛

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man, was already hoping to use it today 😆 😭


**webpack.config.js**
```js
{
loader: 'css-loader',
options: {
url: /filter/
}
}
```

#### `{Function}`

**webpack.config.js**
```js
{
loader: 'css-loader',
options: {
url (url) {
return /filter/.test(url)
}
}
}
```

### `import`

```css
Expand Down
80 changes: 38 additions & 42 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getOptions } from 'loader-utils';
import validateOptions from 'schema-utils';

import postcss from 'postcss';
// TODO(michael-ciniawsky)
// TODO(michael-ciniawsky)
// replace with postcss-icss-{url, import}
import urls from './plugins/url';
import imports from './plugins/import';
Expand All @@ -26,13 +26,9 @@ const DEFAULTS = {
sourceMap: false,
};

export default function loader (css, map, meta) {
export default function loader(css, map, meta) {
// Loader Options
const options = Object.assign(
{},
DEFAULTS,
getOptions(this)
);
const options = Object.assign({}, DEFAULTS, getOptions(this));

validateOptions(schema, options, 'CSS Loader');

Expand All @@ -52,9 +48,8 @@ export default function loader (css, map, meta) {

// URL Plugin
if (options.url) {
plugins.push(urls());
plugins.push(urls(options));
}


// Import Plugin
if (options.import) {
Expand All @@ -65,7 +60,7 @@ export default function loader (css, map, meta) {
if (options.minimize) {
plugins.push(minifier());
}

if (meta) {
const { ast } = meta;
// Reuse CSS AST (PostCSS AST e.g 'postcss-loader')
Expand All @@ -74,34 +69,33 @@ export default function loader (css, map, meta) {
css = ast.root;
}
}
map = options.sourceMap

map = options.sourceMap
? {
prev: map || false,
inline: false,
annotation: false,
sourcesContent: true,
}
: false
prev: map || false,
inline: false,
annotation: false,
sourcesContent: true,
}
: false;

return postcss(plugins)
.process(css, {
from: `/css-loader!${file}`,
map,
to: file,
}).then(({ css, map, messages }) => {
})
.then(({ css, map, messages }) => {
if (meta && meta.messages) {
messages = messages.concat(meta.messages)
messages = messages.concat(meta.messages);
}

// CSS Imports
const imports = messages
.filter((msg) => msg.type === 'import' ? msg : false)
.filter((msg) => (msg.type === 'import' ? msg : false))
.reduce((imports, msg) => {
try {
msg = typeof msg.import === 'function'
? msg.import()
: msg.import;
msg = typeof msg.import === 'function' ? msg.import() : msg.import;

imports += msg;
} catch (err) {
Expand All @@ -110,17 +104,15 @@ export default function loader (css, map, meta) {
this.emitError(err);
}

return imports
}, '')
return imports;
}, '');

// CSS Exports
const exports = messages
.filter((msg) => msg.type === 'export' ? msg : false)
.reduce((exports, msg) => {
try {
msg = typeof msg.export === 'function'
? msg.export()
: msg.export;
.filter((msg) => (msg.type === 'export' ? msg : false))
.reduce((exports, msg) => {
try {
msg = typeof msg.export === 'function' ? msg.export() : msg.export;

exports += msg;
} catch (err) {
Expand All @@ -130,23 +122,27 @@ export default function loader (css, map, meta) {
}

return exports;
}, '')
// TODO(michael-ciniawsky)
}, '');

// TODO(michael-ciniawsky)
// triage if and add CSS runtime back
const result = [
imports ? `// CSS Imports\n${imports}\n` : false,
exports ? `// CSS Exports\n${exports}\n` : false,
`// CSS\nexport default \`${css}\``
`// CSS\nexport default \`${css}\``,
]
.filter(Boolean)
.join('\n');

cb(null, result, map ? map.toJSON() : null);

return null;
})
.catch((err) => {
err.name === 'CssSyntaxError' ? cb(new SyntaxError(err)) : cb(err);
err = err.name === 'CssSyntaxError' ? new SyntaxError(err) : err;

cb(err);

return null;
});
};
}
20 changes: 0 additions & 20 deletions src/lib/Error.js

This file was deleted.

98 changes: 0 additions & 98 deletions src/lib/plugins/import.js

This file was deleted.

Loading