Skip to content

Commit

Permalink
fix(plugin-legacy): fix to use the browserslist configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
erm1116 committed Feb 12, 2022
1 parent 19a58dd commit cb2fdf0
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion packages/plugin-legacy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path')
const { createHash } = require('crypto')
const { build } = require('vite')
const MagicString = require('magic-string').default
const fs = require('fs')

// lazy load babel since it's not used during dev
let babel
Expand All @@ -24,6 +25,11 @@ const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('d

const legacyEnvVarMarker = `__VITE_IS_LEGACY__`

/**
* @type {import('@babel/preset-env').TargetsOptions}
*/
const defaultTargets = 'defaults'

/**
* @param {import('.').Options} options
* @returns {import('vite').Plugin[]}
Expand All @@ -33,7 +39,8 @@ function viteLegacyPlugin(options = {}) {
* @type {import('vite').ResolvedConfig}
*/
let config
const targets = options.targets || 'defaults'
const targets =
options.targets || getDefaultTargets(options.ignoreBrowserslistConfig)
const genLegacy = options.renderLegacyChunks !== false
const genDynamicFallback = genLegacy

Expand Down Expand Up @@ -688,6 +695,35 @@ function wrapIIFEBabelPlugin() {
}
}

/**
* @param {boolean} ignoreBrowserslistConfig
* @returns {import('@babel/preset-env').TargetsOptions}
*/
function getDefaultTargets(ignoreBrowserslistConfig) {
if (ignoreBrowserslistConfig) return defaultTargets

try {
// If browserslist config sources exist, defaultTargets is not needed
// because @babel/preset-env automatically detects targets from browserslist config sources

const getRealPath = (filePath) => path.posix.join(process.cwd(), filePath)

const pkgJsonStr = fs.readFileSync(getRealPath('package.json'), 'utf8')
const pkgJson = JSON.parse(pkgJsonStr)
if ('browserslist' in pkgJson) return null

const rcFileContent = fs.readFileSync(
getRealPath('.browserslistrc'),
'utf8'
)
if (rcFileContent) return null

return defaultTargets
} catch (error) {
return defaultTargets
}
}

module.exports = viteLegacyPlugin

viteLegacyPlugin.default = viteLegacyPlugin
Expand Down

0 comments on commit cb2fdf0

Please sign in to comment.