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

Bump swc to fix ESM helpers #8213

Merged
merged 5 commits into from Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 32 additions & 28 deletions Cargo.lock

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

@@ -0,0 +1 @@
class Test {}
@@ -0,0 +1,8 @@
{
"main": "dist/main.js",
"module": "dist/module.js",
"browserslist": "IE >= 11",
"dependencies": {
"@swc/helpers": "*"
}
}
40 changes: 40 additions & 0 deletions packages/core/integration-tests/test/transpilation.js
Expand Up @@ -11,6 +11,7 @@ import {
ncp,
} from '@parcel/test-utils';
import {symlinkSync} from 'fs';
import nullthrows from 'nullthrows';

const inputDir = path.join(__dirname, '/input');

Expand Down Expand Up @@ -355,6 +356,45 @@ describe('transpilation', function () {
});
});

it('should support commonjs and esm versions of @swc/helpers', async function () {
let b = await bundle(
path.join(__dirname, '/integration/swc-helpers-library/index.js'),
);

let file = await outputFS.readFile(
nullthrows(b.getBundles().find(b => b.env.outputFormat === 'commonjs'))
.filePath,
'utf8',
);
assert(file.includes('@swc/helpers/lib/_class_call_check.js'));

file = await outputFS.readFile(
nullthrows(b.getBundles().find(b => b.env.outputFormat === 'esmodule'))
.filePath,
'utf8',
);
assert(file.includes('@swc/helpers/src/_class_call_check.mjs'));
});

it('should support commonjs versions of @swc/helpers without scope hoisting', async function () {
let b = await bundle(
path.join(__dirname, '/integration/swc-helpers-library/index.js'),
{
targets: {
test: {
distDir,
isLibrary: true,
scopeHoist: false,
},
},
},
);

let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('@swc/helpers/lib/_class_call_check.js'));
await run(b);
});

