@webinex/vite 0.0.1-beta3 → 0.0.1-beta5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,28 +1,47 @@
1
1
  'use strict';
2
2
 
3
3
  const path = require('path');
4
+ const MagicString = require('magic-string');
4
5
 
5
6
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
6
7
 
7
8
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
9
+ const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
8
10
 
9
11
  function scssPathPlugin(options) {
10
12
  const { replace } = options;
11
13
  const regExp = new RegExp(`@use '${replace.key}/`, "g");
12
- const plugin = {
13
- name: "@custom/scss-path-plugin",
14
+ return {
15
+ name: "@webinex/vite/scss-path-plugin",
14
16
  transform: {
15
17
  order: "pre",
16
18
  handler(src, filePath) {
17
19
  if (!filePath.endsWith(".scss") && !filePath.endsWith(".sass")) {
18
- return src;
20
+ return null;
19
21
  }
20
22
  const relative = path__default.relative(path__default.dirname(filePath), replace.path).replace(/\\/g, "/");
21
- return src.replace(regExp, `@use '${relative}/`);
23
+ if (!regExp.test(src)) {
24
+ return null;
25
+ }
26
+ const ms = new MagicString__default(src);
27
+ let match;
28
+ regExp.lastIndex = 0;
29
+ while ((match = regExp.exec(src)) !== null) {
30
+ const start = match.index;
31
+ const end = start + match[0].length;
32
+ ms.overwrite(start, end, `@use '${relative}/`);
33
+ }
34
+ return {
35
+ code: ms.toString(),
36
+ map: ms.generateMap({
37
+ hires: true,
38
+ source: filePath,
39
+ includeContent: true
40
+ })
41
+ };
22
42
  }
23
43
  }
24
44
  };
25
- return plugin;
26
45
  }
27
46
 
28
47
  exports.scssPathPlugin = scssPathPlugin;
@@ -6,7 +6,7 @@ interface ScssPathPluginOptions {
6
6
  path: string;
7
7
  };
8
8
  }
9
- declare function scssPathPlugin(options: ScssPathPluginOptions): Plugin<any>;
9
+ declare function scssPathPlugin(options: ScssPathPluginOptions): Plugin;
10
10
 
11
11
  export { scssPathPlugin };
12
12
  export type { ScssPathPluginOptions };
@@ -6,7 +6,7 @@ interface ScssPathPluginOptions {
6
6
  path: string;
7
7
  };
8
8
  }
9
- declare function scssPathPlugin(options: ScssPathPluginOptions): Plugin<any>;
9
+ declare function scssPathPlugin(options: ScssPathPluginOptions): Plugin;
10
10
 
11
11
  export { scssPathPlugin };
12
12
  export type { ScssPathPluginOptions };
@@ -6,7 +6,7 @@ interface ScssPathPluginOptions {
6
6
  path: string;
7
7
  };
8
8
  }
9
- declare function scssPathPlugin(options: ScssPathPluginOptions): Plugin<any>;
9
+ declare function scssPathPlugin(options: ScssPathPluginOptions): Plugin;
10
10
 
11
11
  export { scssPathPlugin };
12
12
  export type { ScssPathPluginOptions };
@@ -1,22 +1,40 @@
1
1
  import path from 'path';
2
+ import MagicString from 'magic-string';
2
3
 
