Skip to content

Commit

Permalink
feat: improved env support
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 21, 2020
1 parent e1d3779 commit 0c22cbe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -27,6 +28,7 @@ export interface SigmaContext {
hooks: configHooksT
nuxtHooks: configHooksT
ignore: string[]
env: Preset
output: {
dir: string
serverDir: string
Expand Down Expand Up @@ -71,6 +73,7 @@ export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): S
renderer: undefined,
middleware: [],
ignore: [],
env: {},
hooks: {},
nuxtHooks: {},
output: {
Expand Down
39 changes: 23 additions & 16 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand All @@ -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 env = un.env(...presets, {
const builtinPreset: Preset = {
alias: {
depd: require.resolve('@nuxt/un/runtime/npm/depd')
// 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(nodePreset, builtinPreset, sigmaContext.env)
// console.log(env)

const buildServerDir = join(sigmaContext._nuxt.buildDir, 'dist/server')
const runtimeAppDir = join(sigmaContext._internal.runtimeDir, 'app')
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 0c22cbe

Please sign in to comment.