简易的 less、css、style loader

    1. // local-less-loader
    2. const less = require("less");
    3. module.exports = function (source) {
    4. less.render(source, (e, output) => {
    5. this.callback(e, output.css);
    6. });
    7. };
    8. // local-css-loader
    9. module.exports = function (source) {
    10. // 有一些符号需要转一下
    11. return JSON.stringify(source);
    12. };
    13. // local-style-loader
    14. module.exports = function (source) {
    15. const res = `
    16. const e = document.createElement('style');
    17. e.innerHTML = ${source};
    18. document.head.appendChild(e)
    19. `;
    20. return res;
    21. };

    使用,

    1. module.exports = {
    2. module: {
    3. rules: [
    4. {
    5. test: /\.css$/,
    6. use: ["local-style-loader", "local-css-loader"],
    7. },
    8. {
    9. test: /\.less$/,
    10. use: [
    11. "local-style-loader",
    12. "local-css-loader",
    13. "local-postcss-loader",
    14. "local-less-loader",
    15. ],
    16. },
    17. ],
    18. },
    19. resolveLoader: {
    20. modules: ["./node_modules", "./customedLoaders"],
    21. },
    22. };