This commit is contained in:
parent
4393a36a79
commit
24a71fb76f
7 changed files with 12 additions and 204 deletions
|
@ -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,
|
||||
|
|
|
@ -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": {
|
||||
|
|
1
packages/worker/src/sw/iframe/.gitignore
vendored
Normal file
1
packages/worker/src/sw/iframe/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
script-bundled.js
|
|
@ -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> = {}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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: [],
|
||||
}),
|
||||
],
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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
|
Loading…
Reference in a new issue