Skip to content

Commit f261410

Browse files
author
Guillaume Chau
committed
fix(plugin api): IPC now namspaced per project by default, closes vuejs#2189
1 parent 8dd0b11 commit f261410

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

packages/@vue/cli-shared-utils/lib/ipc.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ const DEFAULT_OPTIONS = {
66
networkId: DEFAULT_ID,
77
autoConnect: true,
88
disconnectOnIdle: false,
9-
idleTimeout: DEFAULT_IDLE_TIMEOUT
9+
idleTimeout: DEFAULT_IDLE_TIMEOUT,
10+
namespaceOnProject: true
1011
}
1112

13+
const PROJECT_ID = process.env.VUE_CLI_PROJECT_ID
14+
1215
exports.IpcMessenger = class IpcMessenger {
1316
constructor (options = {}) {
1417
options = Object.assign({}, DEFAULT_OPTIONS, options)
@@ -45,6 +48,13 @@ exports.IpcMessenger = class IpcMessenger {
4548
send (data, type = 'message') {
4649
this.checkConnection()
4750
if (this.connected) {
51+
if (this.options.namespaceOnProject && PROJECT_ID) {
52+
data = {
53+
_projectId: PROJECT_ID,
54+
_data: data
55+
}
56+
}
57+
4858
ipc.of[this.id].emit(type, data)
4959

5060
clearTimeout(this.idleTimer)
@@ -118,6 +128,15 @@ exports.IpcMessenger = class IpcMessenger {
118128
}
119129

120130
_onMessage (data) {
121-
this.listeners.forEach(fn => fn(data))
131+
this.listeners.forEach(fn => {
132+
if (this.options.namespaceOnProject && data._projectId) {
133+
if (data._projectId === PROJECT_ID) {
134+
data = data._data
135+
} else {
136+
return
137+
}
138+
}
139+
fn(data)
140+
})
122141
}
123142
}

packages/@vue/cli-ui/apollo-server/api/PluginApi.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,19 @@ class PluginApi {
318318
* @param {function} cb Callback with 'data' param
319319
*/
320320
ipcOn (cb) {
321-
this.ipcHandlers.push(cb)
322-
return ipc.on(cb)
321+
const handler = cb._handler = ({ data, emit }) => {
322+
if (data._projectId) {
323+
if (data._projectId === this.project.id) {
324+
data = data._data
325+
} else {
326+
return
327+
}
328+
}
329+
// eslint-disable-next-line standard/no-callback-literal
330+
cb({ data, emit })
331+
}
332+
this.ipcHandlers.push(handler)
333+
return ipc.on(handler)
323334
}
324335

325336
/**
@@ -328,9 +339,11 @@ class PluginApi {
328339
* @param {any} cb Callback to be removed
329340
*/
330341
ipcOff (cb) {
331-
const index = this.ipcHandlers.indexOf(cb)
342+
const handler = cb._handler
343+
if (!handler) return
344+
const index = this.ipcHandlers.indexOf(handler)
332345
if (index !== -1) this.ipcHandlers.splice(index, 1)
333-
ipc.off(cb)
346+
ipc.off(handler)
334347
}
335348

336349
/**

packages/@vue/cli-ui/apollo-server/connectors/shared-data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function set ({ id, projectId, value }, context) {
3232
})
3333

3434
const watchers = notify({ id, projectId, value }, context)
35-
log('SharedData set', id, value, `(${watchers.length} watchers)`)
35+
log('SharedData set', id, projectId, value, `(${watchers.length} watchers)`)
3636
return { id, value }
3737
}
3838

@@ -47,7 +47,7 @@ function remove ({ id, projectId }, context) {
4747
})
4848

4949
notify({ id, projectId, value: undefined }, context)
50-
log('SharedData remove', id)
50+
log('SharedData remove', id, projectId)
5151
}
5252

5353
function watch ({ id, projectId }, handler) {

packages/@vue/cli-ui/apollo-server/connectors/tasks.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,9 @@ async function run (id, context) {
292292

293293
task.time = Date.now()
294294

295+
// Task env
295296
process.env.VUE_CLI_CONTEXT = cwd.get()
296-
297+
process.env.VUE_CLI_PROJECT_ID = projects.getCurrent(context).id
297298
const nodeEnv = process.env.NODE_ENV
298299
delete process.env.NODE_ENV
299300

packages/@vue/cli-ui/ui-defaults/tasks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = api => {
7171

7272
if (data.type === 'stats') {
7373
// Stats are read from a file
74-
const statsFile = path.resolve(process.cwd(), `./node_modules/.stats-${type}.json`)
74+
const statsFile = path.resolve(api.getCwd(), `./node_modules/.stats-${type}.json`)
7575
const value = await fs.readJson(statsFile)
7676
setSharedData(id, value)
7777
await fs.remove(statsFile)

0 commit comments

Comments
 (0)