import * as path from 'node:path' import { entryCache } from './entry-cache' import { stringifyRequest } from './util' import type * as webpack from 'webpack' export default function (this: webpack.LoaderContext, source: string, map?: any) { const options = this.getOptions() const stringify = (s: string): string => stringifyRequest(this, s) const pageName = options.name const { isNeedRawLoader } = options.loaderMeta // raw is a placeholder loader to locate changed .vue resource const raw = path.join(__dirname, 'raw.js') const entryCacheLoader = path.join(__dirname, 'entry-cache.js') + `?name=${pageName}` entryCache.set(pageName, { source, map }) const componentPath = isNeedRawLoader ? ['!', raw, entryCacheLoader, this.resourcePath].join('!') : ['!', entryCacheLoader, this.resourcePath].join('!') const { globalObject } = this._compilation?.outputOptions || { globalObject: 'wx' } const prerender = ` if (typeof PRERENDER !== 'undefined') { ${globalObject}._prerender = inst }` return `import { createComponentConfig } from '@tarojs/runtime' import component from ${stringify(componentPath)} var inst = Component(createComponentConfig(component, '${pageName}')) ${options.prerender ? prerender : ''} export default component ` }