@azerion/bunosaurus-plugin-css-cascade-layers 0.0.0-stage → 4.0.0-canary-6927

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,7 @@
1
- # Temporary Holding Version
1
+ # `@docusaurus/plugin-css-cascade-layers`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ CSS Cascade Layer plugin for Docusaurus
4
+
5
+ ## Usage
6
+
7
+ See [plugin-css-cascade-layers documentation](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-css-cascade-layers).
package/lib/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { LoadContext, Plugin } from '@docusaurus/types';
8
+ import type { PluginOptions, Options } from './options';
9
+ export default function pluginCssCascadeLayers(context: LoadContext, options: PluginOptions): Plugin | null;
10
+ export { validateOptions } from './options';
11
+ export type { PluginOptions, Options };
package/lib/index.js ADDED
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.validateOptions = void 0;
10
+ exports.default = pluginCssCascadeLayers;
11
+ const tslib_1 = require("tslib");
12
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
13
+ const postCssPlugin_1 = require("./postCssPlugin");
14
+ const layers_1 = require("./layers");
15
+ const PluginName = 'docusaurus-plugin-css-cascade-layers';
16
+ const LayersDeclarationModule = 'layers.css';
17
+ function getLayersDeclarationPath(context, options) {
18
+ const { generatedFilesDir } = context;
19
+ const pluginId = options.id;
20
+ if (pluginId !== 'default') {
21
+ // Since it's only possible to declare a single layer order
22
+ // using this plugin twice doesn't really make sense
23
+ throw new Error('The CSS Cascade Layers plugin does not support multiple instances.');
24
+ }
25
+ return node_path_1.default.join(generatedFilesDir, PluginName, pluginId, LayersDeclarationModule);
26
+ }
27
+ function pluginCssCascadeLayers(context, options) {
28
+ const layersDeclarationPath = getLayersDeclarationPath(context, options);
29
+ return {
30
+ name: PluginName,
31
+ getClientModules() {
32
+ return [layersDeclarationPath];
33
+ },
34
+ async contentLoaded({ actions }) {
35
+ await actions.createData(LayersDeclarationModule, (0, layers_1.generateLayersDeclaration)(Object.keys(options.layers)));
36
+ },
37
+ configurePostCss(postCssOptions) {
38
+ postCssOptions.plugins.push((0, postCssPlugin_1.PostCssPluginWrapInLayer)(options));
39
+ return postCssOptions;
40
+ },
41
+ };
42
+ }
43
+ var options_1 = require("./options");
44
+ Object.defineProperty(exports, "validateOptions", { enumerable: true, get: function () { return options_1.validateOptions; } });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ export type LayerEntry = [string, (filePath: string) => boolean];
8
+ export declare function isValidLayerName(layer: string): boolean;
9
+ export declare function generateLayersDeclaration(layers: string[]): string;
10
+ export declare function findLayer(filePath: string, layers: LayerEntry[]): string | undefined;
package/lib/layers.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.isValidLayerName = isValidLayerName;
10
+ exports.generateLayersDeclaration = generateLayersDeclaration;
11
+ exports.findLayer = findLayer;
12
+ function isValidLayerName(layer) {
13
+ // TODO improve validation rule to match spec, not high priority
14
+ return !layer.includes(',') && !layer.includes(' ');
15
+ }
16
+ function generateLayersDeclaration(layers) {
17
+ return `@layer ${layers.join(', ')};`;
18
+ }
19
+ function findLayer(filePath, layers) {
20
+ // Using find() => layers order matter
21
+ // The first layer that matches is used in priority even if others match too
22
+ const layerEntry = layers.find((layer) => layer[1](filePath));
23
+ return layerEntry?.[0]; // return layer name
24
+ }
@@ -0,0 +1,10 @@
1
+ import type { OptionValidationContext } from '@docusaurus/types';
2
+ export type PluginOptions = {
3
+ id: string;
4
+ layers: Record<string, (filePath: string) => boolean>;
5
+ };
6
+ export type Options = {
7
+ layers?: PluginOptions['layers'];
8
+ };
9
+ export declare const DEFAULT_OPTIONS: Partial<PluginOptions>;
10
+ export declare function validateOptions({ validate, options, }: OptionValidationContext<Options, PluginOptions>): PluginOptions;
package/lib/options.js ADDED
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_OPTIONS = void 0;
4
+ exports.validateOptions = validateOptions;
5
+ /**
6
+ * Copyright (c) Facebook, Inc. and its affiliates.
7
+ *
8
+ * This source code is licensed under the MIT license found in the
9
+ * LICENSE file in the root directory of this source tree.
10
+ */
11
+ const utils_validation_1 = require("@docusaurus/utils-validation");
12
+ const utils_1 = require("@docusaurus/utils");
13
+ const layers_1 = require("./layers");
14
+ // Not ideal to compute layers using "filePath.includes()"
15
+ // But this is mostly temporary until we add first-class layers everywhere
16
+ function layerFor(...params) {
17
+ return (filePath) => {
18
+ const posixFilePath = (0, utils_1.posixPath)(filePath);
19
+ return params.some((p) => posixFilePath.includes(p));
20
+ };
21
+ }
22
+ // Object order matters, it defines the layer order
23
+ const DEFAULT_LAYERS = {
24
+ 'docusaurus.infima': layerFor('node_modules/infima/dist'),
25
+ 'docusaurus.theme-common': layerFor('packages/docusaurus-theme-common/lib', 'node_modules/@docusaurus/theme-common/lib'),
26
+ 'docusaurus.theme-classic': layerFor('packages/docusaurus-theme-classic/lib', 'node_modules/@docusaurus/theme-classic/lib'),
27
+ 'docusaurus.core': layerFor('packages/docusaurus/lib', 'node_modules/@docusaurus/core/lib'),
28
+ 'docusaurus.plugin-debug': layerFor('packages/docusaurus-plugin-debug/lib', 'node_modules/@docusaurus/plugin-debug/lib'),
29
+ 'docusaurus.theme-mermaid': layerFor('packages/docusaurus-theme-mermaid/lib', 'node_modules/@docusaurus/theme-mermaid/lib'),
30
+ 'docusaurus.theme-live-codeblock': layerFor('packages/docusaurus-theme-live-codeblock/lib', 'node_modules/@docusaurus/theme-live-codeblock/lib'),
31
+ 'docusaurus.theme-search-algolia.docsearch': layerFor('node_modules/@docsearch/css/dist'),
32
+ 'docusaurus.theme-search-algolia': layerFor('packages/docusaurus-theme-search-algolia/lib', 'node_modules/@docusaurus/theme-search-algolia/lib'),
33
+ // docusaurus.website layer ? (declare it, even if empty?)
34
+ };
35
+ exports.DEFAULT_OPTIONS = {
36
+ id: 'default',
37
+ layers: DEFAULT_LAYERS,
38
+ };
39
+ const pluginOptionsSchema = utils_validation_1.Joi.object({
40
+ layers: utils_validation_1.Joi.object()
41
+ .pattern(utils_validation_1.Joi.custom((val, helpers) => {
42
+ if (!(0, layers_1.isValidLayerName)(val)) {
43
+ return helpers.error('any.invalid');
44
+ }
45
+ return val;
46
+ }), utils_validation_1.Joi.function().arity(1).required())
47
+ .default(DEFAULT_LAYERS),
48
+ });
49
+ function validateOptions({ validate, options, }) {
50
+ return validate(pluginOptionsSchema, options);
51
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { PluginCreator } from 'postcss';
8
+ import type { PluginOptions } from './options';
9
+ export declare const PostCssPluginWrapInLayer: PluginCreator<{
10
+ layers: PluginOptions['layers'];
11
+ }>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.PostCssPluginWrapInLayer = void 0;
10
+ const layers_1 = require("./layers");
11
+ function wrapCssRootInLayer(root, layer) {
12
+ const rootBefore = root.clone();
13
+ root.removeAll();
14
+ root.append({
15
+ type: 'atrule',
16
+ name: 'layer',
17
+ params: layer,
18
+ nodes: rootBefore.nodes,
19
+ });
20
+ }
21
+ const PostCssPluginWrapInLayer = (options) => {
22
+ if (!options) {
23
+ throw new Error('PostCssPluginWrapInLayer options are mandatory');
24
+ }
25
+ const layers = Object.entries(options.layers);
26
+ return {
27
+ postcssPlugin: 'postcss-wrap-in-layer',
28
+ Once(root) {
29
+ const filePath = root.source?.input.file;
30
+ if (!filePath) {
31
+ return;
32
+ }
33
+ const layer = (0, layers_1.findLayer)(filePath, layers);
34
+ if (layer) {
35
+ wrapCssRootInLayer(root, layer);
36
+ }
37
+ },
38
+ };
39
+ };
40
+ exports.PostCssPluginWrapInLayer = PostCssPluginWrapInLayer;
41
+ exports.PostCssPluginWrapInLayer.postcss = true;
package/package.json CHANGED
@@ -1,6 +1,34 @@
1
1
  {
2
2
  "name": "@azerion/bunosaurus-plugin-css-cascade-layers",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "4.0.0-canary-6927+e0f3c69e5",
4
+ "description": "CSS Cascade Layer plugin for Docusaurus.",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc --build",
9
+ "watch": "tsc --build --watch"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/azerion/bunosaurus.git",
17
+ "directory": "packages/docusaurus-plugin-css-cascade-layers"
18
+ },
19
+ "license": "MIT",
20
+ "dependencies": {
21
+ "@docusaurus/core": "npm:@azerion/bunosaurus-core@4.0.0-canary-6927+e0f3c69e5",
22
+ "@docusaurus/types": "npm:@azerion/bunosaurus-types@4.0.0-canary-6927+e0f3c69e5",
23
+ "@docusaurus/utils": "npm:@azerion/bunosaurus-utils@4.0.0-canary-6927+e0f3c69e5",
24
+ "@docusaurus/utils-validation": "npm:@azerion/bunosaurus-utils-validation@4.0.0-canary-6927+e0f3c69e5",
25
+ "tslib": "^2.8.1"
26
+ },
27
+ "devDependencies": {
28
+ "@docusaurus/module-type-aliases": "npm:@azerion/bunosaurus-module-type-aliases@4.0.0-canary-6927+e0f3c69e5",
29
+ "postcss": "^8.5.28"
30
+ },
31
+ "engines": {
32
+ "bun": ">=1.4.1"
33
+ }
34
+ }
package/src/index.ts ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import path from 'node:path';
9
+ import {PostCssPluginWrapInLayer} from './postCssPlugin';
10
+ import {generateLayersDeclaration} from './layers';
11
+ import type {LoadContext, Plugin} from '@docusaurus/types';
12
+ import type {PluginOptions, Options} from './options';
13
+
14
+ const PluginName = 'docusaurus-plugin-css-cascade-layers';
15
+
16
+ const LayersDeclarationModule = 'layers.css';
17
+
18
+ function getLayersDeclarationPath(
19
+ context: LoadContext,
20
+ options: PluginOptions,
21
+ ) {
22
+ const {generatedFilesDir} = context;
23
+ const pluginId = options.id;
24
+ if (pluginId !== 'default') {
25
+ // Since it's only possible to declare a single layer order
26
+ // using this plugin twice doesn't really make sense
27
+ throw new Error(
28
+ 'The CSS Cascade Layers plugin does not support multiple instances.',
29
+ );
30
+ }
31
+ return path.join(
32
+ generatedFilesDir,
33
+ PluginName,
34
+ pluginId,
35
+ LayersDeclarationModule,
36
+ );
37
+ }
38
+
39
+ export default function pluginCssCascadeLayers(
40
+ context: LoadContext,
41
+ options: PluginOptions,
42
+ ): Plugin | null {
43
+ const layersDeclarationPath = getLayersDeclarationPath(context, options);
44
+
45
+ return {
46
+ name: PluginName,
47
+
48
+ getClientModules() {
49
+ return [layersDeclarationPath];
50
+ },
51
+
52
+ async contentLoaded({actions}) {
53
+ await actions.createData(
54
+ LayersDeclarationModule,
55
+ generateLayersDeclaration(Object.keys(options.layers)),
56
+ );
57
+ },
58
+
59
+ configurePostCss(postCssOptions) {
60
+ postCssOptions.plugins.push(PostCssPluginWrapInLayer(options));
61
+ return postCssOptions;
62
+ },
63
+ };
64
+ }
65
+
66
+ export {validateOptions} from './options';
67
+
68
+ export type {PluginOptions, Options};
package/src/layers.ts ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ export type LayerEntry = [string, (filePath: string) => boolean];
9
+
10
+ export function isValidLayerName(layer: string): boolean {
11
+ // TODO improve validation rule to match spec, not high priority
12
+ return !layer.includes(',') && !layer.includes(' ');
13
+ }
14
+
15
+ export function generateLayersDeclaration(layers: string[]): string {
16
+ return `@layer ${layers.join(', ')};`;
17
+ }
18
+
19
+ export function findLayer(
20
+ filePath: string,
21
+ layers: LayerEntry[],
22
+ ): string | undefined {
23
+ // Using find() => layers order matter
24
+ // The first layer that matches is used in priority even if others match too
25
+ const layerEntry = layers.find((layer) => layer[1](filePath));
26
+ return layerEntry?.[0]; // return layer name
27
+ }
package/src/options.ts ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import {Joi} from '@docusaurus/utils-validation';
8
+ import {posixPath} from '@docusaurus/utils';
9
+ import {isValidLayerName} from './layers';
10
+ import type {OptionValidationContext} from '@docusaurus/types';
11
+
12
+ export type PluginOptions = {
13
+ id: string; // plugin id
14
+ layers: Record<string, (filePath: string) => boolean>;
15
+ };
16
+
17
+ export type Options = {
18
+ layers?: PluginOptions['layers'];
19
+ };
20
+
21
+ // Not ideal to compute layers using "filePath.includes()"
22
+ // But this is mostly temporary until we add first-class layers everywhere
23
+ function layerFor(...params: string[]) {
24
+ return (filePath: string) => {
25
+ const posixFilePath = posixPath(filePath);
26
+ return params.some((p) => posixFilePath.includes(p));
27
+ };
28
+ }
29
+
30
+ // Object order matters, it defines the layer order
31
+ const DEFAULT_LAYERS: PluginOptions['layers'] = {
32
+ 'docusaurus.infima': layerFor('node_modules/infima/dist'),
33
+ 'docusaurus.theme-common': layerFor(
34
+ 'packages/docusaurus-theme-common/lib',
35
+ 'node_modules/@docusaurus/theme-common/lib',
36
+ ),
37
+ 'docusaurus.theme-classic': layerFor(
38
+ 'packages/docusaurus-theme-classic/lib',
39
+ 'node_modules/@docusaurus/theme-classic/lib',
40
+ ),
41
+ 'docusaurus.core': layerFor(
42
+ 'packages/docusaurus/lib',
43
+ 'node_modules/@docusaurus/core/lib',
44
+ ),
45
+ 'docusaurus.plugin-debug': layerFor(
46
+ 'packages/docusaurus-plugin-debug/lib',
47
+ 'node_modules/@docusaurus/plugin-debug/lib',
48
+ ),
49
+ 'docusaurus.theme-mermaid': layerFor(
50
+ 'packages/docusaurus-theme-mermaid/lib',
51
+ 'node_modules/@docusaurus/theme-mermaid/lib',
52
+ ),
53
+ 'docusaurus.theme-live-codeblock': layerFor(
54
+ 'packages/docusaurus-theme-live-codeblock/lib',
55
+ 'node_modules/@docusaurus/theme-live-codeblock/lib',
56
+ ),
57
+ 'docusaurus.theme-search-algolia.docsearch': layerFor(
58
+ 'node_modules/@docsearch/css/dist',
59
+ ),
60
+ 'docusaurus.theme-search-algolia': layerFor(
61
+ 'packages/docusaurus-theme-search-algolia/lib',
62
+ 'node_modules/@docusaurus/theme-search-algolia/lib',
63
+ ),
64
+ // docusaurus.website layer ? (declare it, even if empty?)
65
+ };
66
+
67
+ export const DEFAULT_OPTIONS: Partial<PluginOptions> = {
68
+ id: 'default',
69
+ layers: DEFAULT_LAYERS,
70
+ };
71
+
72
+ const pluginOptionsSchema = Joi.object<PluginOptions>({
73
+ layers: Joi.object()
74
+ .pattern(
75
+ Joi.custom((val, helpers) => {
76
+ if (!isValidLayerName(val)) {
77
+ return helpers.error('any.invalid');
78
+ }
79
+ return val;
80
+ }),
81
+ Joi.function().arity(1).required(),
82
+ )
83
+ .default(DEFAULT_LAYERS),
84
+ });
85
+
86
+ export function validateOptions({
87
+ validate,
88
+ options,
89
+ }: OptionValidationContext<Options, PluginOptions>): PluginOptions {
90
+ return validate(pluginOptionsSchema, options);
91
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import {findLayer} from './layers';
9
+ import type {Root, PluginCreator} from 'postcss';
10
+ import type {PluginOptions} from './options';
11
+
12
+ function wrapCssRootInLayer(root: Root, layer: string): void {
13
+ const rootBefore = root.clone();
14
+ root.removeAll();
15
+ root.append({
16
+ type: 'atrule',
17
+ name: 'layer',
18
+ params: layer,
19
+ nodes: rootBefore.nodes,
20
+ });
21
+ }
22
+
23
+ export const PostCssPluginWrapInLayer: PluginCreator<{
24
+ layers: PluginOptions['layers'];
25
+ }> = (options) => {
26
+ if (!options) {
27
+ throw new Error('PostCssPluginWrapInLayer options are mandatory');
28
+ }
29
+ const layers = Object.entries(options.layers);
30
+ return {
31
+ postcssPlugin: 'postcss-wrap-in-layer',
32
+ Once(root) {
33
+ const filePath = root.source?.input.file;
34
+ if (!filePath) {
35
+ return;
36
+ }
37
+ const layer = findLayer(filePath, layers);
38
+ if (layer) {
39
+ wrapCssRootInLayer(root, layer);
40
+ }
41
+ },
42
+ };
43
+ };
44
+
45
+ PostCssPluginWrapInLayer.postcss = true;
package/src/types.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ /// <reference types="@docusaurus/module-type-aliases" />