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: 🎸 sourcemap exclude vendors and auto turn on only for dev #194

Merged
merged 3 commits into from
Aug 26, 2024
Merged
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 packages/cli/bin/merkur.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ program
.name('merkur')
.description('CLI for Merkur framework.')
.option('--writeToDisk', 'Write built files to disk.')
.option('--sourcemap', 'Generate sourcemap.')
.option('--runTask [runTask...]', 'Run only defined task.')
.option('--outFile <string>', 'Server out file configuration in es-build.')
.option('--port <number>', 'Widget server port.')
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/CLIConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function createCLIConfig({ args, command } = {}) {
command: command ?? 'unknown',
watch: args?.watch ?? !isProduction,
writeToDisk: args?.writeToDisk ?? isProduction,
sourcemap: args?.sourcemap ?? !isProduction,
outFile: args?.outFile ?? './build/widget.cjs',
port: args?.port ?? 4444,
runTask: args?.runTask ?? [],
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/buildConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import manifestPlugin from 'esbuild-plugin-manifest';
import { EMITTER_EVENTS, emitter, RESULT_KEY } from './emitter.mjs';
import { memoryStaticPlugin } from './plugins/memoryStaticPlugin.mjs';
import { metaPlugin } from './plugins/metaPlugin.mjs';
import { excludeVendorsFromSourceMapPlugin } from './plugins/excludeVendorsFromSourceMapPlugin.mjs';

export async function createBuildConfig({
definition,
Expand All @@ -13,7 +14,7 @@ export async function createBuildConfig({
cliConfig,
context,
}) {
const { isServer, writeToDisk } = config;
const { isServer, writeToDisk, sourcemap } = config;
const { isProduction, outFile, staticFolder, projectFolder } = cliConfig;

const entries = await getEntries({ merkurConfig, cliConfig });
Expand All @@ -23,7 +24,7 @@ export async function createBuildConfig({
entryPoints: isServer ? entries.server : entries.client,
bundle: true,
treeShaking: isProduction,
sourcemap: true,
sourcemap,
minify: isProduction,
target: 'es2022',
write: writeToDisk,
Expand Down Expand Up @@ -60,6 +61,7 @@ export async function createBuildConfig({
},
}),
!writeToDisk && memoryStaticPlugin,
sourcemap && excludeVendorsFromSourceMapPlugin,
metaPlugin,
...(definition.build?.plugins ?? []),
]
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/src/plugins/excludeVendorsFromSourceMapPlugin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from 'node:fs/promises';

export function excludeVendorsFromSourceMapPlugin() {
return {
name: 'excludeVendorsFromSourceMapPlugin',
setup(build) {
build.onLoad({ filter: /node_modules/ }, async (args) => {
const contents = await fs.readFile(args.path, { encoding: 'utf8' });

return {
contents:
contents +
'\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==',
loader: 'default',
};
});
},
};
}
1 change: 1 addition & 0 deletions packages/cli/src/taskConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export async function createTaskConfig({
config: {
isServer: definition?.build?.platform === 'node',
writeToDisk: definition?.build?.write ?? cliConfig.writeToDisk,
sourcemap: definition?.build?.sourcemap ?? cliConfig.sourcemap,
...definition.config,
},
definition,
Expand Down