177 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			177 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
"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 _posthtmlParser() {
 | 
						|
  const data = require("posthtml-parser");
 | 
						|
 | 
						|
  _posthtmlParser = function () {
 | 
						|
    return data;
 | 
						|
  };
 | 
						|
 | 
						|
  return data;
 | 
						|
}
 | 
						|
 | 
						|
function _nullthrows() {
 | 
						|
  const data = _interopRequireDefault(require("nullthrows"));
 | 
						|
 | 
						|
  _nullthrows = function () {
 | 
						|
    return data;
 | 
						|
  };
 | 
						|
 | 
						|
  return data;
 | 
						|
}
 | 
						|
 | 
						|
function _posthtml() {
 | 
						|
  const data = _interopRequireDefault(require("posthtml"));
 | 
						|
 | 
						|
  _posthtml = function () {
 | 
						|
    return data;
 | 
						|
  };
 | 
						|
 | 
						|
  return data;
 | 
						|
}
 | 
						|
 | 
						|
function _posthtmlRender() {
 | 
						|
  const data = require("posthtml-render");
 | 
						|
 | 
						|
  _posthtmlRender = function () {
 | 
						|
    return data;
 | 
						|
  };
 | 
						|
 | 
						|
  return data;
 | 
						|
}
 | 
						|
 | 
						|
function _semver() {
 | 
						|
  const data = _interopRequireDefault(require("semver"));
 | 
						|
 | 
						|
  _semver = function () {
 | 
						|
    return data;
 | 
						|
  };
 | 
						|
 | 
						|
  return data;
 | 
						|
}
 | 
						|
 | 
						|
var _dependencies = _interopRequireDefault(require("./dependencies"));
 | 
						|
 | 
						|
var _inline = _interopRequireDefault(require("./inline"));
 | 
						|
 | 
						|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 | 
						|
 | 
						|
var _default = new (_plugin().Transformer)({
 | 
						|
  canReuseAST({
 | 
						|
    ast
 | 
						|
  }) {
 | 
						|
    return ast.type === 'posthtml' && _semver().default.satisfies(ast.version, '^0.4.0');
 | 
						|
  },
 | 
						|
 | 
						|
  async parse({
 | 
						|
    asset
 | 
						|
  }) {
 | 
						|
    return {
 | 
						|
      type: 'posthtml',
 | 
						|
      version: '0.4.1',
 | 
						|
      program: (0, _posthtmlParser().parser)(await asset.getCode(), {
 | 
						|
        lowerCaseTags: true,
 | 
						|
        lowerCaseAttributeNames: true,
 | 
						|
        sourceLocations: true,
 | 
						|
        xmlMode: asset.type === 'xhtml'
 | 
						|
      })
 | 
						|
    };
 | 
						|
  },
 | 
						|
 | 
						|
  async transform({
 | 
						|
    asset,
 | 
						|
    options
 | 
						|
  }) {
 | 
						|
    if (asset.type === 'htm') {
 | 
						|
      asset.type = 'html';
 | 
						|
    }
 | 
						|
 | 
						|
    asset.bundleBehavior = 'isolated';
 | 
						|
    let ast = (0, _nullthrows().default)(await asset.getAST());
 | 
						|
    let hasScripts = (0, _dependencies.default)(asset, ast);
 | 
						|
    const {
 | 
						|
      assets: inlineAssets,
 | 
						|
      hasScripts: hasInlineScripts
 | 
						|
    } = (0, _inline.default)(asset, ast);
 | 
						|
    const result = [asset, ...inlineAssets]; // empty <script></script> is added to make sure HMR is working even if user
 | 
						|
    // didn't add any.
 | 
						|
 | 
						|
    if (options.hmrOptions && !(hasScripts || hasInlineScripts)) {
 | 
						|
      const script = {
 | 
						|
        tag: 'script',
 | 
						|
        attrs: {
 | 
						|
          src: asset.addURLDependency('hmr.js', {
 | 
						|
            priority: 'parallel'
 | 
						|
          })
 | 
						|
        },
 | 
						|
        content: []
 | 
						|
      };
 | 
						|
      const found = findFirstMatch(ast, [{
 | 
						|
        tag: 'body'
 | 
						|
      }, {
 | 
						|
        tag: 'html'
 | 
						|
      }]);
 | 
						|
 | 
						|
      if (found) {
 | 
						|
        found.content = found.content || [];
 | 
						|
        found.content.push(script);
 | 
						|
      } else {
 | 
						|
        // Insert at the very end.
 | 
						|
        ast.program.push(script);
 | 
						|
      }
 | 
						|
 | 
						|
      asset.setAST(ast);
 | 
						|
      result.push({
 | 
						|
        type: 'js',
 | 
						|
        content: '',
 | 
						|
        uniqueKey: 'hmr.js'
 | 
						|
      });
 | 
						|
    }
 | 
						|
 | 
						|
    return result;
 | 
						|
  },
 | 
						|
 | 
						|
  generate({
 | 
						|
    ast,
 | 
						|
    asset
 | 
						|
  }) {
 | 
						|
    return {
 | 
						|
      content: (0, _posthtmlRender().render)(ast.program, {
 | 
						|
        closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined
 | 
						|
      })
 | 
						|
    };
 | 
						|
  }
 | 
						|
 | 
						|
});
 | 
						|
 | 
						|
exports.default = _default;
 | 
						|
 | 
						|
function findFirstMatch(ast, expressions) {
 | 
						|
  let found;
 | 
						|
 | 
						|
  for (const expression of expressions) {
 | 
						|
    (0, _posthtml().default)().match.call(ast.program, expression, node => {
 | 
						|
      found = node;
 | 
						|
      return node;
 | 
						|
    });
 | 
						|
 | 
						|
    if (found) {
 | 
						|
      return found;
 | 
						|
    }
 | 
						|
  }
 | 
						|
} |