Skip to content

Commit

Permalink
fix: allow use of nuxt3 in non-wsl windows environment (nuxt#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jul 12, 2021
1 parent 4521580 commit 0a3041c
Show file tree
Hide file tree
Showing 43 changed files with 69 additions and 49 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"lerna": "^4.0.0",
"object-hash": "^2.2.0",
"typescript": "^4.3.5",
"unbuild": "^0.3.1"
"unbuild": "^0.3.1",
"upath": "^2.0.1"
}
}
2 changes: 1 addition & 1 deletion packages/app/meta.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { resolve } = require('path')
const { resolve } = require('upath')

module.exports = {
appDir: resolve(__dirname, 'dist')
Expand Down
1 change: 1 addition & 0 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@vueuse/head": "^0.6.0",
"hookable": "^4.4.1",
"ohmyfetch": "^0.2.0",
"upath": "^2.0.1",
"vue": "^3.1.4",
"vue-router": "^4.0.10",
"vuex5": "^0.5.0-testing.3"
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"listhen": "^0.2.4",
"mri": "^1.1.6",
"unbuild": "^0.3.1",
"upath": "^2.0.1",
"v8-compile-cache": "^2.3.0"
}
}
2 changes: 1 addition & 1 deletion packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import { resolve } from 'path'
import { resolve } from 'upath'
import { requireModule } from '../utils/cjs'

export async function invoke (args) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { resolve } from 'upath'
import chokidar from 'chokidar'
import debounce from 'debounce-promise'
import { createServer, createLoadingHandler } from '../utils/server'
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/utils/cjs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { normalize } from 'upath'

export function resolveModule (id, paths?) {
return require.resolve(id, {
return normalize(require.resolve(id, {
paths: [].concat(
// @ts-ignore
global.__NUXT_PREPATHS__,
Expand All @@ -8,7 +10,7 @@ export function resolveModule (id, paths?) {
// @ts-ignore
global.__NUXT_PATHS__
).filter(Boolean)
})
}))
}

export function requireModule (id, paths?) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'path'
import { existsSync, promises as fsp } from 'fs'
import { resolve } from 'upath'
import dotenv from 'dotenv'

export interface LoadDotEnvOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/config/load.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'path'
import { existsSync } from 'fs'
import { resolve } from 'upath'
import defu from 'defu'
import { applyDefaults } from 'untyped'
import * as rc from 'rc9'
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/config/schema/_app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve, join } from 'path'
import { resolve, join } from 'upath'
import { existsSync, readdirSync } from 'fs'
import defu from 'defu'
import { isRelative, joinURL, hasProtocol } from 'ufo'
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/config/schema/_common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join, resolve } from 'path'
import { join, resolve } from 'upath'
import env from 'std-env'
import createRequire from 'create-require'
import { pascalCase } from 'scule'
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/config/schema/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { resolve } from 'upath'
import { joinURL } from 'ufo'

