1.loader 的配置 #

1.1 webpack config #

module: {
        rules: [
            {
                test: /\.css/,
                use: [
                    { loader: 'style-loader' },
                    { loader: 'css-loader' }
                ]
            }
        ]
}

1.2 inline内联 #

import 'style-loader!css-loader!style.css';

2.RuleSet #

[
    { type: 'javascript/auto', resolve: {} },
    { test: /\.mjs$/i,type: 'javascript/esm',resolve: { mainFields: [Array] } },
    { test: /\.json$/i, type: 'json' },
    { test: /\.wasm$/i, type: 'webassembly/experimental' }
]
class NormalModuleFactory {
    this.ruleSet = new RuleSet(options.defaultRules.concat(options.rules));
}

2.1 rule配置 #

static normalizeRule(rule, refs, ident) {
    let condition = {
        test: rule.test,
        include: rule.include,
        exclude: rule.exclude
    };
    newRule.resource = RuleSet.normalizeCondition(condition);
}
static normalizeCondition(condition) {
    if (typeof condition === "string") {
        return str => str.indexOf(condition) === 0;
    }
    if (typeof condition === "function") {
        return condition;
    }
    if (condition instanceof RegExp) {
        return condition.test.bind(condition);
    }
}
static normalizeUse(use, ident) {
    if (typeof use === "function") {
            return data => RuleSet.normalizeUse(use(data), ident);
    }
}

normalizeUse结果

[
    {loader:'style-loader'},
    {loader:'css-loader'}
]

rules:

 [
  {
    resource: [Function],
    resourceQuery: [Function],
    use: [  
      {loader:'style-loader'},
      {loader:'css-loader'}
    ]
  }
]

ruleSet.exec

const result = this.ruleSet.exec({
    resource: resourcePath,
    resourceQuery
});
[
    {
        type: 'use',
        value: {
            loader: 'style-loader',
            options: {}
        },
        enforce: undefined
    },
    {
        type: 'use',
        value: {
            loader: 'css-loader',
            options: {}
        },
        enforce: undefined
    }
]

3.创建模块 #

webpackloaderflow

4.编译模块 #

ruleloader