3
4
  function scssPathPlugin(options) {
4
5
  const { replace } = options;
5
6
  const regExp = new RegExp(`@use '${replace.key}/`, "g");
6
- const plugin = {
7
- name: "@custom/scss-path-plugin",
7
+ return {
8
+ name: "@webinex/vite/scss-path-plugin",
8
9
  transform: {
9
10
  order: "pre",
10
11
  handler(src, filePath) {
11
12
  if (!filePath.endsWith(".scss") && !filePath.endsWith(".sass")) {
12
- return src;
13
+ return null;
13
14
  }
14
15
  const relative = path.relative(path.dirname(filePath), replace.path).replace(/\\/g, "/");
15
- return src.replace(regExp, `@use '${relative}/`);
16
+ if (!regExp.test(src)) {
17
+ return null;
18
+ }
19
+ const ms = new MagicString(src);
20
+ let match;
21
+ regExp.lastIndex = 0;
22
+ while ((match = regExp.exec(src)) !== null) {
23
+ const start = match.index;
24
+ const end = start + match[0].length;
25
+ ms.overwrite(start, end, `@use '${relative}/`);
26
+ }
27
+ return {
28
+ code: ms.toString(),
29
+ map: ms.generateMap({
30
+ hires: true,
31
+ source: filePath,
32
+ includeContent: true
33
+ })
34
+ };
16
35
  }
17
36
  }
18
37
  };
19
- return plugin;
20
38
  }
21
39
 
22
40
  export { scssPathPlugin };
package/package.json CHANGED
@@ -1,28 +1,28 @@
1
1
  {
2
2
  "name": "@webinex/vite",
3
3
  "private": false,
4
- "version": "0.0.1-beta3",
4
+ "version": "0.0.1-beta5",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./external-plugin": {
8
8
  "types": "./dist/external-plugin/index.d.ts",
9
9
  "import": {
10
- "types": "./dist/external-plugin/index.d.ts",
10
+ "types": "./dist/external-plugin/index.d.mts",
11
11
  "default": "./dist/external-plugin/index.mjs"
12
12
  },
13
13
  "require": {
14
- "types": "./dist/external-plugin/index.d.ts",
14
+ "types": "./dist/external-plugin/index.d.cts",
15
15
  "default": "./dist/external-plugin/index.cjs"
16
16
  }
17
17
  },
18
18
  "./i18next-resources-plugin": {
19
19
  "types": "./dist/i18next-resources-plugin/index.d.ts",
20
20
  "import": {
21
- "types": "./dist/i18next-resources-plugin/index.d.ts",
21
+ "types": "./dist/i18next-resources-plugin/index.d.mts",
22
22
  "default": "./dist/i18next-resources-plugin/index.mjs"
23
23
  },
24
24
  "require": {
25
- "types": "./dist/i18next-resources-plugin/index.d.ts",
25
+ "types": "./dist/i18next-resources-plugin/index.d.cts",
26
26
  "default": "./dist/i18next-resources-plugin/index.cjs"
27
27
  }
28
28
  },
@@ -32,11 +32,11 @@
32
32
  "./scss-path-plugin": {
33
33
  "types": "./dist/scss-path-plugin/index.d.ts",
34
34
  "import": {
35
- "types": "./dist/scss-path-plugin/index.d.ts",
35
+ "types": "./dist/scss-path-plugin/index.d.mts",
36
36
  "default": "./dist/scss-path-plugin/index.mjs"
37
37
  },
38
38
  "require": {
39
- "types": "./dist/scss-path-plugin/index.d.ts",
39
+ "types": "./dist/scss-path-plugin/index.d.cts",
40
40
  "default": "./dist/scss-path-plugin/index.cjs"
41
41
  }
42
42
  }
@@ -54,6 +54,7 @@
54
54
  "@types/lodash-es": "^4.17.12",
55
55
  "@types/node": "^22.7.9",
56
56
  "eslint": "^9.13.0",
57
+ "magic-string": "^0.30.17",
57
58
  "prettier": "^3.4.2",
58
59
  "typescript": "~5.6.2",
59
60
  "typescript-eslint": "^8.10.0",
@@ -1,5 +1,6 @@
1
1
  import path from 'path';
2
2
  import { Plugin } from 'vite';
3
+ import MagicString from 'magic-string';
3
4
 
4
5
  export interface ScssPathPluginOptions {
5
6
  replace: {
@@ -8,24 +9,48 @@ export interface ScssPathPluginOptions {
8
9
  };
9
10
  }
10
11
 
11
- export function scssPathPlugin(options: ScssPathPluginOptions) {
12
+ export function scssPathPlugin(options: ScssPathPluginOptions): Plugin {
12
13
  const { replace } = options;
13
14
  const regExp = new RegExp(`@use '${replace.key}/`, 'g');
14
15
 
15
- const plugin: Plugin = {
16
- name: '@custom/scss-path-plugin',
16
+ return {
17
+ name: '@webinex/vite/scss-path-plugin',
18
+
17
19
  transform: {
18
20
  order: 'pre',
19
- handler(this, src, filePath) {
21
+
22
+ handler(src, filePath) {
20
23
  if (!filePath.endsWith('.scss') && !filePath.endsWith('.sass')) {
21
- return src;
24
+ return null;
22
25
  }
23
26
 
24
27
  const relative = path.relative(path.dirname(filePath), replace.path).replace(/\\/g, '/');
25
- return src.replace(regExp, `@use '${relative}/`);
28
+
29
+ if (!regExp.test(src)) {
30
+ return null;
31
+ }
32
+
33
+ const ms = new MagicString(src);
34
+
35
+ let match: RegExpExecArray | null;
36
+ regExp.lastIndex = 0;
37
+
38
+ while ((match = regExp.exec(src)) !== null) {
39
+ const start = match.index;
40
+ const end = start + match[0].length;
41
+
42
+ ms.overwrite(start, end, `@use '${relative}/`);
43
+ }
44
+
45
+ return {
46
+ code: ms.toString(),
47
+ map: ms.generateMap({
48
+ hires: true,
49
+ source: filePath,
50
+ includeContent: true,
51
+ }),
52
+ };
26
53
  },
27
54
  },
28
55
  };
29
-
30
- return plugin;
31
56
  }