Skip to content

Commit

Permalink
feat: support exclude files to be transform imports
Browse files Browse the repository at this point in the history
  • Loading branch information
changfeng committed Dec 10, 2022
1 parent b47b436 commit 598e242
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
69 changes: 42 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Plugin, ResolvedConfig } from 'vite'
import { log, analyzeCode, codeIncludesLibraryName, isTranspileDependencies } from './shared'
import * as fs from 'fs'
import { createRequire } from 'module'
import * as path from 'path'
import chalk from 'chalk'
import { Plugin, ResolvedConfig } from 'vite'

import defaultLibList from './resolvers'
import * as path from 'path'
import * as fs from 'fs'
import { createRequire } from 'module';
import { analyzeCode, codeIncludesLibraryName, isTranspileDependencies, log } from './shared'
import { ImpConfig } from './types'

const optionsCheck = (options: Partial<ImpConfig>) => {
Expand All @@ -23,60 +24,74 @@ export default function vitePluginImp(userConfig: Partial<ImpConfig> = {}): Plug
if (!optionsCheck(userConfig)) {
return { name }
}

return {
name,
async configResolved(resolvedConfig) {
// store the resolved config
viteConfig = resolvedConfig
isSourcemap = !!viteConfig.build?.sourcemap
config = Object.assign({
libList: [],
exclude: [],
ignoreStylePathNotFound: viteConfig.command === 'serve'
}, userConfig)

const libListNameSet: Set<string> = new Set(config.libList.map(lib => lib.libName))
config = Object.assign(
{
libList: [],
exclude: [],
ignoreStylePathNotFound: viteConfig.command === 'serve',
excludeTranspileFiles: [/\.html(\?.*)?$/],
},
userConfig
)

const libListNameSet: Set<string> = new Set(config.libList.map((lib) => lib.libName))
// filter defaultLibList from exclude
let defaultLibFilteredList = defaultLibList.filter(lib => !config.exclude?.includes(lib.libName))
let defaultLibFilteredList = defaultLibList.filter(
(lib) => !config.exclude?.includes(lib.libName)
)

// check user package.json to filter LibList from user dependencies
const userPkgPath = path.resolve(viteConfig.root, 'package.json')
if (fs.existsSync(userPkgPath)) {
// @ts-ignore
const require = createRequire(import.meta.url);
const userPkg = require(userPkgPath);
const require = createRequire(import.meta.url)
const userPkg = require(userPkgPath)
if (userPkg?.dependencies) {
defaultLibFilteredList = defaultLibFilteredList.filter(item => userPkg?.dependencies?.[item.libName])
defaultLibFilteredList = defaultLibFilteredList.filter(
(item) => userPkg?.dependencies?.[item.libName]
)
}
}

// merge defaultLibFilteredList to config.libList
defaultLibFilteredList.forEach(defaultLib => {
defaultLibFilteredList.forEach((defaultLib) => {
if (!libListNameSet.has(defaultLib.libName)) {
config.libList?.push(defaultLib)
libListNameSet.add(defaultLib.libName)
}
})
},
transform(code, id) {
const { transpileDependencies = false } = config
if (
(!/(node_modules)/.test(id) || isTranspileDependencies(transpileDependencies, id))
&& codeIncludesLibraryName(code, config.libList)
) {
const { transpileDependencies = false } = config
if (
!config.excludeTranspileFiles!.some((reg) => reg.test(id)) &&
(!/(node_modules)/.test(id) || isTranspileDependencies(transpileDependencies, id)) &&
codeIncludesLibraryName(code, config.libList)
) {
const sourcemap = this?.getCombinedSourcemap()
const { importStr, codeRemoveOriginImport } = analyzeCode(code, config, viteConfig.command, config.ignoreStylePathNotFound);
const { importStr, codeRemoveOriginImport } = analyzeCode(
code,
config,
viteConfig.command,
config.ignoreStylePathNotFound
)

return {
code: `${importStr};${viteConfig.command === 'serve' ? code : codeRemoveOriginImport}`,
map: isSourcemap ? sourcemap : null
map: isSourcemap ? sourcemap : null,
}
}
return {
code,
map: null
map: null,
}
}
},
}
}
21 changes: 14 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,28 @@ export interface ImpConfig {
*/
exclude?: string[]
/**
* when a style path is not found, don’t show error and give a warning.
* when a style path is not found, don’t show error and give a warning.
* Default: command === 'serve'
*/
ignoreStylePathNotFound?: boolean
ignoreStylePathNotFound?: boolean
/**
* By default `vite-plugin-imp` ignores all files inside node_modules.
* By default `vite-plugin-imp` ignores all files inside node_modules.
* You can enable this option to avoid unexpected untranspiled code from third-party dependencies.
*
* Transpiling all the dependencies could slow down the build process, though.
* If build performance is a concern, you can explicitly transpile only some of the dependencies
*
* Transpiling all the dependencies could slow down the build process, though.
* If build performance is a concern, you can explicitly transpile only some of the dependencies
* by passing an array of package names or name patterns to this option.
*
*
* Default: false
*/
transpileDependencies?: boolean | Array<string | RegExp>
/**
* The regexp patterns used to exclude files be transformed imports
* By default will exclude html files
*
* Default: [/\.html(\?.*)?$/]
*/
excludeTranspileFiles?: RegExp[]
}

export interface ImportMaps {
Expand Down

0 comments on commit 598e242

Please sign in to comment.