build: fixed prod build
All checks were successful
Docs / build (push) Successful in 2m16s

This commit is contained in:
alina 🌸 2025-01-19 19:54:44 +03:00
parent 4393a36a79
commit 888bcde56d
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
10 changed files with 16 additions and 216 deletions

View file

@ -12,7 +12,8 @@ for (const [key, value] of Object.entries(tailwindConfig)) {
export default antfu({
ignores: [
'src/components/Editor/utils/*.json',
'packages/repl/components/Editor/utils/*.json',
'packages/worker/src/sw/iframe/script-bundled.js',
'vendor',
],
typescript: true,

View file

@ -2,8 +2,6 @@ import type { UserConfig } from 'vite'
import { join } from 'node:path'
import { defineConfig, loadEnv } from 'vite'
import solid from 'vite-plugin-solid'
// eslint-disable-next-line import/no-relative-packages
import externalizeDeps from '../../scripts/vite-plugin-externalize-dependencies.ts'
export default defineConfig((env): UserConfig => {
process.env = {
@ -25,9 +23,6 @@ export default defineConfig((env): UserConfig => {
},
plugins: [
solid(),
externalizeDeps({
externals: [],
}),
],
}
})

View file

@ -9,7 +9,8 @@
},
"scripts": {
"dev": "vite",
"build": "vite build",
"build": "pnpm run build:runner-script && vite build",
"build:runner-script": "esbuild src/sw/iframe/script.ts --bundle \"--external:@mtcute/web\" --format=esm --outfile=src/sw/iframe/script-bundled.js",
"preview": "vite preview"
},
"dependencies": {

View file

@ -0,0 +1 @@
script-bundled.js

View file

@ -1,3 +0,0 @@
declare module '@mtcute/web?external' {
export * from '@mtcute/web'
}

View file

@ -1,6 +1,9 @@
// eslint-disable-next-line antfu/no-import-dist
import chobitsuUrl from '../../../../../vendor/chobitsu/dist/chobitsu.js?url'
import runnerScriptUrl from './script.ts?url'
const runnerScriptUrl = import.meta.env.DEV
? new URL('./script.ts', import.meta.url).href
: new URL('./script-bundled.js', import.meta.url).href
export async function generateImportMap(packageJsons: any[]) {
const importMap: Record<string, string> = {}

View file

@ -1,4 +1,4 @@
import { TelegramClient } from '@mtcute/web?external'
import { Long, TelegramClient } from '@mtcute/web'
type ConnectionState = import('@mtcute/web').ConnectionState
type TelegramClientOptions = import('@mtcute/web').TelegramClientOptions

View file

@ -1,8 +1,6 @@
import type { UserConfig } from 'vite'
import { join } from 'node:path'
import { defineConfig, loadEnv } from 'vite'
// eslint-disable-next-line import/no-relative-packages
import externalizeDeps from '../../scripts/vite-plugin-externalize-dependencies.ts'
export default defineConfig((env): UserConfig => {
process.env = {
@ -18,14 +16,11 @@ export default defineConfig((env): UserConfig => {
exclude: ['@mtcute/wasm'],
},
build: {
emptyOutDir: true,
assetsDir: '',
rollupOptions: {
external: ['node:fs/promises', 'node:crypto'],
},
},
plugins: [
externalizeDeps({
externals: [],
}),
],
}
})

View file

@ -816,8 +816,8 @@ packages:
'@mtcute/tl@196.0.0':
resolution: {integrity: sha512-/FEoLfGk7VatEMkBWn4naRQnolodJX0CL0C2rPJr+gmhLSVMQs1a9HqLMIUFYRlEvZ4ayk4ujOWvF9G64xvW5g==}
'@mtcute/wasm@0.19.0':
resolution: {integrity: sha512-QvSCjNR3/Y4knRMfccNXAdPUCgnQfjmOD/LGGHgQq/5/7SfA7f4/wk7G/TdlnBPArqk58EvyrC5qeKEy81y9Xw==}
'@mtcute/wasm@0.19.9':
resolution: {integrity: sha512-HFuZRWXW7dv2wNp7ePqW2AiJqaMtETQAQlGywJdWpM4dG+KpWarP0CvkOvvvvI1JbrDIw0jMoW3G4YP2WZnnFA==}
'@mtcute/web@0.19.5':
resolution: {integrity: sha512-NtO9fKGLobAbm+Nm3OAy0gmY1sphz+Rv+JbUGBoPKY6PQUuT2uBCoRvutoHraBZd91Y2hExHMg2juWHgmJ6QPg==}
@ -3314,13 +3314,13 @@ snapshots:
dependencies:
long: 5.2.3
'@mtcute/wasm@0.19.0': {}
'@mtcute/wasm@0.19.9': {}
'@mtcute/web@0.19.5':
dependencies:
'@fuman/net': 0.0.9
'@mtcute/core': 0.19.6
'@mtcute/wasm': 0.19.0
'@mtcute/wasm': 0.19.9
'@nanostores/persistent@0.10.2(nanostores@0.11.3)':
dependencies:

View file

@ -1,193 +0,0 @@
// based on https://github.com/MilanKovacic/vite-plugin-externalize-dependencies/blob/main/src/index.ts
// but modified to support `?external` imports
import type { Plugin as EsbuildPlugin, OnResolveArgs, PluginBuild } from 'esbuild'
import type { Plugin, ResolvedConfig, UserConfig } from 'vite'
type ExternalCriteria = string | RegExp | ((id: string) => boolean)
interface ModulePrefixTransformPluginOptions {
/** The base path of the vite configuration */
base: string
}
interface PluginOptions {
externals: ExternalCriteria[]
}
const resolvedExternals = new Set<string>()
function isExternal(id: string, externals: ExternalCriteria[]): boolean {
if (id.endsWith('?external')) return true
return externals.some((external) => {
if (typeof external === 'string') {
return id === external || id.startsWith(`${external}/`)
}
if (external instanceof RegExp) {
return external.test(id)
}
if (typeof external === 'function') {
return external(id)
}
return false
})
}
/**
* Creates a plugin for esbuild to externalize specific modules.
* esbuild is used by Vite during development.
* This plugin is injected into optimizeDeps.esbuildOptions.plugins, and runs during the dependency scanning / optimization phase.
*
* @param options - Plugin options
*
* @returns The esbuild plugin
*/
function esbuildPluginExternalize(externals: ExternalCriteria[]): EsbuildPlugin {
return {
name: 'externalize',
setup(build: PluginBuild) {
build.onResolve({ filter: /.*/ }, (args: OnResolveArgs) => {
if (
isExternal(args.path, externals)
&& args.kind === 'import-statement'
) {
resolvedExternals.add(args.path)
return {
path: args.path,
external: true,
}
}
// Supresses the following error:
// The entry point [moduleName] cannot be marked as external
if (isExternal(args.path, externals) && args.kind === 'entry-point') {
resolvedExternals.add(args.path)
return { path: args.path, namespace: 'externalized-modules' }
}
return null
})
// Supresses the following error:
// Do not know how to load path: [namespace:moduleName]
build.onLoad({ filter: /.*/ }, (args) => {
if (isExternal(args.path, externals)) {
return { contents: '' }
}
return null
})
},
}
}
/**
* Creates a plugin to remove prefix from imports injected by Vite.
* If module is externalized, Vite will prefix imports with "/\@id/" during development.
*
* @param options - The plugin options
*
* @returns Vite plugin to remove prefix from imports
*/
function modulePrefixTransform({
base,
}: ModulePrefixTransformPluginOptions): Plugin {
return {
name: 'vite-plugin-remove-prefix',
transform: (code: string): string => {
// Verify if there are any external modules resolved to avoid having /\/@id\/()/g regex
if (resolvedExternals.size === 0) return code
const viteImportAnalysisModulePrefix = '@id/'
const prefixedImportRegex = new RegExp(
`${base}${viteImportAnalysisModulePrefix}(${
[...resolvedExternals]
.map(it => it.replace(/\?/g, '\\?'))
.join('|')})`,
'g',
)
if (prefixedImportRegex.test(code)) {
return code.replace(
prefixedImportRegex,
(_: string, externalName: string) => externalName.replace(/\?external$/g, ''),
)
}
return code
},
}
}
/**
* Creates a Vite plugin to externalize specific modules.
* This plugin is only used during development.
* To externalize modules in production, configure build.rollupOptions.external.
*
* @param externals - The list of modules to externalize.
*
* @returns The Vite plugin.
*/
function vitePluginExternalize(options: PluginOptions): Plugin {
return {
name: 'vite-plugin-externalize',
enforce: 'pre',
apply: 'serve',
config: (config: UserConfig): Omit<UserConfig, 'plugins'> | null | void => {
config.optimizeDeps ??= {}
config.optimizeDeps.esbuildOptions ??= {}
config.optimizeDeps.esbuildOptions.plugins ??= []
// Prevent the plugin from being inserted multiple times
const pluginName = 'externalize'
const isPluginAdded = config.optimizeDeps.esbuildOptions.plugins.some(
(plugin: any) => plugin.name === pluginName,
)
if (!isPluginAdded) {
config.optimizeDeps.esbuildOptions.plugins.push(
esbuildPluginExternalize(options.externals) as any,
)
}
return null
},
configResolved: (resolvedConfig: ResolvedConfig) => {
// Plugins are read-only, and should not be modified,
// however modulePrefixTransformPlugin MUST run after vite:import-analysis (which adds the prefix to imports)
(resolvedConfig.plugins as Plugin[]).push(
modulePrefixTransform({ base: resolvedConfig.base ?? '/' }),
)
},
// Supresses the following warning:
// Failed to resolve import [dependency] from [sourceFile]. Does the file exist?
resolveId: (id: string) => {
if (resolvedExternals.has(id)) {
return { id, external: true }
}
// During subsequent runs after the dependency optimization is completed, esbuild plugin might not be called.
// This will cause the resolvedExternals to be empty, and the plugin will not be able to resolve the external modules, which is why a direct check is required.
if (isExternal(id, options.externals)) {
resolvedExternals.add(id)
return { id, external: true }
}
return null
},
// Supresses the following warning:
// The following dependencies are imported but could not be resolved: [dependency] (imported by [sourceFile])
load: (id: string) => {
if (resolvedExternals.has(id)) {
return { code: 'export default {};' }
}
return null
},
}
}
// Justification: Vite plugins are expected to provide a default export
export default vitePluginExternalize