Unverified Commit 204445ff authored by Maia Teegarden's avatar Maia Teegarden Committed by GitHub

Add/amp attribute patcher (#27700)

* Add amp attribute transform

* Add amp attribute unit tests

* Fix ci if statements

* Checkout code in rust tests

* Add rust fixtures to eslintignore

* Add cargo config

* Configure toolchain for cross compilation
parent 4ab41efd
......@@ -24,4 +24,4 @@ packages/next-env/**/*.d.ts
packages/create-next-app/templates/**
test/integration/eslint/**
test-timings.json
packages/next/build/swc/tests/fixture/**
\ No newline at end of file
packages/next/build/swc/tests/fixture/**
......@@ -85,7 +85,7 @@ jobs:
with:
profile: minimal
override: true
toolchain: stable
toolchain: nightly-2021-03-25
target: aarch64-apple-darwin
- name: Install dependencies
......@@ -111,17 +111,17 @@ jobs:
steps:
- uses: actions/checkout@v2
if: ${{ env.GITHUB_EVENT_NAME == 'workflow_dispatch' }}
if: ${{ github.event_name == 'workflow_dispatch' }}
- uses: actions/download-artifact@v2
with:
name: next-swc-binaries
path: packages/next/native
if: ${{ env.GITHUB_EVENT_NAME == 'workflow_dispatch' }}
if: ${{ github.event_name == 'workflow_dispatch' }}
- uses: EndBug/add-and-commit@v7
with:
add: 'packages/next/native --force'
message: 'Build next-swc binaries'
if: ${{ env.GITHUB_EVENT_NAME == 'workflow_dispatch' }}
if: ${{ github.event_name == 'workflow_dispatch' }}
check:
needs: [build, build-apple-silicon]
......@@ -129,11 +129,26 @@ jobs:
steps:
- uses: actions/checkout@v2
if: ${{ env.GITHUB_EVENT_NAME == 'pull_request' }}
if: ${{ github.event_name == 'pull_request' }}
- uses: actions/download-artifact@v2
with:
name: next-swc-binaries
path: packages/next/native
if: ${{ env.GITHUB_EVENT_NAME == 'pull_request' }}
if: ${{ github.event_name == 'pull_request' }}
- run: git diff --exit-code
if: ${{ env.GITHUB_EVENT_NAME == 'pull_request' }}
if: ${{ github.event_name == 'pull_request' }}
test:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
if: ${{ github.event_name == 'pull_request' }}
- name: Install
if: ${{ github.event_name == 'pull_request' }}
uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2021-03-25
profile: minimal
- run: cd packages/next/build/swc && cargo test
if: ${{ github.event_name == 'pull_request' }}
[build]
rustflags = [
"--cfg", "procmacro2_semver_exempt",
]
\ No newline at end of file
......@@ -29,9 +29,9 @@ log = "0.4.14"
[build-dependencies]
napi-build = "1"
[profile.release]
lto = true
[dev-dependencies]
swc_ecma_transforms_testing = "0.24.0"
testing = "0.12.0"
[profile.release]
lto = true
nightly-2021-03-25
\ No newline at end of file
use swc_atoms::JsWord;
use swc_ecmascript::ast::{
Ident, JSXAttr, JSXAttrName, JSXAttrOrSpread, JSXElementName, JSXOpeningElement,
};
use swc_ecmascript::visit::Fold;
pub fn amp_attributes() -> impl Fold {
AmpAttributePatcher::default()
}
#[derive(Debug, Default)]
struct AmpAttributePatcher {}
impl Fold for AmpAttributePatcher {
fn fold_jsx_opening_element(&mut self, node: JSXOpeningElement) -> JSXOpeningElement {
let JSXOpeningElement {
name,
mut attrs,
span,
self_closing,
type_args,
} = node;
let n = name.clone();
if let JSXElementName::Ident(Ident { sym, .. }) = name {
if sym.starts_with("amp-") {
for i in 0..attrs.len() {
if let JSXAttrOrSpread::JSXAttr(JSXAttr {
name:
JSXAttrName::Ident(Ident {
sym,
span: s,
optional: o,
}),
span,
value,
}) = &attrs[i]
{
if sym as &str == "className" {
attrs[i] = JSXAttrOrSpread::JSXAttr(JSXAttr {
name: JSXAttrName::Ident(Ident {
sym: JsWord::from("class"),
span: s.clone(),
optional: o.clone(),
}),
span: span.clone(),
value: value.clone(),
})
}
}
}
}
}
JSXOpeningElement {
name: n,
attrs,
span,
self_closing,
type_args,
}
}
}
......@@ -45,6 +45,7 @@ use swc_common::{
FilePathMapping, SourceMap,
};
mod amp_attributes;
mod hook_optimizer;
pub mod next_ssg;
mod transform;
......
......@@ -27,6 +27,7 @@ DEALINGS IN THE SOFTWARE.
*/
use crate::{
amp_attributes::amp_attributes,
complete_output, get_compiler,
hook_optimizer::hook_optimizer,
next_ssg::next_ssg,
......@@ -173,7 +174,7 @@ fn process_js_custom(
}
};
let config = BuiltConfig {
pass: chain!(hook_optimizer(), next_ssg(), config.pass),
pass: chain!(hook_optimizer(), next_ssg(), amp_attributes(), config.pass),
syntax: config.syntax,
target: config.target,
minify: config.minify,
......@@ -183,7 +184,8 @@ fn process_js_custom(
is_module: config.is_module,
output_path: config.output_path,
};
//let orig = compiler.get_orig_src_map(&source, &options.config.input_source_map)?;
//let orig = compiler.get_orig_src_map(&source,
// &options.config.input_source_map)?;
let program = compiler.parse_js(
source.clone(),
config.target,
......
use self::amp_attributes::amp_attributes;
use self::next_ssg::next_ssg;
use std::path::PathBuf;
use swc_common::{chain, comments::SingleThreadedComments};
use swc_ecma_transforms_testing::{test, test_fixture};
use swc_ecmascript::{
parser::{EsConfig, Syntax},
transforms::react::jsx,
parser::{EsConfig, Syntax},
transforms::react::jsx,
};
use testing::fixture;
#[path = "../src/amp_attributes.rs"]
mod amp_attributes;
#[path = "../src/next_ssg.rs"]
mod next_ssg;
fn syntax() -> Syntax {
Syntax::Es(EsConfig {
jsx: true,
..Default::default()
})
Syntax::Es(EsConfig {
jsx: true,
..Default::default()
})
}
#[fixture("tests/fixture/amp/**/input.js")]
fn amp_attributes_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(syntax(), &|_tr| amp_attributes(), &input, &output);
}
#[fixture("tests/fixture/ssg/**/input.js")]
fn next_ssg_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|tr| {
let jsx = jsx::<SingleThreadedComments>(
tr.cm.clone(),
None,
swc_ecmascript::transforms::react::Options {
next: false,
runtime: None,
import_source: "".into(),
pragma: "__jsx".into(),
pragma_frag: "__jsxFrag".into(),
throw_if_namespace: false,
development: false,
use_builtins: true,
use_spread: true,
refresh: Default::default(),
},
);
chain!(next_ssg(), jsx)
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|tr| {
let jsx = jsx::<SingleThreadedComments>(
tr.cm.clone(),
None,
swc_ecmascript::transforms::react::Options {
next: false,
runtime: None,
import_source: "".into(),
pragma: "__jsx".into(),
pragma_frag: "__jsxFrag".into(),
throw_if_namespace: false,
development: false,
use_builtins: true,
use_spread: true,
refresh: Default::default(),
},
&input,
&output,
);
);
chain!(next_ssg(), jsx)
},
&input,
&output,
);
}
import React from 'react'
const comp = () => <amp-something className="something" />
import React from 'react'
const comp = () => <amp-something class="something" />
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment