Skip to content

Commit

Permalink
adding esm_aliases and esm_externals
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Apr 22, 2024
1 parent b1aa307 commit 58a42cd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/nest/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"content": "azle"
}
],
"npm_external": ["@nestjs/microservices", "@nestjs/websockets"]
"esm_externals": ["@nestjs/microservices", "@nestjs/websockets"]
}
}
}
14 changes: 9 additions & 5 deletions src/compiler/compile_typescript_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { JavaScript, TypeScript } from './utils/types';
export async function compileTypeScriptToJavaScript(
main: string,
wasmedgeQuickJsPath: string,
npmExternal: string[]
esmAliases: Record<string, string>,
esmExternals: string[]
): Promise<Result<JavaScript, unknown>> {
try {
const imports = `
Expand Down Expand Up @@ -64,7 +65,8 @@ export async function compileTypeScriptToJavaScript(
${imports}
`,
wasmedgeQuickJsPath,
npmExternal
esmAliases,
esmExternals
);

return {
Expand All @@ -78,7 +80,8 @@ export async function compileTypeScriptToJavaScript(
export async function bundleFromString(
ts: TypeScript,
wasmedgeQuickJsPath: string,
npmExternal: string[]
esmAliases: Record<string, string>,
esmExternals: string[]
): Promise<JavaScript> {
const finalWasmedgeQuickJsPath =
process.env.AZLE_WASMEDGE_QUICKJS_DIR ?? wasmedgeQuickJsPath;
Expand All @@ -98,7 +101,7 @@ export async function bundleFromString(

// These are modules that should not be included in the build from the developer side
// These are specified in the dfx.json canister object npm_external property
const externalNotImplementedDev = npmExternal;
const externalNotImplementedDev = esmExternals;

// These will cause runtime errors if their functionality is dependend upon
const externalNotImplemented = [
Expand Down Expand Up @@ -147,7 +150,8 @@ export async function bundleFromString(
__dirname,
'custom_js_modules/async_hooks.ts'
),
https: path.join(__dirname, 'custom_js_modules/https.ts')
https: path.join(__dirname, 'custom_js_modules/https.ts'),
...esmAliases
},
external: [...externalImplemented, ...externalNotImplemented],
plugins: [esbuildPluginTsc()]
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/get_canister_javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
export async function getCanisterJavaScript(
mainPath: string,
wasmedgeQuickJsPath: string,
npmExternal: string[]
esmAliases: Record<string, string>,
esmExternals: string[]
): Promise<Result<string, AzleError>> {
const typeScriptCompilationResult = await compileTypeScriptToJavaScript(
mainPath,
wasmedgeQuickJsPath,
npmExternal
esmAliases,
esmExternals
);

if (!ok(typeScriptCompilationResult)) {
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/get_names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export function getNamesAfterCli() {
'main_reloaded.js'
);

const npmExternal = canisterConfig.npm_external ?? [];
const esmAliases = canisterConfig.esm_aliases ?? {};
const esmExternals = canisterConfig.esm_externals ?? [];

return {
canisterName,
Expand All @@ -108,7 +109,8 @@ export function getNamesAfterCli() {
rustStagingWasmPath,
canisterId,
reloadedJsPath,
npmExternal
esmAliases,
esmExternals
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ async function azle() {
rustStagingWasmPath,
canisterId,
reloadedJsPath,
npmExternal
esmAliases,
esmExternals
} = getNamesAfterCli();

setupFileWatcher(
Expand Down Expand Up @@ -84,7 +85,8 @@ async function azle() {
await getCanisterJavaScript(
canisterConfig.main,
wasmedgeQuickJsPath,
npmExternal
esmAliases,
esmExternals
)
);

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export type JSCanisterConfig = Readonly<{
opt_level?: OptLevel;
assets?: [string, string][];
assets_large?: [string, string][];
npm_external?: string[];
esm_aliases?: Record<string, string>;
esm_externals?: string[];
}>;

export type OptLevel = '0' | '1' | '2' | '3' | '4';
Expand Down

0 comments on commit 58a42cd

Please sign in to comment.