@prisma/nextjs-monorepo-workaround-plugin 6.5.0-dev.42 → 6.5.0-dev.44

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.
Files changed (2) hide show
  1. package/index.js +45 -27
  2. 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
- let schemaCount = 0
43
- const fromDestPrismaMap = {} // { [from]: dest }
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
- // if we have multiple schema.prisma files, we need to rename them
73
- if (f === 'schema.prisma' && fromDestPrismaMap[from] === undefined) {
74
- f += ++schemaCount
75
- }
76
-
77
- // if we already have renamed it, we need to get its "renamed" name
78
- if (f.includes('schema.prisma') && fromDestPrismaMap[from] !== undefined) {
79
- f = path.basename(fromDestPrismaMap[from])
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
- if (f.includes('schema.prisma')) {
83
- // update "schema.prisma" to "schema.prisma{number}" in the sources
84
- const newSourceString = oldSourceContents.replace(/schema\.prisma/g, f)
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
- async (assets) => {
120
+ (assets) => {
108
121
  const nftAssetNames = Object.keys(assets).filter((k) => k.endsWith('.nft.json'))
109
- const nftAsyncActions = nftAssetNames.map((assetName) => {
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.entries(fromDestPrismaMap).forEach(([_from, dest]) => {
122
- ntfLoadedAsJson.files.push(path.relative(assetDir, dest))
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
- await Promise.all(nftAsyncActions)
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(fromDestPrismaMap).map(async ([from, dest]) => {
139
- // only copy if file doesn't exist, necessary for watch mode
140
- if ((await fs.access(dest).catch(() => false)) === false) {
141
- return fs.copyFile(from, dest)
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)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma/nextjs-monorepo-workaround-plugin",
3
- "version": "6.5.0-dev.42",
3
+ "version": "6.5.0-dev.44",
4
4
  "description": "Ensures that your Prisma files are copied",
5
5
  "main": "index.js",
6
6
  "repository": {