Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

feat: experimental vite support #387

Merged
merged 21 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function ssrRefPlugin({ loadOptions, getEnv, types: t }: Babel) {
method = 'hex'
break

case 'getCompositionApiKey':
case 'shallowSsrRef':
case 'ssrPromise':
case 'ssrRef':
Expand Down
2 changes: 2 additions & 0 deletions src/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export { useStatic } from './static'
export * from './defineHelpers'
export * from './wrappers'

export const getCompositionApiKey = (key?: string) => key

export type {
App,
ComponentInstance,
Expand Down
30 changes: 21 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { resolve, join } from 'upath'
import { withTrailingSlash } from 'ufo'
import { readdirSync, copyFileSync, existsSync, mkdirpSync } from 'fs-extra'

import type { Module, NuxtConfig } from '@nuxt/types'

import { name, version } from '../package.json'

import type { Module, NuxtConfig } from '@nuxt/types'
import { compositionApiPlugin } from './vite'

function isFullStatic(options: NuxtConfig) {
return (
Expand Down Expand Up @@ -95,6 +97,24 @@ const compositionApiModule: Module<any> = function compositionApiModule() {
},
})

const entryFile = resolve(this.options.buildDir || '', entryDst)

this.options.build.transpile = this.options.build.transpile || []
this.options.build.transpile.push(/@nuxtjs[\\/]composition-api/)
danielroe marked this conversation as resolved.
Show resolved Hide resolved
this.options.build.transpile.push(entryFile)

// Fake alias to prevent shadowing actual node_module
const aliases = ['@nuxtjs/composition-api', '@nuxtjs/vite-composition-api']

for (const alias of aliases) {
this.options.alias[alias] = entryFile
}

this.nuxt.hook('vite:extend', (ctx: any) => {
ctx.config.plugins.push(compositionApiPlugin())
ctx.config.optimizeDeps.exclude.push('@vue/composition-api')
})

this.options.build = this.options.build || {}
this.options.build.babel = this.options.build.babel || {}

Expand All @@ -107,9 +127,6 @@ const compositionApiModule: Module<any> = function compositionApiModule() {
this.options.build.babel.plugins.push(join(__dirname, 'babel'))
}

this.options.build.transpile = this.options.build.transpile || []
this.options.build.transpile.push(/@nuxtjs[\\/]composition-api/)

const actualPresets = this.options.build.babel.presets

this.options.build.babel.presets = (
Expand All @@ -131,11 +148,6 @@ const compositionApiModule: Module<any> = function compositionApiModule() {
return [[defaultPreset, newOptions]]
}

this.options.alias['@nuxtjs/composition-api'] = resolve(
this.options.buildDir || '',
entryDst
)

this.options.plugins = this.options.plugins || []
this.options.plugins.unshift(resolve(this.options.buildDir || '', pluginDst))
if (
Expand Down
34 changes: 34 additions & 0 deletions src/vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import crypto from 'crypto'

function createKey(
source: string,
method: crypto.BinaryToTextEncoding = 'base64'
) {
const hash = crypto.createHash('md5')
hash.update(source)
return hash.digest(method).toString()
}

export function compositionApiPlugin() {
return {
name: 'composition-api',
danielroe marked this conversation as resolved.
Show resolved Hide resolved
enforce: 'pre',
transform(code: string, id: string) {
code = code.replace(
/@nuxtjs[\\/]composition-api/g,
'@nuxtjs/vite-composition-api'
)
code = code.replace(
/getCompositionApiKey\(\)/g,
(_match, _p1, offset) => {
const key = createKey(`${id}-${offset}`)
return `"${key}"`
}
)
return {
code,
map: null,
}
},
}
}