Skip to content
Merged
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
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,25 @@ build({
The following show the default values of the configuration

```ts
VueSource({
// source root path
root: process.cwd(),

// generate sourceMap
sourceMap: false,
});
interface Options {
/**
* source root path
*
* @default process.cwd()
*/
root?: string;
/**
* generate sourceMap
*
* @default false
*/
sourceMap?: boolean;

/** @default '**\/*.{vue,jsx.tsx}' */
include?: string | RegExp | (string | RegExp)[];
/** @default 'node_modules/**' */
exclude?: string | RegExp | (string | RegExp)[];
}
```

## Playgrounds
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"@babel/parser": "^7.22.16",
"@babel/plugin-syntax-jsx": "^7.22.5",
"@babel/plugin-syntax-typescript": "^7.22.5",
"@rollup/pluginutils": "^5.0.5",
"@vue/compiler-dom": "^3.3.4",
"magic-string": "^0.30.3",
"unplugin": "^1.4.0"
Expand Down
18 changes: 17 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 0 additions & 26 deletions src/core/filter_ID.ts

This file was deleted.

42 changes: 32 additions & 10 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { UnpluginFactory } from 'unplugin';
import { createUnplugin } from 'unplugin';
import type { Options } from '../types';
import { filter_ID } from './filter_ID';
import { type UnpluginFactory, createUnplugin } from 'unplugin';
import { createFilter } from '@rollup/pluginutils';
import { type ResolvedOptions, type Options } from '../types';
import { TRACE_ID } from './constants';
import { transform } from './transform';
import { parse_ID } from './parse_ID';

export const unpluginFactory: UnpluginFactory<Options> = (options = {}) => {
if (process.env.NODE_ENV !== 'development') {
Expand All @@ -12,23 +13,44 @@ export const unpluginFactory: UnpluginFactory<Options> = (options = {}) => {
}

const opts = resolveOptions(options);
const filter = createFilter(opts.include, opts.exclude);

return {
name: 'unplugin-vue-source',
enforce: 'pre',
transformInclude: filter_ID,
transformInclude(id) {
if (filter(id)) {
const parsed = parse_ID(id);

if (parsed.isSfc) {
const { query } = parsed;
// vue cli | vue-loader
if (query.type === 'template') {
return true;
}
return (
// vite-plugin-vue
!query[TRACE_ID] &&
// rollup-plugin-vue
!query['rollup-plugin-vue']
);
}

return true;
}
},
transform(code, id) {
return transform(code, id, opts);
},
};
};

function resolveOptions(options: Options): Required<Options> {
const { root = process.cwd(), sourceMap = false } = options;

function resolveOptions(opts: Options): ResolvedOptions {
return {
root,
sourceMap,
root: opts.root ?? process.cwd(),
sourceMap: opts.sourceMap ?? false,
include: opts.include ?? '**/*.{vue,jsx.tsx}',
exclude: opts.exclude ?? 'node_modules/**',
};
}

Expand Down
20 changes: 8 additions & 12 deletions src/core/parse_ID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@ import { extname } from 'path';
import { TRACE_ID } from './constants';

export interface VueQuery extends Record<string, any> {
vue?: boolean;
type?: 'script' | 'template' | 'style' | 'custom';
[TRACE_ID]?: string;
}

export function parse_ID(id: string, root = '') {
const [file, rawQuery] = id.split('?', 2);

const ext = extname(file).slice(1);
const isSfc = ext === 'vue';
const isTsx = ext === 'tsx';
const isJsx = isTsx || ext === 'jsx';

const query = Object.fromEntries(new URLSearchParams(rawQuery)) as VueQuery;
if (query.vue != null) {
query.vue = true;
if (ext === 'vue') {
return {
file: file.replace(root, ''),
isSfc: true,
query: Object.fromEntries(new URLSearchParams(rawQuery)) as VueQuery,
};
}

return {
file: file.replace(root, ''),
isSfc,
isTsx,
isJsx,
query,
isJsx: true,
isTsx: ext.includes('ts'),
};
}
14 changes: 5 additions & 9 deletions src/core/transform.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import type { Position } from '@vue/compiler-dom';
import { type Position } from '@vue/compiler-dom';
import MagicString from 'magic-string';
import type { Options } from '../types';
import { type ResolvedOptions } from '../types';
import { TRACE_ID } from './constants';
import { parse_ID } from './parse_ID';
import { transform_SFC } from './transform_SFC';
import { transform_JSX } from './transform_JSX';

export function transform(
code: string,
id: string,
options: Required<Options>,
) {
export function transform(code: string, id: string, options: ResolvedOptions) {
const { root, sourceMap } = options;

const s = new MagicString(code);

const parsed = parse_ID(id, root);

if (parsed.isSfc) {
transform_SFC(code, replace);
} else if (parsed.isJsx) {
} else {
transform_JSX(code, replace, parsed);
}

Expand Down
1 change: 1 addition & 0 deletions src/core/transform_JSX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function transform_JSX(
const { start } = node.loc!;
const name = getJSXElementName(nameNode);
const offset = start.index + startIndex + name.length + 1;

cb({
...start,
// babel starts at 0, so we need to add 1
Expand Down
8 changes: 4 additions & 4 deletions src/core/transform_SFC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ export function transform_SFC(code: string, cb: (pos: Position) => void) {
) {
const { start } = node.loc;
const offset = start.offset + node.tag.length + 1;
cb({
...start,
offset,
});

cb({ ...start, offset });
}
},
],
Expand All @@ -49,6 +47,8 @@ function resolveJsxOptions(ast: RootNode) {
) as AttributeNode;
if (!langProp) return;

// <script lang="jsx">...</script>
// <script lang="tsx">...</script>
const lang = langProp.value?.content;
const isTsx = lang === 'tsx';
const isJsx = isTsx || lang === 'jsx';
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ export interface Options {
* @default false
*/
sourceMap?: boolean;

/** @default '**\/*.{vue,jsx.tsx}' */
include?: string | RegExp | (string | RegExp)[];
/** @default 'node_modules/**' */
exclude?: string | RegExp | (string | RegExp)[];
}

export type ResolvedOptions = Required<Options>;