2018-12-22 21:05:13 +00:00
|
|
|
import path from 'path'
|
|
|
|
import consola from 'consola'
|
|
|
|
import escapeRegExp from 'lodash/escapeRegExp'
|
|
|
|
|
|
|
|
export const startsWithAlias = aliasArray => str => aliasArray.some(c => str.startsWith(c))
|
|
|
|
|
|
|
|
export const startsWithSrcAlias = startsWithAlias(['@', '~'])
|
|
|
|
|
|
|
|
export const startsWithRootAlias = startsWithAlias(['@@', '~~'])
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const isWindows = process.platform.startsWith('win')
|
2018-12-22 21:05:13 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const wp = function wp (p = '') {
|
2018-12-22 21:05:13 +00:00
|
|
|
if (isWindows) {
|
|
|
|
return p.replace(/\\/g, '\\\\')
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-06-30 17:47:42 +00:00
|
|
|
// Kept for backward compat (modules may use it from template context)
|
2019-07-10 10:45:49 +00:00
|
|
|
export const wChunk = function wChunk (p = '') {
|
2020-06-30 17:47:42 +00:00
|
|
|
return p
|
2018-12-22 21:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const reqSep = /\//g
|
|
|
|
const sysSep = escapeRegExp(path.sep)
|
|
|
|
const normalize = string => string.replace(reqSep, sysSep)
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const r = function r (...args) {
|
2018-12-22 21:05:13 +00:00
|
|
|
const lastArg = args[args.length - 1]
|
|
|
|
|
|
|
|
if (startsWithSrcAlias(lastArg)) {
|
|
|
|
return wp(lastArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wp(path.resolve(...args.map(normalize)))
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const relativeTo = function relativeTo (...args) {
|
2018-12-22 21:05:13 +00:00
|
|
|
const dir = args.shift()
|
|
|
|
|
|
|
|
// Keep webpack inline loader intact
|
|
|
|
if (args[0].includes('!')) {
|
|
|
|
const loaders = args.shift().split('!')
|
|
|
|
|
|
|
|
return loaders.concat(relativeTo(dir, loaders.pop(), ...args)).join('!')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve path
|
2019-04-22 18:50:07 +00:00
|
|
|
const resolvedPath = r(...args)
|
2018-12-22 21:05:13 +00:00
|
|
|
|
|
|
|
// Check if path is an alias
|
2019-04-22 18:50:07 +00:00
|
|
|
if (startsWithSrcAlias(resolvedPath)) {
|
|
|
|
return resolvedPath
|
2018-12-22 21:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make correct relative path
|
2019-04-22 18:50:07 +00:00
|
|
|
let rp = path.relative(dir, resolvedPath)
|
2018-12-22 21:05:13 +00:00
|
|
|
if (rp[0] !== '.') {
|
2019-01-29 19:23:42 +00:00
|
|
|
rp = '.' + path.sep + rp
|
2018-12-22 21:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return wp(rp)
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export function defineAlias (src, target, prop, opts = {}) {
|
2018-12-22 21:05:13 +00:00
|
|
|
const { bind = true, warn = false } = opts
|
|
|
|
|
|
|
|
if (Array.isArray(prop)) {
|
|
|
|
for (const p of prop) {
|
|
|
|
defineAlias(src, target, p, opts)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let targetVal = target[prop]
|
|
|
|
if (bind && typeof targetVal === 'function') {
|
|
|
|
targetVal = targetVal.bind(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
let warned = false
|
|
|
|
|
|
|
|
Object.defineProperty(src, prop, {
|
|
|
|
get: () => {
|
|
|
|
if (warn && !warned) {
|
|
|
|
warned = true
|
|
|
|
consola.warn({
|
|
|
|
message: `'${prop}' is deprecated'`,
|
|
|
|
additional: new Error().stack.split('\n').splice(2).join('\n')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return targetVal
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-01-08 19:25:56 +00:00
|
|
|
|
|
|
|
const isIndex = s => /(.*)\/index\.[^/]+$/.test(s)
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export function isIndexFileAndFolder (pluginFiles) {
|
2019-01-08 19:25:56 +00:00
|
|
|
// Return early in case the matching file count exceeds 2 (index.js + folder)
|
|
|
|
if (pluginFiles.length !== 2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return pluginFiles.some(isIndex)
|
|
|
|
}
|
2019-01-14 20:31:39 +00:00
|
|
|
|
2020-03-02 18:15:00 +00:00
|
|
|
export const getMainModule = () => {
|
|
|
|
return require.main || (module && module.main) || module
|
|
|
|
}
|