forked from onlook-dev/onlook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.js
184 lines (155 loc) · 5.55 KB
/
webpack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const fs = require('fs');
const path = require('path');
const { parse } = require('@babel/parser');
const generate = require('@babel/generator').default;
const traverse = require('@babel/traverse').default;
const t = require('@babel/types');
const {
DEPENDENCY_NAME,
CONFIG_FILE_PATTERN,
BUILD_TOOL_NAME,
WEBPACK_CONFIG_BASE_NAME,
ONLOOK_WEBPACK_PLUGIN,
BABELRC_FILE
} = require("./constants");
const { exists, hasDependency, isSupportFileExtension } = require("./utils");
/**
* Check if the current project is a Webpack project
*
* @returns {Promise<boolean>}
*/
const isWebpackProject = async () => {
try {
const configPath = CONFIG_FILE_PATTERN[BUILD_TOOL_NAME.WEBPACK];
// Check if the configuration file exists
if (!await exists(configPath)) {
return false;
}
// Check if the dependency exists
if (!await hasDependency(DEPENDENCY_NAME.WEBPACK)) {
return false
}
return true
} catch (err) {
console.error(err);
return false;
}
}
/**
* Babel rule to be added to the webpack.config.* file
*/
const babelRule = t.objectExpression([
t.objectProperty(t.identifier('test'), t.regExpLiteral('\\.(js|mjs|cjs|ts|tsx|jsx)$')),
t.objectProperty(t.identifier('exclude'), t.regExpLiteral('\\/node_modules\\/')),
t.objectProperty(t.identifier('use'), t.objectExpression([
t.objectProperty(t.identifier('loader'), t.stringLiteral('babel-loader')),
t.objectProperty(t.identifier('options'), t.objectExpression([
t.objectProperty(t.identifier('presets'), t.arrayExpression([
t.stringLiteral('@babel/preset-env'),
t.stringLiteral('@babel/preset-react')
]))
]))
]))
]);
/**
* Add the babel rule to the webpack.config.js file
*
* @param {string} configFileExtension
* @returns
*/
function modifyWebpackConfig(configFileExtension) {
if (!isSupportFileExtension(configFileExtension)) {
console.error('Unsupported file extension');
return;
}
const configFileName = `${WEBPACK_CONFIG_BASE_NAME}${configFileExtension}`;
// Define the path to webpack.config.* file
const configPath = path.resolve(process.cwd(), configFileName);
if (!fs.existsSync(configPath)) {
console.error(`${configFileName} not found`);
return;
}
fs.readFile(configPath, 'utf8', (err, fileContent) => {
if (err) {
console.error(`Error reading ${configPath}:`, err);
return;
}
const ast = parse(fileContent, { sourceType: 'module' });
let rulesArray;
// Traverse the AST to find the module.rules array
traverse(ast, {
ObjectProperty(path) {
if (path.node.key.name === 'module' && t.isObjectExpression(path.node.value)) {
const moduleProperties = path.node.value.properties;
moduleProperties.forEach(property => {
if (property.key.name === 'rules' && t.isArrayExpression(property.value)) {
rulesArray = property.value.elements;
}
});
// If module.rules does not exist, create it
if (!rulesArray) {
const rulesProperty = t.objectProperty(
t.identifier('rules'),
t.arrayExpression([])
);
path.node.value.properties.push(rulesProperty);
rulesArray = rulesProperty.value.elements;
}
}
}
});
// Add the babel rule to the rules array
if (rulesArray) {
rulesArray.push(babelRule);
}
// Generate the updated code
const updatedCode = generate(ast, {}, fileContent).code;
// Write the updated content back to next.config.* file
fs.writeFile(configPath, updatedCode, 'utf8', (err) => {
if (err) {
console.error(`Error writing ${configPath}:`, err);
return;
}
console.log(`Successfully updated ${configPath}`);
});
})
}
// Path to the .babelrc file
const babelrcPath = path.resolve(BABELRC_FILE);
// Default .babelrc content if it doesn't exist
const defaultBabelrcContent = {
plugins: []
};
/**
* Modify the .babelrc file to include the "@onlook/react" plugin
*
*/
const modifyBabelrc = () => {
let babelrcContent;
// Check if .babelrc file exists
if (fs.existsSync(babelrcPath)) {
// Read the .babelrc file
const fileContent = fs.readFileSync(babelrcPath, 'utf8');
babelrcContent = JSON.parse(fileContent);
} else {
// Use default .babelrc content if file does not exist
babelrcContent = defaultBabelrcContent;
}
// Ensure plugins array exists
if (!Array.isArray(babelrcContent.plugins)) {
babelrcContent.plugins = [];
}
// Check if "@onlook/react" is already in the plugins array
if (!babelrcContent.plugins.includes(ONLOOK_WEBPACK_PLUGIN)) {
// Add "@onlook/react" to the plugins array
babelrcContent.plugins.push(ONLOOK_WEBPACK_PLUGIN);
}
// Write the updated content back to the .babelrc file
fs.writeFileSync(babelrcPath, JSON.stringify(babelrcContent, null, 2), 'utf8');
console.log('.babelrc has been updated with the "@onlook/react" plugin.');
}
module.exports = {
modifyBabelrc,
isWebpackProject,
modifyWebpackConfig
}