borodean / postcss-assets

An asset manager for PostCSS
MIT License
537 stars 32 forks source link

how shuold i use in my postcss-plugin #83

Closed cleverboy32 closed 5 years ago

cleverboy32 commented 5 years ago
import postcss from 'postcss';
import Import from 'postcss-import';
import Url from 'postcss-url';
import saladcssBem from 'saladcss-bem';
import Mixins from 'postcss-mixins';
import Nested from 'postcss-nested';
import Nesting from 'postcss-nesting';
import AdvancedVariables from 'postcss-advanced-variables';
import PropertyLookup from 'postcss-property-lookup';
import CssReset from 'postcss-css-reset';
import Shape from 'postcss-shape';
import Utils from 'postcss-utils';
import Extend from 'postcss-extend';
import CustomProperties from 'postcss-custom-properties';
import Calc from 'postcss-calc';
import Autoprefixer from 'autoprefixer';
import Assets from 'postcss-assets';

// let Assets be funtion
function assets (options) {
    return () => Assets(options);
}

// plugin chain
const plugins = [
    Import,
    Url,
    saladcssBem,
    Mixins,
    Nested,
    Nesting,
    AdvancedVariables,
    PropertyLookup,
    CssReset,
    Shape,
    Utils,
    Extend,
    CustomProperties,
    Calc,
    Autoprefixer,
    assets
];

const optKey = new Array(16);
optKey[0] = 'import'; 
optKey[2] = 'bem';
optKey[12] = 'customProperties';
optKey[14] = 'autoprefixer';
optKey[15] = 'assets';

export default postcss.plugin('postcss-ui-theme', rawopts => {
    const opts = Object.assign({ 
        autoprefixer: {
            browsers: ['ie > 8', 'last 2 versions']
        },
        assets: {
            basePath: './',
            relative: true
        }
    }, rawopts);

    // initialize all plugins
    const initializedPlugins = plugins.map(
        (plugin, i)  => plugin(opts[optKey[i]] || {})
    );

    return (root, result) => initializedPlugins.reduce(
        (promise, plugin) => {
            return promise.then(() => plugin(result.root, result));
        },
        Promise.resolve());

});

the resolve is not to transform be url

cleverboy32 commented 5 years ago

found that Assets(options) return Processor, not a function, and use

return plugin.process(result.root, result);

can work