Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tailwind configuration works in mono repos #2847

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/lib/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
"import": "./src/plugins/index.js",
"require": null,
"types": "./src/plugins/index.js"
},
"./plugins/discoverPluginPackageRootPathSync": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a dedicated export, as needs to be required from a commonjs context, and esm only modules are included in the main ./plugins export

"default": "./src/plugins/discoverPluginPackageRootPathSync.js",
"import": "./src/plugins/discoverPluginPackageRootPathSync.js",
"require": "./src/plugins/discoverPluginPackageRootPathSync.js",
"types": "./src/plugins/discoverPluginPackageRootPathSync.js"
}
},
"scripts": {
Expand Down
35 changes: 35 additions & 0 deletions packages/lib/sdk/src/plugins/discoverPluginPackageRootPathSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import path from 'path';
import { createRequire } from 'module';
import fs from 'fs';
import { log } from '../logger/index.js';
import chalk from 'chalk';

/**
*
* @param {string} packageName
* @returns {string|null}
*/
export const discoverPluginPackageRootPathSync = (packageName) => {
// note: this will return the directory of the package entrypoint, eg: `some-package/dist/index.js`
// so we need to walk up the tree until we find a `package.json` file to discover the root.
let pluginPackageDirectory = path.dirname(createRequire(import.meta.url).resolve(packageName));
let pluginPackageDirContents = fs.readdirSync(pluginPackageDirectory);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sync filesystem access, as calling from a synchronous context in the tailwind config

while (!pluginPackageDirContents.includes('package.json')) {
pluginPackageDirectory = path.dirname(pluginPackageDirectory);
pluginPackageDirContents = fs.readdirSync(pluginPackageDirectory);
}

if (pluginPackageDirectory === path.dirname(pluginPackageDirectory)) {
// reached root
log.warn(
chalk.yellow(
`Package ${chalk.bold(
`"${packageName}"`
)} not found, run again with --debug for more information`
)
);
return null;
}

return pluginPackageDirectory;
};
21 changes: 4 additions & 17 deletions packages/lib/sdk/src/plugins/loadPluginPackage.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
import chalk from 'chalk';
import { PluginPackageSchema } from './schemas/plugin-package.schema.js';
import fs from 'fs/promises';
import { createRequire } from 'module';
import path from 'path';
import { log } from '../logger/index.js';
import { discoverPluginPackageRootPathSync } from './discoverPluginPackageRootPathSync.js';

/**
*
* @param {string} name
* @returns {Promise<null | import("./schemas/plugin-package.schema.js").PluginPackage & {dir: string}>}
*/
export const loadPluginPackage = async (name) => {
let pluginPackageDirectory = path.dirname(createRequire(import.meta.url).resolve(name));
let pluginPackageDirContents = await fs.readdir(pluginPackageDirectory);
while (!pluginPackageDirContents.includes('package.json')) {
pluginPackageDirectory = path.dirname(pluginPackageDirectory);
pluginPackageDirContents = await fs.readdir(pluginPackageDirectory);
const pluginPackageDirectory = discoverPluginPackageRootPathSync(name);

if (pluginPackageDirectory === path.dirname(pluginPackageDirectory)) {
// reached root
log.warn(
chalk.yellow(
`Package ${chalk.bold(
`"${name}"`
)} not found, run again with --debug for more information`
)
);
return null;
}
if (!pluginPackageDirectory) {
return null;
}

const packagePath = path.join(pluginPackageDirectory, 'package.json');
Expand Down
12 changes: 7 additions & 5 deletions sites/example-project/tailwind.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const evidenceConfig = require('@evidence-dev/sdk/config').getEvidenceConfig();

const fs = require('fs');
const path = require('path');
const {
discoverPluginPackageRootPathSync
} = require('@evidence-dev/sdk/plugins/discoverPluginPackageRootPathSync');

let presets = [evidenceTailwind];

const altConfigFilenames = ['tailwind.config.js', 'tailwind.config.cjs'];
Expand All @@ -24,12 +28,10 @@ const config = {
get files() {
const pluginConfig = evidenceConfig.plugins;
const components = pluginConfig.components;

const componentPaths = Object.keys(components)
.map((pluginName) => [
`./node_modules/${pluginName}/dist/**/*.{html,js,svelte,ts,md}`,
`../../node_modules/${pluginName}/dist/**/*.{html,js,svelte,ts,md}`
])
.flat();
.map((pluginName) => discoverPluginPackageRootPathSync(pluginName))
.map((pluginDirectory) => `${pluginDirectory}/dist/**/*.{html,js,svelte,ts,md}`);

return [
'./src/**/*.{html,js,svelte,ts,md}', // This is used for everything in base evidence template
Expand Down
Loading