Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): enable env sourcemap #4676

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ TODOs.md
*.log
.history
scripts/package.json
*.map
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions packages/uni-h5/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ if (FORMAT === 'es') {
})
}

const enableSourcemap = process.env.ENABLE_SOURCEMAP === 'true'

export default defineConfig({
root: __dirname,
define: {
Expand Down Expand Up @@ -150,5 +152,6 @@ export default defineConfig({
}
},
},
sourcemap: enableSourcemap,
},
})
36 changes: 23 additions & 13 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -35,7 +37,8 @@ function normalizeOutput(file, output = {}) {
file,
format: file.includes('.cjs.') ? 'cjs' : 'es',
exports: 'auto',
interop: 'auto'
interop: 'auto',
sourcemap: enableSourcemap,
},
output
)
Expand Down Expand Up @@ -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: {
Expand All @@ -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 || {}),
Expand Down Expand Up @@ -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
},
},
},
}
}

Expand All @@ -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,
})
}
26 changes: 23 additions & 3 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)
Expand All @@ -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',
})
Expand All @@ -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(','),
],
Expand Down
Loading