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

fix: support polyfill import paths containing an escaping char (e.g. '\') #10859

Merged
merged 1 commit into from
Nov 12, 2022

Conversation

Tal500
Copy link
Contributor

@Tal500 Tal500 commented Nov 9, 2022

Description

When the additionalPolyfill property of plugin-legacy containing polyfill paths with escaping characters (e.g. '\'), the JS fails to compile because plugin-legacy doesn't escape them.
This PR add the escaping characters for this, simply by the trick of JSON.stringify().
This is a breaking change, since old code (e.g. my code) may use a hack to pass an escaped strings to additionalPolyfill, so I suggest to merge it now for Vite 4.

An example of the issue is when you want to include an absolute path of files in Windows (which uses '\' for directory paths), this was my previous sample with my own escaping workaround:

import { dirname, resolve } from 'path';
import legacy from '@vitejs/plugin-legacy';
import type { UserConfig } from 'vite';

const __dirname = dirname(fileURLToPath(import.meta.url));
const localPath = (path: string) => JSON.stringify(resolve(__dirname, path)).slice(1, -1);

const config: UserConfig = {
	plugins: [
		legacy({
			additionalLegacyPolyfills: [
				'custom-event-polyfill',
				'core-js/modules/es.promise.js',
				'whatwg-fetch',
				// ...,
				localPath(...),
				// ...,
			],
		}),
	]
};

export default config;

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Tal500 Tal500 changed the title fix: fix polyfill import paths containing an escaping char (e.g. '\\') fix: fix polyfill import paths containing an escaping char (e.g. '\') Nov 9, 2022
@patak-dev patak-dev changed the title fix: fix polyfill import paths containing an escaping char (e.g. '\') fix: support polyfill import paths containing an escaping char (e.g. '\') Nov 12, 2022
@patak-dev patak-dev merged commit 7ac2535 into vitejs:main Nov 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants