项目作者: Jarred-Sumner

项目描述 :
Automatically configure esbuild from html
高级语言: JavaScript
项目地址: git://github.com/Jarred-Sumner/htmlbuild.git
创建时间: 2021-03-09T10:29:29Z
项目社区:https://github.com/Jarred-Sumner/htmlbuild

开源协议:

下载


htmlbuild

Automatically configure & run esbuild from <script> and <link rel="stylesheet"> used in an .html file.

Installation

npm:

  1. npm install -g @jarred/htmlbuild

yarn:

  1. yarn global add @jarred/htmlbuild

Usage

  1. # All additional flags are forwarded to esbuild.
  2. htb index.html --outdir=dist
  3. htmlbuild index.html --outdir=dist

This lets you use an HTML file to configure esbuild.

Given an HTML file like this:

  1. <html>
  2. <head>
  3. <link href="foo.css" rel="stylesheet" />
  4. <!- Notice the TypeScript: -->
  5. <script src="./index.ts"></script>
  6. <script src="./deep/so/very/deep.ts"></script>
  7. </head>
  8. <body>
  9. HI!
  10. </body>
  11. </html>

htmlbuild parses the HTML and turns each <script> or <link rel="stylsheet"> into an entryPoint. That example generates a config that looks like this:

  1. {
  2. bundle: true,
  3. metafile: true,
  4. entryPoints: [
  5. '/Users/jarredsumner/Code/htmlbuild/test-dir/index.js',
  6. '/Users/jarredsumner/Code/htmlbuild/test-dir/deep/so/very/deep.ts',
  7. '/Users/jarredsumner/Code/htmlbuild/test-dir/foo.css'
  8. ]
  9. }

Then, it runs esbuild with any flags you passed in. Using esbuild’s metadata, it produces a new HTML file that looks like this:

  1. <html>
  2. <head>
  3. <link href="foo.css" rel="stylesheet" />
  4. <link rel="stylesheet" href="index.css" />
  5. <script src="index.js"></script>
  6. <script src="deep/so/very/deep.js"></script>
  7. </head>
  8. <body>
  9. HI!
  10. </body>
  11. </html>

Notice the extra index.css?

That’s because index.ts imported index.css:

  1. import "./index.css";
  2. console.log(
  3. 'It detected the imported index.css file and inserted it directly above <script src="index.js">'
  4. );