Code Monkey home page Code Monkey logo

webpacktag's Introduction

WebpackTag

NuGet version

WebpackTag is an ASP.NET Core Tag Helper for rendering links to CSS and JS files compiled via Webpack

Usage

Install the WebpackTag library using NuGet

Add the WebpackTag services to your Startup.cs:

using WebpackTag;
...
public void ConfigureServices(IServiceCollection services)
{
	services.AddWebpackTag();

Import the tag helpers in your Views/_ViewImports.cshtml file:

@addTagHelper *, WebpackTag

Then use the tag helpers in your view!

<webpack-styles />
<webpack-scripts />

These tag helpers will look for files called webpack-assets.json or asset-manifest.json in your wwwroot or SPA root directory, parse the contents, and render the correct <link> or <style> tags.

Note that when using assets-webpack-plugin, the entrypoints option should be enabled.

Multiple entry points

If your app has multiple entry points, you may specify the entry point name when using the tag

<webpack-styles entry="first" />

Configuration

Additional configuration can be performed when registering the services:

services.AddWebpackTag(options =>
{
	// ...
});

The following configuration options are available:

  • DevServerPort: Port the Webpack devserver is running on. If this is configured, the tag helpers will try hitting http://localhost:{port}/asset-manifest.json to load the manifest.
  • BaseUrl: Sets a string to prefix the generated URLs with. For example, this can be used to use a separate CDN domain in prod:
public class Startup
{
	private readonly IWebHostEnvironment _env;

	public Startup(IConfiguration configuration, IWebHostEnvironment env)
	{
		_env = env;
		Configuration = configuration;
	}

	public void ConfigureServices(IServiceCollection services)
	{
		services.AddWebpackTag(options =>
		{
			options.BaseUrl = _env.IsDevelopment() ? "/" : "https://cdn.example.com/";
		});
		// ...

Samples

This repository contains two samples:

webpacktag's People

Contributors

daniel15 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.