Skip to content

Commit 8aec22a

Browse files
committed
add vite support
1 parent e835060 commit 8aec22a

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ import { List } from './components/List/List'
5959

6060
## Options
6161

62-
| **Name** | **Type** | **Default** | **Description** |
63-
|:-------------------:|:---------:|:-------------------------------------------:|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
64-
| `webpackAlias` | `object` | `{}` | It should be assigned with the `alias` value option from the Webpack config. |
65-
| `jestAlias` | `array` | `[]` | It should be assigned with the `moduleNameMapper` value option from the Jest config. |
66-
| `jestExtensions` | `array` | `["js", "jsx", "ts", "tsx"]` | It should be assigned with the `moduleFileExtensions` value option from the Jest config. |
67-
| `webpackExtensions` | `array` | `[".js", ".jsx", ".ts", ".tsx"]` | It should be assigned with the `extensions` value option from the Webpack config. |
68-
| `isCacheEnabled` | `boolean` | `false` | If `true`, enables file-based cache. |
69-
| `logging` | `object` | `{ type: "disabled", filePath: "log.txt" }` | Specifies logging options.<br>`type` can be `disabled` for no logging, `file` for logs to a file, or `screen` for logs to the console.<br>If type is `file`, `filePath` specifies the log file path. |
62+
| **Name** | **Type** | **Default** | **Description** |
63+
|:-------------------:|:---------:|:---------------------------------------------------------:|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
64+
| `webpackAlias` | `object` | `{}` | It should be assigned with the `alias` value option from the Webpack config. |
65+
| `viteAlias` | `object` | `{}` | It should be assigned with the `alias` value option from the Vite config. |
66+
| `jestAlias` | `array` | `[]` | It should be assigned with the `moduleNameMapper` value option from the Jest config. |
67+
| `webpackExtensions` | `array` | `[".js", ".jsx", ".ts", ".tsx"]` | It should be assigned with the `extensions` value option from the Webpack config. |
68+
| `viteExtensions` | `array` | `[".mjs", ".js", ".mts", ".ts", ".jsx", ".tsx", ".json"]` | It should be assigned with the `extensions` value option from the Vite config. |
69+
| `jestExtensions` | `array` | `["js", "jsx", "ts", "tsx"]` | It should be assigned with the `moduleFileExtensions` value option from the Jest config. |
70+
| `isCacheEnabled` | `boolean` | `false` | If `true`, enables file-based cache. |
71+
| `logging` | `object` | `{ type: "disabled", filePath: "log.txt" }` | Specifies logging options.<br>`type` can be `disabled` for no logging, `file` for logs to a file, or `screen` for logs to the console.<br>If type is `file`, `filePath` specifies the log file path. |
7072

7173
## The Problem
7274

src/executorConfig.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ class WebpackStrategy {
9797
}
9898
}
9999

