unbuild
unbuild copied to clipboard
Commonjs plugin is run on typescript definition files
Using import type { WithRequired } from '@apollo/utils.withrequired' in a file that is processed with unbuild yields the following error:
Error building /workspaces/apollo-server-integration-h3: SyntaxError: Unexpected token (1:8) in /workspaces/apollo-server-integration-h3/node_modules/.pnpm/@[email protected]/node_modules/@apollo/utils.withrequired/dist/index.d.ts
SyntaxError: Unexpected token (1:8) in /workspaces/apollo-server-integration-h3/node_modules/.pnpm/@[email protected]/node_modules/@apollo/utils.withrequired/dist/index.d.ts
at pp$4.raise (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:19861:13)
at pp$9.unexpected (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:17162:8)
at pp$9.semicolon (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:17139:66)
at pp$8.parseExpressionStatement (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:17622:8)
at pp$8.parseStatement (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:17355:24)
at pp$8.parseTopLevel (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:17219:21)
at Parser.parse (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:16991:15)
at Function.parse (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:17041:35)
at Graph.contextParse (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:23356:38)
at tryParse (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/@[email protected][email protected]/node_modules/@rollup/plugin-commonjs/dist/es/index.js:17:12) {
pos: 8,
loc: {
column: 8,
file: '/workspaces/apollo-server-integration-h3/node_modules/.pnpm/@[email protected]/node_modules/@apollo/utils.withrequired/dist/index.d.ts',
line: 1
},
raisedAt: 12,
frame: '1: declare type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;\n' +
' ^\n' +
'2: export = WithRequired;\n' +
'3: //# sourceMappingURL=index.d.ts.map',
id: '/workspaces/apollo-server-integration-h3/node_modules/.pnpm/@[email protected]/node_modules/@apollo/utils.withrequired/dist/index.d.ts',
hook: 'resolveId',
code: 'PLUGIN_ERROR',
plugin: 'commonjs--resolver',
watchFiles: [
'/workspaces/apollo-server-integration-h3/src/index.ts',
'/workspaces/apollo-server-integration-h3/node_modules/.pnpm/@[email protected]/node_modules/@apollo/utils.withrequired/dist/index.d.ts'
]
}
I think the issue lies in https://github.com/unjs/unbuild/blob/da197ec7c8e4b1706241bc4785700ef7027e2448/src/builder/rollup.ts#L237-L240, which runs the commonjs also on typescript definition files (and mjs, which seems to be wrong as well).
Reproduction: https://github.com/apollo-server-integrations/apollo-server-integration-h3/pull/12
The error occurs since these changes with 0.9.2.
Diff: https://github.com/unjs/unbuild/compare/v0.9.1...v0.9.2
It might work if you add the related dependency to the externals array in the configuration or move it from devDependencies into dependencies.
Related: https://github.com/unjs/unbuild/blob/818ced7e7a33b600167e48b1d8208fe17903bb84/src/build.ts#L147-L153
export default defineBuildConfig({
entries: [
'./src/index',
],
rollup: {
emitCJS: true,
dts: {
respectExternal: false
}
},
failOnWarn: false,
declaration: true,
})
Add the respectExternal configuration to false
Same issue when importing ofetch types:
import type { FetchOptions } from 'ofetch'
SyntaxError: Unexpected token (1:12) in /Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/ofetch/dist/index.d.ts
at pp$4.raise (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:21616:13)
at pp$9.unexpected (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:18824:8)
at pp$9.expectContextual (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:18778:41)
at Parser.parseImport (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:26122:14)
at pp$8.parseStatement (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:19000:49)
at pp$8.parseTopLevel (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:18881:21)
at Parser.parse (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:18653:15)
at Function.parse (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:18703:35)
at Graph.contextParse (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:25749:38)
at tryParse (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/@[email protected][email protected]/node_modules/@rollup/plugin-commonjs/dist/es/index.js:17:12) {
pos: 12,
loc: {
column: 12,
file: '/Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/ofetch/dist/index.d.ts',
line: 1
},
raisedAt: 13,
frame: "1: import type { $ as $Fetch } from './shared/ofetch.441891d5.js';\n" +
' ^\n' +
'2: export type { C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as...',
id: '/Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/ofetch/dist/index.d.ts',
hook: 'resolveId',
code: 'PLUGIN_ERROR',
plugin: 'commonjs--resolver',
watchFiles: [
'/Users/tarasbatenkov/Projects/nuxt-test-module/src/module.ts',
'/Users/tarasbatenkov/Projects/nuxt-test-module/src/utils.ts',
'/Users/tarasbatenkov/Projects/nuxt-test-module/src/runtime/plugin.ts',
'/Users/tarasbatenkov/Projects/nuxt-test-module/src/runtime/composables/useOpenFetchOptions.ts',
'/Users/tarasbatenkov/Projects/nuxt-test-module/src/clients.ts',
'/Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/[email protected]/node_modules/ofetch/dist/index.d.ts'
]
}