Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(library-builder): builder #3609

Draft
wants to merge 26 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d195f17
feat(library-builder): init builder
m0ksem Jul 11, 2023
b49d3f8
feat(library-builder): web-components
m0ksem Jul 11, 2023
d2a3f19
feat(library-builder): types and css build
m0ksem Jul 11, 2023
d5c1e88
feat(library-builder): add package.json exports
m0ksem Jul 11, 2023
c48c5f0
feat(library-builder): init build CLI
m0ksem Jul 11, 2023
84d1f0f
feat(library-builder): pretty stdio
m0ksem Jul 11, 2023
b9ee65b
raw
m0ksem Jul 11, 2023
9970779
feat(library-builder): nuxt module builder
m0ksem Jul 11, 2023
d8f69f9
feat(library-builder): add nuxtDir argument
m0ksem Jul 11, 2023
f020b64
feat(library-builder): nuxt module builder with runtime dir
m0ksem Jul 12, 2023
1d15df4
chore(library-builder): typo changes
m0ksem Jul 12, 2023
be49322
chore(library-builder): update readme
m0ksem Jul 12, 2023
ca1b8eb
chore: use library builder in ui package
m0ksem Jul 12, 2023
c301693
fix(library-builder): correct vuestic build
m0ksem Jul 14, 2023
2ffb480
feat(library-builder): check if nuxt and styles exists
m0ksem Jul 21, 2023
b86f330
feat(library-builder): resolve exports
m0ksem Jul 21, 2023
909fa87
feat(library-builder): meta builder
m0ksem Jul 24, 2023
927f03d
fix: correct external handling in vite builds
m0ksem Aug 9, 2023
6d8500a
chore: rename bin command
m0ksem Aug 9, 2023
166173d
chore: added build args
m0ksem Aug 9, 2023
d0ab1f3
fix: build target even if there is no argument
m0ksem Aug 9, 2023
0f6842c
chore: added prepack command
m0ksem Aug 9, 2023
5c88dcb
chore: alpha1
m0ksem Aug 9, 2023
1b86812
chore: alpha release
m0ksem Sep 5, 2023
17a7646
fix: suppress extra warnings
m0ksem Nov 7, 2023
81f338a
feat(library-builder): add lodash vite plugin
pogrib0k Nov 7, 2023
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
Prev Previous commit
Next Next commit
chore: use library builder in ui package
  • Loading branch information
