UNPKG

robotstxt-webpack-plugin

Version:

A webpack plugin to output a robots.txt file

43 lines (34 loc) 1.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _generateRobotstxt = _interopRequireDefault(require("generate-robotstxt")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class RobotstxtPlugin { constructor(options = {}) { this.options = Object.assign({ filePath: "robots.txt" }, options); } apply(compiler) { const plugin = { name: this.constructor.name }; compiler.hooks.compilation.tap(plugin, compilation => { compilation.hooks.additionalAssets.tapPromise(plugin, () => (0, _generateRobotstxt.default)(this.options).then(contents => { const source = new compiler.webpack.sources.RawSource(contents); if (compilation.emitAsset) { compilation.emitAsset(this.options.filePath, source); } else { // Remove this after drop support for webpack@4 compilation.assets[this.options.filePath] = source; } return contents; }).catch(error => { compilation.errors.push(error); })); }); } } exports.default = RobotstxtPlugin;