Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion src/core/transformPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { camelize, genSideEffectsImport, toRegExp } from '../utils'
import type { TransformOptions } from '../types'

interface PluginOptions extends TransformOptions {
layers: string[]
sourcemap?: NuxtOptions['sourcemap']['client']
transformStyles: (name: string) => undefined | string
}
Expand All @@ -16,12 +17,15 @@ const componentsRegExp =
const importsRegExp = toRegExp(Object.keys(allImportsWithStyle), 'g')

export const transformPlugin = createUnplugin((options: PluginOptions) => {
const { include, exclude, transformStyles } = options
const { layers, include, exclude, transformStyles } = options

return {
name: `${libraryName}:transform`,
enforce: 'post',
transformInclude (id) {
if (layers.some(layer => id.startsWith(layer))) {
return true
}
if (exclude.some(pattern => id.match(pattern))) {
return false
}
Expand Down
10 changes: 9 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ import {
localePlugin,
transformPlugin
} from './core/index'
import { getLayersDir } from './utils'
import type { ModuleOptions } from './types'
export type { ModuleOptions } from './types'

export default defineNuxtModule<ModuleOptions>({
meta: {
name: libraryName,
configKey: libraryName
configKey: libraryName,
compatibility: {
nuxt: '>=3'
}
},
defaults,
setup (options, nuxt) {
const layers = getLayersDir(nuxt.options._layers)

resolveOptions()
nuxt.options.imports.autoImport !== false && resolveImports(options)
nuxt.options.components !== false && resolveComponents(options)
Expand All @@ -30,6 +36,7 @@ export default defineNuxtModule<ModuleOptions>({
config.plugins = config.plugins || []
config.plugins.push(
transformPlugin.vite({
layers,
include: options.include,
exclude: options.exclude,
sourcemap: nuxt.options.sourcemap[mode],
Expand All @@ -52,6 +59,7 @@ export default defineNuxtModule<ModuleOptions>({
config.plugins = config.plugins || []
config.plugins.push(
transformPlugin.webpack({
layers,
include: options.include,
exclude: options.exclude,
sourcemap: nuxt.options.sourcemap[mode],
Expand Down
18 changes: 17 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import type { NuxtConfigLayer } from '@nuxt/schema'
import { libraryName } from './config'

export function getLayersDir (layers: NuxtConfigLayer[]) {
const list = []

for (const layer of layers) {
const srcDir = layer.config.srcDir || layer.cwd
if (srcDir.includes('node_modules') && layer.config[libraryName]?.importStyle !== false) {
list.push(srcDir)
}
}

return list
}

export function isObject (value: unknown): value is Record<any, any> {
return value !== null && typeof value === 'object'
}

export function toArray<T extends any | any[]> (
value: T
): T extends any[] ? T : T[] {
return Array.isArray(value) ? value : ([value] as any)
return (Array.isArray(value) ? value : [value]) as any
}

export function toRegExp (arr: string[], flags?: string): RegExp {
Expand Down