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-ssr-plugin

v2.0.0

Published

San-ssr Webpack 插件,用于将 `.san` 编译为可在 Node.js 中执行的 JavaScript 文件。

Downloads

30

Readme

san-ssr-plugin

San-ssr Webpack 插件,用于将 .san 编译为可在 Node.js 中执行的 JavaScript 文件。

需要配合 san-loader 一起使用。

Language npm package npm package npm package semantic-release

快速开始

import webpack from 'webpack';
import SanLoaderPlugin from 'san-loader/lib/plugin';
import SanSSRPlugin from 'san-ssr-plugin';

export default {
    plugins: [
        new SanSSRPlugin({
            output: {
                path: 'js',
            }
        }),
        new SanLoaderPlugin()
    ],
    resolve: {
        extensions: ['.san'],
    },
    module: {
        rules: [
            {
                test: /\.san$/,
                loader: 'san-loader',
            }
        ],
    }
};

测试

npm run test

自定义样式输出格式

san-ssr 本身不负责样式的处理。本插件通过封装 san-ssr 产物(render 函数)的形式处理样式产物:

css = namedModuleCss.reduce((acc, cur) => {
    if (cur.css) {
        acc += `\n${cur.css}`;
    }
    return acc;
}, css);

if (css) {
    code += 'const originSanSSRRenders = module.exports.sanSSRRenders;\n';
    code += 'Object.keys(originSanSSRRenders).forEach(renderName => {\n';
    code += '    originSanSSRRenders[renderName] = makeRender(originSanSSRRenders[renderName]);\n';
    code += '});\n';
    code += 'module.exports = Object.assign(sanSSRResolver.getRenderer({id: "default"}), exports);\n';
    code += 'function makeRender(originRender) {\n';
    code += '    return function (data, ...params) {\n';
    code += '        if (global.__COMPONENT_CONTEXT__) {\n';
    code += `            global.__COMPONENT_CONTEXT__[${styleId}] = ${JSON.stringify(css)};\n`;
    code += '        }\n';
    if (Object.keys(locals).length > 0) {
        code += '        data[\'$style\'] = {\n';
        code += `            ${Object.keys(locals).map(item =>
            `${JSON.stringify(item)}: ${JSON.stringify(locals[item])}`
        ).join(',')}\n`;
        code += '        };\n';
    }
    namedModuleCss.forEach(({name, locals}) => {
        if (locals) {
            if (Object.keys(locals).length > 0) {
                code += `        data[\'$${name}\'] = {\n`;
                code += `            ${Object.keys(locals).map(item =>
                    `${JSON.stringify(item)}: ${JSON.stringify(locals[item])}`
                ).join(',')}\n`;
                code += '        };\n';
            }
        }
    });
    code += '        return originRender(data, ...params);\n';
    code += '    };\n';
    code += '}\n';
}

这段代码会添加在产物的最后,作为输出。

如果该内容不能满足要求,使用者可通过 appendRenderFunction 选项自行设置该内容:

plugins: [
    new SanSsrPlugin({
        appendRenderFunction(
            styleId: string,
            css: string = '',
            locals: Record<string, string>,
            namedModuleCss: Array<{
                name: string;
                css?: string;
                locals?: Record<string, string>;
            }> = []
        ) {
            return ``;
        }
    })
]

参数的具体内容可参考上方默认的输出。

Webpack V5

  1. html-loader 2 以上版本,与 san-loader 的 compileTemplate: 'aPack' 配合使用时,需要将 html-loader 的 sources 选项设置为 false,同时 HTML 中的静态资源无法再自动处理。需要等待 san-loader 适配。

  2. html-loader 与 css-loader 目前默认会输出 esModule 格式的内容,san-loader 与 san-ssr-plugin 都不支持这种写法,需要手动设置 esModule: false:

    {
        loader: 'css-loader',
        options: {
            esModule: false,
        },
    },
    {
        loader: 'html-loader',
        options: {
            esModule: false,
        }
    },