Skip to content

Commit 907c5cb

Browse files
committed
refactor(importmap): PNPMImportMap instead of static 2nd ctor
1 parent 722e12e commit 907c5cb

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

src/ImportMap.mjs

+23-25
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,14 @@ const indent = x => new Array(x).fill(' ').join('')
1515

1616
export default class ImportMap extends Logged {
1717

18-
static async fromPNPM ({
19-
cwd = process.cwd(),
20-
imports = {},
21-
scopes = {},
22-
pkg = JSON.parse(runLong(cwd, 'pnpm', 'ls', '--json', '--depth', 'Infinity'))[0],
23-
write = false,
24-
patchMain = (_,__) => {},
25-
patchExports = (_,__) => {},
26-
patchImports = (_,__) => {},
27-
} = {}) {
28-
const map = new ImportMap({
29-
path: resolve(cwd, 'importmap.json'),
30-
imports,
31-
scopes,
32-
patchMain,
33-
patchExports,
34-
patchImports,
35-
})
36-
await map.add(0, pkg.name, pkg.version, pkg.dependencies)
37-
if (write) {
38-
writeFileSync(map.path, map.stringified)
39-
}
40-
return map
41-
}
42-
4318
constructor ({
4419
path = 'importmap.json',
4520
scopes = {},
4621
imports = {},
4722
patchMain = (_,__) => {},
4823
patchExports = (_,__) => {},
4924
patchImports = (_,__) => {},
25+
exportOrder = ['browser', 'import', 'default']
5026
} = {}) {
5127
super()
5228
this.path = path
@@ -55,6 +31,7 @@ export default class ImportMap extends Logged {
5531
this.patchMain = patchMain
5632
this.patchExports = patchExports
5733
this.patchImports = patchImports
34+
this.exportOrder = exportOrder
5835
}
5936

6037
get stringified () {
@@ -79,6 +56,7 @@ export default class ImportMap extends Logged {
7956
await this.add(depth + 2, name, version, dependencies ?? {}, selfRefs ?? {})
8057
}
8158
}
59+
return this
8260
}
8361

8462
// Rule 01: Add main entrypoint of package.
@@ -163,3 +141,23 @@ export default class ImportMap extends Logged {
163141
}
164142

165143
}
144+
145+
export class PNPMImportMap extends ImportMap {
146+
147+
constructor ({
148+
write = false,
149+
root = process.cwd(),
150+
pkg = JSON.parse(runLong(root, 'pnpm', 'ls', '--json', '--depth', 'Infinity'))[0],
151+
...options
152+
} = {}) {
153+
super({ path: resolve(root, 'importmap.json'), ...options })
154+
this.ready = this.add(0, pkg.name, pkg.version, pkg.dependencies)
155+
this.ready.then(()=>{
156+
if (write) {
157+
this.log('Writing to', bold(this.path))
158+
writeFileSync(this.path, this.stringified)
159+
}
160+
})
161+
}
162+
163+
}

ubik.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from './src/Patcher.mjs'
1212
export { default as Package } from './src/Package.mjs'
1313
export { Publisher, Compiler } from './src/Publisher.mjs'
1414
export { default as ImportMap } from './src/ImportMap.mjs'
15+
export * from './src/ImportMap.mjs'
1516

1617
export function printUsage ({ info } = console) {
1718
info('Welcome to', bold(`@hackbg/ubik ${Ubik.version}`))

0 commit comments

Comments
 (0)