npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

san-sfc-compiler

v0.1.7

Published

Lower level utilities for compiling .san single file components

Downloads

4

Readme

san-sfc-compiler

Lower level utilities for compiling .san single file components

fork from vuejs/component-compiler-utils

API

parseSFC

描述:解析 .san 文件源码

使用:

parseSFC({
  source: '<div><p>{{ helloworld }}</p></div>',
  filename: 'test.san',
});

// 选项
export interface ParseOptions {
  source: string;
  filename?: string;
  sourceRoot?: string;
  needMap?: boolean;
}

方法 / 属性:

| 方法 / 属性 | 描述 | 类型 | | ----------- | ----------------------------- | ------- | | source | 源码 | string | | filename | 文件名 | string | | sourceRoot | sourcemap 选项 | string | | needMap | sourcemap 开关(template 没有 | boolean |

返回:

export interface SFCDescriptor {
  template: SFCBlock | null;
  script: SFCBlock | null;
  styles: SFCBlock[];
  customBlocks: SFCBlock[];
}

其中 SFCBlock 类型为这样:

{
    type!: string;
    content!: string;
    attrs!: Record<string, string | true>;
    start!: number;
    end!: number;
    lang?: string;
    src?: string;
    scoped?: true;
    module?: string | true;
}

content 就是标签内容了。

compileTemplate

描述:编译模板代码,即 <template> 标签中包含的部分

使用:

const result = compileTemplate({
  source: '<div><img src="../images/profile.png" /></div>',
  filename: 'example.san',
  transformAssetUrls: true,
});

方法 / 属性:

| 方法 / 属性 | 描述 | 类型 | | ------------------ | ---------------------------- | ---------------------- | | source | 源码 | string | | filename | 文件名 | string | | id | 一般用在 scoped css 时 | string | | scoped | scoped css 开关 | boolean | | transformAssetUrls | 将 url 转换为 require | boolean | | compileANode | 编译为 anode / apack | 'aNode'/'aPack'/'none' | | preprocessLang | 预处理语言,pugjs 等 | string | | preprocessOptions | 预处理的选项,传递给预处理器 | object |

返回:

{
    code: '<div><img src="require(../images/profile.png);"></div>',
    source: '<div><img src="../images/profile.png" /></div>'
}

compileStyle / compileStyleAsync

描述:编译样式代码,即 <style> 标签中包含的部分

使用:

const style = parseSFC({
  source: `<style lang="less">@red: rgb(255, 0, 0);\n.color { color: @red; }\n</style>`,
  filename: 'test.san',
}).styles[0] as any;

const result = compileStyle({
  id: 'test',
  filename: 'test.san',
  source: style.content,
  scoped: true,
  map: style.map,
  preprocessLang: style.lang,
});

方法 / 属性:

| 方法 / 属性 | 描述 | 类型 | | ----------------- | --------------------------------------- | ------- | | source | 源码 | string | | filename | 文件名 | string | | id | 添加 scoped css id | string | | scoped | scoped css 开关 | boolean | | modules | css modules 开关 | boolean | | mute | 去除 postcss 控制台信息,默认为 true | boolean | | preprocessLang | 预处理语言,less sass 等 | string | | preprocessOptions | 预处理的选项,传递给预处理器 | object | | postcssOptions | postcss 的选项,直接传递给它 | object | | postcssPlugins | postcss 的插件,直接传递给它 | object | | map | sourcemap 的 map,如 less => css 的映射 | object |

返回:

{
  code: '.color[test] {\n  color: #ff0000;\n}\n' // 转换后的代码,
  source: '.color {\n  color: #ff0000;\n}\n' // 源码,
  rawResult: LazyResult // postcss 异步结果,
  map: outMap && outMap.toJSON() // 经过两步转换的 sourcemap,
  errors,
  cssHashMap: {} // css modules 编译结果
}

compileScript

描述:直接使用 ts 的 transpile 方法

export const compileScript = (source: string, config: CompilerOptions) => {
  return typescrtipt.transpile(source, config);
};

如果不清楚,可以参考 test 目录下的测试文件

LICENSE

Copyright (c) Baidu Inc. All rights reserved.

This source code is licensed under the MIT license.