mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 07:05:11 +00:00
chore: update build scripts (#69)
This commit is contained in:
parent
030327b75f
commit
c351574043
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@ -37,6 +37,10 @@ jobs:
|
||||
if: steps.cache.outputs.cache-hit != 'true'
|
||||
run: yarn # --immutable
|
||||
|
||||
|
||||
- name: Stub
|
||||
run: yarn stub
|
||||
|
||||
- name: Lint
|
||||
run: yarn lint
|
||||
|
||||
|
28
package.json
28
package.json
@ -5,42 +5,28 @@
|
||||
"packages/*"
|
||||
],
|
||||
"scripts": {
|
||||
"link": "lerna link",
|
||||
"build": "jiti ./scripts/build-all",
|
||||
"stub": "yarn build --stub",
|
||||
"gentypes": "yarn workspace @nuxt/kit jiti ./scripts/gentypes",
|
||||
"release": "yarn build && lerna publish -m \"chore: release\" && yarn stub",
|
||||
"build": "FORCE_COLOR=1 lerna run prepack --stream --no-prefix",
|
||||
"stub": "lerna run prepack -- --stub",
|
||||
"release": "yarn && yarn build && FORCE_COLOR=1 lerna publish -m \"chore: release\" && yarn stub",
|
||||
"nu": "./node_modules/.bin/nu",
|
||||
"play": "yarn run nu dev playground",
|
||||
"lint": "yarn gentypes && eslint --ext .vue,.ts,.js .",
|
||||
"test": "yarn lint",
|
||||
"lint": "eslint --ext .vue,.ts,.js .",
|
||||
"postinstall": "yarn stub",
|
||||
"version": "yarn && git add yarn.lock"
|
||||
},
|
||||
"resolutions": {
|
||||
"esbuild": "0.10.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nuxtjs/eslint-config": "^6.0.0",
|
||||
"@nuxtjs/eslint-config-typescript": "^6.0.0",
|
||||
"@rollup/plugin-alias": "^3.1.2",
|
||||
"@rollup/plugin-commonjs": "^18.0.0",
|
||||
"@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",
|
||||
"eslint": "^7.23.0",
|
||||
"execa": "^5.0.0",
|
||||
"jest": "^26.6.3",
|
||||
"jiti": "^1.6.4",
|
||||
"lerna": "^4.0.0",
|
||||
"mkdist": "^0.1.3",
|
||||
"pretty-bytes": "^5.6.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"rollup": "^2.44.0",
|
||||
"rollup-plugin-dts": "^3.0.1",
|
||||
"rollup-plugin-esbuild": "^3.0.2",
|
||||
"standard-version": "^9.1.1",
|
||||
"ts-jest": "^26.5.4",
|
||||
"typescript": "^4.2.3"
|
||||
}
|
||||
|
14
packages/app/build.config.ts
Normal file
14
packages/app/build.config.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { BuildConfig } from 'unbuild'
|
||||
|
||||
export default <BuildConfig>{
|
||||
declaration: false,
|
||||
entries: [
|
||||
{ input: 'src/', name: 'app' }
|
||||
],
|
||||
dependencies: [
|
||||
'@vueuse/head',
|
||||
'ohmyfetch',
|
||||
'vue-router',
|
||||
'vuex5'
|
||||
]
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
const { resolve } = require('path')
|
||||
|
||||
module.exports = {
|
||||
appDir: resolve(__dirname, 'dist')
|
||||
appDir: resolve(__dirname, 'dist/app')
|
||||
}
|
||||
|
@ -3,12 +3,19 @@
|
||||
"version": "0.2.1",
|
||||
"repository": "nuxt/framework",
|
||||
"license": "MIT",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.js",
|
||||
"main": "./dist/app/index.js",
|
||||
"module": "./dist/app/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist",
|
||||
"meta.js"
|
||||
],
|
||||
"scripts": {
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"devDependencies": {
|
||||
"unbuild": "^0.1.11"
|
||||
},
|
||||
"dependencies": {
|
||||
"@vueuse/head": "^0.5.1",
|
||||
"hookable": "^4.4.1",
|
||||
@ -16,19 +23,5 @@
|
||||
"vue": "^3.0.11",
|
||||
"vue-router": "^4.0.6",
|
||||
"vuex5": "^0.5.0-testing.3"
|
||||
},
|
||||
"build": {
|
||||
"entries": {
|
||||
"index": {
|
||||
"distDir": ".gen"
|
||||
},
|
||||
"/": {}
|
||||
},
|
||||
"dependencies": [
|
||||
"@vueuse/head",
|
||||
"ohmyfetch",
|
||||
"vue-router",
|
||||
"vuex5"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
export { useNuxt } from './nuxt/composables'
|
||||
|
||||
export * from './nuxt'
|
||||
export * from './shim'
|
||||
export * from './composables'
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { App, getCurrentInstance } from 'vue'
|
||||
import Hookable from 'hookable'
|
||||
import type { App } from 'vue'
|
||||
import { defineGetter } from '../utils'
|
||||
import { callWithNuxt } from './composables'
|
||||
import { defineGetter } from './utils'
|
||||
|
||||
export interface Nuxt {
|
||||
app: App
|
||||
@ -93,3 +92,37 @@ export async function applyPlugins (nuxt: Nuxt, plugins: Plugin[]) {
|
||||
await applyPlugin(nuxt, plugin)
|
||||
}
|
||||
}
|
||||
|
||||
let currentNuxtInstance: Nuxt | null
|
||||
|
||||
export const setNuxtInstance = (nuxt: Nuxt | null) => {
|
||||
currentNuxtInstance = nuxt
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the setup function passed in has access to the Nuxt instance via `useNuxt`.
|
||||
* @param nuxt A Nuxt instance
|
||||
* @param setup The function to call
|
||||
*/
|
||||
export async function callWithNuxt (nuxt: Nuxt, setup: () => any) {
|
||||
setNuxtInstance(nuxt)
|
||||
const p = setup()
|
||||
setNuxtInstance(null)
|
||||
await p
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current Nuxt instance.
|
||||
*/
|
||||
export function useNuxt (): Nuxt {
|
||||
const vm = getCurrentInstance()
|
||||
|
||||
if (!vm) {
|
||||
if (!currentNuxtInstance) {
|
||||
throw new Error('nuxt instance unavailable')
|
||||
}
|
||||
return currentNuxtInstance
|
||||
}
|
||||
|
||||
return vm.appContext.app.$nuxt
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
import { getCurrentInstance } from 'vue'
|
||||
import type { Nuxt } from '../nuxt'
|
||||
|
||||
let currentNuxtInstance: Nuxt | null
|
||||
|
||||
export const setNuxtInstance = (nuxt: Nuxt | null) => {
|
||||
currentNuxtInstance = nuxt
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the setup function passed in has access to the Nuxt instance via `useNuxt`.
|
||||
* @param nuxt A Nuxt instance
|
||||
* @param setup The function to call
|
||||
*/
|
||||
export async function callWithNuxt (nuxt: Nuxt, setup: () => any) {
|
||||
setNuxtInstance(nuxt)
|
||||
const p = setup()
|
||||
setNuxtInstance(null)
|
||||
await p
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current Nuxt instance.
|
||||
*/
|
||||
export function useNuxt (): Nuxt {
|
||||
const vm = getCurrentInstance()
|
||||
|
||||
if (!vm) {
|
||||
if (!currentNuxtInstance) {
|
||||
throw new Error('nuxt instance unavailable')
|
||||
}
|
||||
return currentNuxtInstance
|
||||
}
|
||||
|
||||
return vm.appContext.app.$nuxt
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import type { Plugin } from '@nuxt/app'
|
||||
import { createHead, renderHeadToString } from '@vueuse/head'
|
||||
import type { Plugin } from '@nuxt/app'
|
||||
import { Head, Html, Body, Title, Meta, Link, Script, Style } from './head'
|
||||
|
||||
export default <Plugin> function head (nuxt) {
|
||||
|
@ -6,7 +6,9 @@ import {
|
||||
RouterLink
|
||||
} from 'vue-router'
|
||||
import type { Plugin } from '@nuxt/app'
|
||||
// @ts-ignore
|
||||
import routes from 'nuxt/build/routes'
|
||||
// @ts-ignore
|
||||
import NuxtPage from './NuxtPage.vue'
|
||||
|
||||
export default <Plugin> function router (nuxt) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { createVuex, defineStore, useStore } from 'vuex5/dist/vuex.esm'
|
||||
import type { Plugin } from '@nuxt/app'
|
||||
import { useHydration } from 'nuxt/app/composables'
|
||||
import { useHydration } from '../composables'
|
||||
|
||||
export default <Plugin> function ({ app }) {
|
||||
const vuex = createVuex({ })
|
||||
|
11
packages/cli/build.config.ts
Normal file
11
packages/cli/build.config.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import type { BuildConfig } from 'unbuild'
|
||||
|
||||
export default <BuildConfig>{
|
||||
declaration: false,
|
||||
entries: [
|
||||
'src/index'
|
||||
],
|
||||
externals: [
|
||||
'nuxt3'
|
||||
]
|
||||
}
|
@ -9,18 +9,14 @@
|
||||
"nuxt": "./bin/nuxt.js",
|
||||
"nuxt-cli": "./bin/nuxt.js"
|
||||
},
|
||||
"scripts": {
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"devDependencies": {
|
||||
"unbuild": "^0.1.11"
|
||||
},
|
||||
"files": [
|
||||
"bin",
|
||||
"dist"
|
||||
],
|
||||
"build": {
|
||||
"externals": [
|
||||
"nuxt3"
|
||||
],
|
||||
"entries": {
|
||||
"index": {
|
||||
"format": "cjs"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -13,7 +13,8 @@ async function _main () {
|
||||
const isDev = cmd === 'dev'
|
||||
const rootDir = resolve(process.cwd(), args[1] || '.')
|
||||
|
||||
const { loadNuxt, build } = await import('nuxt3')
|
||||
const pkg = 'nuxt3'
|
||||
const { loadNuxt, build } = require(pkg)
|
||||
|
||||
const nuxt = await loadNuxt({
|
||||
for: isDev ? 'dev' : 'build',
|
||||
|
16
packages/kit/build.config.ts
Normal file
16
packages/kit/build.config.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import type { BuildConfig } from 'unbuild'
|
||||
|
||||
export default <BuildConfig>{
|
||||
declaration: true,
|
||||
entries: [
|
||||
{
|
||||
input: 'src/config/schema/index',
|
||||
name: 'config',
|
||||
builder: 'untyped',
|
||||
defaults: {
|
||||
rootDir: '<rootDir>'
|
||||
}
|
||||
},
|
||||
'src/index'
|
||||
]
|
||||
}
|
@ -8,6 +8,12 @@
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"devDependencies": {
|
||||
"unbuild": "^0.1.11"
|
||||
},
|
||||
"dependencies": {
|
||||
"consola": "^2.15.3",
|
||||
"create-require": "^1.1.1",
|
||||
@ -22,13 +28,5 @@
|
||||
"unctx": "^0.0.3",
|
||||
"untyped": "^0.2.4",
|
||||
"upath": "^2.0.1"
|
||||
},
|
||||
"build": {
|
||||
"prebuild": "jiti ./scripts/gentypes",
|
||||
"entries": {
|
||||
"index": {
|
||||
"format": "cjs"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +0,0 @@
|
||||
import { resolve } from 'path'
|
||||
import { mkdir, writeFile } from 'fs/promises'
|
||||
import { resolveSchema, generateTypes, generateMarkdown } from 'untyped'
|
||||
|
||||
async function main () {
|
||||
const genDir = resolve(__dirname, '../.gen')
|
||||
const srcConfig = await import('../src/config/schema').then(r => r.default)
|
||||
|
||||
const defaults = { rootDir: '/<dir>/' }
|
||||
const schema = resolveSchema(srcConfig, defaults)
|
||||
|
||||
await mkdir(genDir).catch(() => { })
|
||||
await writeFile(resolve(genDir, 'config.md'), generateMarkdown(schema))
|
||||
await writeFile(resolve(genDir, 'config.schema.json'), JSON.stringify(schema, null, 2))
|
||||
await writeFile(resolve(genDir, 'config.defaults.json'), JSON.stringify(defaults, null, 2))
|
||||
await writeFile(resolve(genDir, 'config.d.ts'), 'export ' + generateTypes(schema, 'ConfigSchema'))
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
@ -1,4 +1,4 @@
|
||||
import { ConfigSchema } from '../../.gen/config'
|
||||
import { ConfigSchema } from '../../.gen/config/config'
|
||||
import { ModuleInstallOptions } from './module'
|
||||
import { NuxtHooks } from './hooks'
|
||||
|
||||
|
23
packages/nitro/build.config.ts
Normal file
23
packages/nitro/build.config.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import type { BuildConfig } from 'unbuild'
|
||||
|
||||
export default <BuildConfig>{
|
||||
declaration: true,
|
||||
entries: [
|
||||
'src/index',
|
||||
'src/compat',
|
||||
{ input: 'src/runtime/', format: 'esm', declaration: false }
|
||||
],
|
||||
dependencies: [
|
||||
'@cloudflare/kv-asset-handler',
|
||||
'@netlify/functions',
|
||||
'@nuxt/devalue',
|
||||
'connect',
|
||||
'destr',
|
||||
'ohmyfetch',
|
||||
'ora',
|
||||
'vue-bundle-renderer',
|
||||
'vue-server-renderer',
|
||||
'@vue/server-renderer',
|
||||
'vue'
|
||||
]
|
||||
}
|
@ -7,28 +7,8 @@
|
||||
"dist",
|
||||
"compat.js"
|
||||
],
|
||||
"build": {
|
||||
"entries": {
|
||||
"index": {
|
||||
"format": "cjs"
|
||||
},
|
||||
"compat": {
|
||||
"format": "cjs"
|
||||
},
|
||||
"runtime/": {}
|
||||
},
|
||||
"dependencies": [
|
||||
"@cloudflare/kv-asset-handler",
|
||||
"@nuxt/devalue",
|
||||
"connect",
|
||||
"destr",
|
||||
"ohmyfetch",
|
||||
"ora",
|
||||
"vue-bundle-renderer",
|
||||
"vue-server-renderer",
|
||||
"@vue/server-renderer",
|
||||
"vue"
|
||||
]
|
||||
"scripts": {
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"dependencies": {
|
||||
"@cloudflare/kv-asset-handler": "^0.1.1",
|
||||
@ -92,6 +72,7 @@
|
||||
"@types/globby": "^9.1.0",
|
||||
"@types/http-proxy": "^1.17.5",
|
||||
"@types/node-fetch": "^2.5.9",
|
||||
"@types/serve-static": "^1.13.9"
|
||||
"@types/serve-static": "^1.13.9",
|
||||
"unbuild": "^0.1.11"
|
||||
}
|
||||
}
|
||||
|
14
packages/nuxt3/build.config.ts
Normal file
14
packages/nuxt3/build.config.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import type { BuildConfig } from 'unbuild'
|
||||
|
||||
export default <BuildConfig>{
|
||||
declaration: false,
|
||||
entries: [
|
||||
'src/index'
|
||||
],
|
||||
dependencies: [
|
||||
'@nuxt/app',
|
||||
'@nuxt/vite-builder',
|
||||
'@nuxt/webpack-builder',
|
||||
'nuxt-cli'
|
||||
]
|
||||
}
|
@ -7,18 +7,8 @@
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"build": {
|
||||
"entries": {
|
||||
"index": {
|
||||
"format": "cjs"
|
||||
}
|
||||
},
|
||||
"dependencies": [
|
||||
"@nuxt/app",
|
||||
"@nuxt/vite-builder",
|
||||
"@nuxt/webpack-builder",
|
||||
"nuxt-cli"
|
||||
]
|
||||
"scripts": {
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nuxt/app": "^0.2.1",
|
||||
@ -43,6 +33,7 @@
|
||||
"devDependencies": {
|
||||
"@types/fs-extra": "^9.0.10",
|
||||
"@types/hash-sum": "^1.0.0",
|
||||
"@types/lodash": "^4.14.168"
|
||||
"@types/lodash": "^4.14.168",
|
||||
"unbuild": "^0.1.11"
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Hookable from 'hookable'
|
||||
import { loadNuxtConfig, LoadNuxtConfigOptions, Nuxt, NuxtOptions, installModule } from '@nuxt/kit'
|
||||
import { loadNuxtConfig, LoadNuxtConfigOptions, Nuxt, NuxtOptions, installModule, NuxtConfig } from '@nuxt/kit'
|
||||
import { initNitro } from './nitro'
|
||||
|
||||
export function createNuxt (options: NuxtOptions): Nuxt {
|
||||
@ -39,6 +39,8 @@ async function initNuxt (nuxt: Nuxt) {
|
||||
|
||||
export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
|
||||
for?: 'dev' | 'build'
|
||||
rootDir?: string
|
||||
config?: NuxtConfig
|
||||
}
|
||||
|
||||
export async function loadNuxt (loadOpts: LoadNuxtOptions = {}): Promise<Nuxt> {
|
||||
|
13
packages/vite/build.config.ts
Normal file
13
packages/vite/build.config.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import type { BuildConfig } from 'unbuild'
|
||||
|
||||
export default <BuildConfig>{
|
||||
declaration: false,
|
||||
entries: [
|
||||
'src/index'
|
||||
],
|
||||
dependencies: [
|
||||
'@nuxt/kit',
|
||||
'@vue/compiler-sfc',
|
||||
'vue'
|
||||
]
|
||||
}
|
@ -7,17 +7,11 @@
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"build": {
|
||||
"entries": {
|
||||
"index": {
|
||||
"format": "cjs"
|
||||
}
|
||||
"scripts": {
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"dependencies": [
|
||||
"@nuxt/kit",
|
||||
"@vue/compiler-sfc",
|
||||
"vue"
|
||||
]
|
||||
"devDependencies": {
|
||||
"unbuild": "^0.1.11"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nuxt/kit": "^0.2.0",
|
||||
|
25
packages/webpack/build.config.ts
Normal file
25
packages/webpack/build.config.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import type { BuildConfig } from 'unbuild'
|
||||
|
||||
export default <BuildConfig>{
|
||||
declaration: false,
|
||||
entries: [
|
||||
'src/index'
|
||||
],
|
||||
dependencies: [
|
||||
'@nuxt/kit',
|
||||
'@vue/compiler-sfc',
|
||||
'@vue/babel-preset-jsx',
|
||||
'postcss',
|
||||
'postcss-import-resolver',
|
||||
'postcss-loader',
|
||||
'babel-loader',
|
||||
'vue-loader',
|
||||
'css-loader',
|
||||
'file-loader',
|
||||
'style-resources-loader',
|
||||
'url-loader',
|
||||
'vue-style-loader',
|
||||
'@babel/core',
|
||||
'vue'
|
||||
]
|
||||
}
|
@ -7,29 +7,8 @@
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"build": {
|
||||
"entries": {
|
||||
"index": {
|
||||
"format": "cjs"
|
||||
}
|
||||
},
|
||||
"dependencies": [
|
||||
"@nuxt/kit",
|
||||
"@vue/compiler-sfc",
|
||||
"@vue/babel-preset-jsx",
|
||||
"postcss",
|
||||
"postcss-import-resolver",
|
||||
"postcss-loader",
|
||||
"babel-loader",
|
||||
"vue-loader",
|
||||
"css-loader",
|
||||
"file-loader",
|
||||
"style-resources-loader",
|
||||
"url-loader",
|
||||
"vue-style-loader",
|
||||
"@babel/core",
|
||||
"vue"
|
||||
]
|
||||
"scripts": {
|
||||
"prepack": "unbuild"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.13.14",
|
||||
@ -70,6 +49,7 @@
|
||||
"@types/terser-webpack-plugin": "^5.0.3",
|
||||
"@types/webpack-bundle-analyzer": "^3.9.2",
|
||||
"@types/webpack-dev-middleware": "^4.1.2",
|
||||
"@types/webpack-hot-middleware": "^2.25.4"
|
||||
"@types/webpack-hot-middleware": "^2.25.4",
|
||||
"unbuild": "^0.1.11"
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { defineNuxtComponent, asyncData } from 'nuxt/app/composables'
|
||||
import { defineNuxtComponent, asyncData } from '@nuxt/app'
|
||||
const waitFor = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms))
|
||||
|
||||
export default defineNuxtComponent({
|
||||
|
@ -11,7 +11,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { useAsyncData } from 'nuxt/app/composables'
|
||||
import { useAsyncData } from '@nuxt/app'
|
||||
const waitFor = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms))
|
||||
|
||||
export default {
|
||||
|
@ -1,18 +0,0 @@
|
||||
import { readdir } from 'fs/promises'
|
||||
import { resolve } from 'upath'
|
||||
import { build } from './build'
|
||||
|
||||
async function main () {
|
||||
const pkgsDir = resolve(__dirname, '../packages')
|
||||
const pkgs = await readdir(pkgsDir)
|
||||
|
||||
const stub = process.argv.includes('--stub')
|
||||
|
||||
for (const pkg of pkgs) {
|
||||
const rootDir = resolve(pkgsDir, pkg)
|
||||
process.chdir(rootDir)
|
||||
await build(rootDir, stub)
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
261
scripts/build.ts
261
scripts/build.ts
@ -1,261 +0,0 @@
|
||||
import { promisify } from 'util'
|
||||
import { resolve, relative, dirname } from 'path'
|
||||
import Module from 'module'
|
||||
import { writeFile, mkdir, unlink, symlink } from 'fs/promises'
|
||||
import chalk from 'chalk'
|
||||
import consola from 'consola'
|
||||
import rimraf from 'rimraf'
|
||||
import { RollupOptions, OutputOptions, OutputChunk, rollup } from 'rollup'
|
||||
import commonjs from '@rollup/plugin-commonjs'
|
||||
import nodeResolve from '@rollup/plugin-node-resolve'
|
||||
import alias from '@rollup/plugin-alias'
|
||||
import esbuild from 'rollup-plugin-esbuild'
|
||||
import { mkdist } from 'mkdist'
|
||||
import prettyBytes from 'pretty-bytes'
|
||||
import execa from 'execa'
|
||||
import dts from 'rollup-plugin-dts'
|
||||
|
||||
interface BuildEntry {
|
||||
name: string
|
||||
input: string
|
||||
output: string
|
||||
bundle: boolean
|
||||
srcDir: string
|
||||
distDir: string
|
||||
format: 'esm' | 'cjs'
|
||||
}
|
||||
|
||||
interface BuildContext {
|
||||
rootDir: string
|
||||
entries: BuildEntry[]
|
||||
externals: string[]
|
||||
}
|
||||
|
||||
export async function build (rootDir: string, stub: boolean) {
|
||||
const ctx: BuildContext = {
|
||||
rootDir,
|
||||
entries: [],
|
||||
externals: [...Module.builtinModules]
|
||||
}
|
||||
|
||||
const pkg = require(resolve(ctx.rootDir, 'package.json'))
|
||||
|
||||
const buildOptions = pkg.build || {}
|
||||
if (buildOptions.entries) {
|
||||
if (!Array.isArray(buildOptions.entries)) {
|
||||
buildOptions.entries = Object.entries(buildOptions.entries)
|
||||
}
|
||||
ctx.entries.push(...buildOptions.entries.map(entry => resolveEntry(entry)))
|
||||
}
|
||||
if (pkg.dependencies) {
|
||||
ctx.externals.push(...Object.keys(pkg.dependencies))
|
||||
}
|
||||
if (buildOptions.externals) {
|
||||
ctx.externals.push(...buildOptions.externals)
|
||||
}
|
||||
|
||||
const distDir = resolve(ctx.rootDir, 'dist')
|
||||
await unlink(distDir).catch(() => {})
|
||||
await promisify(rimraf)(distDir)
|
||||
|
||||
if (buildOptions.prebuild) {
|
||||
const [cmd, ...args] = buildOptions.prebuild.split(' ')
|
||||
await execa(cmd, args)
|
||||
}
|
||||
|
||||
if (stub) {
|
||||
const stubbed: string[] = []
|
||||
for (const entry of ctx.entries) {
|
||||
if (entry.bundle) {
|
||||
const input = resolve(ctx.rootDir, entry.input)
|
||||
stubbed.push(entry.output)
|
||||
const output = resolve(ctx.rootDir, entry.output) + '.js'
|
||||
await mkdir(dirname(output)).catch(() => { })
|
||||
const cjsStub = `module.exports = require('jiti')()('${input}')`
|
||||
const esStub = `export * from '${input}'`
|
||||
await writeFile(output, entry.format === 'cjs' ? cjsStub : esStub)
|
||||
await writeFile(output.replace('.js', '.d.ts'), esStub)
|
||||
} else {
|
||||
const outDir = resolve(ctx.rootDir, entry.output)
|
||||
const srcDir = resolve(ctx.rootDir, entry.input)
|
||||
await unlink(outDir).catch(() => { })
|
||||
await symlink(srcDir, outDir)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
consola.info(chalk.cyan(`Building ${pkg.name}`))
|
||||
if (process.env.DEBUG) {
|
||||
consola.info(`
|
||||
${chalk.bold('Root dir:')} ${ctx.rootDir}
|
||||
${chalk.bold('Entries:')}
|
||||
${ctx.entries.map(entry => ' ' + dumpObject(entry)).join('\n')}
|
||||
`)
|
||||
}
|
||||
|
||||
const rollupOptions = getRollupOptions(ctx)
|
||||
const buildEntries: { path: string, bytes?: number, exports?: string[], chunks?: string[] }[] = []
|
||||
const usedImports = new Set<string>()
|
||||
if (rollupOptions) {
|
||||
const buildResult = await rollup(rollupOptions)
|
||||
const outputOptions = rollupOptions.output
|
||||
const { output } = await buildResult.write(outputOptions)
|
||||
|
||||
for (const entry of output.filter(e => e.type === 'chunk') as OutputChunk[]) {
|
||||
for (const id of entry.imports) {
|
||||
usedImports.add(id)
|
||||
}
|
||||
if (entry.isEntry) {
|
||||
buildEntries.push({
|
||||
path: relative(ctx.rootDir, resolve(outputOptions.dir, entry.fileName)),
|
||||
bytes: entry.code.length * 4,
|
||||
exports: entry.exports
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Types
|
||||
rollupOptions.plugins = rollupOptions.plugins || []
|
||||
rollupOptions.plugins.push(dts())
|
||||
const typesBuild = await rollup(rollupOptions)
|
||||
await typesBuild.write(outputOptions)
|
||||
}
|
||||
|
||||
for (const entry of ctx.entries.filter(e => !e.bundle)) {
|
||||
const { writtenFiles } = await mkdist({
|
||||
rootDir: ctx.rootDir,
|
||||
srcDir: entry.input,
|
||||
distDir: entry.output,
|
||||
format: entry.format
|
||||
})
|
||||
buildEntries.push({
|
||||
path: relative(ctx.rootDir, entry.output),
|
||||
chunks: [`${writtenFiles.length} files`]
|
||||
})
|
||||
}
|
||||
|
||||
consola.success(chalk.green('Build succeed for ' + pkg.name))
|
||||
for (const entry of buildEntries) {
|
||||
consola.log(` ${chalk.bold(entry.path)} (` + [
|
||||
entry.bytes && `size: ${chalk.cyan(prettyBytes(entry.bytes))}`,
|
||||
entry.exports && `exports: ${chalk.gray(entry.exports.join(', '))}`,
|
||||
entry.chunks && `chunks: ${chalk.gray(entry.chunks.join(', '))}`
|
||||
].filter(Boolean).join(', ') + ')')
|
||||
}
|
||||
|
||||
if (rollupOptions) {
|
||||
const usedDependencies = new Set<string>()
|
||||
const unusedDependencies = new Set<string>(Object.keys(pkg.dependencies || {}))
|
||||
const implicitDependnecies = new Set<string>()
|
||||
for (const id of usedImports) {
|
||||
unusedDependencies.delete(id)
|
||||
usedDependencies.add(id)
|
||||
}
|
||||
if (Array.isArray(buildOptions.dependencies)) {
|
||||
for (const id of buildOptions.dependencies) {
|
||||
unusedDependencies.delete(id)
|
||||
}
|
||||
}
|
||||
for (const id of usedDependencies) {
|
||||
if (
|
||||
!ctx.externals.includes(id) &&
|
||||
!id.startsWith('chunks/') &&
|
||||
!ctx.externals.includes(id.split('/')[0]) // lodash/get
|
||||
) {
|
||||
implicitDependnecies.add(id)
|
||||
}
|
||||
}
|
||||
if (unusedDependencies.size) {
|
||||
consola.warn('Potential unused dependencies found:', Array.from(unusedDependencies).map(id => chalk.cyan(id)).join(', '))
|
||||
}
|
||||
if (implicitDependnecies.size) {
|
||||
consola.warn('Potential implicit dependencies found:', Array.from(implicitDependnecies).map(id => chalk.cyan(id)).join(', '))
|
||||
}
|
||||
}
|
||||
|
||||
consola.log('')
|
||||
}
|
||||
|
||||
function resolveEntry (input: string | [string, Partial<BuildEntry>] | Partial<BuildEntry>): BuildEntry {
|
||||
let entry: Partial<BuildEntry>
|
||||
if (typeof input === 'string') {
|
||||
entry = { name: 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.format = entry.format ?? 'esm'
|
||||
return entry as BuildEntry
|
||||
}
|
||||
|
||||
function dumpObject (obj: Record<string, any>) {
|
||||
return '{ ' + Object.keys(obj).map(key => `${key}: ${JSON.stringify(obj[key])}`).join(', ') + ' }'
|
||||
}
|
||||
|
||||
function getRollupOptions (ctx: BuildContext): RollupOptions & { output: OutputOptions & { dir: string }} | null {
|
||||
const extensions = ['.ts', '.mjs', '.js', '.json']
|
||||
|
||||
const r = (...path: string[]) => resolve(ctx.rootDir, ...path)
|
||||
|
||||
const entries = ctx.entries.filter(e => e.bundle)
|
||||
if (!entries.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
input: entries.map(e => e.input),
|
||||
|
||||
output: {
|
||||
dir: r('dist'),
|
||||
format: 'cjs',
|
||||
chunkFileNames: 'chunks/[hash].js',
|
||||
exports: 'auto',
|
||||
preferConst: true
|
||||
},
|
||||
|
||||
external (id) {
|
||||
if (id[0] === '.' || id[0] === '/' || id.includes('src/')) {
|
||||
return false
|
||||
}
|
||||
const isExplicitExternal = !!ctx.externals.find(ext => id.includes(ext))
|
||||
if (!isExplicitExternal) {
|
||||
consola.warn(`Inlining external ${id}`)
|
||||
}
|
||||
return isExplicitExternal
|
||||
},
|
||||
|
||||
onwarn (warning, rollupWarn) {
|
||||
if (!warning.code || !['CIRCULAR_DEPENDENCY'].includes(warning.code)) {
|
||||
rollupWarn(warning)
|
||||
}
|
||||
},
|
||||
|
||||
plugins: [
|
||||
alias({
|
||||
entries: {
|
||||
src: resolve(__dirname, 'src')
|
||||
}
|
||||
}),
|
||||
|
||||
nodeResolve({
|
||||
extensions
|
||||
}),
|
||||
|
||||
esbuild({
|
||||
target: 'node12',
|
||||
loaders: {
|
||||
'.json': 'json'
|
||||
}
|
||||
}),
|
||||
|
||||
commonjs({
|
||||
extensions
|
||||
})
|
||||
]
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Node",
|
||||
@ -10,9 +11,7 @@
|
||||
"types": [
|
||||
"node",
|
||||
"jest"
|
||||
],
|
||||
"paths": {
|
||||
"nuxt/app/composables": ["./packages/app/src/composables"]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"exclude": ["./packages/*/dist/*"]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user