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

Failed to bundle with preserveModules: true and preserveEntrySignatures: 'allow-extension' when module has no export name #4905

Closed
Tanimodori opened this issue Mar 16, 2023 · 1 comment · Fixed by #4913

Comments

@Tanimodori
Copy link

Tanimodori commented Mar 16, 2023

Rollup Version

This bug is able to be reproduced in

  • 3.7.5
  • 3.19.1

Operating System (or Browser)

Linux/Windows

Node Version (if applicable)

16.14.2

Link To Reproduction

https://stackblitz.com/edit/rollup-repro-lvtksv?file=package.json

Describe the bug

I'm trying to bundle node scripts with preserveModules: true and preserveEntrySignatures: 'allow-extension'. I need to use some dependency that use native node modules so we have to set preserveModules to preserve the use of __dirname. I also need to externalize node builtins and electron builtins. This setup will fail when I try to build when rollup reads a module that have no export name (just barely export function).

The log stops at

node_modules/.pnpm/yallist@4.0.0/node_modules/yallist/iterator
Cannot read properties of undefined (reading '0')
error during build:
TypeError: Cannot read properties of undefined (reading '0')
    at Chunk.setDynamicImportResolutions (file:///D:/Tanimodori/Project/__3RD__/vite-electron-updater-repro@TMR/node_modules/.pnpm/rollup@3.15.0/node_modules/rollup/dist/es/shared/rollup.js:15902:296)
    at Chunk.renderModules (file:///D:/Tanimodori/Project/__3RD__/vite-electron-updater-repro@TMR/node_modules/.pnpm/rollup@3.15.0/node_modules/rollup/dist/es/shared/rollup.js:15819:14)
    at Chunk.render (file:///D:/Tanimodori/Project/__3RD__/vite-electron-updater-repro@TMR/node_modules/.pnpm/rollup@3.15.0/node_modules/rollup/dist/es/shared/rollup.js:15352:111)
    at file:///D:/Tanimodori/Project/__3RD__/vite-electron-updater-repro@TMR/node_modules/.pnpm/rollup@3.15.0/node_modules/rollup/dist/es/shared/rollup.js:16722:72
    at Array.map (<anonymous>)
    at renderChunks (file:///D:/Tanimodori/Project/__3RD__/vite-electron-updater-repro@TMR/node_modules/.pnpm/rollup@3.15.0/node_modules/rollup/dist/es/shared/rollup.js:16722:53)
    at Bundle.generate (file:///D:/Tanimodori/Project/__3RD__/vite-electron-updater-repro@TMR/node_modules/.pnpm/rollup@3.15.0/node_modules/rollup/dist/es/shared/rollup.js:16935:19)
    at async file:///D:/Tanimodori/Project/__3RD__/vite-electron-updater-repro@TMR/node_modules/.pnpm/rollup@3.15.0/node_modules/rollup/dist/es/shared/rollup.js:25159:27
    at async catchUnfinishedHookActions (file:///D:/Tanimodori/Project/__3RD__/vite-electron-updater-repro@TMR/node_modules/.pnpm/rollup@3.15.0/node_modules/rollup/dist/es/shared/rollup.js:24261:20)
    at async build (file:///D:/Tanimodori/Project/__3RD__/vite-electron-updater-repro@TMR/node_modules/.pnpm/vite@4.1.1_@types+node@18.13.0/node_modules/vite/dist/node/chunks/dep-3007b26d.js:44650:22)
 ELIFECYCLE  Command failed with exit code 1.

Where the setDynamicImportResolutions is at rollup/src/Chunk.ts (permalink)

private setDynamicImportResolutions(fileName: string) {
		const { accessedGlobalsByScope, outputOptions, pluginDriver, snippets } = this;
		for (const resolvedDynamicImport of this.getIncludedDynamicImports()) {
			if (resolvedDynamicImport.chunk) {
				const { chunk, facadeChunk, node, resolution } = resolvedDynamicImport;
				if (chunk === this) {
					node.setInternalResolution(resolution.namespace);
				} else {
					node.setExternalResolution(
						(facadeChunk || chunk).exportMode,
						resolution,
						outputOptions,
						snippets,
						pluginDriver,
						accessedGlobalsByScope,
// THIS FOLLOWING LINE TRIGGERS THE ERROR
						`'${(facadeChunk || chunk).getImportPath(fileName)}'`,
						!facadeChunk?.strictFacade && chunk.exportNamesByVariable.get(resolution.namespace)![0],
						null
					);
				}
			} else {
				const { node, resolution } = resolvedDynamicImport;
				const [resolutionString, assertions] = this.getDynamicImportStringAndAssertions(
					resolution,
					fileName
				);
				node.setExternalResolution(
					'external',
					resolution,
					outputOptions,
					snippets,
					pluginDriver,
					accessedGlobalsByScope,
					resolutionString,
					false,
					assertions
				);
			}
		}
	}

I found somewhere in Chunk.ts (permalink)

// Namespaces do not have an export name
variableModule.getExportNamesByVariable().get(variable)?.[0] || '*',

Is the assertion here that modules must have an export name wrong? I'm not expert on it, just guessing.

The yallist/iterator.js is as follows:

'use strict'
module.exports = function (Yallist) {
  Yallist.prototype[Symbol.iterator] = function* () {
    for (let walker = this.head; walker; walker = walker.next) {
      yield walker.value
    }
  }
}

There's no export name, just bare anonymous function as output.

Downstream issues:

Note that build.rollupOptions.output.entryFileNames is just used to show the current processing files. Removing it will not solve the bug. Removing build.rollupOptions.output.preserveModules will solve the bug but I need __dirname working so it's a not workaround.

Expected Behaviour

npm i
npm run build

The code should be bundled correctly

Actual Behaviour

[!] TypeError: Cannot read properties of undefined (reading '0')
    at Chunk.setDynamicImportResolutions (file:///home/projects/rollup-repro-lvtksv/node_modules/rollup/dist/shared/rollup.js:16586:296)
    at Chunk.renderModules (file:///home/projects/rollup-repro-lvtksv/node_modules/rollup/dist/shared/rollup.js:16503:14)
    at Chunk.render (file:///home/projects/rollup-repro-lvtksv/node_modules/rollup/dist/shared/rollup.js:16036:111)
    at eval (file:///home/projects/rollup-repro-lvtksv/node_modules/rollup/dist/shared/rollup.js:17650:72)
    at Array.map (<anonymous>)
    at renderChunks (file:///home/projects/rollup-repro-lvtksv/node_modules/rollup/dist/shared/rollup.js:17650:53)
    at Bundle.generate (file:///home/projects/rollup-repro-lvtksv/node_modules/rollup/dist/shared/rollup.js:17872:19)
    at async eval (file:///home/projects/rollup-repro-lvtksv/node_modules/rollup/dist/shared/rollup.js:26021:27)
    at async catchUnfinishedHookActions (file:///home/projects/rollup-repro-lvtksv/node_modules/rollup/dist/shared/rollup.js:25204:20)
    at async Promise.all (index 0)
@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4913 as part of rollup@3.20.0. You can test it via npm install rollup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants