|
1 | 1 | /* eslint-disable @typescript-eslint/restrict-template-expressions */
|
2 | 2 |
|
3 | 3 | import {promises as fs} from 'fs';
|
| 4 | +import vm from 'vm'; |
4 | 5 | import path from 'path';
|
5 | 6 | import colors from 'colors';
|
6 | 7 | import {ClassnamesGenerator} from './ClassnamesGenerator';
|
7 |
| -import {TTailwindCSSConfig} from '../types/config'; |
8 | 8 | import {TailwindConfigParser} from './TailwindConfigParser';
|
9 | 9 | import {FileContentGenerator} from './FileContentGenerator';
|
| 10 | +import {TTailwindCSSConfig as TConfig} from '../types/config'; |
10 | 11 |
|
11 | 12 | type TCliOptions = {
|
12 | 13 | configFilename: string | void;
|
@@ -70,22 +71,26 @@ export class GeneratedFileWriter {
|
70 | 71 |
|
71 | 72 | private generateFileContent = (): string => {
|
72 | 73 | // Evaluate the config as a JS object
|
73 |
| - const configEval = eval( |
74 |
| - this._configFileData.replace(/(['"])?plugins(['"])? *: *\[(.*|\n)*?],?/g, ''), |
75 |
| - ) as TTailwindCSSConfig; |
| 74 | + const evaluatedConfig = <TConfig>vm.runInNewContext(this._configFileData, { |
| 75 | + __dirname: path.dirname(path.resolve(`./${this._configFilename}`)), |
| 76 | + module: {}, |
| 77 | + require, |
| 78 | + }); |
76 | 79 |
|
77 |
| - // Parse the config with the config scanner |
78 |
| - const scanner = new TailwindConfigParser(configEval, { |
| 80 | + // Parse the config with the config parser class |
| 81 | + const configParser = new TailwindConfigParser(evaluatedConfig, { |
79 | 82 | pluginTypography: this._configFileData.includes('@tailwindcss/typography'),
|
80 | 83 | pluginCustomForms: this._configFileData.includes('@tailwindcss/custom-forms'),
|
81 | 84 | });
|
82 | 85 |
|
83 | 86 | // Generate all classnames from the config
|
84 |
| - const generatedClassnames = new ClassnamesGenerator(scanner).generate(); |
| 87 | + const generatedClassnames = new ClassnamesGenerator(configParser).generate(); |
85 | 88 |
|
86 |
| - // Create the file content from the generated classes |
87 |
| - const contentGenerator = new FileContentGenerator(generatedClassnames, scanner.getPrefix()); |
88 |
| - 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(); |
89 | 94 |
|
90 | 95 | // Resolve the custom classes import path relative to the output file
|
91 | 96 | let customClassesImportPath: string | null = null;
|
|
0 commit comments