use null marker in id

quicker-redirects
gtmnayan 2 years ago
parent 97bae379ec
commit 0860cf3c4b

@ -1,5 +1,5 @@
// @ts-expect-error custom suffix doesn't have types // @ts-expect-error custom suffix doesn't have types
import js from './redirect.js?minified'; import js from 'minified-raw:./redirect.js';
// prerenderer will choke otherwise // prerenderer will choke otherwise
export const prerender = false; export const prerender = false;

@ -4,24 +4,50 @@ import { readFile } from 'node:fs/promises';
import browserslist from 'browserslist'; import browserslist from 'browserslist';
import { transformWithEsbuild } from 'vite'; import { transformWithEsbuild } from 'vite';
/** @type {import('vite').Plugin[]} */ /**
const plugins = [ * @param {string[]} ext
raw(['.ttf']), * @returns {import("vite").Plugin}
sveltekit(), */
{ function raw(ext) {
name: 'minified-raw-js', return {
name: 'vite-plugin-raw',
async transform(_, id) {
if (ext.some((e) => id.endsWith(e))) {
const buffer = await readFile(id);
return { code: `export default ${JSON.stringify(buffer)}`, map: null };
}
}
};
}
/** @returns {import('vite').Plugin} */
function minified_raw_plugin() {
const prefix = 'minified-raw:';
return {
name: 'minified-raw-js',
async resolveId(id, importer) {
if (id.startsWith(prefix)) {
const resolved = await this.resolve(id.slice(prefix.length), importer);
return '\0' + prefix + resolved.id;
}
},
async load(id) { async load(id) {
if (id.endsWith('.js?minified')) { if (id.startsWith('\0' + prefix)) {
const file = id.replace('?minified', ''); const real_id = id.slice(1 + prefix.length);
let code = await readFile(file, 'utf-8'); const original = await readFile(real_id, 'utf-8');
return `export default ${JSON.stringify( const { code } = await transformWithEsbuild(original, real_id, {
(await transformWithEsbuild(code, file, { minify: true, format: 'esm' })).code minify: true,
)}`; format: 'esm'
});
return `export default ${JSON.stringify(code)}`;
} }
} }
} };
]; }
/** @type {import('vite').Plugin[]} */
const plugins = [raw(['.ttf']), sveltekit(), minified_raw_plugin()];
// Only enable sharp if we're not in a webcontainer env // Only enable sharp if we're not in a webcontainer env
if (!process.versions.webcontainer) { if (!process.versions.webcontainer) {
@ -38,22 +64,6 @@ if (!process.versions.webcontainer) {
); );
} }
/**
* @param {string[]} ext
* @returns {import("vite").Plugin}
*/
function raw(ext) {
return {
name: 'vite-plugin-raw',
async transform(_, id) {
if (ext.some((e) => id.endsWith(e))) {
const buffer = await readFile(id);
return { code: `export default ${JSON.stringify(buffer)}`, map: null };
}
}
};
}
/** @type {import('vite').UserConfig} */ /** @type {import('vite').UserConfig} */
const config = { const config = {
logLevel: 'info', logLevel: 'info',

Loading…
Cancel
Save