From 4fb4e35fe78600a449247555588106bad4b0c331 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 30 Jan 2018 01:45:51 -0500 Subject: [PATCH] fix: --target for global build --- packages/@vue/cli-service-global/index.js | 25 ++++++++++++++----- .../lib/createConfigPlugin.js | 25 +++++++++++-------- .../cli-service/lib/commands/build/index.js | 10 +++++--- .../lib/commands/build/resolveLibConfig.js | 10 ++++---- .../build/resolveWebComponentConfig.js | 10 ++++---- packages/@vue/cli/bin/vue.js | 5 ++-- 6 files changed, 52 insertions(+), 33 deletions(-) diff --git a/packages/@vue/cli-service-global/index.js b/packages/@vue/cli-service-global/index.js index fc4cf9baf..3fce93957 100644 --- a/packages/@vue/cli-service-global/index.js +++ b/packages/@vue/cli-service-global/index.js @@ -8,7 +8,7 @@ const babelPlugin = toPlugin('@vue/cli-plugin-babel') const eslintPlugin = toPlugin('@vue/cli-plugin-eslint') const createConfigPlugin = require('./lib/createConfigPlugin') -function createService (entry) { +function resolveEntry (entry) { const context = process.cwd() entry = entry || findExisting(context, [ @@ -29,6 +29,13 @@ function createService (entry) { process.exit(1) } + return { + context, + entry + } +} + +function createService (context, entry, asLib) { return new Service(context, { projectOptions: { compiler: true, @@ -37,15 +44,21 @@ function createService (entry) { plugins: [ babelPlugin, eslintPlugin, - createConfigPlugin(context, entry) + createConfigPlugin(context, entry, asLib) ] }) } -exports.serve = (entry, args) => { - createService(entry).run('serve', args) +exports.serve = (_entry, args) => { + const { context, entry } = resolveEntry(_entry) + createService(context, entry).run('serve', args) } -exports.build = (entry, args) => { - createService(entry).run('build', args) +exports.build = (_entry, args) => { + const { context, entry } = resolveEntry(_entry) + const asLib = args.target && args.target !== 'app' + if (asLib) { + args.libEntry = entry + } + createService(context, entry, asLib).run('build', args) } diff --git a/packages/@vue/cli-service-global/lib/createConfigPlugin.js b/packages/@vue/cli-service-global/lib/createConfigPlugin.js index 9f1ada428..883ee3338 100644 --- a/packages/@vue/cli-service-global/lib/createConfigPlugin.js +++ b/packages/@vue/cli-service-global/lib/createConfigPlugin.js @@ -2,7 +2,7 @@ const path = require('path') const resolve = require('resolve') const { findExisting } = require('./util') -module.exports = function createConfigPlugin (context, entry) { +module.exports = function createConfigPlugin (context, entry, asLib) { return { id: '@vue/cli-service-global-config', apply: (api, options) => { @@ -111,17 +111,22 @@ module.exports = function createConfigPlugin (context, entry) { } })) - // set html plugin template - const indexFile = findExisting(context, [ - 'index.html', - 'public/index.html' - ]) || path.resolve(__dirname, '../template/index.html') - config - .plugin('html') - .tap(() => [{ template: indexFile }]) + if (!asLib) { + // set html plugin template + const indexFile = findExisting(context, [ + 'index.html', + 'public/index.html' + ]) || path.resolve(__dirname, '../template/index.html') + config + .plugin('html') + .tap(args => { + args[0].template = indexFile + return args + }) + } // disable copy plugin if no public dir - if (!findExisting(context, ['public'])) { + if (asLib || !findExisting(context, ['public'])) { config.plugins.delete('copy') } }) diff --git a/packages/@vue/cli-service/lib/commands/build/index.js b/packages/@vue/cli-service/lib/commands/build/index.js index 4ea19a569..70a7abef8 100644 --- a/packages/@vue/cli-service/lib/commands/build/index.js +++ b/packages/@vue/cli-service/lib/commands/build/index.js @@ -1,7 +1,7 @@ const defaults = { mode: 'production', target: 'app', - entry: 'src/App.vue' + libEntry: 'src/App.vue' } module.exports = (api, options) => { @@ -11,11 +11,13 @@ module.exports = (api, options) => { options: { '--mode': `specify env mode (default: ${defaults.mode})`, '--target': `app | lib | web-component (default: ${defaults.target})`, - '--entry': `entry for lib or web-component (default: ${defaults.entry})`, - '--name': `name for lib or web-component (default: "name" in package.json)` + '--libEntry': `entry for lib or web-component (default: ${defaults.entry})`, + '--libName': `name for lib or web-component (default: "name" in package.json)` } }, args => { - args = Object.assign({}, defaults, args) + for (const key in defaults) { + if (args[key] == null) args[key] = defaults[key] + } api.setMode(args.mode) const chalk = require('chalk') diff --git a/packages/@vue/cli-service/lib/commands/build/resolveLibConfig.js b/packages/@vue/cli-service/lib/commands/build/resolveLibConfig.js index 7641319d5..3cbcfd1b3 100644 --- a/packages/@vue/cli-service/lib/commands/build/resolveLibConfig.js +++ b/packages/@vue/cli-service/lib/commands/build/resolveLibConfig.js @@ -1,21 +1,21 @@ -module.exports = (api, { entry, name }) => { +module.exports = (api, { libEntry, libName }) => { const genConfig = (format, postfix = format) => { api.chainWebpack(config => { - const libName = name || api.service.pkg.name + libName = libName || api.service.pkg.name || libEntry.replace(/\.(js|vue)$/, '') config.entryPoints.clear() // set proxy entry for *.vue files - if (/\.vue$/.test(entry)) { + if (/\.vue$/.test(libEntry)) { config .entry(`${libName}.${postfix}`) .add(require.resolve('./entry-lib.js')) config.resolve .alias - .set('~entry', api.resolve(entry)) + .set('~entry', api.resolve(libEntry)) } else { config .entry(`${libName}.${postfix}`) - .add(api.resolve(entry)) + .add(api.resolve(libEntry)) } config.output diff --git a/packages/@vue/cli-service/lib/commands/build/resolveWebComponentConfig.js b/packages/@vue/cli-service/lib/commands/build/resolveWebComponentConfig.js index 5efaa064f..b81bfff1c 100644 --- a/packages/@vue/cli-service/lib/commands/build/resolveWebComponentConfig.js +++ b/packages/@vue/cli-service/lib/commands/build/resolveWebComponentConfig.js @@ -1,21 +1,21 @@ -module.exports = (api, { entry, name }) => { +module.exports = (api, { libEntry, libName }) => { const genConfig = postfix => { api.chainWebpack(config => { - const libName = name || api.service.pkg.name + libName = libName || api.service.pkg.name || libEntry.replace(/\.(js|vue)$/, '') config.entryPoints.clear() // set proxy entry for *.vue files - if (/\.vue$/.test(entry)) { + if (/\.vue$/.test(libEntry)) { config .entry(`${libName}.${postfix}`) .add(require.resolve('./entry-web-component.js')) config.resolve .alias - .set('~entry', api.resolve(entry)) + .set('~entry', api.resolve(libEntry)) } else { config .entry(`${libName}.${postfix}`) - .add(api.resolve(entry)) + .add(api.resolve(libEntry)) } config.output diff --git a/packages/@vue/cli/bin/vue.js b/packages/@vue/cli/bin/vue.js index f495c850b..584d01bae 100755 --- a/packages/@vue/cli/bin/vue.js +++ b/packages/@vue/cli/bin/vue.js @@ -64,9 +64,8 @@ program program .command('build [entry]') - .option('-t, --target', 'Build target (app, lib, web-component). Default: app') - .option('-f, --format', 'How the lib is exposed (iife, amd, cjs, umd). Default: umd') - .option('-n, --name', 'Library name for umd export') + .option('-t, --target ', 'Build target (app | lib | web-component, default: app)') + .option('-n, --libName ', 'name for lib or web-component') .description('build a .js or .vue file in production mode with zero config') .action((entry, cmd) => { loadCommand('build', '@vue/cli-service-global').build(entry, cleanArgs(cmd))