|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | + |
| 3 | +import { parse as parseQuery } from 'querystring' |
| 4 | +import webpack from 'webpack' |
| 5 | +import { ReplaceSource } from 'webpack-sources' |
| 6 | +import { isFunction, isObject, isRegExp, isString } from '@intlify/shared' |
| 7 | +const Dependency = require('webpack/lib/Dependency') // eslint-disable-line @typescript-eslint/no-var-requires |
| 8 | +const NullFactory = require('webpack/lib/NullFactory') // eslint-disable-line @typescript-eslint/no-var-requires |
| 9 | + |
| 10 | +const PLUGIN_ID = 'IntlifyVuePlugin' |
| 11 | + |
| 12 | +interface NormalModule extends webpack.compilation.Module { |
| 13 | + resource?: string |
| 14 | + request?: string |
| 15 | + userRequest?: string |
| 16 | + addDependency(dep: unknown): void |
| 17 | + parser?: webpack.compilation.normalModuleFactory.Parser |
| 18 | + loaders?: Array<{ |
| 19 | + loader: string |
| 20 | + options: any |
| 21 | + indent?: string |
| 22 | + type?: string |
| 23 | + }> |
| 24 | +} |
| 25 | + |
| 26 | +type InjectionValues = Record<string, any> |
| 27 | + |
| 28 | +class VueComponentDependency extends Dependency { |
| 29 | + static Template: VueComponentDependencyTemplate |
| 30 | + script?: NormalModule |
| 31 | + template?: NormalModule |
| 32 | + values: InjectionValues |
| 33 | + statement: any |
| 34 | + |
| 35 | + constructor( |
| 36 | + script: NormalModule | undefined, |
| 37 | + template: NormalModule | undefined, |
| 38 | + values: InjectionValues, |
| 39 | + statement: any |
| 40 | + ) { |
| 41 | + super() |
| 42 | + this.script = script |
| 43 | + this.template = template |
| 44 | + this.values = values |
| 45 | + this.statement = statement |
| 46 | + } |
| 47 | + |
| 48 | + get type() { |
| 49 | + return 'harmony export expression' |
| 50 | + } |
| 51 | + |
| 52 | + getExports() { |
| 53 | + return { |
| 54 | + exports: ['default'], |
| 55 | + dependencies: undefined |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + updateHash(hash: any) { |
| 60 | + super.updateHash(hash) |
| 61 | + const scriptModule = this.script |
| 62 | + hash.update( |
| 63 | + (scriptModule && |
| 64 | + (!scriptModule.buildMeta || scriptModule.buildMeta.exportsType)) + '' |
| 65 | + ) |
| 66 | + hash.update((scriptModule && scriptModule.id) + '') |
| 67 | + const templateModule = this.template |
| 68 | + hash.update( |
| 69 | + (templateModule && |
| 70 | + (!templateModule.buildMeta || templateModule.buildMeta.exportsType)) + |
| 71 | + '' |
| 72 | + ) |
| 73 | + hash.update((templateModule && templateModule.id) + '') |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function stringifyObj(obj: Record<string, any>): string { |
| 78 | + return `Object({${Object.keys(obj) |
| 79 | + .map(key => { |
| 80 | + const code = obj[key] |
| 81 | + return `${JSON.stringify(key)}:${toCode(code)}` |
| 82 | + }) |
| 83 | + .join(',')}})` |
| 84 | +} |
| 85 | + |
| 86 | +function toCode(code: any): string { |
| 87 | + if (code === null) { |
| 88 | + return 'null' |
| 89 | + } |
| 90 | + |
| 91 | + if (code === undefined) { |
| 92 | + return 'undefined' |
| 93 | + } |
| 94 | + |
| 95 | + if (isString(code)) { |
| 96 | + return JSON.stringify(code) |
| 97 | + } |
| 98 | + |
| 99 | + if (isRegExp(code) && code.toString) { |
| 100 | + return code.toString() |
| 101 | + } |
| 102 | + |
| 103 | + if (isFunction(code) && code.toString) { |
| 104 | + return '(' + code.toString() + ')' |
| 105 | + } |
| 106 | + |
| 107 | + if (isObject(code)) { |
| 108 | + return stringifyObj(code) |
| 109 | + } |
| 110 | + |
| 111 | + return code + '' |
| 112 | +} |
| 113 | + |
| 114 | +function generateCode(dep: VueComponentDependency, importVar: string): string { |
| 115 | + const injectionCodes = [''] |
| 116 | + Object.keys(dep.values).forEach(key => { |
| 117 | + const code = dep.values[key] |
| 118 | + if (isFunction(code)) { |
| 119 | + injectionCodes.push(`${importVar}.${key} = ${JSON.stringify(code(dep))}`) |
| 120 | + } else { |
| 121 | + injectionCodes.push(`${importVar}.${key} = ${toCode(code)}`) |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + let ret = injectionCodes.join('\n') |
| 126 | + ret = ret.length > 0 ? `\n${ret}\n` : '' |
| 127 | + return (ret += `/* harmony default export */ __webpack_exports__["default"] = (${importVar});`) |
| 128 | +} |
| 129 | + |
| 130 | +class VueComponentDependencyTemplate { |
| 131 | + apply(dep: VueComponentDependency, source: ReplaceSource) { |
| 132 | + const repleacements = source.replacements |
| 133 | + const orgReplace = repleacements[repleacements.length - 1] |
| 134 | + const code = generateCode(dep, 'component.exports') |
| 135 | + // console.log('generateCode', code, dep.statement, orgReplace) |
| 136 | + source.replace(orgReplace.start, orgReplace.end, code) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +VueComponentDependency.Template = VueComponentDependencyTemplate |
| 141 | + |
| 142 | +function getScriptBlockModule(parser: any): NormalModule | undefined { |
| 143 | + return parser.state.current.dependencies.find((dep: any) => { |
| 144 | + const req = dep.userRequest || dep.request |
| 145 | + if (req && dep.originModule) { |
| 146 | + const query = parseQuery(req) |
| 147 | + return query.type === 'script' && query.lang === 'js' |
| 148 | + } else { |
| 149 | + return false |
| 150 | + } |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +function getTemplateBlockModule(parser: any): NormalModule | undefined { |
| 155 | + return parser.state.current.dependencies.find((dep: any) => { |
| 156 | + const req = dep.userRequest || dep.request |
| 157 | + if (req && dep.originModule) { |
| 158 | + const query = parseQuery(req) |
| 159 | + return query.type === 'template' |
| 160 | + } else { |
| 161 | + return false |
| 162 | + } |
| 163 | + }) |
| 164 | +} |
| 165 | + |
| 166 | +function toVueComponentDependency(parser: any, values: InjectionValues) { |
| 167 | + return function vueComponentDependencyw(statement: any) { |
| 168 | + // console.log('toVueComponentDependency##statement', statement) |
| 169 | + const dep = new VueComponentDependency( |
| 170 | + getScriptBlockModule(parser), |
| 171 | + getTemplateBlockModule(parser), |
| 172 | + values, |
| 173 | + statement |
| 174 | + ) |
| 175 | + // dep.loc = statement.loc |
| 176 | + parser.state.current.addDependency(dep) |
| 177 | + return true |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +export default class IntlifyVuePlugin implements webpack.Plugin { |
| 182 | + injections: InjectionValues |
| 183 | + |
| 184 | + constructor(injections: InjectionValues = {}) { |
| 185 | + this.injections = injections |
| 186 | + } |
| 187 | + |
| 188 | + apply(compiler: webpack.Compiler): void { |
| 189 | + const injections = this.injections |
| 190 | + |
| 191 | + compiler.hooks.compilation.tap( |
| 192 | + PLUGIN_ID, |
| 193 | + (compilation, { normalModuleFactory }) => { |
| 194 | + compilation.dependencyFactories.set( |
| 195 | + // @ts-ignore |
| 196 | + VueComponentDependency, |
| 197 | + new NullFactory() |
| 198 | + ) |
| 199 | + compilation.dependencyTemplates.set( |
| 200 | + // @ts-ignore |
| 201 | + VueComponentDependency, |
| 202 | + // @ts-ignore |
| 203 | + new VueComponentDependency.Template() |
| 204 | + ) |
| 205 | + |
| 206 | + const handler = ( |
| 207 | + parser: webpack.compilation.normalModuleFactory.Parser |
| 208 | + ) => { |
| 209 | + parser.hooks.exportExpression.tap( |
| 210 | + PLUGIN_ID, |
| 211 | + (statement, declaration) => { |
| 212 | + if ( |
| 213 | + (parser as any).state.module.resource.endsWith('.vue') && |
| 214 | + declaration.object.name === 'component' && |
| 215 | + declaration.property.name === 'exports' |
| 216 | + ) { |
| 217 | + // console.log('exportExpression', statement, declaration) |
| 218 | + return toVueComponentDependency(parser, injections)(statement) |
| 219 | + } |
| 220 | + } |
| 221 | + ) |
| 222 | + } |
| 223 | + |
| 224 | + normalModuleFactory.hooks.parser |
| 225 | + .for('javascript/auto') |
| 226 | + .tap(PLUGIN_ID, handler) |
| 227 | + normalModuleFactory.hooks.parser |
| 228 | + .for('javascript/dynamic') |
| 229 | + .tap(PLUGIN_ID, handler) |
| 230 | + normalModuleFactory.hooks.parser |
| 231 | + .for('javascript/esm') |
| 232 | + .tap(PLUGIN_ID, handler) |
| 233 | + } |
| 234 | + ) |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +/* eslint-enable @typescript-eslint/no-explicit-any */ |
0 commit comments