it('should print errors from transpilation', async function () {
let source = path.join(
__dirname,
Expand Down
6 changes: 3 additions & 3 deletions packages/transformers/js/core/Cargo.toml
Expand Up @@ -8,9 +8,9 @@ edition = "2018"
crate-type = ["rlib"]

[dependencies]
swc_ecmascript = { version = "0.161.0", features = ["parser", "transforms", "module", "optimization", "react", "typescript", "utils", "visit", "codegen", "utils", "preset_env"] }
swc_common = { version = "0.18.8", features = ["tty-emitter", "sourcemap"] }
swc_atoms = "0.2.11"
swc_ecmascript = { version = "0.164.0", features = ["parser", "transforms", "module", "optimization", "react", "typescript", "utils", "visit", "codegen", "utils", "preset_env"] }
swc_common = { version = "0.18.9", features = ["tty-emitter", "sourcemap"] }
swc_atoms = "0.2.12"
indoc = "1.0.3"
serde = "1.0.123"
serde_bytes = "0.11.5"
Expand Down
47 changes: 38 additions & 9 deletions packages/transformers/js/core/src/dependency_collector.rs
Expand Up @@ -97,19 +97,36 @@ struct DependencyCollector<'a> {
impl<'a> DependencyCollector<'a> {
fn add_dependency(
&mut self,
specifier: JsWord,
mut specifier: JsWord,
span: swc_common::Span,
kind: DependencyKind,
attributes: Option<HashMap<swc_atoms::JsWord, bool>>,
is_optional: bool,
source_type: SourceType,
) -> Option<JsWord> {
// Rewrite SWC helpers from ESM to CJS for library output.
let mut is_specifier_rewritten = false;
if self.config.is_library && !self.config.is_esm_output {
if let Some(suffix) = specifier.strip_prefix("@swc/helpers/src/") {
if let Some(prefix) = suffix.strip_suffix(".mjs") {
specifier = format!("@swc/helpers/lib/{}.js", prefix).into();
is_specifier_rewritten = true;
}
}
}

// For normal imports/requires, the specifier will remain unchanged.
// For other types of dependencies, the specifier will be changed to a hash
// that also contains the dependency kind. This way, multiple kinds of dependencies
// to the same specifier can be used within the same file.
let placeholder = match kind {
DependencyKind::Import | DependencyKind::Export | DependencyKind::Require => None,
DependencyKind::Import | DependencyKind::Export | DependencyKind::Require => {
if is_specifier_rewritten {
Some(specifier.as_ref().to_owned())
} else {
None
}
}
_ => Some(format!(
"{:x}",
hash!(format!("{}:{}:{}", self.config.filename, specifier, kind))
Expand Down Expand Up @@ -256,12 +273,12 @@ impl<'a> Fold for DependencyCollector<'a> {
node.fold_children_with(self)
}

fn fold_import_decl(&mut self, node: ast::ImportDecl) -> ast::ImportDecl {
fn fold_import_decl(&mut self, mut node: ast::ImportDecl) -> ast::ImportDecl {
if node.type_only {
return node;
}

self.add_dependency(
let rewritten = self.add_dependency(
node.src.value.clone(),
node.src.span,
DependencyKind::Import,
Expand All @@ -270,30 +287,38 @@ impl<'a> Fold for DependencyCollector<'a> {
self.config.source_type,
);

if let Some(rewritten) = rewritten {
node.src.value = rewritten.into();
}

node
}

fn fold_named_export(&mut self, node: ast::NamedExport) -> ast::NamedExport {
if let Some(src) = &node.src {
fn fold_named_export(&mut self, mut node: ast::NamedExport) -> ast::NamedExport {
if let Some(src) = &mut node.src {
if node.type_only {
return node;
}

self.add_dependency(
let rewritten = self.add_dependency(
src.value.clone(),
src.span,
DependencyKind::Export,
None,
false,
self.config.source_type,
);

if let Some(rewritten) = rewritten {
src.value = rewritten.into();
}
}

node
}

fn fold_export_all(&mut self, node: ast::ExportAll) -> ast::ExportAll {
self.add_dependency(
fn fold_export_all(&mut self, mut node: ast::ExportAll) -> ast::ExportAll {
let rewritten = self.add_dependency(
node.src.value.clone(),
node.src.span,
DependencyKind::Export,
Expand All @@ -302,6 +327,10 @@ impl<'a> Fold for DependencyCollector<'a> {
self.config.source_type,
);

if let Some(rewritten) = rewritten {
node.src.value = rewritten.into();
}

node
}

Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/js/package.json
Expand Up @@ -35,7 +35,7 @@
"@parcel/source-map": "^2.0.0",
"@parcel/utils": "2.6.0",
"@parcel/workers": "2.6.0",
"@swc/helpers": "^0.3.15",
"@swc/helpers": "^0.4.2",
"browserslist": "^4.6.6",
"detect-libc": "^1.0.3",
"nullthrows": "^1.1.1",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -2357,10 +2357,10 @@
"@swc/core-win32-ia32-msvc" "^1.2.106"
"@swc/core-win32-x64-msvc" "^1.2.106"

"@swc/helpers@^0.3.15":
version "0.3.15"
resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.3.15.tgz#02abb377c91c39e0a6034f1161870c18621b9129"
integrity sha512-rpZHDdzwhfe06gF98SUAi7TfI344zKb1Pd2D9gxUMTNnhMobDHrv2UiVVcbDXmkx84U5AaXJmBrmfT9g1TPasQ==
"@swc/helpers@^0.4.2":
version "0.4.2"
resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.4.2.tgz#ed1f6997ffbc22396665d9ba74e2a5c0a2d782f8"
integrity sha512-556Az0VX7WR6UdoTn4htt/l3zPQ7bsQWK+HqdG4swV7beUCxo/BqmvbOpUkTIm/9ih86LIf1qsUnywNL3obGHw==
dependencies:
tslib "^2.4.0"

Expand Down