Skip to content

Commit a818387

Browse files
authored
improvement: intlify vue plugin (#161)
1 parent ddbada8 commit a818387

File tree

2 files changed

+22
-41
lines changed

2 files changed

+22
-41
lines changed

src/plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ declare class IntlifyVuePlugin implements webpack.Plugin {
99
let Plugin: typeof IntlifyVuePlugin
1010

1111
console.warn(
12-
`[@intlify/vue-i18n-loader] IntlifyVuePlugin is experimental! This plugin is used for Intlify tools. Don't use this plugin to enhancement Component options.`
12+
`[@intlify/vue-i18n-loader] IntlifyVuePlugin is experimental! This plugin is used for Intlify tools. Don't use this plugin to enhancement Component options of your application.`
1313
)
1414

1515
if (webpack.version && webpack.version[0] > '4') {

src/pluginWebpack4.ts

+21-40
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import webpack from 'webpack'
55
import { ReplaceSource } from 'webpack-sources'
66
import { isFunction, isObject, isRegExp, isString } from '@intlify/shared'
77
const Dependency = require('webpack/lib/Dependency') // eslint-disable-line @typescript-eslint/no-var-requires
8-
// const Template = require('webpack/lib/Template') // eslint-disable-line @typescript-eslint/no-var-requires
98

109
const PLUGIN_ID = 'IntlifyVuePlugin'
1110

@@ -63,23 +62,6 @@ class VueComponentDependency extends Dependency {
6362
}
6463
}
6564

66-
/*
67-
function getRequestAndIndex(
68-
script: NormalModule,
69-
template: NormalModule
70-
): [string, number] {
71-
if (script && template) {
72-
return [script.userRequest!, 1]
73-
} else if (script && !template) {
74-
return [script.userRequest!, 0]
75-
} else if (!script && template) {
76-
return [template.userRequest!, 0]
77-
} else {
78-
return ['', -1]
79-
}
80-
}
81-
*/
82-
8365
function stringifyObj(obj: Record<string, any>): string {
8466
return `Object({${Object.keys(obj)
8567
.map(key => {
@@ -118,15 +100,6 @@ function toCode(code: any): string {
118100
}
119101

120102
function generateCode(dep: VueComponentDependency, importVar: string): string {
121-
// const [request, index] = getRequestAndIndex(dep.script!, dep.template!)
122-
// if (!require) {
123-
// return ''
124-
// }
125-
126-
// const importVar = `${Template.toIdentifier(
127-
// `${request}`
128-
// )}__WEBPACK_IMPORTED_MODULE_${index.toString()}__["default"]`
129-
130103
const injectionCodes = ['']
131104
Object.keys(dep.values).forEach(key => {
132105
const code = dep.values[key]
@@ -152,7 +125,6 @@ class VueComponentDependencyTemplate {
152125

153126
const code = generateCode(dep, orgReplace.content)
154127
// console.log('generateCode', code, dep.statement, orgReplace)
155-
// source.insert(dep.statement.declaration.range[0], code)
156128
source.replace(orgReplace.start, orgReplace.end, code)
157129
}
158130
}
@@ -192,7 +164,6 @@ function toVueComponentDependency(parser: any, values: InjectionValues) {
192164
values,
193165
statement
194166
)
195-
// dep.loc = expr.loc
196167
parser.state.current.addDependency(dep)
197168
return true
198169
}
@@ -223,19 +194,29 @@ export default class IntlifyVuePlugin implements webpack.Plugin {
223194
new VueComponentDependency.Template()
224195
)
225196

197+
const handler = (
198+
parser: webpack.compilation.normalModuleFactory.Parser
199+
) => {
200+
parser.hooks.exportExpression.tap(
201+
PLUGIN_ID,
202+
(statement, declaration) => {
203+
if (declaration.name === 'script') {
204+
// console.log('exportExpression', statement, declaration)
205+
return toVueComponentDependency(parser, injections)(statement)
206+
}
207+
}
208+
)
209+
}
210+
226211
normalModuleFactory.hooks.parser
227212
.for('javascript/auto')
228-
.tap(PLUGIN_ID, parser => {
229-
parser.hooks.exportExpression.tap(
230-
PLUGIN_ID,
231-
(statement, declaration) => {
232-
if (declaration.name === 'script') {
233-
// console.log('exportExpression', statement, declaration)
234-
return toVueComponentDependency(parser, injections)(statement)
235-
}
236-
}
237-
)
238-
})
213+
.tap(PLUGIN_ID, handler)
214+
normalModuleFactory.hooks.parser
215+
.for('javascript/dynamic')
216+
.tap(PLUGIN_ID, handler)
217+
normalModuleFactory.hooks.parser
218+
.for('javascript/esm')
219+
.tap(PLUGIN_ID, handler)
239220
}
240221
)
241222
}

0 commit comments

Comments
 (0)