Skip to content

Commit

Permalink
improvement: intlify vue plugin (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Feb 26, 2021
1 parent ddbada8 commit a818387
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare class IntlifyVuePlugin implements webpack.Plugin {
let Plugin: typeof IntlifyVuePlugin

console.warn(
`[@intlify/vue-i18n-loader] IntlifyVuePlugin is experimental! This plugin is used for Intlify tools. Don't use this plugin to enhancement Component options.`
`[@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.`
)

if (webpack.version && webpack.version[0] > '4') {
Expand Down
61 changes: 21 additions & 40 deletions src/pluginWebpack4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import webpack from 'webpack'
import { ReplaceSource } from 'webpack-sources'
import { isFunction, isObject, isRegExp, isString } from '@intlify/shared'
const Dependency = require('webpack/lib/Dependency') // eslint-disable-line @typescript-eslint/no-var-requires
// const Template = require('webpack/lib/Template') // eslint-disable-line @typescript-eslint/no-var-requires

const PLUGIN_ID = 'IntlifyVuePlugin'

Expand Down Expand Up @@ -63,23 +62,6 @@ class VueComponentDependency extends Dependency {
}
}

/*
function getRequestAndIndex(
script: NormalModule,
template: NormalModule
): [string, number] {
if (script && template) {
return [script.userRequest!, 1]
} else if (script && !template) {
return [script.userRequest!, 0]
} else if (!script && template) {
return [template.userRequest!, 0]
} else {
return ['', -1]
}
}
*/

function stringifyObj(obj: Record<string, any>): string {
return `Object({${Object.keys(obj)
.map(key => {
Expand Down Expand Up @@ -118,15 +100,6 @@ function toCode(code: any): string {
}

function generateCode(dep: VueComponentDependency, importVar: string): string {
// const [request, index] = getRequestAndIndex(dep.script!, dep.template!)
// if (!require) {
// return ''
// }

// const importVar = `${Template.toIdentifier(
// `${request}`
// )}__WEBPACK_IMPORTED_MODULE_${index.toString()}__["default"]`

const injectionCodes = ['']
Object.keys(dep.values).forEach(key => {
const code = dep.values[key]
Expand All @@ -152,7 +125,6 @@ class VueComponentDependencyTemplate {

const code = generateCode(dep, orgReplace.content)
// console.log('generateCode', code, dep.statement, orgReplace)
// source.insert(dep.statement.declaration.range[0], code)
source.replace(orgReplace.start, orgReplace.end, code)
}
}
Expand Down Expand Up @@ -192,7 +164,6 @@ function toVueComponentDependency(parser: any, values: InjectionValues) {
values,
statement
)
// dep.loc = expr.loc
parser.state.current.addDependency(dep)
return true
}
Expand Down Expand Up @@ -223,19 +194,29 @@ export default class IntlifyVuePlugin implements webpack.Plugin {
new VueComponentDependency.Template()
)

const handler = (
parser: webpack.compilation.normalModuleFactory.Parser
) => {
parser.hooks.exportExpression.tap(
PLUGIN_ID,
(statement, declaration) => {
if (declaration.name === 'script') {
// console.log('exportExpression', statement, declaration)
return toVueComponentDependency(parser, injections)(statement)
}
}
)
}

normalModuleFactory.hooks.parser
.for('javascript/auto')
.tap(PLUGIN_ID, parser => {
parser.hooks.exportExpression.tap(
PLUGIN_ID,
(statement, declaration) => {
if (declaration.name === 'script') {
// console.log('exportExpression', statement, declaration)
return toVueComponentDependency(parser, injections)(statement)
}
}
)
})
.tap(PLUGIN_ID, handler)
normalModuleFactory.hooks.parser
.for('javascript/dynamic')
.tap(PLUGIN_ID, handler)
normalModuleFactory.hooks.parser
.for('javascript/esm')
.tap(PLUGIN_ID, handler)
}
)
}
Expand Down

0 comments on commit a818387

Please sign in to comment.