Skip to content

Commit 9c2e6e4

Browse files
authored
fix: resolve full path to runtime externals (#275)
1 parent 0fe04e8 commit 9c2e6e4

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"citty": "^0.1.6",
3636
"consola": "^3.2.3",
3737
"defu": "^6.1.4",
38+
"magic-regexp": "^0.8.0",
3839
"mlly": "^1.7.0",
3940
"pathe": "^1.1.2",
4041
"pkg-types": "^1.1.1",

pnpm-lock.yaml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/build.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { existsSync, promises as fsp } from 'node:fs'
22
import { pathToFileURL } from 'node:url'
3-
import { dirname, resolve } from 'pathe'
3+
import { basename, dirname, join, resolve } from 'pathe'
4+
import { filename } from 'pathe/utils'
45
import { readPackageJSON } from 'pkg-types'
56
import { parse } from 'tsconfck'
67
import type { TSConfig } from 'pkg-types'
78
import { defu } from 'defu'
9+
import { anyOf, createRegExp } from 'magic-regexp'
810
import { consola } from 'consola'
911
import type { ModuleMeta, NuxtModule } from '@nuxt/schema'
10-
import { findExports } from 'mlly'
12+
import { findExports, resolvePath } from 'mlly'
1113
import { defineCommand } from 'citty'
1214

1315
import { name, version } from '../../package.json'
@@ -71,7 +73,6 @@ export default defineCommand({
7173
cjsBridge: true,
7274
},
7375
externals: [
74-
/src\/runtime/,
7576
'@nuxt/schema',
7677
'@nuxt/schema-nightly',
7778
'@nuxt/schema-edge',
@@ -91,8 +92,44 @@ export default defineCommand({
9192
compilerOptions: await loadTSCompilerOptions(entry.input),
9293
})
9394
},
95+
async 'rollup:options'(ctx, options) {
96+
options.plugins ||= []
97+
if (!Array.isArray(options.plugins))
98+
options.plugins = [options.plugins]
99+
100+
const runtimeEntries = ctx.options.entries.filter(entry => entry.builder === 'mkdist')
101+
102+
const runtimeDirs = runtimeEntries.map(entry => basename(entry.input))
103+
const RUNTIME_RE = createRegExp(anyOf(...runtimeDirs).and('/'))
104+
105+
// Add extension for imports of runtime files in build
106+
options.plugins.unshift({
107+
name: 'nuxt-module-builder:runtime-externals',
108+
async resolveId(id, importer) {
109+
if (!RUNTIME_RE.test(id))
110+
return
111+
112+
const resolved = await this.resolve(id, importer, { skipSelf: true })
113+
if (!resolved)
114+
return
115+
116+
for (const entry of runtimeEntries) {
117+
if (!resolved.id.includes(entry.input))
118+
continue
119+
120+
const distFile = await resolvePath(join(dirname(resolved.id.replace(entry.input, entry.outDir!)), filename(resolved.id)))
121+
if (distFile) {
122+
return {
123+
external: true,
124+
id: distFile,
125+
}
126+
}
127+
}
128+
},
129+
})
130+
},
94131
async 'rollup:done'(ctx) {
95-
// Generate CommonJS stub
132+
// Generate CommonJS stub
96133
await writeCJSStub(ctx.options.outDir)
97134

98135
// Load module meta

test/build.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { beforeAll, describe, it, expect } from 'vitest'
44
import { execaCommand } from 'execa'
55
import { readPackageJSON } from 'pkg-types'
66
import { join } from 'pathe'
7+
import { findStaticImports } from 'mlly'
78
import { version } from '../package.json'
89

910
describe('module builder', () => {
@@ -96,6 +97,12 @@ describe('module builder', () => {
9697
expect(pluginDts).toMatchFileSnapshot('__snapshots__/plugin.d.ts')
9798
})
9899

100+
it('should correctly add extensions to imports from runtime/ directory', async () => {
101+
const moduleDts = await readFile(join(distDir, 'module.d.ts'), 'utf-8')
102+
const runtimeImport = findStaticImports(moduleDts).find(i => i.specifier.includes('runtime'))
103+
expect(runtimeImport!.code.trim()).toMatchInlineSnapshot(`"import { SharedTypeFromRuntime } from '../dist/runtime/plugins/plugin.js';"`)
104+
})
105+
99106
// TODO: https://github.com/nuxt/module-builder/issues/239
100107
it('should generate components correctly', async () => {
101108
const componentFile = await readFile(join(distDir, 'runtime/components/TestMe.vue'), 'utf-8')

0 commit comments

Comments
 (0)