m0ksem committed Jul 12, 2023
commit ca1b8ebffb9ae28632f54a5cb65ac37349a295e1
6 changes: 4 additions & 2 deletions packages/library-builder/src/builder/build-nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ export const buildNuxt = async (options: {
cwd: string,
outDir: string,
nuxtDir: string,
entry: string,
}) => {
const { cwd, outDir, nuxtDir } = options
const { cwd, outDir, nuxtDir, entry } = options

const nuxtModulePath = join(cwd, nuxtDir)

if (!existsSync(nuxtModulePath)) {
console.warn('Skipping building nuxt module, because it does not exist')
console.warn('Skipping building nuxt module, because it does not exist in ' + nuxtModulePath)
return
}

return buildNuxtModule({
rootDir: nuxtModulePath,
outDir: join(cwd, outDir, '/nuxt'),
entry,
cwd,
})
}
3 changes: 2 additions & 1 deletion packages/library-builder/src/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export const build = async (options: {
if (targets.includes('nuxt')) {
tasks.push(
buildNuxt({
cwd,
cwd,
entry,
outDir,
nuxtDir
})
Expand Down
2 changes: 1 addition & 1 deletion packages/library-builder/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ export const args = yargs(process.argv)
default: 'dist',
})
.help(true)
.parseSync()
.parseSync()
3 changes: 1 addition & 2 deletions packages/library-builder/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ import { args } from './args'

build({
cwd: process.cwd(),
outDir: args.outDir,
entry: args.entry,
...args,
})
12 changes: 12 additions & 0 deletions packages/library-builder/src/nuxt.shim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare module '#app' {
export * from 'nuxt/app'
}

declare module '#app/components/nuxt-link' {
export * from 'nuxt/dist/app/components/nuxt-link.ts'
export { default as default } from 'nuxt/dist/app/components/nuxt-link.ts'
}

declare module '#imports' {
export * from '@unhead/vue'
}
5 changes: 5 additions & 0 deletions packages/library-builder/src/nuxt/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { buildModule } from './module-builder';

export async function buildNuxtModule(options: {
cwd: string,
entry: string,
rootDir: string,
outDir?: string,
}) {
Expand All @@ -34,6 +35,10 @@ export async function buildNuxtModule(options: {
cwd: options.cwd,
rootDir: options.rootDir,
outDir: outDir,
externals: dependencies,
paths: packageJson.name ? {
[packageJson.name]: [join(options.cwd, options.entry)],
} : {}
})
])
}
31 changes: 30 additions & 1 deletion packages/library-builder/src/nuxt/module-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import { replaceNext } from '../plugins/replace-next';
async function buildModule(opts: {
cwd: string,
rootDir: string,
externals?: string[],
paths?: Record<string, string[]>,
outDir?: string,
}) {
const { build } = await import('unbuild');
const outDir = opts.outDir || "dist";
const { externals = [], paths = {} } = opts

return build(opts.cwd, false, {
failOnWarn: false, // Disable process.exit(1) on warnings
Expand All @@ -30,6 +33,15 @@ async function buildModule(opts: {
cjsBridge: true,
dts: {
tsconfig: join(opts.cwd, "tsconfig.json"),
compilerOptions: {
moduleResolution: 2,
paths: paths,
skipLibCheck: true,
strict: false,
skipDefaultLibCheck: true,
rootDir: '',
emitDeclarationOnly: true,
},
},
},
externals: [
Expand All @@ -41,7 +53,8 @@ async function buildModule(opts: {
"nuxt-edge",
"nuxt3",
"vue",
"vue-demi"
"vue-demi",
...externals,
],
hooks: {
"rollup:options"(_ctx, options) {
Expand All @@ -54,6 +67,22 @@ async function buildModule(opts: {
} else {
options.plugins = [plugin, options.plugins];
}

options.external = (source, importer, isResolved) => {
// Resolve '#app' imports
if (source.startsWith('#')) {
return true
}

if (source.includes('node_modules')) {
return true
}

if (source.startsWith('/') || source.startsWith('.')) {
return false
}
return true
}
},
async "rollup:done"(ctx) {
await writeCJSStub(ctx.options.outDir);
Expand Down
16 changes: 12 additions & 4 deletions packages/library-builder/src/nuxt/runtime-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const nuxtRuntimeDirViteConfig = (options: {

return defineViteConfig({
root: entryDir,

build: {
lib: {
entry: inputs,
Expand All @@ -27,17 +28,24 @@ export const nuxtRuntimeDirViteConfig = (options: {
return true
}

const importName = source.split('/')[0]

if (external.includes(importName)) {
if (source.includes('node_modules')) {
return true
}

return false
if (source.startsWith('/') || source.startsWith('.')) {
return false
}
return true
},
},

outDir: outDir,

ssr: true,
},

optimizeDeps: {
disabled: true,
},

plugins: [
Expand Down
1 change: 0 additions & 1 deletion packages/library-builder/src/utils/define-vite-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { type InlineConfig } from 'vite';
export const defineViteConfig = <T extends InlineConfig>(config: T) => {
config.configFile = false
config.logLevel = 'warn'
config.appType = 'custom'
config.build!.ssr = true

return config;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// @ts-ignore // TODO: mimic #app module
import { defineNuxtPlugin } from '#app'

// @replace-next-line: import { createLibraryPlugin } from 'my-cool-library'
import { createLibraryPlugin } from '../../main'
// @replace-next-line: import 'my-cool-library'
import { createLibraryPlugin } from 'my-cool-library'

export default defineNuxtPlugin((nuxtApp: any) => {
nuxtApp.vueApp.use(createLibraryPlugin())
Expand Down
3 changes: 3 additions & 0 deletions packages/library-builder/tests/demo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"paths": {
"@/*": [
"src/*"
],
"my-cool-library": [
"./src/main.ts"
]
},
}
Expand Down
22 changes: 19 additions & 3 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"serve": "vue-cli-service serve --mode book",
"serve:production": "vue-cli-service serve --mode production",
"build": "tsx build/build.ts",
"build:builder": "library-build",
"build:types": "vue-tsc --declaration --emitDeclarationOnly --outDir dist/types",
"build:book": "vue-cli-service build --mode book",
"lint": "vue-cli-service lint",
Expand Down Expand Up @@ -146,6 +147,7 @@
},
"exports": {
".": {
"types": "./dist/types/main.d.ts",
"import": {
"node": "./dist/esm-node/main.mjs",
"default": "./dist/es/main.js"
Expand All @@ -154,15 +156,29 @@
},
"./css": {
"import": {
"node": "./dist/vuestic-ui.css",
"node": "./dist/main.css",
"default": "./dist/styles/index.css"
}
},
"./web-components": "./dist/web-components/main.js",
"./web-components": {
"types": "./dist/types/main.d.ts",
"default": "./dist/web-components/main.js"
},
"./iife": "./dist/iife/main.js",
"./types": "./dist/types/main.d.ts",
"./styles/*": "./dist/styles/*",
"./dist/*": "./dist/*"
"./dist/*": "./dist/*",
"./components/*": {
"types": "./dist/types/components/*",
"import": {
"node": "./dist/esm-node/components/*",
"default": "./dist/es/components/*"
}
},
"./nuxt": {
"import": "./dist/nuxt/module.mjs",
"require": "./dist/nuxt/module.cjs"
}
},
"typesVersions": {
"*": {
Expand Down
18 changes: 18 additions & 0 deletions packages/ui/src/nuxt/composables/use-components.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { addComponent } from '@nuxt/kit'
import importNames from '../config/components'

/** Register vuestic composables globally with auto-import */
export const useVuesticComponents = () => {
importNames.forEach((name) => {
/**
* Add component from vuestic-ui package.
* Nuxt will tree-shake components that not included in options.
* Nuxt also will add component to auto-import.
*/
addComponent({
name,
export: name,
filePath: 'vuestic-ui',
})
})
}
20 changes: 20 additions & 0 deletions packages/ui/src/nuxt/composables/use-composables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { resolveInRuntime } from '../utils/resolve'
import type { Import } from 'unimport'

import { addImports } from '@nuxt/kit'
import importNames from '../config/composables'

/** Register vuestic composables globally with auto-import */
export const useVuesticComposables = () => {
const composablesFrom = resolveInRuntime('./runtime/composables.mjs')
const autoImportsList = importNames
.map<Import>((item) => ({
name: item,
// TODO: We probably need to rename all composables to `useVuesticGlobalConfig`, `useVuesticModal`, etc.
// Let's have it as an option in the future if there will be any issues.
as: item,
from: composablesFrom,
}))

addImports(autoImportsList)
}
29 changes: 29 additions & 0 deletions packages/ui/src/nuxt/composables/use-config-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { existsSync, writeFileSync, readFileSync } from 'fs'
import { resolveAlias, useNuxt } from '@nuxt/kit'
import { resolve } from 'pathe'
import { resolveInRuntime } from '../utils/resolve'
import { watch } from 'chokidar'

export const useConfigFile = async () => {
const root = resolveAlias('~/')
const configPath = resolve(root, 'vuestic.config.ts')
const nuxtConfigPath = resolve(root, 'nuxt.config.ts')
const nuxt = useNuxt()

const restartNuxt = () => {
writeFileSync(nuxtConfigPath, readFileSync(nuxtConfigPath, 'utf-8'))
}

if (existsSync(configPath)) {
nuxt.options.alias['#vuestic-config'] = configPath
} else {
nuxt.options.alias['#vuestic-config'] = resolveInRuntime('./runtime/config.mjs')
}

const watcher = watch(configPath)
.on('ready', () => {
watcher
.on('add', restartNuxt)
.on('unlink', restartNuxt)
})
}
38 changes: 38 additions & 0 deletions packages/ui/src/nuxt/composables/use-css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useNuxt } from '@nuxt/kit'
import type { Nuxt, NuxtOptions } from '@nuxt/schema'
import type { VuesticOptions } from '../types'

const VUESTIC_DEFAULT_FONTS = [
{ rel: 'stylesheet', href: 'https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,400;1,700&display=swap' },
{ rel: 'stylesheet', href: 'https://fonts.googleapis.com/icon?family=Material+Icons&display=swap' },
]

const registerDefaultFonts = (nuxt: Nuxt) => {
nuxt.options.app = nuxt.options.app || {} as NuxtOptions['app']
nuxt.options.app.head = nuxt.options.app.head || {}
nuxt.options.app.head.link = nuxt.options.app.head.link || []
nuxt.options.app.head.link.push(...VUESTIC_DEFAULT_FONTS)
}

/** Register CSS modules */
export const useVuesticCSS = (options: VuesticOptions) => {
const nuxt = useNuxt()

if (Array.isArray(options.css)) {
/** Register essential css. Vuestic will not work without essential.css */
nuxt.options.css.push('vuestic-ui/styles/essential.css')

/** Register other css modules that user has chosen */
options.css.forEach((cssModuleName) => {
nuxt.options.css.push(`vuestic-ui/styles/${cssModuleName}.css`)
})
} else if (options.css === true) {
/** Register all CSS */
nuxt.options.css.push('vuestic-ui/dist/styles/index.css')
}

if (options.fonts) {
registerDefaultFonts(nuxt)
}
// Do not register any CSS if `options.css` is `false`
}
20 changes: 20 additions & 0 deletions packages/ui/src/nuxt/composables/use-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { addPluginTemplate } from '@nuxt/kit'

import { resolveInRuntime } from '../utils/resolve'

import type { VuesticOptions } from '../types'

/** Register vuestic nuxt plugin */
export const useVuesticPlugin = (options: VuesticOptions) => {
const pluginPath = resolveInRuntime('./runtime/plugin.mjs')

addPluginTemplate({
src: pluginPath,
filename: pluginPath.split('/').pop(),

// Use JSON.stringify() here, because it will be inserted in ejs template as string. Then we will JSON.parse it.
options: {
value: JSON.stringify(options),
},
})
}
8 changes: 8 additions & 0 deletions packages/ui/src/nuxt/composables/use-transpile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useNuxt } from '@nuxt/kit'

/** Transpile vuestic-ui */
export const useTranspile = () => {
const nuxt = useNuxt()

nuxt.options.build.transpile.push('vuestic-ui')
}
Loading
Loading