1.0.3 • Published 4 years ago

generate-examples-index-webpack-plugin v1.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

generate-examples-index-webpack-plugin

Webpack plugin to generate an index html page from the examples folder

Installation

npm:

npm install -D generate-examples-index-webpack-plugin

yarn:

yarn add --dev generate-examples-index-webpack-plugin

Usage

Use it as your regular webpack plugin.

// webpack.config.js
const ExamplesGenerator = require('generate-examples-index-webpack-plugin');

module.exports = {
  plugins: [
    new ExamplesGenerator(),
  ],
};

Example of files for the examples folder:

  • examples/
    • foo/
      • example1.ts
      • example1.html
      • example2.html
      • FooComponent.ts
    • bar/
      • index1.ts
      • index1.html
    • utils/
      • sharedFunctions.ts
      • Class1.ts
      • Class2.ts

Then, this plugin will generate a site with the following index:

  • foo/
    • example1
    • example2
  • bar/
    • index1

foo/example1, foo/example2 and bar/index1 are shown because there are html files which creates an entry point.

foo/example1.html will include example1.ts, and bar/index1.html will do the same with bar/index1.ts. foo/example2 will not include nothing automatically but it can still include built files from your application like this <script src="<% htmlWebpackPlugin.options.app %>/entryFile.js"></script>

On the other hand, foo/FooComponentjs and the files inside utils are not shown in the index because they have no associated .html file. As you can imagine, they are just files imported by the other main ones.

Like the previously mentioned app, some values are provided to be used in the html templates used for the templates. You can access them placing <% htmlWebpackPlugin.options.PLACEHOLDER %> in the html code, being PLACEHOLDER one of the following values:

Available placeholders

Index page

Placeholder nameDescription
assetsRoute to a folder with a copy of the application build results
buildDateDate of the building time. The resulting string can be customised with the provided date options
examplesFolderName of the original examples folder
examplesIndexHTML code with the generated examples index. Can be customised providing with the examplesIndexHtmlGenerator option
projectNameName of the project, directly from project.json
projectVersionVersion of the project, directly from project.json
reportLinkLink to the resulting report link generated if the analyzer option is not disabled

Examples pages

Placeholder nameDescription
appRelative path to the folder containing the results of the project build
assetsRelative path to the folder with the static assets (internal from the plugin and the content from the static option)
backLinkRelative path to the index page
breadcrumbsHTML code with the breadcrumbs for the current example
buildDateDate of the building time. The resulting string can be customised with the provided date options
examplesFolderName of the original examples folder
projectCssRelative path to the CSS file generated for the project
projectNameName of the project, directly from project.json
projectVersionVersion of the project, directly from project.json

Options

new ExamplesGenerator();
NameTypeDefaultDescription
addJsExtensionbooleanSet to true if your webpack options output.filename has no extension (because it comes from the vendor/file name). If not specified, it will try to guess the correct value from your webpack.options.output.filename value
analyzerboolean'assets/analyzer.htmlPath for the file with the analysis results of your examples provided by webpack-bundle-analyzer. Leave undefined to disable it
appstring'assets/app'Where the files of your application will be placed. It's accessible via the app placeholder.
assetsstring'assets'Where the assets will be placed.
breadcrumbsHtmlGeneratorfunctionundefinedfunction(route) returning the HTML for the breadcrumbs placeholder. Leave undefined to use the default one
buildTimeLocalestring'ja'How the date (accessible via the buildTime placeholder) will be outputted. Locale for Date.toLocaleString
buildTimeOptionsobjectHow the date (accessible via the buildTime placeholder) will be outputted. Options for Date.toLocaleString
examplesstring'examples'Path were your examples are placed. Accessible with the examplesFolder placeholder.
examplesFilterHtmlGeneratorfunctionundefinedfunction(entry) returning the HTML for the filtering box. Leave undefined to use the default one
examplesIndexHtmlGeneratorfunctionundefinedfunction(entry) returning the HTML for the examplesIndex placeholder. Leave undefined to use the default one
excludeAssetsbooleanfalseSet to true to hide the assets produced by this plugin
extensionsstring[]['js', 'jsx', 'ts', 'tsx']Extensions to check when finding the code file associated to an html page
noEntriesbooleanfalseSet to true if your project has no entry files (webpack is just used to generate examples with this plugin), to avoid the message ERROR in Entry module not found: Error: Can't resolve './src'
outputPathstring'examplesBuild'Your examples will be built and placed here
packageJsonstringundefinedRoute to the package.json of your application, just to retrieve information as the package name, version... If left undefined the plugin will try to find it automatically. It also can be an object with the data directly
staticstringassetsContent in this folder will be copied into the assets path so it's accessible via the assets placeholder
templateIndexstringundefinedPath with the index.html to use as a template. Leave undefined to use the default one
vendorContentstring[][]List of files or libraries to include in the generated vendor file
vendorFolderstringundefinedAll the files included in the folder specified by this path will be included in the vendor file
vendorNamestringvendorName for the generated vendor file

Changelog

1.0.3

  • Added options.extensions to allow other code files than .ts and .js (such as .tsx and .jsx)

1.0.2

  • assets are copied only if they exist to prevent errors from copy-webpack-plugin

1.0.1

  • @webpack-contrib/schema-utils moved to dependencies (not devDependencies)

1.0.0

  • First version.