Skip to content

Commit

Permalink
feat: support alias in config
Browse files Browse the repository at this point in the history
BREAKING CHANGE: in resolvers, idToRequest has been renamed to alias
  • Loading branch information
yyx990803 committed May 10, 2020
1 parent a4524b4 commit 86d550a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
29 changes: 13 additions & 16 deletions src/node/build/buildPluginResolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,29 @@ export const createBuildResolvePlugin = (
return {
name: 'vite:resolve',
async resolveId(id: string) {
id = resolver.alias(id) || id
if (id === hmrClientId) {
return hmrClientId
} else if (id.startsWith('/')) {
}
if (id === 'vue' || id.startsWith('@vue/')) {
const vuePaths = resolveVue(root)
if (id in vuePaths) {
return (vuePaths as any)[id]
}
}
if (id.startsWith('/')) {
const resolved = resolver.requestToFile(id)
if (await fs.pathExists(resolved)) {
debug(id, `-->`, resolved)
return resolved
}
} else if (id === 'vue' || id.startsWith('@vue/')) {
const vuePaths = resolveVue(root)
if (id in vuePaths) {
return (vuePaths as any)[id]
}
} else if (!id.startsWith('.')) {
const request = resolver.idToRequest(id)
if (request) {
const resolved = resolver.requestToFile(request)
debug(id, `-->`, request, `--> `, resolved)
return resolved
} else {
const webModulePath = await resolveWebModule(root, id)
if (webModulePath) {
return webModulePath
}
const webModulePath = await resolveWebModule(root, id)
if (webModulePath) {
return webModulePath
}
}
// fallback to node-resolve
},
load(id: string) {
if (id === hmrClientId) {
Expand Down
3 changes: 2 additions & 1 deletion src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export async function build(options: BuildConfig = {}): Promise<BuildResult> {
outDir = path.resolve(root, 'dist'),
assetsDir = 'assets',
assetsInlineLimit = 4096,
alias = {},
resolvers = [],
vueCompilerOptions,
rollupInputOptions = {},
Expand All @@ -74,7 +75,7 @@ export async function build(options: BuildConfig = {}): Promise<BuildResult> {
const resolvedAssetsPath = path.join(outDir, assetsDir)
const cssFileName = 'style.css'

const resolver = createResolver(root, resolvers)
const resolver = createResolver(root, resolvers, alias)

const { htmlPlugin, renderIndex } = await createBuildHtmlPlugin(
root,
Expand Down
2 changes: 2 additions & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Rollup, {
OutputOptions as RollupOutputOptions
} from 'rollup'

export { Resolver }

/**
* Options shared between server and build.
*/
Expand Down
26 changes: 13 additions & 13 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import {
export interface Resolver {
requestToFile(publicPath: string, root: string): string | undefined
fileToRequest(filePath: string, root: string): string | undefined
idToRequest?(id: string): string | undefined
alias?(id: string): string | undefined
}

export interface InternalResolver {
requestToFile(publicPath: string): string
fileToRequest(filePath: string): string
idToRequest(id: string): string | undefined
alias(id: string): string | undefined
}

const defaultRequestToFile = (publicPath: string, root: string): string => {
Expand All @@ -38,12 +38,6 @@ const defaultFileToRequest = (filePath: string, root: string): string => {
return `/${slash(path.relative(root, filePath))}`
}

const defaultIdToRequest = (id: string) => {
if (id.startsWith('@') && id.indexOf('/') < 0) {
return `/${id}`
}
}

export const supportedExts = ['.js', '.ts', '.jsx', '.tsx', '.json']

const debug = require('debug')('vite:resolve')
Expand Down Expand Up @@ -78,7 +72,8 @@ const resolveExt = (id: string) => {

export function createResolver(
root: string,
resolvers: Resolver[]
resolvers: Resolver[],
alias: Record<string, string>
): InternalResolver {
return {
requestToFile: (publicPath) => {
Expand All @@ -103,12 +98,17 @@ export function createResolver(
}
return defaultFileToRequest(filePath, root)
},
idToRequest: (id: string) => {
alias: (id: string) => {
let aliased: string | undefined = alias[id]
if (aliased) {
return aliased
}
for (const r of resolvers) {
const request = r.idToRequest && r.idToRequest(id)
if (request) return request
aliased = r.alias && r.alias(id)
if (aliased) {
return aliased
}
}
return defaultIdToRequest(id)
}
}
}
9 changes: 7 additions & 2 deletions src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ const internalPlugins: ServerPlugin[] = [
]

export function createServer(config: ServerConfig = {}): Server {
const { root = process.cwd(), plugins = [], resolvers = [] } = config
const {
root = process.cwd(),
plugins = [],
resolvers = [],
alias = {}
} = config
const app = new Koa()
const server = http.createServer(app.callback())
const watcher = chokidar.watch(root, {
ignored: [/node_modules/]
}) as HMRWatcher
const resolver = createResolver(root, resolvers)
const resolver = createResolver(root, resolvers, alias)
const context = {
root,
app,
Expand Down
7 changes: 4 additions & 3 deletions src/node/utils/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const isImportRequest = (ctx: Context): boolean => {
return jsSrcFileRE.test(referer)
}

const moduleRE = /^[^\/\.]/
const bareImportRE = /^[^\/\.]/
const fileExtensionRE = /\.\w+$/

export const resolveImport = (
Expand All @@ -59,8 +59,9 @@ export const resolveImport = (
resolver: InternalResolver,
timestamp?: string
): string => {
if (moduleRE.test(id)) {
return resolver.idToRequest(id) || `/@modules/${id}`
id = resolver.alias(id) || id
if (bareImportRE.test(id)) {
return `/@modules/${id}`
} else {
let { pathname, query } = resolveRelativeRequest(importer, id)
// append an extension to extension-less imports
Expand Down

0 comments on commit 86d550a

Please sign in to comment.