|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Replace Imports Codemod (v2.11.0+)`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn about the codemod that helps you replace imports in your codebase when upgrading to Medusa v2.11.0. |
|
8
|
|
|
9
|
## What is the Replace Imports Codemod? |
|
10
|
|
|
11
|
[Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0) optimized the package structure by consolidating several external packages into the `@medusajs/framework` package. |
|
12
|
|
|
13
|
Previously, you had to install and manage packages related to MikroORM, Awilix, OpenTelemetry, and the `pg` package separately in your Medusa application. Starting with v2.11.0, these packages are included in the `@medusajs/framework` package. |
|
14
|
|
|
15
|
For example, instead of importing `@mikro-orm/core`, you now import it from `@medusajs/framework/mikro-orm/core`. This applies to all of the following packages: |
|
16
|
|
|
17
|
- `@mikro-orm/*` packages (for example, `@mikro-orm/core`, `@mikro-orm/migrations`, etc.) -> `@medusajs/framework/mikro-orm/{subpath}` |
|
18
|
- `awilix` -> `@medusajs/framework/awilix` |
|
19
|
- `pg` -> `@medusajs/framework/pg` |
|
20
|
- `@opentelemetry/instrumentation-pg` -> `@medusajs/framework/opentelemetry/instrumentation-pg` |
|
21
|
- `@opentelemetry/resources` -> `@medusajs/framework/opentelemetry/resources` |
|
22
|
- `@opentelemetry/sdk-node` -> `@medusajs/framework/opentelemetry/sdk-node` |
|
23
|
- `@opentelemetry/sdk-trace-node` -> `@medusajs/framework/opentelemetry/sdk-trace-node` |
|
24
|
|
|
25
|
To help you update your codebase to reflect these changes, Medusa provides a codemod that automatically replaces imports of these packages throughout your codebase. |
|
26
|
|
|
27
|
--- |
|
28
|
|
|
29
|
## Using the Replace Imports Codemod |
|
30
|
|
|
31
|
To use the replace imports codemod, create the file `replace-imports.js` in the root of your Medusa application with the following content: |
|
32
|
|
|
33
|
```js |
|
34
|
#!/usr/bin/env node |
|
35
|
|
|
36
|
const fs = require("fs") |
|
37
|
const path = require("path") |
|
38
|
const { execSync } = require("child_process") |
|
39
|
|
|
40
|
/** |
|
41
|
* Script to replace imports and require statements from mikro-orm/{subpath}, awilix, and pg |
|
42
|
* to their @medusajs/framework equivalents |
|
43
|
*/ |
|
44
|
|
|
45
|
// Define the replacement mappings |
|
46
|
const replacements = [ |
|
47
|
// MikroORM imports - replace mikro-orm/{subpath} with @medusajs/framework/mikro-orm/{subpath} |
|
48
|
{ |
|
49
|
pattern: /from\s+['"]@?mikro-orm\/([^'"]+)['"]/g, |
|
50
|
// eslint-disable-next-line quotes |
|
51
|
replacement: 'from "@medusajs/framework/mikro-orm/$1"', |
|
52
|
}, |
|
53
|
// Awilix imports - replace awilix with @medusajs/framework/awilix |
|
54
|
{ |
|
55
|
pattern: /from\s+['"]awilix['"]/g, |
|
56
|
// eslint-disable-next-line quotes |
|
57
|
replacement: 'from "@medusajs/framework/awilix"', |
|
58
|
}, |
|
59
|
// PG imports - replace pg with @medusajs/framework/pg |
|
60
|
{ |
|
61
|
pattern: /from\s+['"]pg['"]/g, |
|
62
|
// eslint-disable-next-line quotes |
|
63
|
replacement: 'from "@medusajs/framework/pg"', |
|
64
|
}, |
|
65
|
// OpenTelemetry imports - replace @opentelemetry/instrumentation-pg, @opentelemetry/resources, |
|
66
|
// @opentelemetry/sdk-node, and @opentelemetry/sdk-trace-node with @medusajs/framework/opentelemetry/{subpath} |
|
67
|
{ |
|
68
|
pattern: /from\s+['"]@?opentelemetry\/(instrumentation-pg|resources|sdk-node|sdk-trace-node)['"]/g, |
|
69
|
// eslint-disable-next-line quotes |
|
70
|
replacement: 'from "@medusajs/framework/opentelemetry/$1"', |
|
71
|
}, |
|
72
|
// MikroORM require statements - replace require('@?mikro-orm/{subpath}') with require('@medusajs/framework/mikro-orm/{subpath}') |
|
73
|
{ |
|
74
|
pattern: /require\s*\(\s*['"]@?mikro-orm\/([^'"]+)['"]\s*\)/g, |
|
75
|
// eslint-disable-next-line quotes |
|
76
|
replacement: 'require("@medusajs/framework/mikro-orm/$1")', |
|
77
|
}, |
|
78
|
// Awilix require statements - replace require('awilix') with require('@medusajs/framework/awilix') |
|
79
|
{ |
|
80
|
pattern: /require\s*\(\s*['"]awilix['"]\s*\)/g, |
|
81
|
// eslint-disable-next-line quotes |
|
82
|
replacement: 'require("@medusajs/framework/awilix")', |
|
83
|
}, |
|
84
|
// PG require statements - replace require('pg') with require('@medusajs/framework/pg') |
|
85
|
{ |
|
86
|
pattern: /require\s*\(\s*['"]pg['"]\s*\)/g, |
|
87
|
// eslint-disable-next-line quotes |
|
88
|
replacement: 'require("@medusajs/framework/pg")', |
|
89
|
}, |
|
90
|
// OpenTelemetry require statements - replace require('@opentelemetry/instrumentation-pg'), |
|
91
|
// require('@opentelemetry/resources'), require('@opentelemetry/sdk-node'), and |
|
92
|
// require('@opentelemetry/sdk-trace-node') with require('@medusajs/framework/opentelemetry/{subpath}') |
|
93
|
{ |
|
94
|
pattern: /require\s*\(\s*['"]@?opentelemetry\/(instrumentation-pg|resources|sdk-node|sdk-trace-node)['"]\s*\)/g, |
|
95
|
// eslint-disable-next-line quotes |
|
96
|
replacement: 'require("@medusajs/framework/opentelemetry/$1")', |
|
97
|
}, |
|
98
|
] |
|
99
|
|
|
100
|
function processFile(filePath) { |
|
101
|
try { |
|
102
|
const content = fs.readFileSync(filePath, "utf8") |
|
103
|
let modifiedContent = content |
|
104
|
let wasModified = false |
|
105
|
|
|
106
|
replacements.forEach(({ pattern, replacement }) => { |
|
107
|
const newContent = modifiedContent.replace(pattern, replacement) |
|
108
|
if (newContent !== modifiedContent) { |
|
109
|
wasModified = true |
|
110
|
modifiedContent = newContent |
|
111
|
} |
|
112
|
}) |
|
113
|
|
|
114
|
if (wasModified) { |
|
115
|
fs.writeFileSync(filePath, modifiedContent) |
|
116
|
console.log(`✓ Updated: ${filePath}`) |
|
117
|
return true |
|
118
|
} |
|
119
|
|
|
120
|
return false |
|
121
|
} catch (error) { |
|
122
|
console.error(`✗ Error processing ${filePath}:`, error.message) |
|
123
|
return false |
|
124
|
} |
|
125
|
} |
|
126
|
|
|
127
|
function getTargetFiles() { |
|
128
|
try { |
|
129
|
// Get the current script's filename to exclude it from processing |
|
130
|
const currentScript = path.basename(__filename) |
|
131
|
|
|
132
|
// Find TypeScript/JavaScript files, excluding common directories that typically don't contain target imports |
|
133
|
const findCommand = `find . -name node_modules -prune -o -name .git -prune -o -name dist -prune -o -name build -prune -o -name coverage -prune -o -name "*.ts" -print -o -name "*.js" -print -o -name "*.tsx" -print -o -name "*.jsx" -print` |
|
134
|
const files = execSync(findCommand, { |
|
135
|
encoding: "utf8", |
|
136
|
maxBuffer: 50 * 1024 * 1024, // 50MB buffer |
|
137
|
}) |
|
138
|
.split("\n") |
|
139
|
.filter((line) => line.trim()) |
|
140
|
|
|
141
|
console.log(files) |
|
142
|
|
|
143
|
const targetFiles = [] |
|
144
|
let processedCount = 0 |
|
145
|
|
|
146
|
console.log(`📄 Scanning ${files.length} files for target imports and require statements...`) |
|
147
|
|
|
148
|
for (const file of files) { |
|
149
|
try { |
|
150
|
// Skip the current script file |
|
151
|
const fileName = path.basename(file) |
|
152
|
if (fileName === currentScript) { |
|
153
|
processedCount++ |
|
154
|
continue |
|
155
|
} |
|
156
|
const content = fs.readFileSync(file, "utf8") |
|
157
|
if ( |
|
158
|
/from\s+['"]@?mikro-orm\//.test(content) || |
|
159
|
/from\s+['"]awilix['"]/.test(content) || |
|
160
|
/from\s+['"]pg['"]/.test(content) || |
|
161
|
/require\s*\(\s*['"]@?mikro-orm\//.test(content) || |
|
162
|
/require\s*\(\s*['"]awilix['"]/.test(content) || |
|
163
|
/require\s*\(\s*['"]pg['"]/.test(content) |
|
164
|
) { |
|
165
|
targetFiles.push(file.startsWith("./") ? file.slice(2) : file) |
|
166
|
} |
|
167
|
processedCount++ |
|
168
|
if (processedCount % 100 === 0) { |
|
169
|
process.stdout.write( |
|
170
|
`\r📄 Processed ${processedCount}/${files.length} files...` |
|
171
|
) |
|
172
|
} |
|
173
|
} catch (fileError) { |
|
174
|
// Skip files that can't be read |
|
175
|
continue |
|
176
|
} |
|
177
|
} |
|
178
|
|
|
179
|
if (processedCount > 0) { |
|
180
|
console.log(`\r📄 Processed ${processedCount} files. `) |
|
181
|
} |
|
182
|
|
|
183
|
return targetFiles |
|
184
|
} catch (error) { |
|
185
|
console.error("Error finding target files:", error.message) |
|
186
|
return [] |
|
187
|
} |
|
188
|
} |
|
189
|
|
|
190
|
function main() { |
|
191
|
console.log("🔄 Finding files with target imports and require statements...") |
|
192
|
|
|
193
|
const targetFiles = getTargetFiles() |
|
194
|
|
|
195
|
if (targetFiles.length === 0) { |
|
196
|
console.log("ℹ️ No files found with target imports or require statements.") |
|
197
|
return |
|
198
|
} |
|
199
|
|
|
200
|
console.log(`📁 Found ${targetFiles.length} files to process`) |
|
201
|
|
|
202
|
let modifiedCount = 0 |
|
203
|
let errorCount = 0 |
|
204
|
|
|
205
|
targetFiles.forEach((filePath) => { |
|
206
|
const fullPath = path.resolve(filePath) |
|
207
|
if (fs.existsSync(fullPath)) { |
|
208
|
if (processFile(fullPath)) { |
|
209
|
modifiedCount++ |
|
210
|
} |
|
211
|
} else { |
|
212
|
console.warn(`⚠️ File not found: ${filePath}`) |
|
213
|
errorCount++ |
|
214
|
} |
|
215
|
}) |
|
216
|
|
|
217
|
console.log("\n📊 Summary:") |
|
218
|
console.log(` Files processed: ${targetFiles.length}`) |
|
219
|
console.log(` Files modified: ${modifiedCount}`) |
|
220
|
console.log(` Errors: ${errorCount}`) |
|
221
|
|
|
222
|
if (modifiedCount > 0) { |
|
223
|
console.log("\n✅ Import replacement completed successfully!") |
|
224
|
console.log("\n💡 Next steps:") |
|
225
|
console.log(" 1. Review the changes with: git diff") |
|
226
|
console.log(" 2. Run your tests to ensure everything works correctly") |
|
227
|
console.log(" 3. Commit the changes if you're satisfied") |
|
228
|
} else { |
|
229
|
console.log( |
|
230
|
"\n✅ No modifications needed - all imports are already correct!" |
|
231
|
) |
|
232
|
} |
|
233
|
} |
|
234
|
|
|
235
|
// Run if called directly |
|
236
|
if (require.main === module) { |
|
237
|
main() |
|
238
|
} |
|
239
|
|
|
240
|
module.exports = { processFile, getTargetFiles, main } |
|
241
|
``` |
|
242
|
|
|
243
|
This script scans your project for files that import from `mikro-orm/{subpath}`, `awilix`, or `pg`, and replaces those imports with their new equivalents from `@medusajs/framework`. It handles both ES module `import` statements and CommonJS `require` statements in JavaScript and TypeScript files. |
|
244
|
|
|
245
|
Next, run the following command in your terminal to make the script executable: |
|
246
|
|
|
247
|
<Note title="Windows Users"> |
|
248
|
|
|
249
|
You can run the script using `node` without changing permissions. |
|
250
|
|
|
251
|
</Note> |
|
252
|
|
|
253
|
```bash |
|
254
|
chmod +x replace-imports.js |
|
255
|
``` |
|
256
|
|
|
257
|
Finally, execute the script with the following command: |
|
258
|
|
|
259
|
```bash |
|
260
|
node replace-imports.js |
|
261
|
``` |
|
262
|
|
|
263
|
This will scan your project files, apply the necessary import replacements, and provide a summary of the changes made. |
|
264
|
|
|
265
|
--- |
|
266
|
|
|
267
|
## Next Steps |
|
268
|
|
|
269
|
After running the codemod, review the changes made to your codebase. You can use `git diff` to see the modifications. Additionally, run your tests to ensure everything works as expected. |
|
270
|
|
|
271
|
If everything is working correctly, you can remove the `replace-imports.js` file from your project. You can also remove the following packages from your `package.json`, as they're now included in the `@medusajs/framework` package: |
|
272
|
|
|
273
|
- `@mikro-orm/*` packages (for example, `@mikro-orm/core`, `@mikro-orm/migrations`, etc.) |
|
274
|
- `awilix` |
|
275
|
- `pg` |
|
276
|
- `@opentelemetry/instrumentation-pg` |
|
277
|
- `@opentelemetry/resources` |
|
278
|
- `@opentelemetry/sdk-node` |
|
279
|
- `@opentelemetry/sdk-trace-node` |