Skip to content

Commit

Permalink
fix: set the vm's __dirname global to the path of the config file
Browse files Browse the repository at this point in the history
Fixes #120
This is a workaround for errors from relative imports inside the config
by using the `__dirname` global in imports
  • Loading branch information
muhammadsammy committed Feb 23, 2021
1 parent c0750a4 commit 0b3d7c8
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/cli/core/GeneratedFileWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,26 @@ export class GeneratedFileWriter {

private generateFileContent = (): string => {
// Evaluate the config as a JS object
const configEval = vm.runInNewContext(this._configFileData, {require, module: {}}) as TConfig;
const evaluatedConfig = <TConfig>vm.runInNewContext(this._configFileData, {
__dirname: path.dirname(path.resolve(`./${this._configFilename}`)),
module: {},
require,
});

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

// Generate all classnames from the config
const generatedClassnames = new ClassnamesGenerator(scanner).generate();
const generatedClassnames = new ClassnamesGenerator(configParser).generate();

// Create the file content from the generated classes
const contentGenerator = new FileContentGenerator(generatedClassnames, scanner.getPrefix());
const fileContentTemplate = contentGenerator.generateFileContent();
// Create the file content from the generated classnames
const fileContentTemplate = new FileContentGenerator(
generatedClassnames,
configParser.getPrefix(),
).generateFileContent();

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

0 comments on commit 0b3d7c8

Please sign in to comment.