From cb93b6ad7604400396ee16726197f233ef4d3bca Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Thu, 14 Nov 2024 16:54:46 -0500 Subject: [PATCH] acorn options util and static and and private test cases --- packages/cli/src/lib/parsing-utils.js | 8 +++ packages/cli/src/lib/walker-package-ranger.js | 6 +- .../resource/plugin-standard-javascript.js | 6 +- .../src/components/counter.js | 12 ++-- packages/plugin-css-modules/src/index.js | 13 +--- .../serve.default/src/components/card.js | 70 ++++++------------- 6 files changed, 43 insertions(+), 72 deletions(-) create mode 100644 packages/cli/src/lib/parsing-utils.js diff --git a/packages/cli/src/lib/parsing-utils.js b/packages/cli/src/lib/parsing-utils.js new file mode 100644 index 000000000..4f886b774 --- /dev/null +++ b/packages/cli/src/lib/parsing-utils.js @@ -0,0 +1,8 @@ +const acornOptions = { + ecmaVersion: 'latest', + sourceType: 'module' +}; + +export { + acornOptions +}; \ No newline at end of file diff --git a/packages/cli/src/lib/walker-package-ranger.js b/packages/cli/src/lib/walker-package-ranger.js index 303d8ee2c..ac0f992b7 100644 --- a/packages/cli/src/lib/walker-package-ranger.js +++ b/packages/cli/src/lib/walker-package-ranger.js @@ -4,6 +4,7 @@ import fs from 'fs'; import { getNodeModulesLocationForPackage } from './node-modules-utils.js'; import path from 'path'; import * as walk from 'acorn-walk'; +import { acornOptions } from './parsing-utils.js'; const importMap = {}; @@ -46,10 +47,7 @@ async function getPackageEntryPath(packageJson) { async function walkModule(modulePath, dependency) { const moduleContents = fs.readFileSync(modulePath, 'utf-8'); - walk.simple(acorn.parse(moduleContents, { - ecmaVersion: 'latest', - sourceType: 'module' - }), { + walk.simple(acorn.parse(moduleContents, acornOptions), { async ImportDeclaration(node) { let { value: sourceValue } = node.source; const absoluteNodeModulesLocation = await getNodeModulesLocationForPackage(dependency); diff --git a/packages/cli/src/plugins/resource/plugin-standard-javascript.js b/packages/cli/src/plugins/resource/plugin-standard-javascript.js index be9e04f10..8b2c3647b 100644 --- a/packages/cli/src/plugins/resource/plugin-standard-javascript.js +++ b/packages/cli/src/plugins/resource/plugin-standard-javascript.js @@ -9,6 +9,7 @@ import { ResourceInterface } from '../../lib/resource-interface.js'; import terser from '@rollup/plugin-terser'; import * as acorn from 'acorn'; import * as walk from 'acorn-walk'; +import { acornOptions } from '../../lib/parsing-utils.js'; class StandardJavaScriptResource extends ResourceInterface { constructor(compilation, options) { @@ -42,10 +43,7 @@ class StandardJavaScriptResource extends ResourceInterface { const body = await response.clone().text(); let polyfilled = body; - walk.simple(acorn.parse(body, { - ecmaVersion: 'latest', - sourceType: 'module' - }), { + walk.simple(acorn.parse(body, acornOptions), { async ImportDeclaration(node) { const line = body.slice(node.start, node.end); const { value } = node.source; diff --git a/packages/cli/test/cases/serve.default.ssr/src/components/counter.js b/packages/cli/test/cases/serve.default.ssr/src/components/counter.js index 6cdf89304..6e9c72650 100644 --- a/packages/cli/test/cases/serve.default.ssr/src/components/counter.js +++ b/packages/cli/test/cases/serve.default.ssr/src/components/counter.js @@ -13,9 +13,12 @@ template.innerHTML = ` `; class MyCounter extends HTMLElement { + static staticProperty = 'foo'; + #count; + constructor() { super(); - this.count = 0; + this.#count = 0; this.attachShadow({ mode: 'open' }); } @@ -23,19 +26,20 @@ class MyCounter extends HTMLElement { this.shadowRoot.appendChild(template.content.cloneNode(true)); this.shadowRoot.getElementById('inc').onclick = () => this.inc(); this.shadowRoot.getElementById('dec').onclick = () => this.dec(); + this.#count = this.getAttribute('count') ?? this.#count; this.update(); } inc() { - this.update(++this.count); // eslint-disable-line + this.update(++this.#count); // eslint-disable-line } dec() { - this.update(--this.count); // eslint-disable-line + this.update(--this.#count); // eslint-disable-line } update(count) { - this.shadowRoot.getElementById('count').innerHTML = count || this.count; + this.shadowRoot.getElementById('count').innerHTML = count || this.#count; } } diff --git a/packages/plugin-css-modules/src/index.js b/packages/plugin-css-modules/src/index.js index 44ec6442d..aff376e20 100644 --- a/packages/plugin-css-modules/src/index.js +++ b/packages/plugin-css-modules/src/index.js @@ -11,6 +11,7 @@ import * as acornWalk from 'acorn-walk'; import * as acorn from 'acorn'; import { hashString } from '@greenwood/cli/src/lib/hashing-utils.js'; import { transform } from 'sucrase'; +import { acornOptions } from '@greenwood/cli/src/lib/parsing-utils.js'; const MODULES_MAP_FILENAME = '__css-modules-map.json'; /* @@ -39,11 +40,7 @@ function walkAllImportsForCssModules(scriptUrl, sheets, compilation) { }); acornWalk.simple( - acorn.parse(result.code, { - ecmaVersion: 'latest', - sourceType: 'module' - }), - { + acorn.parse(result.code, acornOptions), { ImportDeclaration(node) { const { specifiers = [], source = {} } = node; const { value = '' } = source; @@ -245,11 +242,7 @@ class StripCssModulesResource extends ResourceInterface { let contents = await response.text(); acornWalk.simple( - acorn.parse(contents, { - ecmaVersion: 'latest', - sourceType: 'module' - }), - { + acorn.parse(contents, acornOptions), { ImportDeclaration(node) { const { specifiers = [], source = {}, start, end } = node; const { value = '' } = source; diff --git a/packages/plugin-renderer-lit/test/cases/serve.default/src/components/card.js b/packages/plugin-renderer-lit/test/cases/serve.default/src/components/card.js index 32fb3e205..50f4c4d90 100644 --- a/packages/plugin-renderer-lit/test/cases/serve.default/src/components/card.js +++ b/packages/plugin-renderer-lit/test/cases/serve.default/src/components/card.js @@ -1,55 +1,25 @@ -import { LitElement, html } from 'lit'; +import { LitElement, html, css } from 'lit'; export default class Card extends LitElement { - // TODO we have a clash on acorn version? - // or this issue - https://github.com/ProjectEvergreen/greenwood/issues/1183 - // static properties = { - // title: '', - // thumbnail: '' - // }; - // static styles = css` - // div { - // display: flex; - // flex-direction: column; - // align-items: center; - // gap: 0.5rem; - // border: 1px solid #818181; - // width: fit-content; - // border-radius: 10px; - // padding: 2rem 1rem; - // height: 680px; - // justify-content: space-between; - // background-color: #fff; - // overflow-x: hidden; - // } - // button { - // background: var(--color-accent); - // color: var(--color-white); - // padding: 1rem 2rem; - // border: 0; - // font-size: 1rem; - // border-radius: 5px; - // cursor: pointer; - // } - // img { - // max-width: 500px; - // min-width: 500px; - // width: 100%; - // } - // h3 { - // font-size: 1.85rem; - // } - - // @media(max-width: 768px) { - // img { - // max-width: 300px; - // min-width: 300px; - // } - // div { - // height: 500px; - // } - // } - // `; + static styles = css` + h3 { + font-size: 1.85rem; + } + button { + background: var(--color-accent); + color: var(--color-white); + padding: 1rem 2rem; + border: 0; + font-size: 1rem; + border-radius: 5px; + cursor: pointer; + } + img { + max-width: 500px; + min-width: 500px; + width: 100%; + } + `; constructor() { super();