From 9bbb82bc9026afabc2835e297c2b60aa834c9fda Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Mon, 30 Nov 2020 10:30:24 +0800 Subject: [PATCH] feat: allow manually specifying whether server-rendering is targeted (#1764) This option provides a solution for #1734 When testing with mocha + mochapack, even though the target environment is `node`, the compiled component is expected to be run with `jsdom` rather than with a Node.js server, so it should still be a client bundle. --- src/index.ts | 4 +++- src/resolveScript.ts | 2 +- src/templateLoader.ts | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index cb7e5beae..f13a034e8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,6 +43,8 @@ export interface VueLoaderOptions { hotReload?: boolean exposeFilename?: boolean appendExtension?: boolean + + isServerBuild?: boolean } let errorEmitted = false @@ -85,7 +87,7 @@ export default function loader( const options = (loaderUtils.getOptions(loaderContext) || {}) as VueLoaderOptions - const isServer = target === 'node' + const isServer = options.isServerBuild ?? target === 'node' const isProduction = mode === 'production' const { descriptor, errors } = parse(source, { diff --git a/src/resolveScript.ts b/src/resolveScript.ts index 964d06ffa..309df9cf7 100644 --- a/src/resolveScript.ts +++ b/src/resolveScript.ts @@ -33,7 +33,7 @@ export function resolveScript( } const isProd = loaderContext.mode === 'production' - const isServer = loaderContext.target === 'node' + const isServer = options.isServerBuild ?? loaderContext.target === 'node' const enableInline = canInlineTemplate(descriptor, isProd) const cacheToUse = isServer ? serverCache : clientCache diff --git a/src/templateLoader.ts b/src/templateLoader.ts index 196315272..53cd0fcfc 100644 --- a/src/templateLoader.ts +++ b/src/templateLoader.ts @@ -20,7 +20,7 @@ const TemplateLoader: webpack.loader.Loader = function (source, inMap) { const options = (loaderUtils.getOptions(loaderContext) || {}) as VueLoaderOptions - const isServer = loaderContext.target === 'node' + const isServer = options.isServerBuild ?? loaderContext.target === 'node' const isProd = loaderContext.mode === 'production' const query = qs.parse(loaderContext.resourceQuery.slice(1)) const scopeId = query.id as string