├── .prettierrc.json
├── .npmignore
├── .gitignore
├── .travis.yml
├── jasmine.json
├── declarations.d.ts
├── .editorconfig
├── tsconfig.json
├── LICENSE.md
├── index.ts
├── package.json
├── README.md
├── test
└── plugin.spec.ts
└── CHANGELOG.md
/.prettierrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true
3 | }
4 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | **/*
2 | !**/*.js
3 | !**/*.map
4 | !**/*.d.ts
5 | !**/*.md
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules
2 | /coverage
3 | /.nyc_output
4 | **/*.js
5 | **/*.map
6 | **/*.d.ts
7 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "node"
4 | script:
5 | - npm test
6 | - npm run coveralls
7 |
--------------------------------------------------------------------------------
/jasmine.json:
--------------------------------------------------------------------------------
1 | {
2 | "spec_files": ["**/*spec.ts"],
3 | "stopSpecOnExpectationFailure": false,
4 | "random": true
5 | }
6 |
--------------------------------------------------------------------------------
/declarations.d.ts:
--------------------------------------------------------------------------------
1 | declare module 'rollup-pluginutils' {
2 | export function createFilter(
3 | include?: string | string[],
4 | exclude?: string | string[]
5 | ): (id: string) => boolean;
6 | }
7 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # editorconfig.org
2 |
3 | root = true
4 |
5 | [*]
6 | indent_style = space
7 | indent_size = 2
8 | end_of_line = lf
9 | charset = utf-8
10 | trim_trailing_whitespace = true
11 | insert_final_newline = true
12 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "declaration": true,
4 | "module": "commonjs",
5 | "moduleResolution": "node",
6 | "noUnusedLocals": true,
7 | "noUnusedParameters": true,
8 | "sourceMap": true,
9 | "strict": true,
10 | "target": "esnext"
11 | },
12 | "include": ["index.ts", "declarations.d.ts"]
13 | }
14 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright 2018 Elizabeth Mitchell
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 |
--------------------------------------------------------------------------------
/index.ts:
--------------------------------------------------------------------------------
1 | ///
2 | import * as minify from 'minify-html-literals';
3 | import {
4 | Plugin,
5 | SourceDescription,
6 | TransformHook,
7 | PluginContext
8 | } from 'rollup';
9 | import { createFilter } from 'rollup-pluginutils';
10 |
11 | /**
12 | * Plugin options.
13 | */
14 | export interface Options {
15 | /**
16 | * Pattern or array of patterns of files to minify.
17 | */
18 | include?: string | string[];
19 | /**
20 | * Pattern or array of patterns of files not to minify.
21 | */
22 | exclude?: string | string[];
23 | /**
24 | * Minify options, see
25 | * https://www.npmjs.com/package/minify-html-literals#options.
26 | */
27 | options?: Partial;
28 | /**
29 | * If true, any errors while parsing or minifying will abort the bundle
30 | * process. Defaults to false, which will only show a warning.
31 | */
32 | failOnError?: boolean;
33 | /**
34 | * Override minify-html-literals function.
35 | */
36 | minifyHTMLLiterals?: typeof minify.minifyHTMLLiterals;
37 | /**
38 | * Override include/exclude filter.
39 | */
40 | filter?: (id: string) => boolean;
41 | }
42 |
43 | export default function(
44 | options: Options = {}
45 | ): Plugin & { transform: TransformHook } {
46 | if (!options.minifyHTMLLiterals) {
47 | options.minifyHTMLLiterals = minify.minifyHTMLLiterals;
48 | }
49 |
50 | if (!options.filter) {
51 | options.filter = createFilter(options.include, options.exclude);
52 | }
53 |
54 | const minifyOptions = options.options || {};
55 |
56 | return {
57 | name: 'minify-html-literals',
58 | transform(this: PluginContext, code: string, id: string) {
59 | if (options.filter!(id)) {
60 | try {
61 | return options.minifyHTMLLiterals!(code, {
62 | ...minifyOptions,
63 | fileName: id
64 | });
65 | } catch (error) {
66 | // check if Error ese treat as string
67 | const message =
68 | error instanceof Error ? error.message : (error as string);
69 |
70 | if (options.failOnError) {
71 | this.error(message);
72 | } else {
73 | this.warn(message);
74 | }
75 | }
76 | }
77 | }
78 | };
79 | }
80 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "rollup-plugin-minify-html-literals",
3 | "version": "1.2.6",
4 | "description": "Rollup plugin to minify HTML template literal strings",
5 | "main": "index.js",
6 | "types": "index.d.ts",
7 | "author": "Liz Mitchell ",
8 | "license": "MIT",
9 | "keywords": [
10 | "rollup-plugin",
11 | "minify",
12 | "html",
13 | "literal",
14 | "literals",
15 | "template",
16 | "tagged",
17 | "lit-html"
18 | ],
19 | "repository": {
20 | "type": "git",
21 | "url": "https://github.com/asyncLiz/rollup-plugin-minify-html-literals.git"
22 | },
23 | "scripts": {
24 | "clean": "rimraf \"src/**/*.{js*,d.ts}\" && rimraf \"index.{js*,d.ts}\" && rimraf \"{.nyc_output/,coverage/}\"",
25 | "prebuild": "npm run clean",
26 | "build": "tsc",
27 | "pretest": "npm run clean",
28 | "test": "nyc mocha -r ts-node/register -r source-map-support/register test/*.ts",
29 | "coveralls": "cat coverage/lcov.info | coveralls",
30 | "lint": "prettier --write \"*.{js,ts,json,md}\" \"{src,test}/**/*\"",
31 | "precommit": "lint-staged",
32 | "release": "standard-version",
33 | "prepublishOnly": "npm run build"
34 | },
35 | "lint-staged": {
36 | "*.{js,ts,json,md}": [
37 | "prettier --write",
38 | "git add"
39 | ]
40 | },
41 | "nyc": {
42 | "extension": [
43 | ".ts"
44 | ],
45 | "include": [
46 | "index.ts"
47 | ],
48 | "exclude": [
49 | "index.d.ts"
50 | ],
51 | "reporter": [
52 | "html",
53 | "lcovonly",
54 | "text-summary"
55 | ],
56 | "all": true
57 | },
58 | "dependencies": {
59 | "minify-html-literals": "^1.3.5",
60 | "rollup-pluginutils": "^2.8.2"
61 | },
62 | "peerDependencies": {
63 | "rollup": "^0.65.2 || ^1.0.0 || ^2.0.0 || ^3.0.0"
64 | },
65 | "devDependencies": {
66 | "@types/chai": "^4.1.4",
67 | "@types/mocha": "^5.2.5",
68 | "@types/node": "^14.14.32",
69 | "@types/sinon": "^5.0.1",
70 | "chai": "^4.1.2",
71 | "coveralls": "^3.0.2",
72 | "husky": "^0.14.3",
73 | "lint-staged": "^8.2.1",
74 | "mocha": "^7.2.0",
75 | "nyc": "^15.0.0",
76 | "prettier": "^1.13.7",
77 | "rimraf": "^2.6.2",
78 | "rollup": "^3.5.1",
79 | "sinon": "^6.1.4",
80 | "source-map-support": "^0.5.6",
81 | "standard-version": "^9.0.0",
82 | "ts-node": "^7.0.0",
83 | "typescript": "^4.2.3"
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # rollup-plugin-minify-html-literals
2 |
3 | [](https://www.npmjs.com/package/rollup-plugin-minify-html-literals)
4 | [](https://travis-ci.com/asyncLiz/rollup-plugin-minify-html-literals)
5 | [](https://coveralls.io/github/asyncLiz/rollup-plugin-minify-html-literals?branch=master)
6 |
7 | Uses [minify-html-literals](https://www.npmjs.com/package/minify-html-literals) to minify HTML and CSS markup inside JavaScript template literal strings.
8 |
9 | ## Usage
10 |
11 | ```js
12 | import babel from 'rollup-plugin-babel';
13 | import minifyHTML from 'rollup-plugin-minify-html-literals';
14 | import { uglify } from 'rollup-plugin-uglify';
15 |
16 | export default {
17 | entry: 'index.js',
18 | dest: 'dist/index.js',
19 | plugins: [
20 | minifyHTML(),
21 | // Order plugin before transpilers and other minifiers
22 | babel(),
23 | uglify()
24 | ]
25 | };
26 | ```
27 |
28 | By default, this will minify any tagged template literal string whose tag contains "html" or "css" (case insensitive). [Additional options](https://www.npmjs.com/package/minify-html-literals#options) may be specified to control what templates should be minified.
29 |
30 | ## Options
31 |
32 | ```js
33 | export default {
34 | entry: 'index.js',
35 | dest: 'dist/index.js',
36 | plugins: [
37 | minifyHTML({
38 | // minimatch of files to minify
39 | include: [],
40 | // minimatch of files not to minify
41 | exclude: [],
42 | // set to `true` to abort bundling on a minification error
43 | failOnError: false,
44 | // minify-html-literals options
45 | // https://www.npmjs.com/package/minify-html-literals#options
46 | options: null,
47 |
48 | // Advanced Options
49 | // Override minify-html-literals function
50 | minifyHTMLLiterals: null,
51 | // Override rollup-pluginutils filter from include/exclude
52 | filter: null
53 | })
54 | ]
55 | };
56 | ```
57 |
58 | ## Examples
59 |
60 | ### Minify Polymer Templates
61 |
62 | ```js
63 | import minifyHTML from 'rollup-plugin-minify-html-literals';
64 | import { defaultShouldMinify } from 'minify-html-literals';
65 |
66 | export default {
67 | entry: 'index.js',
68 | dest: 'dist/index.js',
69 | plugins: [
70 | minifyHTML({
71 | options: {
72 | shouldMinify(template) {
73 | return (
74 | defaultShouldMinify(template) ||
75 | template.parts.some(part => {
76 | // Matches Polymer templates that are not tagged
77 | return (
78 | part.text.includes('