@prisma/nextjs-monorepo-workaround-plugin 6.5.0-dev.7 → 6.5.0-dev.70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +45 -27
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -39,8 +39,8 @@ class PrismaPlugin {
|
|
|
39
39
|
const { webpack } = compiler
|
|
40
40
|
const { Compilation, sources } = webpack
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
const
|
|
42
|
+
const originAssetsToCopies = {} // { [original]: [dest1, dest2, ...] }
|
|
43
|
+
const origins = []
|
|
44
44
|
|
|
45
45
|
// read bundles to find which prisma files to copy (for all users)
|
|
46
46
|
compiler.hooks.compilation.tap('PrismaPlugin', (compilation) => {
|
|
@@ -67,27 +67,40 @@ class PrismaPlugin {
|
|
|
67
67
|
const prismaFiles = await getPrismaFiles(match[1])
|
|
68
68
|
|
|
69
69
|
prismaFiles.forEach((f) => {
|
|
70
|
+
// build the asset original path
|
|
70
71
|
const from = path.join(prismaDir, f)
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
72
|
+
// look for an existing origin to get its index (origin = prisma directory)
|
|
73
|
+
const originIndexLookup = origins.indexOf(prismaDir)
|
|
74
|
+
const originIndex =
|
|
75
|
+
originIndexLookup !== -1
|
|
76
|
+
? // if origin exist, take the index as the unique key for this origin,
|
|
77
|
+
originIndexLookup
|
|
78
|
+
: // else push the new origin and get it's index as well
|
|
79
|
+
// (push returns new length of the array, we subtract 1 to get 0-based index)
|
|
80
|
+
origins.push(prismaDir) - 1
|
|
81
|
+
// get the existing copies for this origin asset or set to an empty array
|
|
82
|
+
const assetCopies = (originAssetsToCopies[from] = originAssetsToCopies[from] || [])
|
|
83
|
+
|
|
84
|
+
// build the copy filename
|
|
85
|
+
// only the schema.prisma asset filename should be suffixed by originIndex
|
|
86
|
+
// as the rest should be the same across all the origins
|
|
87
|
+
const copyFilename = f === 'schema.prisma' ? `${f}${originIndex}` : f
|
|
88
|
+
// build the copy path by appending filename to the destination folder
|
|
89
|
+
// (where the asset we are copying this for will be emitted)
|
|
90
|
+
const copyPath = path.join(assetDir, copyFilename)
|
|
91
|
+
// if this copy is new for the origin asset, we add it to the copies array
|
|
92
|
+
if (!assetCopies.includes(copyPath)) {
|
|
93
|
+
assetCopies.push(copyPath)
|
|
80
94
|
}
|
|
81
95
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
96
|
+
// finally, we update the reference to schema.prisma file to point
|
|
97
|
+
// to the updated filename in the emitted asset
|
|
98
|
+
if (f === 'schema.prisma') {
|
|
99
|
+
// update "schema.prisma" to "schema.prisma{originIndex}" in the sources
|
|
100
|
+
const newSourceString = oldSourceContents.replace(/schema\.prisma/g, copyFilename)
|
|
85
101
|
const newRawSource = new sources.RawSource(newSourceString)
|
|
86
102
|
compilation.updateAsset(assetName, newRawSource)
|
|
87
103
|
}
|
|
88
|
-
|
|
89
|
-
// update copy map
|
|
90
|
-
fromDestPrismaMap[from] = path.join(assetDir, f)
|
|
91
104
|
})
|
|
92
105
|
}
|
|
93
106
|
})
|
|
@@ -104,9 +117,9 @@ class PrismaPlugin {
|
|
|
104
117
|
name: 'PrismaPlugin',
|
|
105
118
|
stage: Compilation.PROCESS_ASSETS_STAGE_ANALYSE,
|
|
106
119
|
},
|
|
107
|
-
|
|
120
|
+
(assets) => {
|
|
108
121
|
const nftAssetNames = Object.keys(assets).filter((k) => k.endsWith('.nft.json'))
|
|
109
|
-
|
|
122
|
+
nftAssetNames.forEach((assetName) => {
|
|
110
123
|
// prepare paths
|
|
111
124
|
const outputDir = compiler.outputPath
|
|
112
125
|
const assetPath = path.resolve(outputDir, assetName)
|
|
@@ -118,8 +131,9 @@ class PrismaPlugin {
|
|
|
118
131
|
const ntfLoadedAsJson = JSON.parse(oldSourceContents)
|
|
119
132
|
|
|
120
133
|
// update sources
|
|
121
|
-
Object.
|
|
122
|
-
|
|
134
|
+
Object.values(originAssetsToCopies).forEach((copies) => {
|
|
135
|
+
const copiesPaths = copies.map((copy) => path.relative(assetDir, copy))
|
|
136
|
+
ntfLoadedAsJson.files.push(...copiesPaths)
|
|
123
137
|
})
|
|
124
138
|
|
|
125
139
|
// persist sources
|
|
@@ -128,18 +142,22 @@ class PrismaPlugin {
|
|
|
128
142
|
compilation.updateAsset(assetName, newRawSource)
|
|
129
143
|
})
|
|
130
144
|
|
|
131
|
-
|
|
145
|
+
return Promise.resolve()
|
|
132
146
|
},
|
|
133
147
|
)
|
|
134
148
|
})
|
|
135
149
|
|
|
136
150
|
// copy prisma files to output as the final step (for all users)
|
|
137
151
|
compiler.hooks.done.tapPromise('PrismaPlugin', async () => {
|
|
138
|
-
const asyncActions = Object.entries(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
152
|
+
const asyncActions = Object.entries(originAssetsToCopies).map(async ([from, copies]) => {
|
|
153
|
+
await Promise.all(
|
|
154
|
+
copies.map(async (copy) => {
|
|
155
|
+
// only copy if file doesn't exist, necessary for watch mode
|
|
156
|
+
if ((await fs.access(copy).catch(() => false)) === false) {
|
|
157
|
+
return fs.copyFile(from, copy)
|
|
158
|
+
}
|
|
159
|
+
}),
|
|
160
|
+
)
|
|
143
161
|
})
|
|
144
162
|
|
|
145
163
|
await Promise.all(asyncActions)
|