1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getPostCssConfigSync = exports.getPostCssConfig = exports.getCSSPath = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const cli_utils_1 = require("@design-systems/cli-utils");
|
6 | const cli_utils_2 = require("@design-systems/cli-utils");
|
7 | const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
8 | const path_1 = tslib_1.__importDefault(require("path"));
|
9 | const postcss_1 = tslib_1.__importDefault(require("postcss"));
|
10 | const postcss_load_config_1 = tslib_1.__importDefault(require("postcss-load-config"));
|
11 | const code_frame_1 = require("@babel/code-frame");
|
12 | const utils_1 = require("./utils");
|
13 | const logger = cli_utils_1.createLogger({ scope: 'build' });
|
14 |
|
15 | function 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 | }
|
21 | exports.getCSSPath = getCSSPath;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function getUserPostcssConfig(cwd = process.cwd()) {
|
27 | const CONFIG_FILENAME = 'postcss.config.js';
|
28 |
|
29 | const pkgConfigPath = path_1.default.join(cwd, CONFIG_FILENAME);
|
30 | if (fs_extra_1.default.existsSync(pkgConfigPath)) {
|
31 | return pkgConfigPath;
|
32 | }
|
33 |
|
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 |
|
39 | return '';
|
40 | }
|
41 |
|
42 | const reportConfigError = (error) => {
|
43 |
|
44 |
|
45 | if (!error.message.includes('No PostCSS Config found')) {
|
46 | logger.error('Something is wrong in your postcss.config!');
|
47 |
|
48 | console.log(error);
|
49 | process.exit(1);
|
50 | }
|
51 | };
|
52 |
|
53 | async 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 | }
|
71 | exports.getPostCssConfig = getPostCssConfig;
|
72 |
|
73 | function 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 | }
|
91 | exports.getPostCssConfigSync = getPostCssConfigSync;
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | async function transpile({ inFile, inDir, outDir, configFile, watch }) {
|
100 |
|
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 |
|
131 | console.log(message);
|
132 | if (!watch) {
|
133 | process.exit(1);
|
134 | }
|
135 | }
|
136 | }
|
137 | exports.default = transpile;
|
138 |
|
\ | No newline at end of file |