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

feat: export constants from @vite/plugin-legacy #5948

Closed
wants to merge 2 commits into from

Conversation

dishuostec
Copy link

@dishuostec dishuostec commented Dec 3, 2021

Description

This PR does not changed anything, it just exports some constants.

Additional context

I'm trying to integrate @vitejs/plugin-legacy into SvelteKit.

SvelteKit has an independent logic of HTML rendering. It seems that SvelteKit could not simply use @vitejs/plugin-legacy to directly modify html. With these constants, it is able to do two things:

  1. Find with file are polyfills generated by plugin.

  2. Generates script tags like this:

import * as constants from '@vitejs/plugin-legacy/constants';

const {
	dynamicFallbackInlineCode,
	safari10NoModuleFix,
	legacyPolyfillId,
	legacyEntryId,
	// systemJSInlineCode,
} = constants;

const systemJSInlineCode = `(function(s,i){i(s.getAttribute('data-src')).then(function(m){window._sveltekit_init(m.start,i,s.getAttribute('data-nodes'))})})(document.getElementById('${legacyEntryId}'),function(m){return System.import(m)});`;

/**
 * @param {{
 *   legacy_polyfill_asset?: string;
 *   entry_file: string;
 *   branch: any[];
 *   get_entry: (entry:string)=>string;
 * }} config
 * @return {string[]}
 */
export function render_script_tags({ legacy_polyfill_asset, get_entry, entry_file, branch }) {
	const legacyEntryFilename = get_entry(entry_file);

	const scripts = [];

	// inject dynamic import fallback entry
	if (legacy_polyfill_asset) {
		scripts.push(`<script type="module">${dynamicFallbackInlineCode}</script>`);
	}

	// inject Safari 10 nomodule fix
	if (legacyEntryFilename) {
		scripts.push(`<script nomodule>${safari10NoModuleFix}</script>`);
	}

	// inject legacy polyfills
	if (legacy_polyfill_asset) {
		scripts.push(
			`<script nomodule id="${legacyPolyfillId}" src="${legacy_polyfill_asset}"></script>`
		);
	}

	// inject legacy entry
	if (legacyEntryFilename) {
		scripts.push(
			`<script nomodule id="${legacyEntryId}" data-src="${legacyEntryFilename}" data-nodes="${(
				branch || []
			)
				.map(({ node }) => get_entry(node.entry))
				.join(',')}">${systemJSInlineCode}</script>`
		);
	}

	return scripts;
}

@Shinigami92 Shinigami92 added plugin: legacy p2-nice-to-have Not breaking anything but nice to have (priority) labels Dec 3, 2021
@bluwy
Copy link
Member

bluwy commented Dec 4, 2021

Related PRs from SvelteKit: sveltejs/kit#2953 and sveltejs/kit#2745

@shynome
Copy link

shynome commented Dec 28, 2021

sveltekit needs @vite/plugin-legacy, hope this pr can be merged as soon as possible

@bluwy
Copy link
Member

bluwy commented Dec 28, 2021

The PR title needs to be semantic, e.g. feat: export constants from plugin-legacy. Code-wise, I'm probably not qualified to review the overall benefits of this, but if it's something the team is comfortable of exporting, I think we should export them from index.js instead? I don't see much benefits having them at constants.js

@sapphi-red
Copy link
Member

It seems this is same with #4045.

@Niputi Niputi changed the title Export some constants from @vite/plugin-legacy feat: export constants from @vite/plugin-legacy Apr 28, 2022
@bluwy
Copy link
Member

bluwy commented May 15, 2022

It seems this is same with #4045.

Yeah it seems like so. Let's move the discussion over to #4045 then as there's more info there. Thanks for the PR and example usecase!

@bluwy bluwy closed this May 15, 2022
@bluwy bluwy mentioned this pull request May 15, 2022
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority) plugin: legacy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants