"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function _plugin() { const data = require("@parcel/plugin"); _plugin = function () { return data; }; return data; } function _path() { const data = _interopRequireDefault(require("path")); _path = function () { return data; }; return data; } function _posthtml() { const data = _interopRequireDefault(require("posthtml")); _posthtml = function () { return data; }; return data; } function _posthtmlParser() { const data = require("posthtml-parser"); _posthtmlParser = function () { return data; }; return data; } function _posthtmlRender() { const data = require("posthtml-render"); _posthtmlRender = function () { return data; }; return data; } function _nullthrows() { const data = _interopRequireDefault(require("nullthrows")); _nullthrows = function () { return data; }; return data; } function _semver() { const data = _interopRequireDefault(require("semver")); _semver = function () { return data; }; return data; } function _utils() { const data = require("@parcel/utils"); _utils = function () { return data; }; return data; } var _loadPlugins = _interopRequireDefault(require("./loadPlugins")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _default = new (_plugin().Transformer)({ async loadConfig({ config, options, logger }) { if (!config.isSource) { return; } let configFile = await config.getConfig(['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js'], { packageKey: 'posthtml' }); if (configFile) { let isJavascript = _path().default.extname(configFile.filePath) === '.js'; if (isJavascript) { // We have to invalidate on startup in case the config is non-deterministic, // e.g. using unknown environment variables, reading from the filesystem, etc. logger.warn({ message: 'WARNING: Using a JavaScript PostHTML config file means losing out on caching features of Parcel. Use a .posthtmlrc (JSON) file whenever possible.' }); config.invalidateOnStartup(); // Also add the config as a dev dependency so we attempt to reload in watch mode. config.addDevDependency({ specifier: (0, _utils().relativePath)(_path().default.dirname(config.searchPath), configFile.filePath), resolveFrom: config.searchPath }); } // Load plugins. This must be done before adding dev dependencies so we auto install. let plugins = await (0, _loadPlugins.default)(configFile.contents.plugins, config.searchPath, options); // Now add dev dependencies so we invalidate when they change. let pluginArray = Array.isArray(configFile.contents.plugins) ? configFile.contents.plugins : Object.keys(configFile.contents.plugins); for (let p of pluginArray) { if (typeof p === 'string') { config.addDevDependency({ specifier: p, resolveFrom: configFile.filePath }); } } configFile.contents.plugins = plugins; // tells posthtml that we have already called parse configFile.contents.skipParse = true; delete configFile.contents.render; return configFile.contents; } }, canReuseAST({ ast }) { return ast.type === 'posthtml' && _semver().default.satisfies(ast.version, '^0.4.0'); }, async parse({ asset, config }) { // if we don't have a config it is posthtml is not configure, don't parse if (!config) { return; } return { type: 'posthtml', version: '0.4.1', program: (0, _posthtmlParser().parser)(await asset.getCode(), { lowerCaseAttributeNames: true, sourceLocations: true, xmlMode: asset.type === 'xhtml' }) }; }, async transform({ asset, config }) { if (!config) { return [asset]; } let ast = (0, _nullthrows().default)(await asset.getAST()); let res = await (0, _posthtml().default)(config.plugins).process(ast.program, { ...config, plugins: config.plugins }); if (res.messages) { await Promise.all(res.messages.map(({ type, file: filePath }) => { if (type === 'dependency') { return asset.invalidateOnFileChange(filePath); } return Promise.resolve(); })); } asset.setAST({ type: 'posthtml', version: '0.4.1', program: JSON.parse(JSON.stringify(res.tree)) // posthtml adds functions to the AST that are not serializable }); return [asset]; }, generate({ ast, asset }) { return { content: (0, _posthtmlRender().render)(ast.program, { closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined }) }; } }); exports.default = _default;