Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Allow style-loader to accept loaders #566

Merged
merged 4 commits into from
Dec 8, 2017
Merged
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
47 changes: 35 additions & 12 deletions packages/style-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ module.exports = (neutrino, opts = {}) => {
ruleId: 'style',
styleUseId: 'style',
cssUseId: 'css',
css: { importLoaders: 0 },
style: {},
hot: true,
hotUseId: 'hot',
modules: true,
modulesSuffix: '-modules',
modulesTest: cssModulesTest,
loaders: [],
extractId: 'extract',
extract: {
plugin: {
Expand Down Expand Up @@ -52,23 +55,43 @@ module.exports = (neutrino, opts = {}) => {
}

rules.forEach(options => {
neutrino.config.module
.rule(options.ruleId)
const styleRule = neutrino.config.module.rule(options.ruleId);
const loaders = [
{
loader: require.resolve('style-loader'),
options: options.style,
useId: options.styleUseId
},
{
loader: require.resolve('css-loader'),
options: Object.assign(options.css, {
importLoaders: options.css.importLoaders + options.loaders.length
}),
useId: options.cssUseId
},
...options.loaders
]
.map((loader, index) => {
const obj = typeof loader === 'object' ? loader : { loader };

return Object.assign(obj, {
useId: obj.useId || `${options.cssUseId}-${index}`
Copy link
Member

Choose a reason for hiding this comment

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

I like this, but this will need to be clearly noted in the documentation, that without the useId, it will generate an id in this format.

Copy link
Member

@eliperelman eliperelman Dec 8, 2017

Choose a reason for hiding this comment

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

I already have the docs written in #574.

});
});

loaders.forEach(loader => {
styleRule
.test(options.test)
.use(options.styleUseId)
.loader(require.resolve('style-loader'))
.when(options.style, use => use.options(options.style))
.end()
.use(options.cssUseId)
.loader(require.resolve('css-loader'))
.when(options.css, use => use.options(options.css));
.use(loader.useId)
.loader(loader.loader)
.when(loader.options, use => use.options(loader.options));
});

if (options.extract) {
const styleRule = neutrino.config.module.rule(options.ruleId);
const styleEntries = styleRule.uses.entries();
const useKeys = Object.keys(styleEntries).filter(key => key !== options.styleUseId);
const useIds = Object.keys(styleEntries).filter(key => key !== options.styleUseId);
const extractLoader = Object.assign({
use: useKeys.map(key => ({
use: useIds.map(key => ({
loader: styleEntries[key].get('loader'),
options: styleEntries[key].get('options')
})),
Expand Down