From 227e04b92e3bfa2f00054f5f973296104039546a Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 21 Nov 2020 12:42:02 +0100 Subject: [PATCH] feat: improved env support --- packages/nitro/src/context.ts | 3 +++ packages/nitro/src/rollup/config.ts | 39 +++++++++++++++++------------ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/packages/nitro/src/context.ts b/packages/nitro/src/context.ts index bb713abc55..44d45fde09 100644 --- a/packages/nitro/src/context.ts +++ b/packages/nitro/src/context.ts @@ -2,6 +2,7 @@ import { resolve } from 'upath' import defu from 'defu' import type { NuxtOptions } from '@nuxt/types' import Hookable, { configHooksT } from 'hookable' +import type { Preset } from '@nuxt/un' import { tryImport, resolvePath, detectTarget, extendPreset } from './utils' import * as PRESETS from './presets' @@ -27,6 +28,7 @@ export interface SigmaContext { hooks: configHooksT nuxtHooks: configHooksT ignore: string[] + env: Preset output: { dir: string serverDir: string @@ -71,6 +73,7 @@ export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): S renderer: undefined, middleware: [], ignore: [], + env: {}, hooks: {}, nuxtHooks: {}, output: { diff --git a/packages/nitro/src/rollup/config.ts b/packages/nitro/src/rollup/config.ts index 8e811154d9..5f5e6a230b 100644 --- a/packages/nitro/src/rollup/config.ts +++ b/packages/nitro/src/rollup/config.ts @@ -1,4 +1,3 @@ -import Module from 'module' import { dirname, join, relative, resolve } from 'upath' import { InputOptions, OutputOptions } from 'rollup' import { terser } from 'rollup-plugin-terser' @@ -10,6 +9,7 @@ import replace from '@rollup/plugin-replace' import virtual from '@rollup/plugin-virtual' import inject from '@rollup/plugin-inject' import analyze from 'rollup-plugin-analyzer' +import type { Preset } from '@nuxt/un' import * as un from '@nuxt/un' import hasha from 'hasha' @@ -24,24 +24,31 @@ import { autoMock } from './automock' export type RollupConfig = InputOptions & { output: OutputOptions } export const getRollupConfig = (sigmaContext: SigmaContext) => { - const extensions: string[] = ['.ts', '.js', '.json', '.node'] + const extensions: string[] = ['.ts', '.mjs', '.js', '.json', '.node'] - const external: InputOptions['external'] = [] + const nodePreset = sigmaContext.node === false ? un.nodeless : un.node - const presets = [] - - if (sigmaContext.node === false) { - presets.push(un.nodeless) - } else { - presets.push(un.node) - external.push(...Module.builtinModules) + const builtinPreset: Preset = { + alias: { + // General + depd: require.resolve('@nuxt/un/runtime/npm/depd'), + // Vue 2 + encoding: 'un/mock/proxy', + he: 'un/mock/proxy', + resolve: 'un/mock/proxy', + 'source-map': 'un/mock/proxy', + 'lodash.template': 'un/mock/proxy', + 'serialize-javascript': 'un/mock/proxy', + // Vue 3 + '@babel/parser': 'un/mock/proxy', + '@vue/compiler-core': 'un/mock/proxy', + '@vue/compiler-dom': 'un/mock/proxy', + '@vue/compiler-ssr': 'un/mock/proxy' + } } - const env = un.env(...presets, { - alias: { - depd: require.resolve('@nuxt/un/runtime/npm/depd') - } - }) + const env = un.env(nodePreset, builtinPreset, sigmaContext.env) + // console.log(env) const buildServerDir = join(sigmaContext._nuxt.buildDir, 'dist/server') const runtimeAppDir = join(sigmaContext._internal.runtimeDir, 'app') @@ -75,7 +82,7 @@ export const getRollupConfig = (sigmaContext: SigmaContext) => { outro: '', preferConst: true }, - external, + external: env.external, plugins: [], onwarn (warning, rollupWarn) { if (!['CIRCULAR_DEPENDENCY', 'EVAL'].includes(warning.code)) {