How to use the @lwc/errors.CompilerError.from function in @lwc/errors

To help you get started, we’ve selected a few @lwc/errors examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github salesforce / lwc / packages / @lwc / template-compiler / src / index.ts View on Github external
export function compileToFunction(source: string): Function {
    const options = mergeConfig({});
    options.format = 'function';

    const state = new State(source, options);

    const parsingResults = parseTemplate(source, state);

    for (const warning of parsingResults.warnings) {
        if (warning.level === DiagnosticLevel.Error) {
            throw CompilerError.from(warning);
        } else if (warning.level === DiagnosticLevel.Warning) {
            /* eslint-disable-next-line no-console */
            console.warn(warning.message);
        } else {
            /* eslint-disable-next-line no-console */
            console.log(warning.message);
        }
    }

    if (!parsingResults.root) {
        throw generateCompilerError(TemplateErrors.INVALID_TEMPLATE);
    }

    const { code } = generate(parsingResults.root, state);
    return new Function(TEMPLATE_MODULES_PARAMETER, code);
}
github salesforce / lwc / packages / @lwc / compiler / src / transformers / template.ts View on Github external
filename: string,
    options: NormalizedCompilerOptions
): FileTransformerResult {
    let result;

    try {
        result = compile(src, {
            experimentalDynamicDirective: !!options.experimentalDynamicComponent,
        });
    } catch (e) {
        throw normalizeToCompilerError(TransformerErrors.HTML_TRANSFORMER_ERROR, e, { filename });
    }

    const fatalError = result.warnings.find(warning => warning.level === DiagnosticLevel.Error);
    if (fatalError) {
        throw CompilerError.from(fatalError, { filename });
    }

    // Rollup only cares about the mappings property on the map. Since producing a source map for
    // the template doesn't make sense, the transform returns an empty mappings.
    return {
        code: serialize(result.code, filename, options),
        map: { mappings: '' },
    };
}
github salesforce / lwc / packages / @lwc / compiler / src / transformers / template.ts View on Github external
filename: string,
    options: NormalizedTransformOptions
): FileTransformerResult {
    let result;

    try {
        result = compile(src, {
            experimentalDynamicDirective: !!options.experimentalDynamicComponent,
        });
    } catch (e) {
        throw normalizeToCompilerError(TransformerErrors.HTML_TRANSFORMER_ERROR, e, { filename });
    }

    const fatalError = result.warnings.find(warning => warning.level === DiagnosticLevel.Error);
    if (fatalError) {
        throw CompilerError.from(fatalError, { filename });
    }

    // Rollup only cares about the mappings property on the map. Since producing a source map for
    // the template doesn't make sense, the transform returns an empty mappings.
    return {
        code: serialize(result.code, filename, options),
        map: { mappings: '' },
    };
}