Skip to content

Commit

Permalink
feat: add it as flag
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhdaware committed Nov 6, 2024
1 parent d3a9bb3 commit ade7e44
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
7 changes: 7 additions & 0 deletions packages/abell/src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export type AbellOptions = {
* E.g. if you want to use top-level await in entry.build.ts, you can set target here without having to change target of your final client bundle
*/
serverBuild?: ViteUserConfig['build'];

/**
* Allows `.abell` files to be imported in client-side code.
*
* Abell variables like `Abell.root`, `__dirname`, `__filename` etc return empty string when this flag is set
*/
experimentalAllowClientSide?: boolean;
};

export interface AbellViteConfig extends ViteUserConfig {
Expand Down
14 changes: 8 additions & 6 deletions packages/abell/src/vite-plugin-abell/compiler/compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ describe('compile()', () => {
});
expect(js.trim().replace(`\\\\test.abell`, '/test.abell'))
.toMatchInlineSnapshot(`
"import { evaluateAbellBlock as e } from 'abell/dist/utils/evaluateAbellBlock';
"import { default as _path } from 'path';
import { evaluateAbellBlock as e } from 'abell/dist/utils/evaluateAbellBlock';
const __filename = \\"/test.abell\\";
const __dirname = \\"/\\";
const root = \\"\\"
const __dirname = _path.dirname(__filename);
const root = _path.relative(__dirname, \\"/\\");
export const html = (props = {}) => {
const Abell = { props, __filename, __dirname };
Expand Down Expand Up @@ -93,13 +94,14 @@ describe('compile()', () => {
});
expect(js.trim().replace(`\\\\test.abell`, '/test.abell'))
.toMatchInlineSnapshot(`
"import { evaluateAbellBlock as e } from 'abell/dist/utils/evaluateAbellBlock';
"import { default as _path } from 'path';
import { evaluateAbellBlock as e } from 'abell/dist/utils/evaluateAbellBlock';
import x from './x';
const __filename = \\"/test.abell\\";
const __dirname = \\"/\\";
const root = \\"\\"
const __dirname = _path.dirname(__filename);
const root = _path.relative(__dirname, \\"/\\");
export const html = (props = {}) => {
const Abell = { props, __filename, __dirname };
Expand Down
19 changes: 13 additions & 6 deletions packages/abell/src/vite-plugin-abell/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface CompileOptions {
filepath: string;
cwd?: string;
outputType?: 'js-string' | 'syntax-blocks';
isClientSide?: boolean;
}

interface HTMLOutputCompileOptions extends CompileOptions {
Expand Down Expand Up @@ -77,15 +78,21 @@ export function compile(
};
}

const __filename = options.filepath;
const __dirname = path.dirname(options.filepath);

const jsOut = `
${options.isClientSide ? '' : `import { default as _path } from 'path';`}
import { evaluateAbellBlock as e } from 'abell/dist/utils/evaluateAbellBlock';
${importBlock.text}
const __filename = ${JSON.stringify(__filename)};
const __dirname = ${JSON.stringify(__dirname)};
const root = ${JSON.stringify(path.relative(__dirname, options.cwd ?? ''))}
const __filename = ${
options.isClientSide ? '"";' : JSON.stringify(options.filepath)
};
const __dirname = ${
options.isClientSide ? '"";' : '_path.dirname(__filename);'
}
const root = ${
options.isClientSide
? '"";'
: `_path.relative(__dirname, ${JSON.stringify(options.cwd)});`
}
export const html = (props = {}) => {
const Abell = { props, __filename, __dirname };
${declarationBlocks.text}
Expand Down
3 changes: 2 additions & 1 deletion packages/abell/src/vite-plugin-abell/vite-plugin-abell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export function vitePluginAbell(abellOptions?: AbellOptions): PluginOption {
if (id.endsWith('.abell')) {
const jsCode = compile(src, {
filepath: id,
cwd: process.cwd()
cwd: process.cwd(),
isClientSide: abellOptions?.experimentalAllowClientSide ?? false
});
let outCode = jsCode;
// If loader is not defined, skip the esbuild transform
Expand Down

0 comments on commit ade7e44

Please sign in to comment.