export default {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/module/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path, { basename, parse } from 'path'
import fs from 'fs'
import path, { basename, parse } from 'upath'
import hash from 'hash-sum'
import consola from 'consola'
import { useNuxt } from '../nuxt'
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/utils/cjs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'path'
import { join, normalize } from 'upath'
import jiti from 'jiti'

// TODO: use create-require for jest environment
Expand Down Expand Up @@ -81,7 +81,7 @@ export function requireModulePkg (id: string, opts: RequireModuleOptions = {}) {

/** Resolve the path of a module. */
export function resolveModule (id: string, opts: ResolveModuleOptions = {}) {
return _require.resolve(id, {
return normalize(_require.resolve(id, {
paths: [].concat(
// @ts-ignore
global.__NUXT_PREPATHS__,
Expand All @@ -90,7 +90,7 @@ export function resolveModule (id: string, opts: ResolveModuleOptions = {}) {
// @ts-ignore
global.__NUXT_PATHS__
).filter(Boolean)
})
}))
}

/** Try to resolve the path of a module, but don't emit an error if it can't be found. */
Expand Down
6 changes: 3 additions & 3 deletions packages/nitro/src/rollup/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, join, relative, resolve } from 'upath'
import { dirname, join, normalize, relative, resolve } from 'upath'
import { InputOptions, OutputOptions } from 'rollup'
import defu from 'defu'
import { terser } from 'rollup-plugin-terser'
Expand Down Expand Up @@ -198,8 +198,8 @@ export const getRollupConfig = (nitroContext: NitroContext) => {
rollupConfig.plugins.push(alias({
entries: {
'#nitro': nitroContext._internal.runtimeDir,
'#nitro-renderer': require.resolve(resolve(nitroContext._internal.runtimeDir, 'app', renderer)),
'#config': require.resolve(resolve(nitroContext._internal.runtimeDir, 'app/config')),
'#nitro-renderer': normalize(require.resolve(resolve(nitroContext._internal.runtimeDir, 'app', renderer))),
'#config': normalize(require.resolve(resolve(nitroContext._internal.runtimeDir, 'app/config'))),
'#nitro-vue-renderer': vue2ServerRenderer,
'#build': nitroContext._nuxt.buildDir,
'~': nitroContext._nuxt.srcDir,
Expand Down
3 changes: 2 additions & 1 deletion packages/nitro/src/rollup/plugins/automock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import consola from 'consola'
import { normalize } from 'upath'

const internalRegex = /^\.|\?|\.[mc]?js$|.ts$|.json$/

Expand All @@ -9,7 +10,7 @@ export function autoMock () {
if (src && !internalRegex.test(src)) {
consola.warn('Auto mock external ', src)
return {
id: require.resolve('unenv/runtime/mock/proxy')
id: normalize(require.resolve('unenv/runtime/mock/proxy'))
}
}
return null
Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/src/rollup/plugins/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Based on https://github.com/egoist/rollup-plugin-esbuild (MIT)

import { extname, relative } from 'path'
import { extname, relative } from 'upath'
import { Plugin, PluginContext } from 'rollup'
import { Loader, TransformResult, transform } from 'esbuild'
import { createFilter, FilterPattern } from '@rollup/pluginutils'
Expand Down
9 changes: 7 additions & 2 deletions packages/nitro/src/rollup/plugins/externals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve, dirname } from 'upath'
import { resolve, dirname, normalize } from 'upath'
import { copyFile, mkdirp } from 'fs-extra'
import { nodeFileTrace, NodeFileTraceOptions } from '@vercel/nft'
import type { Plugin } from 'rollup'
Expand All @@ -23,6 +23,11 @@ export function externals (opts: NodeExternalsOptions): Plugin {
return null
}

// Normalize path on windows
if (process.platform === 'win32') {
id = id.replace(/\\/g, '/')
}

// Normalize from node_modules
const _id = id.split('node_modules/').pop()

Expand All @@ -42,7 +47,7 @@ export function externals (opts: NodeExternalsOptions): Plugin {
// Try to resolve for nft
if (opts.trace !== false) {
let _resolvedId = _id
try { _resolvedId = require.resolve(_resolvedId, { paths: opts.moduleDirectories }) } catch (_err) {}
try { _resolvedId = normalize(require.resolve(_resolvedId, { paths: opts.moduleDirectories })) } catch (_err) { }
resolvedExternals.add(_resolvedId)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/src/rollup/plugins/virtual.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Based on https://github.com/rollup/plugins/blob/master/packages/virtual/src/index.ts
import * as path from 'path'
import * as path from 'upath'

import { Plugin } from 'rollup'

Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt3/src/nitro.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalize } from 'upath'
import { wpfs, getNitroContext, createDevServer, resolveMiddleware, build, prepare, generate } from '@nuxt/nitro'
import type { Nuxt } from '@nuxt/kit'

Expand All @@ -22,7 +23,7 @@ export function initNitro (nuxt: Nuxt) {

// Add nitro client plugin (to inject $fetch helper)
nuxt.hook('app:resolve', (app) => {
app.plugins.push({ src: require.resolve('@nuxt/nitro/dist/runtime/app/nitro.client.mjs') })
app.plugins.push({ src: normalize(require.resolve('@nuxt/nitro/dist/runtime/app/nitro.client.mjs')) })
})

// Expose process.env.NITRO_PRESET
Expand Down
7 changes: 4 additions & 3 deletions packages/nuxt3/src/nuxt.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalize } from 'upath'
import Hookable from 'hookable'
import { loadNuxtConfig, LoadNuxtOptions, Nuxt, NuxtOptions, nuxtCtx, installModule, ModuleContainer } from '@nuxt/kit'
import { initNitro } from './nitro'
Expand Down Expand Up @@ -52,9 +53,9 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
const { appDir } = await import('@nuxt/app/meta')
options.appDir = appDir
options._majorVersion = 3
options.alias.vue = require.resolve('vue/dist/vue.esm-bundler.js')
options.buildModules.push(require.resolve('@nuxt/pages/module'))
options.buildModules.push(require.resolve('@nuxt/component-discovery/module'))
options.alias.vue = normalize(require.resolve('vue/dist/vue.esm-bundler.js'))
options.buildModules.push(normalize(require.resolve('@nuxt/pages/module')))
options.buildModules.push(normalize(require.resolve('@nuxt/component-discovery/module')))

const nuxt = createNuxt(options)

Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt3/src/template.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { basename, extname } from 'path'
import { basename, extname } from 'upath'
import hash from 'hash-sum'
import { camelCase } from 'scule'

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { resolve } from 'upath'
import * as vite from 'vite'
import vitePlugin from '@vitejs/plugin-vue'
import { cacheDirPlugin } from './plugins/cache-dir'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { resolve } from 'upath'
import * as vite from 'vite'
import vuePlugin from '@vitejs/plugin-vue'
import { mkdirp, writeFile } from 'fs-extra'
Expand Down
1 change: 1 addition & 0 deletions packages/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"style-resources-loader": "^1.4.1",
"time-fix-plugin": "^2.0.7",
"ufo": "^0.7.7",
"upath": "^2.0.1",
"url-loader": "^4.1.1",
"vue": "3.1.4",
"vue-loader": "^16.3.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/configs/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import querystring from 'querystring'
import path from 'upath'
import webpack from 'webpack'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'

Expand Down
3 changes: 2 additions & 1 deletion packages/webpack/src/plugins/transform-setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalize } from 'upath'
import { getQuery } from 'ufo'

export default class NuxtSetupTransformerPlugin {
Expand All @@ -10,7 +11,7 @@ export default class NuxtSetupTransformerPlugin {
enforce: 'post',
use: [{
ident: 'NuxtSetupTransformerPlugin',
loader: require.resolve('@nuxt/webpack-builder/dist/nuxt-setup-loader')
loader: normalize(require.resolve('@nuxt/webpack-builder/dist/nuxt-setup-loader'))
}]
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/plugins/vue/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* https://github.com/vuejs/vue/blob/dev/src/server/webpack-plugin/client.js
*/

import { dirname } from 'path'
import { dirname } from 'upath'
import hash from 'hash-sum'
import { uniq } from 'lodash'
import { writeFile, mkdirp } from 'fs-extra'
Expand Down
5 changes: 3 additions & 2 deletions packages/webpack/src/presets/babel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalize } from 'upath'
import TerserWebpackPlugin from 'terser-webpack-plugin'
import { reservedVueTags } from '../utils/reserved-tags'
import { WebpackConfigContext } from '../utils/config'
Expand All @@ -6,7 +7,7 @@ export function babel (ctx: WebpackConfigContext) {
const { config, options } = ctx

const babelLoader = {
loader: require.resolve('babel-loader'),
loader: normalize(require.resolve('babel-loader')),
options: getBabelOptions(ctx)
}

Expand Down Expand Up @@ -66,7 +67,7 @@ function getBabelOptions (ctx: WebpackConfigContext) {
babelOptions.plugins = babelOptions.plugins(ctx)
}

const defaultPreset = [require.resolve('../../babel-preset-app'), {}]
const defaultPreset = [normalize(require.resolve('../../babel-preset-app')), {}]

if (typeof babelOptions.presets === 'function') {
babelOptions.presets = babelOptions.presets(ctx, defaultPreset)
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/presets/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve, normalize } from 'path'
import { resolve, normalize } from 'upath'
import TimeFixPlugin from 'time-fix-plugin'
import WebpackBar from 'webpackbar'
import consola from 'consola'
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/presets/style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path'
import path from 'upath'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin'
import { fileName, WebpackConfigContext, applyPresets } from '../utils/config'
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/presets/vue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { resolve } from 'upath'
import VueLoaderPlugin from 'vue-loader/dist/pluginWebpack5'
import { DefinePlugin } from 'webpack'
import NuxtSetupTransformerPlugin from '../plugins/transform-setup'
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack/src/utils/babel-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function getPolyfills (targets, includes, { ignoreBrowserslistConfig, configPath
}

function isPackageHoisted (packageName) {
const path = require('path')
const installedPath = require.resolve(packageName)
const path = require('upath')
const installedPath = path.normalize(require.resolve(packageName))
const relativePath = path.resolve(__dirname, '..', 'node_modules', packageName)
return installedPath !== relativePath
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/utils/mfs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path'
import path from 'upath'
import pify from 'pify'
import { Volume, createFsFromVolume } from 'memfs'

Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/utils/postcss.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-nocheck
import fs from 'fs'
import path from 'path'
import path from 'upath'
import consola from 'consola'
import { defaults, merge, cloneDeep } from 'lodash'
import createResolver from 'postcss-import-resolver'
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import type { IncomingMessage, ServerResponse } from 'http'
import path from 'upath'
import pify from 'pify'
import webpack from 'webpack'
import Glob from 'glob'
Expand Down
2 changes: 1 addition & 1 deletion scripts/bump-edge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises as fsp } from 'fs'
import { resolve } from 'path'
import { execSync } from 'child_process'
import { resolve } from 'upath'
import globby from 'globby'

async function loadPackage (dir: string) {
Expand Down
2 changes: 1 addition & 1 deletion test/presets/cloudflare.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { resolve } from 'upath'
import { readFile } from 'fs-extra'
import { JSDOM } from 'jsdom'

Expand Down
2 changes: 1 addition & 1 deletion test/presets/lambda.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { resolve } from 'upath'
import { testNitroBuild, setupTest, testNitroBehavior } from './_utils'

describe('nitro:preset:lambda', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/presets/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { resolve } from 'upath'
import { testNitroBuild, startServer, setupTest, testNitroBehavior } from './_utils'

describe('nitro:preset:node', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/presets/vercel.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'path'
import { resolve } from 'upath'
import { testNitroBuild, setupTest, startServer, testNitroBehavior } from './_utils'

describe('nitro:preset:vercel', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve, dirname } from 'path'
import { existsSync, readFileSync, writeFileSync, rmSync, mkdirSync } from 'fs'
import { execSync } from 'child_process'
import { resolve, dirname } from 'upath'
import defu from 'defu'
import hash from 'object-hash'
import type { LoadNuxtOptions, NuxtConfig } from '@nuxt/kit'
Expand Down
Loading

0 comments on commit 0a3041c

Please sign in to comment.