Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commonjs plugin is run on typescript definition files #135

Open
tobiasdiez opened this issue Oct 18, 2022 · 4 comments
Open

Commonjs plugin is run on typescript definition files #135

tobiasdiez opened this issue Oct 18, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@tobiasdiez
Copy link

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/@apollo+utils.withrequired@1.0.1/node_modules/@apollo/utils.withrequired/dist/index.d.ts
SyntaxError: Unexpected token (1:8) in /workspaces/apollo-server-integration-h3/node_modules/.pnpm/@apollo+utils.withrequired@1.0.1/node_modules/@apollo/utils.withrequired/dist/index.d.ts
    at pp$4.raise (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/rollup@3.2.2/node_modules/rollup/dist/es/shared/rollup.js:19861:13)
    at pp$9.unexpected (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/rollup@3.2.2/node_modules/rollup/dist/es/shared/rollup.js:17162:8)
    at pp$9.semicolon (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/rollup@3.2.2/node_modules/rollup/dist/es/shared/rollup.js:17139:66)
    at pp$8.parseExpressionStatement (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/rollup@3.2.2/node_modules/rollup/dist/es/shared/rollup.js:17622:8)
    at pp$8.parseStatement (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/rollup@3.2.2/node_modules/rollup/dist/es/shared/rollup.js:17355:24)
    at pp$8.parseTopLevel (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/rollup@3.2.2/node_modules/rollup/dist/es/shared/rollup.js:17219:21)
    at Parser.parse (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/rollup@3.2.2/node_modules/rollup/dist/es/shared/rollup.js:16991:15)
    at Function.parse (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/rollup@3.2.2/node_modules/rollup/dist/es/shared/rollup.js:17041:35)
    at Graph.contextParse (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/rollup@3.2.2/node_modules/rollup/dist/es/shared/rollup.js:23356:38)
    at tryParse (file:///workspaces/apollo-server-integration-h3/node_modules/.pnpm/@rollup+plugin-commonjs@23.0.0_rollup@3.2.2/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/@apollo+utils.withrequired@1.0.1/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/@apollo+utils.withrequired@1.0.1/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/@apollo+utils.withrequired@1.0.1/node_modules/@apollo/utils.withrequired/dist/index.d.ts'
  ]
}

I think the issue lies in

ctx.options.rollup.commonjs && commonjs({
extensions: DEFAULT_EXTENSIONS,
...ctx.options.rollup.commonjs
}),
, which runs the commonjs also on typescript definition files (and mjs, which seems to be wrong as well).

Reproduction: apollo-server-integrations/apollo-server-integration-h3#12

@deleonio
Copy link

deleonio commented Nov 1, 2022

The error occurs since these changes with 0.9.2.

Diff: v0.9.1...v0.9.2

@yusufkandemir
Copy link

It might work if you add the related dependency to the externals array in the configuration or move it from devDependencies into dependencies.

Related:

unbuild/src/build.ts

Lines 147 to 153 in 818ced7

// Infer dependencies from pkg
options.dependencies = Object.keys(pkg.dependencies || {});
options.peerDependencies = Object.keys(pkg.peerDependencies || {});
options.devDependencies = Object.keys(pkg.devDependencies || {});
// Add all dependencies as externals
options.externals.push(...options.dependencies, ...options.peerDependencies);

@seho-dev
Copy link

export default defineBuildConfig({
    entries: [
        './src/index',
    ],
    rollup: {
        emitCJS: true,
        dts: {
            respectExternal: false
        }
    },
    failOnWarn: false,
    declaration: true,
})

Add the respectExternal configuration to false

@enkot
Copy link

enkot commented Sep 20, 2023

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/ofetch@1.3.3/node_modules/ofetch/dist/index.d.ts
    at pp$4.raise (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/rollup@3.29.2/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/rollup@3.29.2/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/rollup@3.29.2/node_modules/rollup/dist/es/shared/node-entry.js:18778:41)
    at Parser.parseImport (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/rollup@3.29.2/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/rollup@3.29.2/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/rollup@3.29.2/node_modules/rollup/dist/es/shared/node-entry.js:18881:21)
    at Parser.parse (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/rollup@3.29.2/node_modules/rollup/dist/es/shared/node-entry.js:18653:15)
    at Function.parse (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/rollup@3.29.2/node_modules/rollup/dist/es/shared/node-entry.js:18703:35)
    at Graph.contextParse (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/rollup@3.29.2/node_modules/rollup/dist/es/shared/node-entry.js:25749:38)
    at tryParse (file:///Users/tarasbatenkov/Projects/nuxt-test-module/node_modules/.pnpm/@rollup+plugin-commonjs@25.0.4_rollup@3.29.2/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/ofetch@1.3.3/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/ofetch@1.3.3/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/ofetch@1.3.3/node_modules/ofetch/dist/index.d.ts'
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants