// compiler-sfc src: https://github.com/vuejs/vue-next/blob/master/packages/compiler-sfc/src/index.ts#L1 import { parse as sfc_parse, compileStyleAsync as sfc_compileStyleAsync, compileScript as sfc_compileScript, compileTemplate as sfc_compileTemplate, SFCAsyncStyleCompileOptions, SFCTemplateCompileOptions, } from '@vue/compiler-sfc' import { babelParserDefaultPlugins as vue_babelParserDefaultPlugins } from '@vue/shared' import * as vue_CompilerDOM from '@vue/compiler-dom' import { parse as babel_parse, ParserPlugin as babel_ParserPlugin } from '@babel/parser'; import { transformFromAstAsync as babel_transformFromAstAsync, types as t, } from '@babel/core'; // @ts-ignore (Could not find a declaration file for module '@babel/plugin-transform-modules-commonjs') import babelPluginTransformModulesCommonjs from '@babel/plugin-transform-modules-commonjs' // https://github.com/vuejs/jsx-next import jsx from '@vue/babel-plugin-jsx' import { formatErrorLineColumn, formatError, withCache, hash, renameDynamicImport, parseDeps, interopRequireDefault, transformJSCode, loadDeps, createModule, loadModuleInternal, } from './tools' import { Options, ModuleExport, CustomBlockCallback } from './types' export { version as vueVersion } from '@vue/compiler-sfc/../../package.json' /** * @ignore */ type PreprocessLang = SFCAsyncStyleCompileOptions['preprocessLang']; /** * the version of the library (process.env.VERSION is set by webpack, at compile-time) */ const version : string = process.env.VERSION; const genSourcemap : boolean = !!process.env.GEN_SOURCEMAP; /** * @internal */ const isProd : boolean = process.env.NODE_ENV === 'production'; /** * @internal */ export async function createSFCModule(source : string, filename : string, options : Options) : Promise { const component = {}; const { delimiters, moduleCache, compiledCache, getResource, addStyle, log, additionalBabelPlugins = [], customBlockHandler } = options; // vue-loader next: https://github.com/vuejs/vue-loader/blob/next/src/index.ts#L91 const { descriptor, errors } = sfc_parse(source, { filename, sourceMap: genSourcemap, }); const customBlockCallbacks : CustomBlockCallback[] = customBlockHandler !== undefined ? await Promise.all( descriptor.customBlocks.map((block) => customBlockHandler(block, filename, options)) ) : []; const componentHash = hash(filename, version); const scopeId = `data-v-${componentHash}`; // hack: asynchronously preloads the language processor before it is required by the synchronous preprocessCustomRequire() callback, see below if ( descriptor.template && descriptor.template.lang ) await loadModuleInternal({ refPath: filename, relPath: descriptor.template.lang }, options); const hasScoped = descriptor.styles.some(e => e.scoped); const compileTemplateOptions : SFCTemplateCompileOptions = descriptor.template ? { // hack, since sourceMap is not configurable an we want to get rid of source-map dependency. see genSourcemap compiler: { ...vue_CompilerDOM, compile: (template, options) => vue_CompilerDOM.compile(template, { ...options, sourceMap: genSourcemap }) }, source: descriptor.template.src ? (await getResource({ refPath: filename, relPath: descriptor.template.src }, options).getContent()).content.toString() : descriptor.template.content, filename: descriptor.filename, isProd, scoped: hasScoped, id: scopeId, compilerOptions: { delimiters, scopeId: hasScoped ? scopeId : undefined, mode: 'module', // see: https://github.com/vuejs/vue-next/blob/15baaf14f025f6b1d46174c9713a2ec517741d0d/packages/compiler-core/src/options.ts#L160 }, // transformAssetUrls preprocessLang: descriptor.template.lang, preprocessCustomRequire: id => moduleCache[id], // makes consolidate optional, see https://github.com/vuejs/vue-next/blob/15baaf14f025f6b1d46174c9713a2ec517741d0d/packages/compiler-sfc/src/compileTemplate.ts#L111-L113 } : null; if ( descriptor.script || descriptor.scriptSetup ) { // eg: https://github.com/vuejs/vue-loader/blob/6ed553f70b163031457acc961901313390cde9ef/src/index.ts#L136 // doc: