diff --git a/.gitignore b/.gitignore index d5633cc636b..df078546e5b 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ TODOs.md *.log .history scripts/package.json +*.map \ No newline at end of file diff --git a/package.json b/package.json index 3c4a5fd5084..78299e57c27 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "clean": "rm -rf node_modules **/*/node_modules && pnpm install", "build": "node scripts/build.js", "build:h5": "node scripts/build.js uni-app uts uni-uts-v1 uni-app-uvue uni-cli-shared uni-h5 uni-i18n uni-stat uni-shared uni-h5-vite vite-plugin-uni", + "build:h5:sourcemap": "ENABLE_SOURCEMAP=true node scripts/build.js uni-app uts uni-uts-v1 uni-app-uvue uni-cli-shared uni-h5 uni-i18n uni-stat uni-shared uni-h5-vite vite-plugin-uni", "build:app": "node scripts/build.js uni-app-plus uni-app-vite uni-app-vue uni-app-uvue", "build:mp": "node scripts/build.js uni-mp-vue uni-mp-vite uni-mp-compiler uni-mp-alipay uni-mp-baidu uni-mp-kuaishou uni-mp-lark uni-mp-qq uni-mp-toutiao uni-mp-weixin uni-mp-xhs uni-quickapp-webview", "size": "npm run build size-check", diff --git a/packages/uni-h5/vite.config.ts b/packages/uni-h5/vite.config.ts index 205e37db174..8f58d320482 100644 --- a/packages/uni-h5/vite.config.ts +++ b/packages/uni-h5/vite.config.ts @@ -61,6 +61,8 @@ if (FORMAT === 'es') { }) } +const enableSourcemap = process.env.ENABLE_SOURCEMAP === 'true' + export default defineConfig({ root: __dirname, define: { @@ -150,5 +152,6 @@ export default defineConfig({ } }, }, + sourcemap: enableSourcemap, }, }) diff --git a/rollup.config.mjs b/rollup.config.mjs index c491eb82653..623a8f34960 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -22,6 +22,8 @@ const packageDir = path.resolve(packagesDir, process.env.TARGET) const resolve = (p) => path.resolve(packageDir, p) const pkg = require(resolve(`package.json`)) +const enableSourcemap = process.env.ENABLE_SOURCEMAP === 'true' + // ensure TS checks only once for each build let hasTSChecked = false @@ -35,7 +37,8 @@ function normalizeOutput(file, output = {}) { file, format: file.includes('.cjs.') ? 'cjs' : 'es', exports: 'auto', - interop: 'auto' + interop: 'auto', + sourcemap: enableSourcemap, }, output ) @@ -86,8 +89,11 @@ function resolveTsconfigJson() { function createConfig(entryFile, output, buildOption) { const shouldEmitDeclarations = process.env.TYPES != null && !hasTSChecked const tsOptions = { - check: !process.env.TRANSPILE_ONLY && - (!process.env.CI && process.env.NODE_ENV === 'production' && !hasTSChecked), + check: + !process.env.TRANSPILE_ONLY && + !process.env.CI && + process.env.NODE_ENV === 'production' && + !hasTSChecked, tsconfig: resolveTsconfigJson(), cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'), tsconfigOverride: { @@ -113,8 +119,8 @@ function createConfig(entryFile, output, buildOption) { buildOption.external === false ? [] : Array.isArray(buildOption.external) - ? buildOption.external - : [ + ? buildOption.external + : [ 'vue', '@vue/shared', ...Object.keys(pkg.dependencies || {}), @@ -176,14 +182,14 @@ function createConfig(entryFile, output, buildOption) { buildOption.treeshake === false ? false : { - moduleSideEffects(id) { - if (id.endsWith('polyfill.ts')) { - console.log('[WARN]:sideEffects[' + id + ']') - return true - } - return false + moduleSideEffects(id) { + if (id.endsWith('polyfill.ts')) { + console.log('[WARN]:sideEffects[' + id + ']') + return true + } + return false + }, }, - }, } } @@ -209,5 +215,9 @@ function createReplacePlugin(buildOption, format) { replacements[key] = process.env[key] } }) - return replace({ delimiters: ['', ''], values: replacements, preventAssignment: true }) + return replace({ + delimiters: ['', ''], + values: replacements, + preventAssignment: true, + }) } diff --git a/scripts/build.js b/scripts/build.js index cda52193387..833ce4c508a 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -155,7 +155,11 @@ async function build(target) { ['build', '--config', path.resolve(pkgDir, 'vite.config.ts')], { stdio: 'inherit', - env: Object.assign({ FORMAT: 'es', UNI_APP_X: 'true' }, process.env, env), + env: Object.assign( + { FORMAT: 'es', UNI_APP_X: 'true' }, + process.env, + env + ), cwd: pkgDir, } ) @@ -164,21 +168,32 @@ async function build(target) { ['build', '--config', path.resolve(pkgDir, 'vite.config.ts')], { stdio: 'inherit', - env: Object.assign({ FORMAT: 'cjs', UNI_APP_X: 'true' }, process.env, env), + env: Object.assign( + { FORMAT: 'cjs', UNI_APP_X: 'true' }, + process.env, + env + ), cwd: pkgDir, } ) } } if (hasTscBundler) { + const enableSourceMap = process.env.ENABLE_SOURCEMAP === 'true' const args = [ '--listEmittedFiles', '-p', + // enable sourcemap path.resolve(pkgDir, 'tsconfig.json'), ] if (types) { args.push('--declaration') } + + if (enableSourceMap) { + args.push('--sourceMap') + } + await execa('tsc', args, { stdio: 'inherit', }) @@ -189,7 +204,12 @@ async function build(target) { [ '-c', '--environment', - [`NODE_ENV:${env}`, types ? `TYPES:true` : ``, `TARGET:${target}`, transpileOnly ? `TRANSPILE_ONLY:true` : ``] + [ + `NODE_ENV:${env}`, + types ? `TYPES:true` : ``, + `TARGET:${target}`, + transpileOnly ? `TRANSPILE_ONLY:true` : ``, + ] .filter(Boolean) .join(','), ],