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): add support for build async code #32

Merged
merged 1 commit into from
Feb 2, 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
5 changes: 5 additions & 0 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export default class Build extends BaseCommand {
options: ['production', 'prod', 'development', 'dev'],
default: 'production',
})(),
experimentalAsync: Flags.boolean({
description: 'Build async code',
default: false
}),
}

public async run(): Promise<void> {
Expand Down Expand Up @@ -101,6 +105,7 @@ export default class Build extends BaseCommand {
customWebpack: flags.webpack,
buildEntries,
isDev,
isAsync: flags.experimentalAsync,
})
if (!flags.silent) {
this.action.stop()
Expand Down
24 changes: 19 additions & 5 deletions src/lib/runWebpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { formatWebpackMessages } from '../lib/formatWebpackMessages'

export const MAX_BUILD_SIZE = 1024 * 400

const BUILD_ASYNC_CODE_TEMPLATE = `
import main from '{filePath}';
main.apply(null, globalThis.scriptArgs).then(result => {console.info(result);globalThis.scriptOutput = result});
`

const BUILD_CODE_TEMPLATE = `
import main from '{filePath}';
globalThis.scriptOutput = main.apply(null, globalThis.scriptArgs);
Expand Down Expand Up @@ -72,13 +77,16 @@ const getBaseConfig = (
},
})


function modifyFilePath(filePath: string) {
const parsedPath = path.parse(filePath.replace(/([^/]+)$/, '_$1'))
function removeExtension(filePath: string) {
const parsedPath = path.parse(filePath)
const newPath = path.join(parsedPath.dir, parsedPath.name)
return newPath
}

function modifyTargetPath(filePath: string) {
return removeExtension(filePath.replace(/([^/]+)$/, '_$1'))
}

export async function runWebpack({
buildEntries,
projectDir,
Expand All @@ -87,6 +95,7 @@ export async function runWebpack({
customWebpack,
isDev = false,
clean = false,
isAsync = false
}: {
buildEntries: Configuration['entry'],
projectDir: string,
Expand All @@ -95,13 +104,18 @@ export async function runWebpack({
customWebpack?: string,
isDev: boolean,
clean: boolean,
isAsync?: boolean
}): Promise<Stats> {
const build_code_template = isAsync ? BUILD_ASYNC_CODE_TEMPLATE : BUILD_CODE_TEMPLATE
const virtualModules = new VirtualModulesPlugin(Object.entries(buildEntries || {}).reduce((acc, [, value]) => {
acc[path.join(projectDir, modifyFilePath(value))] = BUILD_CODE_TEMPLATE.replace(/{filePath}/g, upath.join(projectDir, value))
acc[path.join(projectDir, modifyTargetPath(value))] = build_code_template.replace(
/{filePath}/g,
upath.join(projectDir, removeExtension(value)),
)
return acc
}, {} as Record<string, string>))
const newBuildEntries = Object.entries(buildEntries || {}).reduce((acc, [key, value]) => {
acc[key] = upath.join(projectDir, modifyFilePath(value))
acc[key] = upath.join(projectDir, modifyTargetPath(value))
return acc
}, {} as Record<string, string>)

Expand Down
Loading