100+
class ViteStrategy {
101+
prepareExtensions(extensions) {
102+
return extensions;
103+
}
104+
105+
prepareAlias(alias) {
106+
return alias;
107+
}
108+
}
109+
100110
class JestStrategy {
101111
prepareExtensions(extensions) {
102112
return extensions.map(ext => `.${ext}`);
@@ -114,6 +124,10 @@ class ExecutorFactory {
114124
const webpack = new ExecutorConfig(alias, extensions, new WebpackStrategy());
115125
webpack.load();
116126
return webpack;
127+
case 'vite':
128+
const vite = new ExecutorConfig(alias, extensions, new ViteStrategy());
129+
vite.load();
130+
return vite;
117131
case 'jest':
118132
const jest = new ExecutorConfig(alias, extensions, new JestStrategy());
119133
jest.load();

src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const importDeclarationVisitor = (path, state) => {
1313
if (AST.getSpecifierType(importsSpecifiers[0]) === "namespace") return;
1414
const parsedJSFile = state.filename;
1515
const importsPath = path.node.source.value;
16+
if (importsPath.startsWith("/") || importsPath.startsWith("\\")) return;
1617
if (builtinModules.includes(importsPath)) return;
1718
logger.log(`Source import line: ${generate(path.node, { comments: false, concise: true }).code}`);
1819
resolver.from = parsedJSFile;
@@ -47,6 +48,8 @@ module.exports = function (babel) {
4748
let executor;
4849
if (pluginOptions.executorName === "webpack") {
4950
executor = ExecutorFactory.createExecutor("webpack", options.webpackAlias, options.webpackExtensions);
51+
} else if (pluginOptions.executorName === "vite") {
52+
executor = ExecutorFactory.createExecutor("vite", options.viteAlias, options.viteExtensions);
5053
} else if (pluginOptions.executorName === "jest") {
5154
executor = ExecutorFactory.createExecutor("jest", options.jestAlias, options.jestExtensions);
5255
} else {

src/pluginOptions.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ let instance;
22

33
class PluginOptions {
44
constructor() {
5+
this.executorName = "";
56
this.options = {
6-
executorName: "",
77
webpackAlias: {},
88
webpackExtensions: [".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx"],
9+
viteAlias: {},
10+
viteExtensions: [".mjs", ".js", ".mts", ".ts", ".jsx", ".tsx", ".json"],
911
jestAlias: [],
1012
jestExtensions: ["js", "jsx", ".mjs", ".cjs", "ts", "tsx"],
1113
isCacheEnabled: false,
@@ -31,6 +33,8 @@ class PluginOptions {
3133
getExecutorName(options) {
3234
if (options.webpackAlias || options.webpackExtensions) {
3335
return "webpack";
36+
} else if (options.viteAlias || options.viteExtensions) {
37+
return "vite";
3438
} else if (options.jestAlias || options.jestExtensions) {
3539
return "jest";
3640
} else {

tests/js/local/imports.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ describe("aliases", () => {
7272
].join("\n").replaceAll("\\","\\\\"));
7373
});
7474

75+
test("transformation of vite alias", () => {
76+
const alias = {
77+
components: ospath.resolve(__dirname, 'components'),
78+
}
79+
const pluginOptions = { viteAlias: alias };
80+
expect(
81+
pluginTransform(
82+
'import { Text } from "components/Texts";',
83+
__filename,
84+
pluginOptions
85+
)
86+
).toBe([
87+
`import { Text } from \"${ospath.resolve(__dirname)}\\components\\Texts\\Text\\Text.js";`,
88+
].join("\n").replaceAll("\\","\\\\"));
89+
});
90+
7591
test("transformation of jest alias", () => {
7692
const alias = Object.entries({
7793
"^abc/(.*)$": "./components/$1"

tests/ts/local/imports.test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const pluginTransform = require("../../pluginTransform");
22
const ospath = require("path");
33

44
describe('typescript transformation', () => {
5-
test("transformation of a named import", () => {
5+
test("transformation of a named import - jest", () => {
66
const extensions = ["ts", "tsx"];
77
const pluginOptions = { jestExtensions: extensions };
88
expect(
@@ -16,7 +16,21 @@ describe('typescript transformation', () => {
1616
].join("\n").replaceAll("\\","\\\\"));
1717
});
1818

19-
test("transformation of a named import that was originally re-exported from a default export in the barrel file", () => {
19+
test("transformation of a named import - vite", () => {
20+
const extensions = [".ts", ".tsx"];
21+
const pluginOptions = { viteExtensions: extensions };
22+
expect(
23+
pluginTransform(
24+
'import {namedExport} from "./components";',
25+
__filename,
26+
pluginOptions
27+
)
28+
).toBe([
29+
`import { namedExport } from \"${ospath.resolve(__dirname)}\\components\\namedExport\\namedExport.ts";`,
30+
].join("\n").replaceAll("\\","\\\\"));
31+
});
32+
33+
test("transformation of a named import that was originally re-exported from a default export in the barrel file - webpack", () => {
2034
const extensions = [".ts", ".tsx"];
2135
const pluginOptions = { webpackExtensions: extensions };
2236
expect(

0 commit comments

Comments
 (0)