Skip to content

Commit 0b3d7c8

Browse files
committed
fix: set the vm's __dirname global to the path of the config file
Fixes #120 This is a workaround for errors from relative imports inside the config by using the `__dirname` global in imports
1 parent c0750a4 commit 0b3d7c8

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/cli/core/GeneratedFileWriter.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,26 @@ export class GeneratedFileWriter {
7171

7272
private generateFileContent = (): string => {
7373
// Evaluate the config as a JS object
74-
const configEval = vm.runInNewContext(this._configFileData, {require, module: {}}) as TConfig;
74+
const evaluatedConfig = <TConfig>vm.runInNewContext(this._configFileData, {
75+
__dirname: path.dirname(path.resolve(`./${this._configFilename}`)),
76+
module: {},
77+
require,
78+
});
7579

7680
// Parse the config with the config parser class
77-
const scanner = new TailwindConfigParser(configEval, {
81+
const configParser = new TailwindConfigParser(evaluatedConfig, {
7882
pluginTypography: this._configFileData.includes('@tailwindcss/typography'),
7983
pluginCustomForms: this._configFileData.includes('@tailwindcss/custom-forms'),
8084
});
8185

8286
// Generate all classnames from the config
83-
const generatedClassnames = new ClassnamesGenerator(scanner).generate();
87+
const generatedClassnames = new ClassnamesGenerator(configParser).generate();
8488

85-
// Create the file content from the generated classes
86-
const contentGenerator = new FileContentGenerator(generatedClassnames, scanner.getPrefix());
87-
const fileContentTemplate = contentGenerator.generateFileContent();
89+
// Create the file content from the generated classnames
90+
const fileContentTemplate = new FileContentGenerator(
91+
generatedClassnames,
92+
configParser.getPrefix(),
93+
).generateFileContent();
8894

8995
// Resolve the custom classes import path relative to the output file
9096
let customClassesImportPath: string | null = null;

0 commit comments

Comments
 (0)