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

add vite plugin #329

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"jsx.js",
"loader.js",
"macro.js",
"carmiRoot.js"
"carmiRoot.js",
"vite.js"
],
"publishConfig": {
"registry": "https://registry.npmjs.org/"
Expand Down Expand Up @@ -63,7 +64,8 @@
"resolve": "^1.10.0",
"tempy": "^0.2.1",
"toposort": "^1.0.6",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"vite": "2.9.13"
},
"devDependencies": {
"babel-plugin-tester": "^5.5.1",
Expand Down
40 changes: 40 additions & 0 deletions src/vitePlugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const babel = require('@babel/core');
const carmiBabelPlugin = require('../babelPlugin');
const compileFile = require('../babelPlugin/compileFile');

const isCarmiFile = (id) => /\.carmi\.(js|mjs)$/.test(id);

function carmiVitePlugin(options = {}) {
return {
name: 'vite-plugin-carmi',
enforce: 'pre',

async transform(code, id) {
if (!isCarmiFile(id)) {
return null;
}

const babelResult = await babel.transformAsync(code, {
filename: id,
plugins: [carmiBabelPlugin],
ast: true
});

if (!babelResult || !babelResult.code) {
return null;
}

const compiledCode = compileFile(id, {
isDebug: options.debug,
disableCurrentLineFunctionName: options.disableCurrentLineFunctionName
});

return {
code: compiledCode,
map: babelResult.map
};
}
};
}

module.exports = carmiVitePlugin;
4 changes: 4 additions & 0 deletions src/vitePlugin/test.carmi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @carmi

const {root} = require('../../index');
module.exports = {first: root.get(0), sum: root.call('sum')};
94 changes: 94 additions & 0 deletions src/vitePlugin/vite-plugin.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const vitePlugin = require('./index');

describe('Carmi Vite Plugin', () => {
let mockPluginContext;


beforeEach(() => {
mockPluginContext = {
addWatchFile: jest.fn(),
emitFile: jest.fn()
};
jest.clearAllMocks();
});

const getCarmiTestFile = async (plugin, mockCode) => await plugin.transform.call(mockPluginContext, mockCode, './src/vitePlugin/test.carmi.js');

it('transforms .carmi.js files', async () => {
const plugin = vitePlugin();
const mockCode = `
const { root } = require('carmi');
module.exports = { first: root.get(0) };
`;

const result = await getCarmiTestFile(plugin, mockCode);

expect(result).toBeDefined();
expect(typeof result.code).toBe('string');
expect(result.code).toContain('function model');
});

it('does not transform regular .js files', async () => {
const plugin = vitePlugin();
const mockCode = `
const regularJsCode = 'Hello, World!';
`;

const result = await plugin.transform.call(mockPluginContext, mockCode, 'regular.js');

expect(result).toBeNull();
});

it('transforms files with @carmi comment', async () => {
const plugin = vitePlugin();
const mockCode = `
// @carmi
const { root } = require('carmi');
module.exports = { first: root.get(0) };
`;

const result = await getCarmiTestFile(plugin, mockCode);

expect(result).toBeDefined();
expect(result.code).toContain('function model');
});

it('does not transform files without .carmi.js extension or @carmi comment', async () => {
const plugin = vitePlugin();
const mockCode = `
const { root } = require('carmi');
module.exports = { first: root.get(0) };
`;

const result = await plugin.transform.call(mockPluginContext, mockCode, 'test.js');

expect(result).toBeNull();
});

it('handles carmi compiler options', async () => {
const customOptions = {optimize: true};
const plugin = vitePlugin(customOptions);
const mockCode = `
// @carmi
const { root } = require('carmi');
module.exports = { first: root.get(0) };
`;

const result = await getCarmiTestFile(plugin, mockCode);

expect(result).toBeDefined();
expect(typeof result.code).toBe('string');
});

it('handles syntax errors gracefully', async () => {
const plugin = vitePlugin();
const mockCode = `
// @carmi
const { root } = require('carmi');
module.exports = { first: root.get(0 }; // Missing closing parenthesis
`;

await expect(plugin.transform.call(mockPluginContext, mockCode, 'test.carmi.js'))
.rejects.toThrow();
});
});
19 changes: 19 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {defineConfig} = require('vite');
const carmiPlugin = require('./src/vitePlugin');
const {resolve} = require('path');

module.exports = api => defineConfig({
plugins: [carmiPlugin()],
build: {
outDir: 'dist',
rollupOptions: {
input: {
main: resolve(__dirname, 'test.carmi.js')
},
output: {
dir: 'dist',
format: 'es'
}
}
}
});
1 change: 1 addition & 0 deletions vite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./src/vitePlugin');
Loading
Loading