Skip to content

fix(cli): resolve plugins relative to the package context #5794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const PluginAPI = require('./PluginAPI')
const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand')
const defaultsDeep = require('lodash.defaultsdeep')
const { chalk, warn, error, isPlugin, resolvePluginId, loadModule, resolvePkg } = require('@vue/cli-shared-utils')
const { chalk, warn, error, isPlugin, resolvePluginId, loadModule, resolvePkg, resolveModule } = require('@vue/cli-shared-utils')

const { defaults, validate } = require('./options')
const checkWebpack = require('@vue/cli-service/lib/util/checkWebpack')
Expand Down Expand Up @@ -143,9 +143,9 @@ module.exports = class Service {
}

resolvePlugins (inlinePlugins, useBuiltIn) {
const idToPlugin = id => ({
const idToPlugin = (id, absolutePath) => ({
id: id.replace(/^.\//, 'built-in:'),
apply: require(id)
apply: require(absolutePath || id)
})

let plugins
Expand All @@ -161,7 +161,7 @@ module.exports = class Service {
'./config/css',
'./config/prod',
'./config/app'
].map(idToPlugin)
].map((id) => idToPlugin(id))

if (inlinePlugins) {
plugins = useBuiltIn !== false
Expand All @@ -176,16 +176,15 @@ module.exports = class Service {
this.pkg.optionalDependencies &&
id in this.pkg.optionalDependencies
) {
let apply = () => {}
try {
apply = require(id)
} catch (e) {
let apply = loadModule(id, this.pkgContext)
if (!apply) {
warn(`Optional dependency ${id} is not installed.`)
apply = () => {}
}

return { id, apply }
} else {
return idToPlugin(id)
return idToPlugin(id, resolveModule(id, this.pkgContext))
}
})
plugins = builtInPlugins.concat(projectPlugins)
Expand Down
7 changes: 7 additions & 0 deletions packages/@vue/cli-shared-utils/lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ const resolve = semver.satisfies(process.version, '>=10.0.0')
: resolveFallback

exports.resolveModule = function (request, context) {
// createRequire doesn't work with jest mock modules
// (which we used in migrator for inquirer, and in tests for cli-service)
// TODO: it's supported in Jest 25
if (process.env.VUE_CLI_TEST && (request.endsWith('migrator') || context === '/')) {
return request
}

let resolvedPath
try {
try {
Expand Down