| 1 | 'use strict';
|
|---|
| 2 | const browserslist = require('browserslist');
|
|---|
| 3 | const plugins = require('./plugins');
|
|---|
| 4 |
|
|---|
| 5 | /** @typedef {{lint?: boolean}} Options */
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * @type {import('postcss').PluginCreator<Options>}
|
|---|
| 9 | * @param {Options} opts
|
|---|
| 10 | * @return {import('postcss').Plugin}
|
|---|
| 11 | */
|
|---|
| 12 | function pluginCreator(opts = {}) {
|
|---|
| 13 | return {
|
|---|
| 14 | postcssPlugin: 'stylehacks',
|
|---|
| 15 |
|
|---|
| 16 | OnceExit(css, { result }) {
|
|---|
| 17 | /** @type {typeof result.opts & browserslist.Options} */
|
|---|
| 18 | const resultOpts = result.opts || {};
|
|---|
| 19 | const browsers = browserslist(null, {
|
|---|
| 20 | stats: resultOpts.stats,
|
|---|
| 21 | path: __dirname,
|
|---|
| 22 | env: resultOpts.env,
|
|---|
| 23 | });
|
|---|
| 24 |
|
|---|
| 25 | /** @type {import('./plugin').Plugin[]} */
|
|---|
| 26 | const processors = [];
|
|---|
| 27 | for (const Plugin of plugins) {
|
|---|
| 28 | const hack = new Plugin(result);
|
|---|
| 29 | if (!browsers.some((browser) => hack.targets.has(browser))) {
|
|---|
| 30 | processors.push(hack);
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 | css.walk((node) => {
|
|---|
| 34 | processors.forEach((proc) => {
|
|---|
| 35 | if (!proc.nodeTypes.has(node.type)) {
|
|---|
| 36 | return;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | if (opts.lint) {
|
|---|
| 40 | return proc.detectAndWarn(node);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | return proc.detectAndResolve(node);
|
|---|
| 44 | });
|
|---|
| 45 | });
|
|---|
| 46 | },
|
|---|
| 47 | };
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /** @type {(node: import('postcss').Node) => boolean} */
|
|---|
| 51 | pluginCreator.detect = (node) => {
|
|---|
| 52 | return plugins.some((Plugin) => {
|
|---|
| 53 | const hack = new Plugin();
|
|---|
| 54 |
|
|---|
| 55 | return hack.any(node);
|
|---|
| 56 | });
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | pluginCreator.postcss = true;
|
|---|
| 60 | module.exports = pluginCreator;
|
|---|