Skip to content

Commit a2e9473

Browse files
authored
feat: support for use in the nuxt layers (#47)
1 parent 7d5a610 commit a2e9473

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/core/transformPlugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { camelize, genSideEffectsImport, toRegExp } from '../utils'
77
import type { TransformOptions } from '../types'
88

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

1819
export const transformPlugin = createUnplugin((options: PluginOptions) => {
19-
const { include, exclude, transformStyles } = options
20+
const { layers, include, exclude, transformStyles } = options
2021

2122
return {
2223
name: `${libraryName}:transform`,
2324
enforce: 'post',
2425
transformInclude (id) {
26+
if (layers.some(layer => id.startsWith(layer))) {
27+
return true
28+
}
2529
if (exclude.some(pattern => id.match(pattern))) {
2630
return false
2731
}

src/module.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@ import {
99
localePlugin,
1010
transformPlugin
1111
} from './core/index'
12+
import { getLayersDir } from './utils'
1213
import type { ModuleOptions } from './types'
1314
export type { ModuleOptions } from './types'
1415

1516
export default defineNuxtModule<ModuleOptions>({
1617
meta: {
1718
name: libraryName,
18-
configKey: libraryName
19+
configKey: libraryName,
20+
compatibility: {
21+
nuxt: '>=3'
22+
}
1923
},
2024
defaults,
2125
setup (options, nuxt) {
26+
const layers = getLayersDir(nuxt.options._layers)
27+
2228
resolveOptions()
2329
nuxt.options.imports.autoImport !== false && resolveImports(options)
2430
nuxt.options.components !== false && resolveComponents(options)
@@ -30,6 +36,7 @@ export default defineNuxtModule<ModuleOptions>({
3036
config.plugins = config.plugins || []
3137
config.plugins.push(
3238
transformPlugin.vite({
39+
layers,
3340
include: options.include,
3441
exclude: options.exclude,
3542
sourcemap: nuxt.options.sourcemap[mode],
@@ -52,6 +59,7 @@ export default defineNuxtModule<ModuleOptions>({
5259
config.plugins = config.plugins || []
5360
config.plugins.push(
5461
transformPlugin.webpack({
62+
layers,
5563
include: options.include,
5664
exclude: options.exclude,
5765
sourcemap: nuxt.options.sourcemap[mode],

src/utils.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1+
import type { NuxtConfigLayer } from '@nuxt/schema'
2+
import { libraryName } from './config'
3+
4+
export function getLayersDir (layers: NuxtConfigLayer[]) {
5+
const list = []
6+
7+
for (const layer of layers) {
8+
const srcDir = layer.config.srcDir || layer.cwd
9+
if (srcDir.includes('node_modules') && layer.config[libraryName]?.importStyle !== false) {
10+
list.push(srcDir)
11+
}
12+
}
13+
14+
return list
15+
}
16+
117
export function isObject (value: unknown): value is Record<any, any> {
218
return value !== null && typeof value === 'object'
319
}
420

521
export function toArray<T extends any | any[]> (
622
value: T
723
): T extends any[] ? T : T[] {
8-
return Array.isArray(value) ? value : ([value] as any)
24+
return (Array.isArray(value) ? value : [value]) as any
925
}
1026

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

0 commit comments

Comments
 (0)