mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
chore(nuxt3): add more types (#51)
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
parent
6c86b222a8
commit
2db79adfb8
@ -24,6 +24,7 @@
|
||||
"@rollup/plugin-node-resolve": "^11.2.1",
|
||||
"@types/jest": "^26.0.22",
|
||||
"@types/node": "^14.14.37",
|
||||
"@types/rimraf": "^3.0.0",
|
||||
"chalk": "^4.1.0",
|
||||
"defu": "^3.2.2",
|
||||
"esbuild": "^0.10.0",
|
||||
|
@ -36,6 +36,12 @@
|
||||
"lodash": "^4.17.21",
|
||||
"nuxt-cli": "^0.1.1",
|
||||
"scule": "^0.1.1",
|
||||
"ufo": "^0.6.10"
|
||||
"ufo": "^0.6.10",
|
||||
"upath": "^2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/fs-extra": "^9.0.10",
|
||||
"@types/hash-sum": "^1.0.0",
|
||||
"@types/lodash": "^4.14.168"
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ export async function createApp (
|
||||
pages: {
|
||||
dir: 'pages'
|
||||
}
|
||||
})
|
||||
} as NuxtApp)
|
||||
|
||||
// Resolve app.main
|
||||
const resolveOptions = {
|
||||
|
@ -9,7 +9,7 @@ import {
|
||||
scanTemplates,
|
||||
NuxtTemplate
|
||||
} from './template'
|
||||
import { createWatcher } from './watch'
|
||||
import { createWatcher, WatchCallback } from './watch'
|
||||
import { createApp, NuxtApp } from './app'
|
||||
import Ignore from './utils/ignore'
|
||||
|
||||
@ -20,7 +20,7 @@ export class Builder {
|
||||
templates: NuxtTemplate[]
|
||||
app: NuxtApp
|
||||
|
||||
constructor (nuxt) {
|
||||
constructor (nuxt: Nuxt) {
|
||||
this.nuxt = nuxt
|
||||
this.ignore = new Ignore({
|
||||
rootDir: nuxt.options.srcDir,
|
||||
@ -65,7 +65,7 @@ function watch (builder: Builder) {
|
||||
|
||||
// Watch user app
|
||||
// TODO: handle multiples app dirs
|
||||
const appPattern = `${builder.app.dir}/**/*.{${nuxt.options.extensions.join(',')}}`
|
||||
const appPattern = `${builder.app.dir}/**/*{${nuxt.options.extensions.join(',')}}`
|
||||
const appWatcher = createWatcher(appPattern, { ...options, cwd: builder.app.dir }, ignore)
|
||||
// appWatcher.debug('srcDir')
|
||||
const refreshTemplates = debounce(() => generate(builder), 100)
|
||||
@ -77,7 +77,8 @@ function watch (builder: Builder) {
|
||||
appWatcher.watch(/^plugins/, refreshTemplates, ['add', 'unlink'])
|
||||
|
||||
// Shared Watcher
|
||||
const watchHookDebounced = debounce((event, file) => builder.nuxt.callHook('builder:watch', event, file), 100)
|
||||
const watchHook: WatchCallback = (event, path) => builder.nuxt.callHook('builder:watch', event, path)
|
||||
const watchHookDebounced = debounce(watchHook, 100)
|
||||
appWatcher.watchAll(watchHookDebounced)
|
||||
nuxtAppWatcher.watchAll(watchHookDebounced)
|
||||
}
|
||||
|
@ -9,18 +9,18 @@ export interface NuxtPlugin {
|
||||
}
|
||||
|
||||
const MODES_REGEX = /\.(server|client)(\.\w+)*$/
|
||||
const getPluginMode = (src) => {
|
||||
const getPluginMode = (src: string) => {
|
||||
const [, mode = 'all'] = src.match(MODES_REGEX) || []
|
||||
|
||||
return mode
|
||||
return mode as NuxtPlugin['mode']
|
||||
}
|
||||
|
||||
export function resolvePlugins (builder: Builder, app: NuxtApp) {
|
||||
return resolveFiles(builder, 'plugins/**/*.{js,ts}', app.dir)
|
||||
.then(plugins => plugins.map((src) => {
|
||||
return {
|
||||
src,
|
||||
mode: getPluginMode(src)
|
||||
}
|
||||
}))
|
||||
export async function resolvePlugins (builder: Builder, app: NuxtApp) {
|
||||
const plugins = await resolveFiles(builder, 'plugins/**/*.{js,ts}', app.dir)
|
||||
|
||||
return plugins.map(src => ({
|
||||
src,
|
||||
mode: getPluginMode(src)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
import { join, relative, dirname } from 'path'
|
||||
import fsExtra from 'fs-extra'
|
||||
import globby from 'globby'
|
||||
import lodashTemplate from 'lodash/template'
|
||||
import { join, relative, dirname } from 'upath'
|
||||
|
||||
import * as nxt from './utils/nxt'
|
||||
import type { Builder } from './builder'
|
||||
|
||||
export interface NuxtTemplate {
|
||||
src: string // Absolute path to source file
|
||||
@ -10,7 +12,7 @@ export interface NuxtTemplate {
|
||||
data?: any
|
||||
}
|
||||
|
||||
export function templateData (builder) {
|
||||
export function templateData (builder: Builder) {
|
||||
return {
|
||||
globals: builder.globals,
|
||||
app: builder.app,
|
||||
@ -21,7 +23,7 @@ export function templateData (builder) {
|
||||
|
||||
async function compileTemplate (tmpl: NuxtTemplate, destDir: string) {
|
||||
const srcContents = await fsExtra.readFile(tmpl.src, 'utf-8')
|
||||
let compiledSrc
|
||||
let compiledSrc: string
|
||||
try {
|
||||
compiledSrc = lodashTemplate(srcContents, {})(tmpl.data)
|
||||
} catch (err) {
|
||||
@ -38,7 +40,7 @@ export function compileTemplates (templates: NuxtTemplate[], destDir: string) {
|
||||
return Promise.all(templates.map(t => compileTemplate(t, destDir)))
|
||||
}
|
||||
|
||||
export async function scanTemplates (dir: string, data?: Object) {
|
||||
export async function scanTemplates (dir: string, data?: Record<string, any>) {
|
||||
const templateFiles = (await globby(join(dir, '/**')))
|
||||
|
||||
return templateFiles.map(src => ({
|
||||
@ -48,7 +50,7 @@ export async function scanTemplates (dir: string, data?: Object) {
|
||||
}))
|
||||
}
|
||||
|
||||
export function watchTemplate (template: NuxtTemplate, _watcher: any, _cb: Function) {
|
||||
export function watchTemplate (template: NuxtTemplate, _watcher: any, _cb: () => any) {
|
||||
template.data = new Proxy(template.data, {
|
||||
// TODO: deep watch option changes
|
||||
})
|
||||
|
@ -42,27 +42,27 @@ export default class Ignore {
|
||||
}
|
||||
|
||||
readIgnoreFile () {
|
||||
if (this.findIgnoreFile()) {
|
||||
if (this.findIgnoreFile() && this.ignoreFile) {
|
||||
return fs.readFileSync(this.ignoreFile, 'utf8')
|
||||
}
|
||||
}
|
||||
|
||||
addIgnoresRules () {
|
||||
const content = this.readIgnoreFile()
|
||||
if (!this.ignore) {
|
||||
this.ignore = ignore(this.ignoreOptions)
|
||||
}
|
||||
if (content) {
|
||||
this.ignore.add(content)
|
||||
}
|
||||
if (this.ignoreArray && this.ignoreArray.length > 0) {
|
||||
if (!this.ignore) {
|
||||
this.ignore = ignore(this.ignoreOptions)
|
||||
}
|
||||
this.ignore.add(this.ignoreArray)
|
||||
}
|
||||
}
|
||||
|
||||
filter (paths: string[]) {
|
||||
filter (paths: string[] = []) {
|
||||
if (this.ignore) {
|
||||
return this.ignore.filter([].concat(paths || []))
|
||||
return this.ignore.filter(([] as string[]).concat(paths))
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
@ -20,7 +20,18 @@ export const importSources = (sources: string | string[], { lazy = false } = {})
|
||||
}).join('\n')
|
||||
}
|
||||
|
||||
export const serializeRoute = (route: NuxtRoute) => {
|
||||
interface SerializedRoute {
|
||||
name?: string
|
||||
path: string
|
||||
children: SerializedRoute[]
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
__file: string
|
||||
component: string
|
||||
}
|
||||
|
||||
export const serializeRoute = (route: NuxtRoute): SerializedRoute => {
|
||||
return {
|
||||
name: route.name,
|
||||
path: route.path,
|
||||
|
@ -1,37 +1,42 @@
|
||||
import chokidar, { WatchOptions } from 'chokidar'
|
||||
import defu from 'defu'
|
||||
import consola from 'consola'
|
||||
|
||||
import Ignore from './utils/ignore'
|
||||
|
||||
export type WatchEvent = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'
|
||||
|
||||
export type WatchCallback = (event: WatchEvent, path: string) => void
|
||||
export type WatchFilter = (event: WatchEvent, path: string) => boolean | null
|
||||
|
||||
export function createWatcher (
|
||||
pattern: string,
|
||||
options?: WatchOptions,
|
||||
ignore?: Ignore
|
||||
) {
|
||||
const opts = defu(options, {
|
||||
const opts = defu(options!, {
|
||||
ignored: [],
|
||||
ignoreInitial: true
|
||||
})
|
||||
const watcher = chokidar.watch(pattern, opts)
|
||||
const watchAll = (cb: Function, filter?: Function) => {
|
||||
const watchAll = (cb: WatchCallback, filter?: WatchFilter) => {
|
||||
watcher.on('all', (event, path: string) => {
|
||||
if (ignore && ignore.ignores(path)) {
|
||||
return
|
||||
}
|
||||
const _event = { event, path }
|
||||
if (!filter || filter(_event)) {
|
||||
cb(_event)
|
||||
if (!filter || filter(event, path)) {
|
||||
cb(event, path)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const watch = (pattern: string | RegExp, cb: Function, events?: string[]) =>
|
||||
watchAll(cb, ({ event, path }) => path.match(pattern) && (!events || events.includes(event)))
|
||||
const watch = (pattern: string | RegExp, cb: WatchCallback, events?: WatchEvent[]) =>
|
||||
watchAll(cb, (event, path) => path.match(pattern) && (!events || events.includes(event)))
|
||||
|
||||
const debug = (tag: string = '[Watcher]') => {
|
||||
consola.log(tag, 'Watching ', pattern)
|
||||
watchAll((e) => {
|
||||
consola.log(tag, e.event, e.path)
|
||||
watchAll((event, path) => {
|
||||
consola.log(tag, event, path)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,8 @@ export async function build (rootDir: string, stub: boolean) {
|
||||
if (buildOptions.entries) {
|
||||
if (!Array.isArray(buildOptions.entries)) {
|
||||
buildOptions.entries = Object.entries(buildOptions.entries)
|
||||
} ctx.entries.push(...buildOptions.entries.map(entry => resolveEntry(entry)))
|
||||
}
|
||||
ctx.entries.push(...buildOptions.entries.map(entry => resolveEntry(entry)))
|
||||
}
|
||||
if (pkg.dependencies) {
|
||||
ctx.externals.push(...Object.keys(pkg.dependencies))
|
||||
@ -84,7 +85,7 @@ export async function build (rootDir: string, stub: boolean) {
|
||||
return
|
||||
}
|
||||
|
||||
consola.info(chalk.cyan(`Builduing ${pkg.name}`))
|
||||
consola.info(chalk.cyan(`Building ${pkg.name}`))
|
||||
if (process.env.DEBUG) {
|
||||
consola.info(`
|
||||
${chalk.bold('Root dir:')} ${ctx.rootDir}
|
||||
@ -98,7 +99,7 @@ export async function build (rootDir: string, stub: boolean) {
|
||||
const usedImports = new Set<string>()
|
||||
if (rollupOptions) {
|
||||
const buildResult = await rollup(rollupOptions)
|
||||
const outputOptions = rollupOptions.output as OutputOptions
|
||||
const outputOptions = rollupOptions.output
|
||||
const { output } = await buildResult.write(outputOptions)
|
||||
|
||||
for (const entry of output.filter(e => e.type === 'chunk') as OutputChunk[]) {
|
||||
@ -115,6 +116,7 @@ export async function build (rootDir: string, stub: boolean) {
|
||||
}
|
||||
|
||||
// Types
|
||||
rollupOptions.plugins = rollupOptions.plugins || []
|
||||
rollupOptions.plugins.push(dts())
|
||||
const typesBuild = await rollup(rollupOptions)
|
||||
await typesBuild.write(outputOptions)
|
||||
@ -179,32 +181,33 @@ function resolveEntry (input: string | [string, Partial<BuildEntry>] | Partial<B
|
||||
let entry: Partial<BuildEntry>
|
||||
if (typeof input === 'string') {
|
||||
entry = { name: input }
|
||||
}
|
||||
if (Array.isArray(input)) {
|
||||
} else if (Array.isArray(input)) {
|
||||
entry = { name: input[0], ...input[1] }
|
||||
} else {
|
||||
entry = input
|
||||
}
|
||||
entry.input = entry.input ?? resolve(entry.srcDir || 'src', './' + entry.name)
|
||||
entry.output = entry.output ?? resolve(entry.distDir || 'dist', './' + entry.name)
|
||||
entry.bundle = entry.bundle ?? !(entry.input.endsWith('/') || entry.name.endsWith('/'))
|
||||
entry.bundle = entry.bundle ?? !(entry.input.endsWith('/') || entry.name?.endsWith('/'))
|
||||
entry.format = entry.format ?? 'esm'
|
||||
return entry as BuildEntry
|
||||
}
|
||||
|
||||
function dumpObject (obj) {
|
||||
function dumpObject (obj: Record<string, any>) {
|
||||
return '{ ' + Object.keys(obj).map(key => `${key}: ${JSON.stringify(obj[key])}`).join(', ') + ' }'
|
||||
}
|
||||
|
||||
function getRollupOptions (ctx: BuildContext): RollupOptions | null {
|
||||
function getRollupOptions (ctx: BuildContext): RollupOptions & { output: OutputOptions & { dir: string }} | null {
|
||||
const extensions = ['.ts', '.mjs', '.js', '.json']
|
||||
|
||||
const r = (...path) => resolve(ctx.rootDir, ...path)
|
||||
const r = (...path: string[]) => resolve(ctx.rootDir, ...path)
|
||||
|
||||
const entries = ctx.entries.filter(e => e.bundle)
|
||||
if (!entries.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
return <RollupOptions>{
|
||||
return {
|
||||
input: entries.map(e => e.input),
|
||||
|
||||
output: {
|
||||
@ -227,7 +230,7 @@ function getRollupOptions (ctx: BuildContext): RollupOptions | null {
|
||||
},
|
||||
|
||||
onwarn (warning, rollupWarn) {
|
||||
if (!['CIRCULAR_DEPENDENCY'].includes(warning.code)) {
|
||||
if (!warning.code || !['CIRCULAR_DEPENDENCY'].includes(warning.code)) {
|
||||
rollupWarn(warning)
|
||||
}
|
||||
},
|
||||
|
46
yarn.lock
46
yarn.lock
@ -2333,6 +2333,16 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/glob@npm:*":
|
||||
version: 7.1.3
|
||||
resolution: "@types/glob@npm:7.1.3"
|
||||
dependencies:
|
||||
"@types/minimatch": "*"
|
||||
"@types/node": "*"
|
||||
checksum: 633bf1dda9a30899b233ed6b97c75cdd59f2ee856a12240c85474ce6889e26b3b3520b62de56f6bb61824af0ef51b311a0cae305f27ba0de8ddc4898a3673d42
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/graceful-fs@npm:^4.1.2":
|
||||
version: 4.1.5
|
||||
resolution: "@types/graceful-fs@npm:4.1.5"
|
||||
@ -2342,6 +2352,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/hash-sum@npm:^1.0.0":
|
||||
version: 1.0.0
|
||||
resolution: "@types/hash-sum@npm:1.0.0"
|
||||
checksum: 258949e981df134cfc20a1ada051bc94fdc139249a30141459eace36cd336e0500d83e33bb418d9b3f852e8ea33b6b86512828358f5e21a376189b500cde8115
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/http-proxy@npm:^1.17.5":
|
||||
version: 1.17.5
|
||||
resolution: "@types/http-proxy@npm:1.17.5"
|
||||
@ -2400,6 +2417,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/lodash@npm:^4.14.168":
|
||||
version: 4.14.168
|
||||
resolution: "@types/lodash@npm:4.14.168"
|
||||
checksum: 9a4e25f89fc035b9f0388f1f7be85e5eff49f9e6db0d93432c9a89fce0916f8a89db4e8290415f7ea02de6b00d3573826378dcb655b7b2d20530a6e8d6dd6fd0
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/mime@npm:^1":
|
||||
version: 1.3.2
|
||||
resolution: "@types/mime@npm:1.3.2"
|
||||
@ -2414,6 +2438,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/minimatch@npm:*":
|
||||
version: 3.0.4
|
||||
resolution: "@types/minimatch@npm:3.0.4"
|
||||
checksum: abbe7031d8a6144c36f1803c5c1914885c2349d5d73fc45aae44807c12c4c803b8acfb134c71c7eff75c462c218697f982b96633f8fdf71b83ec50eba36122a6
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/minimatch@npm:^3.0.3":
|
||||
version: 3.0.3
|
||||
resolution: "@types/minimatch@npm:3.0.3"
|
||||
@ -2489,6 +2520,16 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/rimraf@npm:^3.0.0":
|
||||
version: 3.0.0
|
||||
resolution: "@types/rimraf@npm:3.0.0"
|
||||
dependencies:
|
||||
"@types/glob": "*"
|
||||
"@types/node": "*"
|
||||
checksum: 00a00a430f838ddafdbc17386f08d928b1ae7aeada84d112f4369a7a89b4d47c1bb6849688f1d6ee404a08338ab52a80c742f0c3040d63ccc2aaec04bd1009f5
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@types/serve-static@npm:^1.13.9":
|
||||
version: 1.13.9
|
||||
resolution: "@types/serve-static@npm:1.13.9"
|
||||
@ -10410,6 +10451,7 @@ __metadata:
|
||||
"@rollup/plugin-node-resolve": ^11.2.1
|
||||
"@types/jest": ^26.0.22
|
||||
"@types/node": ^14.14.37
|
||||
"@types/rimraf": ^3.0.0
|
||||
chalk: ^4.1.0
|
||||
defu: ^3.2.2
|
||||
esbuild: ^0.10.0
|
||||
@ -10439,6 +10481,9 @@ __metadata:
|
||||
"@nuxt/nitro": ^0.2.2
|
||||
"@nuxt/vite-builder": ^0.1.1
|
||||
"@nuxt/webpack-builder": ^0.1.1
|
||||
"@types/fs-extra": ^9.0.10
|
||||
"@types/hash-sum": ^1.0.0
|
||||
"@types/lodash": ^4.14.168
|
||||
chokidar: ^3.5.1
|
||||
consola: ^2.15.3
|
||||
defu: ^3.2.2
|
||||
@ -10451,6 +10496,7 @@ __metadata:
|
||||
nuxt-cli: ^0.1.1
|
||||
scule: ^0.1.1
|
||||
ufo: ^0.6.10
|
||||
upath: ^2.0.1
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user