Skip to content

Commit

Permalink
feat(build script): generate sourcemap for developer (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiESAEX authored Dec 23, 2022
1 parent a701b7c commit 14d786e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 7 additions & 2 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions scripts/compiler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function transform(code, options) {
return result.code;
}

function transformNodeCode(code) {
function transformNodeCode(code, config) {
return transform(code, {
presets: [
[
Expand All @@ -19,6 +19,8 @@ function transformNodeCode(code) {
},
],
],
sourceFileName: config.sourceMap ? config.sourceFileName : undefined,
sourceMaps: config.sourceMap ? 'inline' : false,
});
}

Expand All @@ -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);
Expand Down

0 comments on commit 14d786e

Please sign in to comment.