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 ...@@ -24,4 +24,4 @@ packages/next-env/**/*.d.ts
packages/create-next-app/templates/** packages/create-next-app/templates/**
test/integration/eslint/** test/integration/eslint/**
test-timings.json test-timings.json
packages/next/build/swc/tests/fixture/** packages/next/build/swc/tests/fixture/**
\ No newline at end of file
...@@ -85,7 +85,7 @@ jobs: ...@@ -85,7 +85,7 @@ jobs:
with: with:
profile: minimal profile: minimal
override: true override: true
toolchain: stable toolchain: nightly-2021-03-25
target: aarch64-apple-darwin target: aarch64-apple-darwin
- name: Install dependencies - name: Install dependencies
...@@ -111,17 +111,17 @@ jobs: ...@@ -111,17 +111,17 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
if: ${{ env.GITHUB_EVENT_NAME == 'workflow_dispatch' }} if: ${{ github.event_name == 'workflow_dispatch' }}
- uses: actions/download-artifact@v2 - uses: actions/download-artifact@v2
with: with:
name: next-swc-binaries name: next-swc-binaries
path: packages/next/native path: packages/next/native
if: ${{ env.GITHUB_EVENT_NAME == 'workflow_dispatch' }} if: ${{ github.event_name == 'workflow_dispatch' }}
- uses: EndBug/add-and-commit@v7 - uses: EndBug/add-and-commit@v7
with: with:
add: 'packages/next/native --force' add: 'packages/next/native --force'
message: 'Build next-swc binaries' message: 'Build next-swc binaries'
if: ${{ env.GITHUB_EVENT_NAME == 'workflow_dispatch' }} if: ${{ github.event_name == 'workflow_dispatch' }}
check: check:
needs: [build, build-apple-silicon] needs: [build, build-apple-silicon]
...@@ -129,11 +129,26 @@ jobs: ...@@ -129,11 +129,26 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
if: ${{ env.GITHUB_EVENT_NAME == 'pull_request' }} if: ${{ github.event_name == 'pull_request' }}
- uses: actions/download-artifact@v2 - uses: actions/download-artifact@v2
with: with:
name: next-swc-binaries name: next-swc-binaries
path: packages/next/native path: packages/next/native
if: ${{ env.GITHUB_EVENT_NAME == 'pull_request' }} if: ${{ github.event_name == 'pull_request' }}
- run: git diff --exit-code - 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" ...@@ -29,9 +29,9 @@ log = "0.4.14"
[build-dependencies] [build-dependencies]
napi-build = "1" napi-build = "1"
[profile.release]
lto = true
[dev-dependencies] [dev-dependencies]
swc_ecma_transforms_testing = "0.24.0" swc_ecma_transforms_testing = "0.24.0"
testing = "0.12.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::{ ...@@ -45,6 +45,7 @@ use swc_common::{
FilePathMapping, SourceMap, FilePathMapping, SourceMap,
}; };
mod amp_attributes;
mod hook_optimizer; mod hook_optimizer;
pub mod next_ssg; pub mod next_ssg;
mod transform; mod transform;
......
...@@ -27,6 +27,7 @@ DEALINGS IN THE SOFTWARE. ...@@ -27,6 +27,7 @@ DEALINGS IN THE SOFTWARE.
*/ */
use crate::{ use crate::{
amp_attributes::amp_attributes,
complete_output, get_compiler, complete_output, get_compiler,
hook_optimizer::hook_optimizer, hook_optimizer::hook_optimizer,
next_ssg::next_ssg, next_ssg::next_ssg,
...@@ -173,7 +174,7 @@ fn process_js_custom( ...@@ -173,7 +174,7 @@ fn process_js_custom(
} }
}; };
let config = BuiltConfig { let config = BuiltConfig {
pass: chain!(hook_optimizer(), next_ssg(), config.pass), pass: chain!(hook_optimizer(), next_ssg(), amp_attributes(), config.pass),
syntax: config.syntax, syntax: config.syntax,
target: config.target, target: config.target,
minify: config.minify, minify: config.minify,
...@@ -183,7 +184,8 @@ fn process_js_custom( ...@@ -183,7 +184,8 @@ fn process_js_custom(
is_module: config.is_module, is_module: config.is_module,
output_path: config.output_path, 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( let program = compiler.parse_js(
source.clone(), source.clone(),
config.target, config.target,
......
use self::amp_attributes::amp_attributes;
use self::next_ssg::next_ssg; use self::next_ssg::next_ssg;
use std::path::PathBuf; use std::path::PathBuf;
use swc_common::{chain, comments::SingleThreadedComments}; use swc_common::{chain, comments::SingleThreadedComments};
use swc_ecma_transforms_testing::{test, test_fixture}; use swc_ecma_transforms_testing::{test, test_fixture};
use swc_ecmascript::{ use swc_ecmascript::{
parser::{EsConfig, Syntax}, parser::{EsConfig, Syntax},
transforms::react::jsx, transforms::react::jsx,
}; };
use testing::fixture; use testing::fixture;
#[path = "../src/amp_attributes.rs"]
mod amp_attributes;
#[path = "../src/next_ssg.rs"] #[path = "../src/next_ssg.rs"]
mod next_ssg; mod next_ssg;
fn syntax() -> Syntax { fn syntax() -> Syntax {
Syntax::Es(EsConfig { Syntax::Es(EsConfig {
jsx: true, jsx: true,
..Default::default() ..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")] #[fixture("tests/fixture/ssg/**/input.js")]
fn next_ssg_fixture(input: PathBuf) { fn next_ssg_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js"); let output = input.parent().unwrap().join("output.js");
test_fixture( test_fixture(
syntax(), syntax(),
&|tr| { &|tr| {
let jsx = jsx::<SingleThreadedComments>( let jsx = jsx::<SingleThreadedComments>(
tr.cm.clone(), tr.cm.clone(),
None, None,
swc_ecmascript::transforms::react::Options { swc_ecmascript::transforms::react::Options {
next: false, next: false,
runtime: None, runtime: None,
import_source: "".into(), import_source: "".into(),
pragma: "__jsx".into(), pragma: "__jsx".into(),
pragma_frag: "__jsxFrag".into(), pragma_frag: "__jsxFrag".into(),
throw_if_namespace: false, throw_if_namespace: false,
development: false, development: false,
use_builtins: true, use_builtins: true,
use_spread: true, use_spread: true,
refresh: Default::default(), refresh: Default::default(),
},
);
chain!(next_ssg(), jsx)
}, },
&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