razor-partial-views-webpack-plugin

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 常用JavaScript包
软件类型 开源软件
地区 不详
投 递 者 荆钱明
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Razor Partial Views Webpack Plugin

Plugin for generating ASP.NET Razor partial views for assets built with webpack.

Build Status

Usage

razor-partial-views-webpack-plugin use rules for generating cshtml/vbhtml views wrapping assets built with webpack. With the plugin comes templates for scripts and styles, but any type of asset can be used as Razor view source.

Installation

  • npm install razor-partial-views-webpack-plugin --save-dev
  • yarn add razor-partial-views-webpack-plugin --dev

Getting started

To get familiar with the output from razor-partial-views-webpack-plugin, the quickest way is using the plugin's defaults, to generate partial views for all JavaScript and CSS.

// webpack.config.js
plugins: [new RazorPartialViewsWebpackPlugin()];

Options

The templating process can be customized to fit the needs of your application. Here follows the configuration options that are supported.

new RazorPartialViewsWebpackPlugin({
  // "csharp" (default) or "vb"
  target: "chsharp",
  rules: [
    {
      // regex match asset filename(s)
      // (takes precedence over `name`)
      test: /(app|vendor).*\.js$/
    },
    {
      // match asset by name
      name: "runtime",
      // no attributes in `template` are required
      template: {
        // prepend header to view
        header: () => "<!-- a header -->",
        // usings in view
        using: ["System", "System.Web"],
        // view's model
        model: "dynamic",
        // append footer to view
        footer: () => `@* View generated ${new Date().toISOString()} *@`,
        // if needed, use a custom template
        path: path.join(__dirname, "templates/custom-template.tmpl"),
        // in custom template, placeholder to find & replace with asset
        // (default ##URL##/##SOURCE##)
        replace: /##CONTENT-GOES-HERE##/
      },
      // `output` not required, defaults to:
      // - webpack's output directory
      // - load asset by URL
      // - asset name from chunk name/filename
      output: {
        inline: true,
        async: false,
        defer: false,
        // asset is ESM module
        module: false,
        // ...or fallback if module not supported
        nomodule: false,
        // assign predicable name to generated partial view
        name: defaultName => `generated-${defaultName}`,
        // output view to custom location
        path: path.join(__dirname, "Views/_GeneratedViews")
      }
    }
  ]
});

Example configuration

razor-partial-views-webpack-plugin supports referencing and inlining assets generated by webpack. Here follows an example configuration based on webpack's caching docs. A partial view for styles is also created.

const path = require("path");

const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const RazorPartialViewsWebpackPlugin = require("razor-partial-views-webpack-plugin");

module.exports = {
  entry: {
    app: path.join(__dirname, "app.js")
  },
  output: {
    path: path.join(__dirname, "dist"),
    publicPath: "/dist/",
    filename: "[name].[contenthash].js"
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader
          },
          "css-loader"
        ]
      }
    ]
  },
  optimization: {
    runtimeChunk: "single",
    splitChunks: {
      cacheGroups: {
        defaultVendors: {
          test: /[\\/]node_modules[\\/]/,
          name: "vendors",
          chunks: "all"
        },
        defaultStyles: {
          name: "styles",
          test: /\.css$/,
          chunks: "all",
          enforce: true
        }
      }
    }
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "[name].[contenthash].css",
      ignoreOrder: false
    }),
    new RazorPartialViewsWebpackPlugin({
      rules: [
        {
          name: ["app", "vendors", "styles"]
        },
        {
          name: "runtime",
          output: {
            inline: true
          }
        }
      ]
    })
  ]
};

Options in use

Included in the plugin repository is an example webpack setup where various rules are used. By executing npm run example, partial views are created e.g. for inlining CSS and webpack's runtime.

Compiling views

When running your ASP.NET web site, the generated views will be compiled. Below follows a few tips to keep in mind:

  • If you run into Compiler Error Message: CS0103: The name 'model' does not exist in the current context, this Stack Overflow answer guides you in the right direction.
  • If you're executing razor-partial-views-webpack-plugin on a build server, make sure you've included the generated views' output directory in the artifact.

Even more control

razor-partial-views-webpack-plugin is an extension of templated-assets-webpack-plugin. For more configuration options and detailed control, use templated-assets-webpack-plugin.

Feedback

  • For feedback, bugs or change requests, please use Issues.
  • For direct contact, tweet @jouni_kantola.
 相关资料
  • imi 的 Partial 思想是从 C# 中借鉴而来。Partial 可以将一个类的部分,分成几个文件,分别书写。 截止目前最新的 PHP 7.4 版本,还未在语言层面上支持 Partial 特性。 但依赖于 imi 强大的容器对象,终于在 PHP 中实现了 Partial 特性。 使用非常简单,使用 trait 编写,加上 @Partial 注解,可以方便地注入。 Partial 使用场景 多

  • Razor 是一个轻巧而优雅的servlet mvc框架 # 又一个轮子? no,写就她是为了证实我个人的某些想法,并在这个过程中练练手,这两种冲动碰撞在一起,自然而然地产生了Razor # Razor的现在和未来? 作为一个mvc框架,最最核心的功能已经完成了九成,总体的设计方案基本上已经稳定下来。 未来我还会不断完善Razor,加入更多的功能和想法,我也可能修正现有的设计,从而使她变得完全不同

  • 在MVC框架中,字母V代表Views 。 它将应用程序逻辑和表示逻辑分开。 视图存储在resources/views目录中。 通常,视图包含将由应用程序提供的HTML。 例子 (Example) 请观察以下示例以了解有关视图的更多信息 - Step 1 - 复制以下代码并将其保存在resources/views/test.php <html> <body> <h1>Hello,

  • views 主要包括versions.py,其中定义了ViewBuilder类和全局的get_view_builder()方法,该方法返回一个ViewBuilder类的实例。

  • AngularJS通过单个页面上的多个视图支持单页面应用程序。 为此,AngularJS提供了ng-view和ng-template指令以及$ routeProvider服务。 ng-view Directive ng-view指令只是创建一个占位符,可以根据配置放置相应的视图(HTML或ng-template视图)。 用法 (Usage) 在主模块中使用ng-view定义div。 <div ng

  • MVC中的字母“V”用于视图。 视图负责根据请求将输出发送给用户。 View Classes是加速开发过程的有效方法。 查看模板 CakePHP的View Templates文件具有默认扩展名。 ctp (CakePHP模板)。 这些模板从控制器获取数据,然后渲染输出,以便可以正确地显示给用户。 我们可以在模板中使用变量,各种控制结构。 模板文件存储在src/Template/ ,以使用这些文件的