提交 8cf16992 编写于 作者: fxy060608's avatar fxy060608

feat(h5): respect import.meta.env.BASE_URL

上级 e92bbf82
module.exports = [
{
path: 'packages/size-check/dist/assets',
running: false,
},
{
path: 'packages/uni-h5-vue/dist',
running: false,
},
{
path: 'packages/uni-mp-vue/dist',
running: false,
},
{
path: 'packages/uni-mp-alipay/dist',
running: false,
},
{
path: 'packages/uni-mp-baidu/dist',
running: false,
},
{
path: 'packages/uni-mp-qq/dist',
running: false,
},
{
path: 'packages/uni-mp-toutiao/dist',
running: false,
},
{
path: 'packages/uni-mp-weixin/dist',
running: false,
},
{
path: 'packages/uni-quickapp-webview/dist',
running: false,
},
]
......@@ -6,6 +6,11 @@ declare var __PLATFORM__: UniApp.PLATFORM
declare var __PLATFORM_PREFIX__: 'wx' | 'qq' | 'my' | 'swan' | 'tt' | 'qa'
declare var __GLOBAL__: Record<string, any>
// importMeta(es|cjs)
declare var __IMPORT_META_ENV_BASE_URL__:
| 'import.meta.env.BASE_URL'
| '__IMPORT_META_ENV_BASE_URL__'
// Global compile-time constants
declare var __DEV__: boolean
declare var __TEST__: boolean
......
{
"name" : "hello",
"appid" : "__UNI__4DCD124",
"description" : "",
"versionName" : "1.0.0",
"versionCode" : "100",
"transformPx" : false,
"name": "hello",
"appid": "__UNI__4DCD124",
"description": "",
"versionName": "1.0.0",
"versionCode": "100",
"transformPx": false,
/* 5+App特有相关 */
"app-plus" : {
"usingComponents" : true,
"nvueStyleCompiler" : "uni-app",
"compilerVersion" : 3,
"splashscreen" : {
"alwaysShowBeforeRender" : true,
"waiting" : true,
"autoclose" : true,
"delay" : 0
"app-plus": {
"usingComponents": true,
"nvueStyleCompiler": "uni-app",
"compilerVersion": 3,
"splashscreen": {
"alwaysShowBeforeRender": true,
"waiting": true,
"autoclose": true,
"delay": 0
},
/* 模块配置 */
"modules" : {},
"modules": {},
/* 应用发布信息 */
"distribute" : {
"distribute": {
/* android打包配置 */
"android" : {
"permissions" : [
"android": {
"permissions": [
"<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>",
"<uses-permission android:name=\"android.permission.VIBRATE\"/>",
......@@ -41,29 +41,29 @@
]
},
/* ios打包配置 */
"ios" : {},
"ios": {},
/* SDK配置 */
"sdkConfigs" : {}
"sdkConfigs": {}
}
},
/* 快应用特有相关 */
"quickapp" : {},
"quickapp": {},
/* 小程序特有相关 */
"mp-weixin" : {
"appid" : "",
"setting" : {
"urlCheck" : false
"mp-weixin": {
"appid": "",
"setting": {
"urlCheck": false
},
"usingComponents" : true
"usingComponents": true
},
"mp-alipay" : {
"usingComponents" : true
"mp-alipay": {
"usingComponents": true
},
"mp-baidu" : {
"usingComponents" : true
"mp-baidu": {
"usingComponents": true
},
"mp-toutiao" : {
"usingComponents" : true
"mp-toutiao": {
"usingComponents": true
},
"uniStatistics": {
"enable": false
......
......@@ -1917,9 +1917,9 @@ postcss-modules@^4.0.0:
string-hash "^1.1.1"
postcss-selector-parser@^6.0.2, postcss-selector-parser@^6.0.4:
version "6.0.5"
resolved "https://registry.yarnpkg.com/postcss-selector-parser/-/postcss-selector-parser-6.0.5.tgz#042d74e137db83e6f294712096cb413f5aa612c4"
integrity sha512-aFYPoYmXbZ1V6HZaSvat08M97A8HqO6Pjz+PiNpw/DhuRrC72XWAdp3hL6wusDCN31sSmcZyMGa2hZEuX+Xfhg==
version "6.0.6"
resolved "https://registry.yarnpkg.com/postcss-selector-parser/-/postcss-selector-parser-6.0.6.tgz#2c5bba8174ac2f6981ab631a42ab0ee54af332ea"
integrity sha512-9LXrvaaX3+mcv5xkg5kFwqSzSH1JIObIx51PrndZwlmznwXRfxMddDvo9gve3gVR8ZTKgoFDdWkbRFmEhT4PMg==
dependencies:
cssesc "^3.0.0"
util-deprecate "^1.0.2"
......
此差异已折叠。
此差异已折叠。
import { ComponentPublicInstance } from 'vue'
import { getRealRoute } from '@dcloudio/uni-core'
declare global {
interface ImportMeta {
env: {
BASE_URL: string
}
}
}
export function findElem(vm: ComponentPublicInstance) {
return vm.$el
}
......@@ -8,18 +16,9 @@ export function findElem(vm: ComponentPublicInstance) {
const SCHEME_RE = /^([a-z-]+:)?\/\//i
const DATA_RE = /^data:.*,.*/
const baseUrl = __IMPORT_META_ENV_BASE_URL__
function addBase(filePath: string) {
const base = __uniConfig.router.base
if (!base) {
return filePath
}
if (base !== '/') {
// 部分地址已经带了base(如被webpack处理过的资源自动带了publicPath)
if (('/' + filePath).indexOf(base) === 0) {
return '/' + filePath
}
}
return base + filePath
return baseUrl + filePath
}
export function getRealPath(filePath: string) {
......
......@@ -30,6 +30,10 @@ const rollupPlugins = [
defineTaskApi: `/*#__PURE__*/ defineTaskApi`,
defineSyncApi: `/*#__PURE__*/ defineSyncApi`,
defineAsyncApi: `/*#__PURE__*/ defineAsyncApi`,
__IMPORT_META_ENV_BASE_URL__:
FORMAT === 'cjs'
? '__IMPORT_META_ENV_BASE_URL__'
: 'import.meta.env.BASE_URL', //直接使用import.meta.env.BASE_URL会被vite替换成'/'
},
preventAssignment: true,
}),
......@@ -90,6 +94,7 @@ export default defineConfig({
vueJsx({ optimize: true, isCustomElement }),
],
build: {
target: 'es2020', // keep import.meta...
emptyOutDir: FORMAT === 'es',
minify: false,
lib: {
......
......@@ -84,8 +84,16 @@ export async function createSSRServer(options: CliOptions & ServerOptions) {
? 'https'
: 'http'
let port = options.port || serverOptions.port || 3000
const hostname = options.host
let hostname: string | undefined
if (options.host === undefined || options.host === 'localhost') {
// Use a secure default
hostname = '127.0.0.1'
} else if (options.host === true) {
// probably passed --host in the CLI, without arguments
hostname = undefined // undefined typically means 0.0.0.0 or :: (listen on all IPs)
} else {
hostname = options.host as string
}
return new Promise((resolve, reject) => {
const onSuccess = () => {
const interfaces = os.networkInterfaces()
......
......@@ -2,7 +2,7 @@ import path from 'path'
import { extend } from '@vue/shared'
import { Plugin, UserConfig } from 'vite'
import { normalizePath } from '@dcloudio/uni-cli-shared'
import { normalizePath, parseManifestJsonOnce } from '@dcloudio/uni-cli-shared'
import { VitePluginUniResolvedOptions } from '..'
import { createCss } from './css'
......@@ -28,7 +28,9 @@ export function createConfig(
options.inputDir = normalizeInputDir(config)
options.compiler.init()
const define = createDefine(options, config, env)
const { h5 } = parseManifestJsonOnce(options.inputDir)
return {
base: (h5 && h5.router && h5.router.base) || '',
define: extend(define, options.compiler.define()),
resolve: createResolve(options, config),
optimizeDeps: createOptimizeDeps(options),
......
......@@ -5,6 +5,7 @@ export function createServer(
_options: VitePluginUniResolvedOptions
): UserConfig['server'] {
return {
host: true,
watch: {
ignored: ['**/uniCloud**'],
},
......
import { ResolvedConfig } from 'vite'
import { isSsr, rewriteSsrNativeTag, rewriteSsrRenderStyle } from '../utils'
import {
isSsr,
initSsrDefine,
rewriteSsrNativeTag,
rewriteSsrRenderStyle,
} from '../utils'
// import alias from 'module-alias'
export function initConfig(config: ResolvedConfig) {
if (isSsr(config.command, config)) {
initSsrDefine(config)
rewriteSsrNativeTag()
rewriteSsrRenderStyle(process.env.UNI_INPUT_DIR)
}
......
import path from 'path'
import fs from 'fs-extra'
import { isString, NormalizedStyle } from '@vue/shared'
import { extend, isString, NormalizedStyle } from '@vue/shared'
import {
isNativeTag,
createRpx2Unit,
......@@ -32,6 +32,10 @@ export function isSsrManifest(
return false
}
export function initSsrDefine(config: ResolvedConfig) {
extend(globalThis, { __IMPORT_META_ENV_BASE_URL__: config.env.BASE_URL })
}
function serializeDefine(define: Record<string, any>): string {
let res = `{`
for (const key in define) {
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册