UNPKG

5.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getPostCssConfigSync = exports.getPostCssConfig = exports.getCSSPath = void 0;
4const tslib_1 = require("tslib");
5const cli_utils_1 = require("@design-systems/cli-utils");
6const cli_utils_2 = require("@design-systems/cli-utils");
7const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
8const path_1 = tslib_1.__importDefault(require("path"));
9const postcss_1 = tslib_1.__importDefault(require("postcss"));
10const postcss_load_config_1 = tslib_1.__importDefault(require("postcss-load-config"));
11const code_frame_1 = require("@babel/code-frame");
12const utils_1 = require("./utils");
13const logger = cli_utils_1.createLogger({ scope: 'build' });
14/** Get the output path for a css file. */
15function getCSSPath(inFile, inDir, outDir) {
16 const name = path_1.default.basename(inFile);
17 const outPath = utils_1.getOutPath(inDir, inFile, outDir);
18 const cssFile = path_1.default.join(outPath, name);
19 return cssFile;
20}
21exports.getCSSPath = getCSSPath;
22/**
23 * Check package and monorepo cwd for postcss.config.js and returns
24 * path. Defaults to empty string.
25 */
26function getUserPostcssConfig(cwd = process.cwd()) {
27 const CONFIG_FILENAME = 'postcss.config.js';
28 // Try package cwd
29 const pkgConfigPath = path_1.default.join(cwd, CONFIG_FILENAME);
30 if (fs_extra_1.default.existsSync(pkgConfigPath)) {
31 return pkgConfigPath;
32 }
33 // Try monorepo cwd
34 const monoRepoConfigPath = path_1.default.join(cli_utils_2.getMonorepoRoot(cwd), CONFIG_FILENAME);
35 if (fs_extra_1.default.existsSync(monoRepoConfigPath)) {
36 return monoRepoConfigPath;
37 }
38 // Default to empty string
39 return '';
40}
41/** Fail the process if the postcss.config has errors */
42const reportConfigError = (error) => {
43 // Error on anything but the following message. This message is
44 // present when the user hasn't configured postcss
45 if (!error.message.includes('No PostCSS Config found')) {
46 logger.error('Something is wrong in your postcss.config!');
47 // eslint-disable-next-line no-console
48 console.log(error);
49 process.exit(1);
50 }
51};
52/** Load the user's postcss config or our default. */
53async function getPostCssConfig({ useModules, configFile = require.resolve('./configs/postcss.config'), outDir = 'dist', cwd = process.cwd(), reportError = true }) {
54 const context = useModules
55 ? {
56 env: 'module',
57 outDir
58 }
59 : {};
60 try {
61 return await postcss_load_config_1.default(context, getUserPostcssConfig(cwd));
62 }
63 catch (error) {
64 if (reportError) {
65 reportConfigError(error);
66 logger.trace(error);
67 }
68 return postcss_load_config_1.default(context, configFile);
69 }
70}
71exports.getPostCssConfig = getPostCssConfig;
72/** Load the user's postcss config or our default synchronously. */
73function getPostCssConfigSync({ useModules, configFile = require.resolve('./configs/postcss.config'), outDir = 'dist', cwd = process.cwd(), reportError = true }) {
74 const context = useModules
75 ? {
76 env: 'module',
77 outDir
78 }
79 : {};
80 try {
81 return postcss_load_config_1.default.sync(context, getUserPostcssConfig(cwd));
82 }
83 catch (error) {
84 if (reportError) {
85 reportConfigError(error);
86 logger.trace(error);
87 }
88 return postcss_load_config_1.default.sync(context, configFile);
89 }
90}
91exports.getPostCssConfigSync = getPostCssConfigSync;
92/**
93 * Transpile a CSS file using babel.
94 * Write the CJS version to disk.
95 * Write the transpiled version to disk.
96 *
97 * @returns the transpiled CSS
98 */
99async function transpile({ inFile, inDir, outDir, configFile, watch }) {
100 // Append .js to the end of the file so auto importing works
101 const cssFile = getCSSPath(inFile, inDir, outDir);
102 const { plugins, options } = await getPostCssConfig({
103 useModules: true,
104 configFile,
105 outDir
106 });
107 const processor = postcss_1.default(plugins);
108 const fileContents = await fs_extra_1.default.readFile(inFile);
109 try {
110 const result = await processor.process(fileContents, Object.assign(Object.assign({}, options), { plugins, to: cssFile, from: inFile, map: { inline: false } }));
111 await fs_extra_1.default.outputFile(`${cssFile}.map`, result.map
112 .toString()
113 .replace(/<input css (\d+)>/g, '../src/<input css $1>'));
114 return result;
115 }
116 catch (error) {
117 logger.error('Something went wrong building your css!');
118 let message = error;
119 if (error.name === 'CssSyntaxError') {
120 const { line, column, file, reason } = error;
121 message = utils_1.formatError({
122 file,
123 line,
124 column,
125 message: reason,
126 tool: 'POSTCSS',
127 code: code_frame_1.codeFrameColumns(error.source, { start: { line, column } }, { highlightCode: true })
128 });
129 }
130 // eslint-disable-next-line no-console
131 console.log(message);
132 if (!watch) {
133 process.exit(1);
134 }
135 }
136}
137exports.default = transpile;
138//# sourceMappingURL=postcss.js.map
\No newline at end of file