diff --git a/scripts/build.mjs b/scripts/build.mjs index 4474c208..ed5b4e7d 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -9,11 +9,10 @@ import chalk from 'chalk'; import merge from 'deepmerge'; import chokidar from 'chokidar'; import yargsParser from 'yargs-parser'; +import buildConfig from '../build.config.js'; import compiler from './compiler.mjs'; import randomColor from './randomColor.mjs'; -import buildConfig from '../build.config.js'; - const argv = yargsParser(process.argv.slice(2)); const ESM_OUTPUT_DIR = 'es'; @@ -67,6 +66,9 @@ function getGlobalConfig() { async function getPkgConfig(config, pkgName) { const pkgConfigPath = path.join(getPkgPath(pkgName), CONFIG_FILE_NAME); + if (argv.watch) { + config.sourceMap = true; + } if (fs.existsSync(pkgConfigPath)) { const content = await import(process.platform === 'win32' ? `file://${pkgConfigPath}` : pkgConfigPath); const result = merge(config, content.default); @@ -109,6 +111,9 @@ function transformFile(filePath, outputPath, config, log) { try { const code = fs.readFileSync(filePath, 'utf-8'); const shortFilePath = genShortPath(filePath); + if (config.sourceMap) { + config.sourceFileName = filePath; + } const transformedCode = compiler(code, config); const type = config.target === 'browser' ? ESM_OUTPUT_DIR : NODE_CJS_OUTPUT_DIR; diff --git a/scripts/compiler.mjs b/scripts/compiler.mjs index 6c9e173a..70cb530e 100644 --- a/scripts/compiler.mjs +++ b/scripts/compiler.mjs @@ -8,7 +8,7 @@ function transform(code, options) { return result.code; } -function transformNodeCode(code) { +function transformNodeCode(code, config) { return transform(code, { presets: [ [ @@ -19,6 +19,8 @@ function transformNodeCode(code) { }, ], ], + sourceFileName: config.sourceMap ? config.sourceFileName : undefined, + sourceMaps: config.sourceMap ? 'inline' : false, }); } @@ -41,7 +43,7 @@ function transformBrowserCode(code) { export default function compiler(code, config) { if (!config.target || config.target === 'node') { - return transformNodeCode(code); + return transformNodeCode(code, config); } if (config.target === 'browser') { return transformBrowserCode(code);