Skip to content

Commit

Permalink
precompile stylelint-config-swissquote with esm
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Feb 4, 2024
1 parent 609a8d1 commit f011c0a
Show file tree
Hide file tree
Showing 26 changed files with 296 additions and 102 deletions.
117 changes: 117 additions & 0 deletions packages/stylelint-config-swissquote/build.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { getExternals } from "../../utils/externals.js";

const commonExternals = {
// Provided by other Crafty packages
...getExternals(),

stylelint: "../../shims/stylelint.js",
"stylelint/lib/utils/optionsMatches":
"../stylelint-utils/stylelint-optionsMatches.js",
};

const FAKE_PRETTIER_PARSER = "../../shims/prettier-parser.js";

export default [
(builder) =>
builder("stylelint-prettier")
.esm()
.package()
.externals({
...commonExternals,

// We don't need most prettier parsers
"./parser-angular.js": FAKE_PRETTIER_PARSER,
"./parser-babel.js": FAKE_PRETTIER_PARSER,
"./parser-espree.js": FAKE_PRETTIER_PARSER,
"./parser-flow.js": FAKE_PRETTIER_PARSER,
"./parser-glimmer.js": FAKE_PRETTIER_PARSER,
"./parser-graphql.js": FAKE_PRETTIER_PARSER,
"./parser-html.js": FAKE_PRETTIER_PARSER,
"./parser-markdown.js": FAKE_PRETTIER_PARSER,
"./parser-meriyah.js": FAKE_PRETTIER_PARSER,
"./parser-typescript.js": FAKE_PRETTIER_PARSER,
"./parser-yaml.js": FAKE_PRETTIER_PARSER,
}),
(builder) => builder("postcss-selector-parser").esm().package(),
(builder) => builder("postcss-resolve-nested-selector").esm().package(),
(builder) => builder("postcss-value-parser").esm().package(),
(builder) =>
builder("stylelint-scss")
.esm()
.package()
.externals({
...commonExternals,

// We use other packages created here
"postcss-selector-parser": "../postcss-selector-parser/index.mjs",
"postcss-value-parser": "../postcss-value-parser/index.mjs",
"postcss-resolve-nested-selector":
"../postcss-resolve-nested-selector/index.mjs",

// We mock the few lodash functions really used
lodash: "../../shims/lodash.js",
}),
(builder) =>
builder("stylelint-no-unsupported-browser-features")
.esm()
.externals({
...commonExternals,

postcss: "postcss",
"postcss/lib/result": "postcss/lib/result",
"postcss/lib/list": "postcss/lib/list",

// We keep postcss-scss external so we can have it in common with preset-postcss
"postcss-scss": "postcss-scss",

// Used by stylelint-no-unsupported-browser-features
"caniuse-lite": "caniuse-lite",
"/caniuse-lite(/.*)/": "caniuse-lite$1",

// We mock the few lodash functions really used
lodash: "../../shims/lodash.js",
})
.package(),
(builder) =>
builder("stylelint-utils")
.esm()
.packages((pkg) =>
pkg
.package(
"stylelint/lib/utils/declarationValueIndex.mjs",
"declarationValueIndex",
"dist/stylelint-utils/stylelint-declarationValueIndex.js"
)
.package(
"stylelint/lib/utils/isStandardSyntaxFunction.mjs",
"isStandardSyntaxFunction",
"dist/stylelint-utils/stylelint-isStandardSyntaxFunction.js"
)
.package(
"stylelint/lib/utils/isStandardSyntaxRule.mjs",
"isStandardSyntaxRule",
"dist/stylelint-utils/stylelint-isStandardSyntaxRule.js"
)
.package(
"stylelint/lib/utils/isStandardSyntaxSelector.mjs",
"isStandardSyntaxSelector",
"dist/stylelint-utils/stylelint-isStandardSyntaxSelector.js"
)
.package(
"stylelint/lib/utils/isKeyframeSelector.mjs",
"isKeyframeSelector",
"dist/stylelint-utils/stylelint-isKeyframeSelector.js"
)
.package(
"stylelint/lib/utils/optionsMatches.mjs",
"optionMatches",
"dist/stylelint-utils/stylelint-optionsMatches.js"
)
)
.externals({
"../reference/keywordSets": "../../shims/stylelint-keywordSets.js",
})
.options({
sourceMap: false,
}),
];
6 changes: 3 additions & 3 deletions packages/stylelint-config-swissquote/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ function warn(value, options) {
export default {
plugins: [
path.join(__dirname, "index.js"),
require.resolve("stylelint-prettier"),
require.resolve("stylelint-scss"),
require.resolve("stylelint-no-unsupported-browser-features")
require.resolve("./packages/stylelint-prettier.js"),
require.resolve("./packages/stylelint-scss.js"),
require.resolve("./packages/stylelint-no-unsupported-browser-features.js")
],
rules: {
// Formatting
Expand Down
2 changes: 1 addition & 1 deletion packages/stylelint-config-swissquote/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import stylelint from "stylelint";
import stylelint from "./shims/stylelint.js";

import noBlockInsideBlock from "./src/rules/no-block-inside-block.js";
import noUtilityReassignment from "./src/rules/no-utility-reassignment.js";
Expand Down
19 changes: 11 additions & 8 deletions packages/stylelint-config-swissquote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@
"@swissquote/crafty": "1.24.0",
"caniuse-lite": "^1.0.30001582",
"postcss-resolve-nested-selector": "0.1.1",
"postcss-scss": "4.0.9",
"postcss-scss": "4.0.9"
},
"devDependencies": {
"ava": "6.1.1",
"c8": "9.1.0",
"postcss": "8.4.33",
"postcss-resolve-nested-selector": "0.1.1",
"postcss-selector-parser": "6.0.15",
"postcss-value-parser": "4.2.0",
"prettier": "3.2.4",
"stylelint": "16.2.1",
"stylelint-no-unsupported-browser-features": "8.0.1",
"stylelint-prettier": "5.0.0",
"stylelint-scss": "6.1.0"
},
"devDependencies": {
"ava": "6.1.1",
"c8": "9.1.0",
"postcss": "8.4.33",
"prettier": "3.2.4"
},
"peerDependencies": {
"@swissquote/crafty-commons": "1.24.0",
"postcss": "^8.0.0"
"postcss": "^8.0.0",
"stylelint": "^16.0.0"
},
"scripts": {
"lint": "node ../crafty-preset-eslint/src/commands/jsLint.js --preset recommended --preset node '*.js' src",
"build": "node ../../utils/build.mjs",
"test": "ava",
"test:ci": "c8 --clean --reporter=text --reporter=lcov --reporter=html --include=src --include=packages --include='*.js' ava"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../dist/postcss-resolve-nested-selector/index.mjs";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../dist/postcss-selector-parser/index.mjs";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../dist/postcss-value-parser/index.mjs";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../dist/stylelint-no-unsupported-browser-features/index.mjs";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../dist/stylelint-prettier/index.mjs";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../dist/stylelint-scss/index.mjs";
36 changes: 36 additions & 0 deletions packages/stylelint-config-swissquote/shims/lodash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Since the used modules only use a tiny fraction of lodash we don't want that huge module
// Reimplement them here waiting for https://github.com/stylelint-scss/stylelint-scss/pull/554 to be finished

export function isBoolean(value) {
return typeof value === "boolean" || value instanceof Boolean;
}

export function isNumber(value) {
return typeof value === "number" || value instanceof Number;
}

export function isRegExp(value) {
return value instanceof RegExp;
}

export function isString(value) {
return typeof value === "string" || value instanceof String;
}

/**
* All credits go to https://github.com/developit/dlv
*/
/* eslint-disable no-param-reassign */
export function get(obj, key, def, p, undef) {
key = key.split ? key.split(".") : key;
for (p = 0; p < key.length; p++) {
obj = obj ? obj[key[p]] : undef;
}
return obj === undef ? def : obj;
}

export function pick(obj, keys) {
return Object.fromEntries(
Object.entries(obj).filter((entry) => keys.indexOf(entry[0]) > -1)
);
}
10 changes: 10 additions & 0 deletions packages/stylelint-config-swissquote/shims/prettier-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
parsers: new Proxy(
{},
{
get: function (target, prop, receiver) {
return {};
},
}
),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const keywordSets = {};

// Include only these keywords
keywordSets.keyframeSelectorKeywords = new Set(['from', 'to']);

export default keywordSets;
23 changes: 23 additions & 0 deletions packages/stylelint-config-swissquote/shims/stylelint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Stylelint is bundled with Crafty
// If this package is used within Crafty, we need to use it from "@swissquote/crafty-preset-stylelint"
// Otherwise we fallback in loading stylelint directly

let imported;

try {
imported = await import("@swissquote/crafty-preset-stylelint/packages/stylelint.js");
} catch (e) {
imported = await import("stylelint");
}

export const postcss= true;
export const lint = imported.default.lint;
export const rules = imported.default.rules;
export const formatters = imported.default.formatters;
export const createPlugin = imported.default.createPlugin;
export const resolveConfig = imported.default.resolveConfig;
export const utils = imported.default.utils;
export const reference = imported.default.reference;

export default imported.default;

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stylelint from "stylelint";
import stylelint from "../../shims/stylelint.js";

import selectorParser from "postcss-selector-parser";
import resolveNestedSelector from "postcss-resolve-nested-selector";
import selectorParser from "../../packages/postcss-selector-parser.js";
import resolveNestedSelector from "../../packages/postcss-resolve-nested-selector.js";

import cssRuleHasSelectorEndingWithColon from "../utils/cssRuleHasSelectorEndingWithColon.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stylelint from "stylelint";

import selectorParser from "postcss-selector-parser";
import resolveNestedSelector from "postcss-resolve-nested-selector";
import selectorParser from "../../packages/postcss-selector-parser.js";
import resolveNestedSelector from "../../packages/postcss-resolve-nested-selector.js";

import cssRuleHasSelectorEndingWithColon from "../utils/cssRuleHasSelectorEndingWithColon.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import stylelint from "stylelint";
import stylelint from "../../shims/stylelint.js";

import declarationValueIndex from "stylelint/lib/utils/declarationValueIndex.mjs";
import isStandardSyntaxFunction from "stylelint/lib/utils/isStandardSyntaxFunction.mjs";
import valueParser from "postcss-value-parser";
import declarationValueIndex from "../../dist/stylelint-utils/stylelint-declarationValueIndex.js";
import isStandardSyntaxFunction from "../../dist/stylelint-utils/stylelint-isStandardSyntaxFunction.js";
import valueParser from "../../packages/postcss-value-parser.js";

const ruleName = "swissquote/no-negative-var";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stylelint from "stylelint";
import stylelint from "../../shims/stylelint.js";

import selectorParser from "postcss-selector-parser";
import resolveNestedSelector from "postcss-resolve-nested-selector";
import selectorParser from "../../packages/postcss-selector-parser.js";
import resolveNestedSelector from "../../packages/postcss-resolve-nested-selector.js";

import cssRuleHasSelectorEndingWithColon from "../utils/cssRuleHasSelectorEndingWithColon.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stylelint from "stylelint";
import isKeyframeSelector from "stylelint/lib/utils/isKeyframeSelector.mjs";
import isStandardSyntaxRule from "stylelint/lib/utils/isStandardSyntaxRule.mjs";
import isStandardSyntaxSelector from "stylelint/lib/utils/isStandardSyntaxSelector.mjs";
import stylelint from "../../shims/stylelint.js";
import isKeyframeSelector from "../../dist/stylelint-utils/stylelint-isKeyframeSelector.js";
import isStandardSyntaxRule from "../../dist/stylelint-utils/stylelint-isStandardSyntaxRule.js";
import isStandardSyntaxSelector from "../../dist/stylelint-utils/stylelint-isStandardSyntaxSelector.js";
import resolveNestedSelector from "../utils/resolveNestedSelector.js";
import parseSelector from "../utils/parseSelector.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stylelint from "stylelint";
import stylelint from "../../shims/stylelint.js";

import selectorParser from "postcss-selector-parser";
import resolveNestedSelector from "postcss-resolve-nested-selector";
import selectorParser from "../../packages/postcss-selector-parser.js";
import resolveNestedSelector from "../../packages/postcss-resolve-nested-selector.js";

import cssRuleHasSelectorEndingWithColon from "../utils/cssRuleHasSelectorEndingWithColon.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createRequire } from "node:module";
import stylelint from "stylelint";
import stylelint from "../../shims/stylelint.js";
import browserslist from "@swissquote/crafty-commons/packages/browserslist.js";

import valueParser from "postcss-value-parser";
import declarationValueIndex from "stylelint/lib/utils/declarationValueIndex.mjs";
import isStandardSyntaxFunction from "stylelint/lib/utils/isStandardSyntaxFunction.mjs";
import valueParser from "../../packages/postcss-value-parser.js";
import declarationValueIndex from "../../dist/stylelint-utils/stylelint-declarationValueIndex.js";
import isStandardSyntaxFunction from "../../dist/stylelint-utils/stylelint-isStandardSyntaxFunction.js";

const require = createRequire(import.meta.url);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test from "ava";

import isKeyframeSelector from "stylelint/lib/utils/isKeyframeSelector.mjs";
import isStandardSyntaxRule from "stylelint/lib/utils/isStandardSyntaxRule.mjs";
import isStandardSyntaxSelector from "stylelint/lib/utils/isStandardSyntaxSelector.mjs";
import isKeyframeSelector from "../../../dist/stylelint-utils/stylelint-isKeyframeSelector.js";
import isStandardSyntaxRule from "../../../dist/stylelint-utils/stylelint-isStandardSyntaxRule.js";
import isStandardSyntaxSelector from "../../../dist/stylelint-utils/stylelint-isStandardSyntaxSelector.js";

import postcss from "postcss";
import scssSyntax from "postcss-scss";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import selectorParser from "postcss-selector-parser";
import selectorParser from "../../packages/postcss-selector-parser.js";

export default function parseSelector(resolvedSelector) {
const container = selectorParser.selector();
Expand Down

0 comments on commit f011c0a

Please sign in to comment.