Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/@vue/cli-plugin-pwa/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ module.exports = api => {
'register-service-worker': '^1.0.0'
}
})
api.injectImports(`src/main.js`, `import './registerServiceWorker'`)
api.injectImports(api.entryFile, `import './registerServiceWorker'`)
api.render('./template')

if (api.invoking && api.hasPlugin('typescript')) {
const convertFiles = require('@vue/cli-plugin-typescript/generator/convert')
convertFiles(api)
}
}
22 changes: 22 additions & 0 deletions packages/@vue/cli-plugin-typescript/generator/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = (api, { tsLint = false } = {}) => {
// delete all js files that have a ts file of the same name
// and simply rename other js files to ts
const jsRE = /\.js$/
const excludeRE = /^tests\/e2e\/|(\.config|rc)\.js$/
const convertLintFlags = require('../lib/convertLintFlags')
api.postProcessFiles(files => {
for (const file in files) {
if (jsRE.test(file) && !excludeRE.test(file)) {
const tsFile = file.replace(jsRE, '.ts')
if (!files[tsFile]) {
let content = files[file]
if (tsLint) {
content = convertLintFlags(content)
}
files[tsFile] = content
}
delete files[file]
}
}
})
}
21 changes: 1 addition & 20 deletions packages/@vue/cli-plugin-typescript/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,5 @@ module.exports = (api, {
hasJest
})

// delete all js files that have a ts file of the same name
// and simply rename other js files to ts
const jsRE = /\.js$/
const excludeRE = /^tests\/e2e\/|(\.config|rc)\.js$/
const convertLintFlags = require('../lib/convertLintFlags')
api.postProcessFiles(files => {
for (const file in files) {
if (jsRE.test(file) && !excludeRE.test(file)) {
const tsFile = file.replace(jsRE, '.ts')
if (!files[tsFile]) {
let content = files[file]
if (tsLint) {
content = convertLintFlags(content)
}
files[tsFile] = content
}
delete files[file]
}
}
})
require('./convert')(api, { tsLint })
}
11 changes: 8 additions & 3 deletions packages/@vue/cli-service/generator/router/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module.exports = (api, options) => {
api.injectImports(`src/main.js`, `import router from './router'`)
api.injectRootOptions(`src/main.js`, `router`)
api.injectImports(api.entryFile, `import router from './router'`)
api.injectRootOptions(api.entryFile, `router`)
api.extendPackage({
dependencies: {
'vue-router': '^3.0.1'
}
})
api.render('./template')

if (options.invoking) {
if (api.invoking) {
api.postProcessFiles(files => {
const appFile = files[`src/App.vue`]
if (appFile) {
Expand All @@ -26,5 +26,10 @@ module.exports = (api, options) => {
console.log(files[`src/App.vue`])
}
})

if (api.hasPlugin('typescript')) {
const convertFiles = require('@vue/cli-plugin-typescript/generator/convert')
convertFiles(api)
}
}
}
9 changes: 7 additions & 2 deletions packages/@vue/cli-service/generator/vuex/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
module.exports = (api, options) => {
api.injectImports(`src/main.js`, `import store from './store'`)
api.injectRootOptions(`src/main.js`, `store`)
api.injectImports(api.entryFile, `import store from './store'`)
api.injectRootOptions(api.entryFile, `store`)
api.extendPackage({
dependencies: {
vuex: '^3.0.1'
}
})
api.render('./template')

if (api.invoking && api.hasPlugin('typescript')) {
const convertFiles = require('@vue/cli-plugin-typescript/generator/convert')
convertFiles(api)
}
}
4 changes: 3 additions & 1 deletion packages/@vue/cli/lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module.exports = class Generator {
pkg = {},
plugins = [],
completeCbs = [],
files = {}
files = {},
invoking = false
} = {}) {
this.context = context
this.plugins = plugins
Expand All @@ -31,6 +32,7 @@ module.exports = class Generator {
this.imports = {}
this.rootOptions = {}
this.completeCbs = completeCbs
this.invoking = invoking

// for conflict resolution
this.depSources = {}
Expand Down
21 changes: 21 additions & 0 deletions packages/@vue/cli/lib/GeneratorAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class GeneratorAPI {
name: toShortPluginId(id),
link: getPluginLink(id)
}))

this._entryFile = undefined
}

/**
Expand Down Expand Up @@ -225,6 +227,25 @@ class GeneratorAPI {
_options.add(opt)
})
}

/**
* Get the entry file taking into account typescript.
*
* @readonly
*/
get entryFile () {
if (this._entryFile) return this._entryFile
return (this._entryFile = fs.existsSync(this.resolve('src/main.ts')) ? 'src/main.ts' : 'src/main.js')
}

/**
* Is the plugin being invoked?
*
* @readonly
*/
get invoking () {
return this.generator.invoking
}
}

function extractCallDir () {
Expand Down
6 changes: 2 additions & 4 deletions packages/@vue/cli/lib/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ async function add (pluginName, options = {}, context = process.cwd()) {
async function addRouter (context) {
invoke.runGenerator(context, {
id: 'core:router',
apply: require('@vue/cli-service/generator/router'),
options: { invoking: true }
apply: require('@vue/cli-service/generator/router')
})
}

async function addVuex (context) {
invoke.runGenerator(context, {
id: 'core:vuex',
apply: require('@vue/cli-service/generator/vuex'),
options: { invoking: true }
apply: require('@vue/cli-service/generator/vuex')
})
}

Expand Down
3 changes: 2 additions & 1 deletion packages/@vue/cli/lib/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
pkg,
plugins: [plugin],
files: await readFiles(context),
completeCbs: createCompleteCbs
completeCbs: createCompleteCbs,
invoking: true
})

log()
Expand Down