Skip to content

Commit

Permalink
fix: load JS in CLI with dependencies as well (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Mar 7, 2024
1 parent ca4d1cb commit 6c01ee9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
14 changes: 7 additions & 7 deletions packages/cli-tools/src/configs/juno.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
} from '@junobuild/config';
import {existsSync} from 'node:fs';
import {access, readFile} from 'node:fs/promises';
import {join} from 'node:path';
import {extname, join} from 'node:path';
import type {ConfigFilename, ConfigType} from '../types/config';
import {nodeRequire} from '../utils/node.utils';

Expand Down Expand Up @@ -95,13 +95,13 @@ export const readJunoConfig = async <
const {configPath, configType} = junoConfigFile({...rest});

switch (configType) {
case 'ts': {
const {default: userConfig} = nodeRequire<ConfigFnOrObject>(configPath);
return config(userConfig);
}
case 'ts':
case 'js': {
const {default: userConfig} = await import(configPath);
return config(userConfig as ConfigFnOrObject);
const {default: userConfig} = nodeRequire<ConfigFnOrObject>({
id: configPath,
extension: extname(configPath)
});
return config(userConfig);
}
default: {
const buffer = await readFile(configPath);
Expand Down
26 changes: 8 additions & 18 deletions packages/cli-tools/src/utils/node.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import {
type JunoConfig,
type JunoDevConfig
} from '@junobuild/config';
import {readFileSync} from 'node:fs';

/**
* Adapted source from Stencil (https://github.com/ionic-team/stencil/blob/main/src/compiler/sys/node-require.ts)
*/
export const nodeRequire = <T>(id: string): {default: T} => {
export const nodeRequire = <T>({id, extension}: {id: string; extension: string}): {default: T} => {
// ensure we cleared out node's internal require() cache for this file
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete require.cache[id];
Expand All @@ -24,21 +23,12 @@ export const nodeRequire = <T>(id: string): {default: T} => {
try {
// let's override node's require for a second
// don't worry, we'll revert this when we're done
require.extensions['.ts'] = (module: NodeJS.Module, fileName: string) => {
let sourceText = readFileSync(fileName, 'utf8');

if (fileName.endsWith('.ts')) {
// looks like we've got a typed config file
// let's transpile it to .js quick
sourceText = transformFileSync(fileName, {
presets: [ts.default],
plugins: [mod.default]
}).code;
} else {
// quick hack to turn a modern es module
// into and old school commonjs module
sourceText = sourceText.replace(/export\s+\w+\s+(\w+)/gm, 'exports.$1');
}
require.extensions[extension] = (module: NodeJS.Module, fileName: string) => {
// let's transpile with Babel regardless if TS or JS
const sourceText = transformFileSync(fileName, {
presets: [ts.default],
plugins: [mod.default]
}).code;

interface NodeModuleWithCompile extends NodeModule {
// eslint-disable-next-line @typescript-eslint/method-signature-style
Expand Down Expand Up @@ -68,7 +58,7 @@ export const nodeRequire = <T>(id: string): {default: T} => {
return require(id);
} finally {
// all set, let's go ahead and reset the require back to the default
require.extensions['.ts'] = undefined;
require.extensions[extension] = undefined;

// Redo our hack
Module._load = originalLoad;
Expand Down

0 comments on commit 6c01ee9

Please sign in to comment.