|
1 |
| -import { promises as fs } from 'fs' |
| 1 | +import { promises as fs, existsSync } from 'fs' |
2 | 2 | import { rollup } from 'rollup'
|
3 | 3 | import file from './file.js'
|
4 | 4 | import testFile from './testFile.js'
|
5 | 5 | import path from '../path-to-url.js'
|
6 | 6 | import { fileURLToPath } from 'url'
|
7 |
| -import { join } from 'path' |
| 7 | +import { join, dirname } from 'path' |
8 | 8 | import rmtree from '@tgrajewski/rmtree'
|
9 | 9 | import preserveShebangs from 'rollup-plugin-preserve-shebangs'
|
10 | 10 |
|
@@ -163,6 +163,37 @@ class Package {
|
163 | 163 | await unlink(new URL(dist + '/cjs/_ipjsInput.js'))
|
164 | 164 | }
|
165 | 165 |
|
| 166 | + async stubFiles (dist, files) { |
| 167 | + await Promise.all( |
| 168 | + files.map(async (file) => { |
| 169 | + if (file === '.') { |
| 170 | + file = 'index.js' |
| 171 | + } |
| 172 | + if (file.startsWith('./')) { |
| 173 | + file = file.substring(2) |
| 174 | + } |
| 175 | + const dir = dirname(file) |
| 176 | + if (dir !== '.') { |
| 177 | + try { |
| 178 | + await mkdir(new URL(dist + '/' + dir), { |
| 179 | + recursive: true |
| 180 | + }) |
| 181 | + } catch (err) { |
| 182 | + if (err.code !== 'EEXIST') { |
| 183 | + throw err |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + if (existsSync(new URL(dist + '/' + file))) { |
| 189 | + return |
| 190 | + } |
| 191 | + |
| 192 | + await writeFile(new URL(dist + '/' + file), '') |
| 193 | + }) |
| 194 | + ) |
| 195 | + } |
| 196 | + |
166 | 197 | async deflate (dist) {
|
167 | 198 | if (!(dist instanceof URL)) dist = path(dist)
|
168 | 199 | rmtree(fileURLToPath(dist))
|
@@ -210,6 +241,7 @@ class Package {
|
210 | 241 | json.exports = json.exports.import
|
211 | 242 | json.browser = json.browser.import
|
212 | 243 | }
|
| 244 | + await this.stubFiles(dist, Object.keys(json.browser)) |
213 | 245 | let files = Promise.all(pending)
|
214 | 246 | pending.push(writeFile(new URL(dist + '/package.json'), JSON.stringify(json, null, 2)))
|
215 | 247 | const typeModule = {
|
|
0 commit comments