Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use content tag parser #1

Open
wants to merge 2 commits into
base: add-glimmer-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/import-cost/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@babel/traverse": "^7.12.9",
"@babel/types": "^7.12.7",
"cheerio": "^1.0.0-rc.10",
"content-tag": "^1.1.2",
"css-loader": "^6.7.1",
"file-loader": "^6.2.0",
"memfs": "^3.4.1",
Expand Down
15 changes: 3 additions & 12 deletions packages/import-cost/src/parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const cheerio = require('cheerio');
const { getPackages: getPackagesFromJS } = require('./babel-parser.js');
const { Lang } = require('./langs.js');
const { Preprocessor } = require('content-tag');

function extractScriptFromHtml(html) {
try {
Expand All @@ -23,20 +24,10 @@ function getScriptTagLineNumber(html) {
return 0;
}

function stripTemplateTag(source) {
// Replace all template tags within an assignment expression or a return to a no-op
let sourceStripped = source.replace(
/(=|return)\s*?<template>(.|\n)*?<\/template>/g,
'$1 () => {}',
);

// Strip all template tags within a class body
return sourceStripped.replace(/<template>(.|\n)*?<\/template>/g, '');
}

function getPackages(fileName, source, language) {
if ([Lang.GLIMMER_JS, Lang.GLIMMER_TS].some(l => l === language)) {
const scriptSource = stripTemplateTag(source);
const preprocessor = new Preprocessor();
const scriptSource = preprocessor.process(source);
const baseLanguage = Lang.GLIMMER_TS ? Lang.TYPESCRIPT : Lang.JAVASCRIPT;
return getPackagesFromJS(fileName, scriptSource, baseLanguage);
} else if ([Lang.SVELTE, Lang.VUE].some(l => l === language)) {
Expand Down