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

Build output is different when building on Windows or Mac #4496

Closed
mabramishvili opened this issue May 11, 2022 · 2 comments · Fixed by #4591
Closed

Build output is different when building on Windows or Mac #4496

mabramishvili opened this issue May 11, 2022 · 2 comments · Fixed by #4591

Comments

@mabramishvili
Copy link

mabramishvili commented May 11, 2022

Rollup Version

2.70.1

Operating System (or Browser)

Windows

Node Version (if applicable)

14.16.1

Link To Reproduction

not available

Expected Behaviour

Seems like on Windows, preserveModulesRoot: 'src' will only work on entry files (files defined in "input"). While on OSX it will work on all files.

I would expect it to be consistent between different environments.

Found this question but there is no working answer:
https://stackoverflow.com/questions/64570174/rollup-preservemodulesroot-not-working-as-expected

Actual Behaviour

I have a build output defined like:

{
  input: 'src/index.ts',
  output: [
    {
      dir: 'dist/cjs',
      format: 'cjs',
      preserveModules: true,
      preserveModulesRoot: 'src',
      sourcemap: true,
      exports: 'named',
    },
    {
      dir: 'dist/esm',
      format: 'esm',
      preserveModules: true,
      sourcemap: true,
      preserveModulesRoot: 'src',
      exports: 'named',
    },
  ],
}

When building on Windows, the output in dist is like:

dist
    -- esm
        --components
        --src
            --components
       index.d.ts
       index.js

esm/components will only include d.ts files and esm/src/components will actually include .js files

When building on OSX, the output in dist is like:

dist
    -- esm
        --components
       index.d.ts
       index.js

notice there is no "src" folder and all files are placed inside esm/components.

@cleverpp
Copy link
Contributor

+1, i have same problem。

after debugging, I found that it is caused by the following code

at 'rollup/src/utils/options/normalizeOutputOptions.ts'

...
preserveModulesRoot: getPreserveModulesRoot(config),
....

const getPreserveModulesRoot = (
	config: OutputOptions
): NormalizedOutputOptions['preserveModulesRoot'] => {
	const { preserveModulesRoot } = config;
	if (preserveModulesRoot === null || preserveModulesRoot === undefined) {
		return undefined;
	}
	return resolve(preserveModulesRoot);
};

but, at ''src/chunk.ts" 中

const currentPath = `${currentDir}/${fileName}`;
const { preserveModulesRoot } = options;
if (preserveModulesRoot && currentPath.startsWith(preserveModulesRoot)) {
	path = currentPath.slice(preserveModulesRoot.length).replace(/^[\\/]/, '');
} else {
	path = relative(preserveModulesRelativeDir, currentPath);
}

currentPath not resolve, but preserveModulesRoot had resolve, so in windows platform,currentPath.startsWith(preserveModulesRoot) always be false。

@cleverpp cleverpp mentioned this issue Jul 28, 2022
9 tasks
@Tanimodori
Copy link

+1 This bug leads to preserveModuleRoot not working. That startsWith is definitely the smoking gun. Until this pr get merged, my temp fix (see Tanimodori/generator-vite-electron-builder@ae8351a) is adding plugin to fix:

const config = {
  plugins: [
    {
      name: 'fixPreserveModulesRoot',
      renderStart(options) {
        options.preserveModulesRoot = options.preserveModulesRoot?.replace(
          new RegExp(/\\/, 'g'),
          '/',
        );
      },
    },
  ],
};

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

Successfully merging a pull request may close this issue.

3 participants