Skip to content

feat: support vue.config.mjs #6405

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 6 commits into from
Apr 13, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
fix: use callback in init() to fix cases where .init() must be sy…
…nchronous

Such as in the `webpack.config.js`
  • Loading branch information
haoqunjiang committed Apr 8, 2021
commit f3ec4d8b3f23ca57a6799a1e90db55787a76924c
46 changes: 26 additions & 20 deletions packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const checkWebpack = require('./util/checkWebpack')
const loadFileConfig = require('./util/loadFileConfig')
const resolveUserConfig = require('./util/resolveUserConfig')

// Seems we can't use `instanceof Promise` here (would fail the tests)
const isPromise = p => p && typeof p.then === 'function'
module.exports = class Service {
constructor (context, { plugins, pkg, inlineOptions, useBuiltIn } = {}) {
checkWebpack(context)
Expand Down Expand Up @@ -56,7 +58,7 @@ module.exports = class Service {
return pkg
}

async init (mode = process.env.VUE_CLI_MODE) {
init (mode = process.env.VUE_CLI_MODE) {
if (this.initialized) {
return
}
Expand All @@ -71,23 +73,31 @@ module.exports = class Service {
this.loadEnv()

// load user config
const userOptions = await this.loadUserOptions()
this.projectOptions = defaultsDeep(userOptions, defaults())
const userOptions = this.loadUserOptions()
const loadedCallback = (loadedUserOptions) => {
this.projectOptions = defaultsDeep(loadedUserOptions, defaults())

debug('vue:project-config')(this.projectOptions)
debug('vue:project-config')(this.projectOptions)

// apply plugins.
this.plugins.forEach(({ id, apply }) => {
if (this.pluginsToSkip.has(id)) return
apply(new PluginAPI(id, this), this.projectOptions)
})
// apply plugins.
this.plugins.forEach(({ id, apply }) => {
if (this.pluginsToSkip.has(id)) return
apply(new PluginAPI(id, this), this.projectOptions)
})

// apply webpack configs from project config file
if (this.projectOptions.chainWebpack) {
this.webpackChainFns.push(this.projectOptions.chainWebpack)
// apply webpack configs from project config file
if (this.projectOptions.chainWebpack) {
this.webpackChainFns.push(this.projectOptions.chainWebpack)
}
if (this.projectOptions.configureWebpack) {
this.webpackRawConfigFns.push(this.projectOptions.configureWebpack)
}
}
if (this.projectOptions.configureWebpack) {
this.webpackRawConfigFns.push(this.projectOptions.configureWebpack)

if (isPromise(userOptions)) {
return userOptions.then(loadedCallback)
} else {
return loadedCallback(userOptions)
}
}

Expand Down Expand Up @@ -322,13 +332,9 @@ module.exports = class Service {
loadUserOptions () {
const { fileConfig, fileConfigPath } = loadFileConfig(this.context)

// Seems we can't use `instanceof Promise` here (would fail the tests)
if (fileConfig && typeof fileConfig.then === 'function') {
if (isPromise(fileConfig)) {
return fileConfig
.then(mod => {
// fs.writeFileSync(`${this.context}/aaaa`, `mod ${JSON.stringify(mod, null, 2)}`)
return mod.default
})
.then(mod => mod.default)
.then(loadedConfig => resolveUserConfig({
inlineOptions: this.inlineOptions,
pkgConfig: this.pkg.vue,
Expand Down