Skip to content

Commit 0ea049b

Browse files
committed
fix(runtime): Fix Runtime component adding in vue2
Within Vue2, the addComponent will occasionally fail due to the component / script signature being inconsistent. To fix this we're using the vuetify addComponent code as it's known to work Vue3 does not seem to have this issue so it's still using the old version Have also taken the opportunity to update the dependencies so the build does not include them.
1 parent 45f2799 commit 0ea049b

File tree

13 files changed

+861
-747
lines changed

13 files changed

+861
-747
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ coverage
66
index.js
77
examples
88
generator
9+
src/runtime

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
var loader = require('./dist');
1+
var plugin = require('./dist');
22

3-
module.exports = loader;
3+
module.exports = plugin;

package.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"types": "dist",
77
"repository": {
88
"type": "git",
9-
"url": "git+https://github.com/loonpwn/vue-cli-plugin-import-components.git"
9+
"url": "git+https://github.com/harlan-zw/vue-cli-plugin-import-components.git"
1010
},
1111
"files": [
1212
"/dist",
@@ -23,7 +23,7 @@
2323
"automatic"
2424
],
2525
"scripts": {
26-
"build": "yarn clean && bili src/index.ts,src/loader.ts",
26+
"build": "yarn clean && bili src/index.ts,src/loader.ts && mkdist --src src/runtime --dist dist/runtime --format cjs",
2727
"clean": "rimraf dist",
2828
"test": "jest --coverage",
2929
"release": "dotenv release-it --",
@@ -32,9 +32,9 @@
3232
"author": "Harlan Wilton",
3333
"license": "MIT",
3434
"bugs": {
35-
"url": "https://github.com/loonpwn/vue-cli-plugin-import-components/issues"
35+
"url": "https://github.com/harlan-zw/vue-cli-plugin-import-components/issues"
3636
},
37-
"homepage": "https://github.com/loonpwn/vue-cli-plugin-import-components#readme",
37+
"homepage": "https://github.com/harlan-zw/vue-cli-plugin-import-components#readme",
3838
"publishConfig": {
3939
"access": "public"
4040
},
@@ -49,19 +49,21 @@
4949
"@types/node": "14.14.21",
5050
"@types/webpack": "^4.41.26",
5151
"@vue/cli-service": "^4.5.10",
52-
"@vue/cli-shared-utils": "^4.5.10",
5352
"@vue/compiler-sfc": "^3.0.5",
5453
"bili": "^5.0.5",
55-
"chalk": "^4.1.0",
5654
"dotenv-cli": "^4.0.0",
5755
"eslint": "^7.29.0",
58-
"loader-utils": "^2.0.0",
59-
"lodash": "^4.17.20",
56+
"mkdist": "^0.3.2",
6057
"pug": "^3.0.0",
6158
"release-it": "^14.2.2",
6259
"rollup-plugin-typescript2": "^0.29.0",
63-
"typescript": "^4.1.3",
64-
"upath": "^2.0.1"
60+
"typescript": "^4.1.3"
6561
},
66-
"dependencies": {}
62+
"dependencies": {
63+
"globby": "^11.0.4",
64+
"loader-utils": "^2.0.0",
65+
"lodash": "^4.17.21",
66+
"upath": "^2.0.1",
67+
"@vue/cli-shared-utils": "^4.5.13"
68+
}
6769
}

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ const plugin: ServicePlugin = (api: PluginAPI, options: VueCliPluginComponentsOp
6060

6161
if (vueVersion === 2) {
6262
pluginOptions.extractor = vue2.extractTagsFromSfc as TagExtractor
63+
pluginOptions.injector = vue2.injectComponents
6364
pluginOptions.compiler = loadVue2TemplateCompiler(api)
6465
}
6566
else if (vueVersion === 3) {
6667
pluginOptions.extractor = vue3.extractTagsFromSfc as TagExtractor
68+
pluginOptions.injector = vue3.injectComponents
6769
pluginOptions.compiler = loadVue3Compiler(api)
6870
}
6971
else {
@@ -89,4 +91,9 @@ const plugin: ServicePlugin = (api: PluginAPI, options: VueCliPluginComponentsOp
8991
return true
9092
}
9193

94+
export {
95+
VueCliPluginComponentsOptions,
96+
PluginOptions,
97+
}
98+
9299
export default plugin

src/loader.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { loader as webpackLoader } from 'webpack'
22
import { getOptions } from 'loader-utils'
33
import { resolve } from 'upath'
44
import { matcher, scanComponents } from './util/scanComponents'
5-
import { injectComponents } from './util/injectComponents'
65
import { PluginOptions, Component } from './types'
76

87
export default async function loader(this: webpackLoader.LoaderContext, source: string) {
@@ -33,5 +32,5 @@ export default async function loader(this: webpackLoader.LoaderContext, source:
3332
if (components.length <= 0)
3433
return source
3534

36-
return injectComponents(source, components)
35+
return options.injector.call(this, source, components)
3736
}

src/runtime/installComponents.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Source: Vuetify Loader, see https://github.com/vuetifyjs/vuetify-loader/blob/master/lib/runtime/installComponents.js
2+
3+
// IMPORTANT: Do NOT use ES2015 features in this file (except for modules).
4+
// This module is a runtime utility for cleaner component module output and will
5+
// be included in the final webpack user bundle.
6+
7+
module.exports = function installComponents (component, components) {
8+
var options = typeof component.exports === 'function'
9+
? component.exports.extendOptions
10+
: component.options
11+
12+
if (typeof component.exports === 'function') {
13+
options.components = component.exports.options.components
14+
}
15+
16+
options.components = options.components || {}
17+
18+
for (var i in components) {
19+
options.components[i] = options.components[i] || components[i]
20+
}
21+
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface PluginOptions {
2929

3030
// hidden options
3131
extractor: TagExtractor
32+
injector: any
3233
vueVersion: 2 | 3
3334
compiler: any
3435
}

src/vue2/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as parseSfcComponent } from './parseSfc'
22
export { default as compileTemplateFromDescriptor } from './compileTemplateFromDescriptor'
33
export { default as extractTagsFromSfc } from './extractTagsFromSfc'
4+
export { default as injectComponents } from './injectComponents'

src/vue2/injectComponents.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { loader as webpackLoader } from 'webpack'
2+
import { stringifyRequest } from 'loader-utils'
3+
import type { Component } from '../types'
4+
5+
const installComponents = require.resolve('./runtime/installComponents')
6+
7+
export default function injectComponents(this: webpackLoader.LoaderContext, source: string, components: Component[]) {
8+
const installComponentRuntime = `const installComponents = require(${stringifyRequest(this, `!${installComponents}`)})`
9+
10+
const injectScript
11+
= `/* vue-cli-plugin-import-components */
12+
${installComponentRuntime}
13+
${components.map(c => c.import).join('\n')}
14+
installComponents(component, {${components.map(c => c.pascalName).join(', ')}});`
15+
16+
const hotReload = source.indexOf('/* hot reload */')
17+
if (hotReload > -1)
18+
source = `${source.slice(0, hotReload) + injectScript}\n\n${source.slice(hotReload)}`
19+
else
20+
source += `\n\n${injectScript}`
21+
22+
return source
23+
}

src/vue3/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as parseSfcComponent } from './parseSfc'
22
export { default as compileTemplateFromDescriptor } from './compileTemplateFromDescriptor'
33
export { default as extractTagsFromSfc } from './extractTagsFromSfc'
4+
export { default as injectComponents } from './injectComponents'

0 commit comments

Comments
 (0)