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

Resolve helpers relative to JS transformer #6278

Merged
merged 1 commit into from
May 13, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(async () => {
console.log(await Promise.resolve(1));
})();
23 changes: 22 additions & 1 deletion packages/core/integration-tests/test/transpilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
distDir,
inputFS as fs,
outputFS,
overlayFS,
run,
ncp,
} from '@parcel/test-utils';
import {symlinkSync} from 'fs';

Expand Down Expand Up @@ -148,7 +150,6 @@ describe('transpilation', function() {
);

let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
console.log(file);
assert(file.includes('React.createElement("div"'));
assert(file.includes('...a'));
assert(!file.includes('@swc/helpers'));
Expand Down Expand Up @@ -179,6 +180,26 @@ describe('transpilation', function() {
assert(!file.includes('es.array.concat'));
});

it('should resolve @swc/helpers and regenerator-runtime relative to parcel', async function() {
let dir = path.join(
'/tmp/' +
Math.random()
.toString(36)
.slice(2),
);
await outputFS.mkdirp(dir);
ncp(path.join(__dirname, '/integration/swc-helpers'), dir);
Comment on lines +184 to +191
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this tmp directory for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so it's outside the monorepo. Otherwise it'll still resolve the helpers

await bundle(path.join(dir, 'index.js'), {
mode: 'production',
inputFS: overlayFS,
defaultTargetOptions: {
engines: {
browsers: '>= 0.25%',
},
},
});
});

describe('tests needing the real filesystem', () => {
afterEach(async () => {
if (process.platform === 'win32') {
Expand Down
1 change: 1 addition & 0 deletions packages/transformers/js/src/JSTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ export default (new Transformer({
isAsync: dep.kind === 'DynamicImport',
isOptional: dep.is_optional,
meta,
resolveFrom: dep.is_helper ? __filename : undefined,
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/transformers/js/src/dependency_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct DependencyDescriptor {
pub specifier: swc_atoms::JsWord,
pub attributes: Option<HashMap<swc_atoms::JsWord, bool>>,
pub is_optional: bool,
pub is_helper: bool,
}

/// This pass collects dependencies in a module and compiles references as needed to work with Parcel's JSRuntime.
Expand Down Expand Up @@ -84,6 +85,7 @@ impl<'a> DependencyCollector<'a> {
specifier,
attributes,
is_optional,
is_helper: span.is_dummy(),
});
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/transformers/js/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl<'a> InlineFS<'a> {
specifier: path.to_str().unwrap().into(),
attributes: None,
is_optional: false,
is_helper: false,
});

// If buffer, wrap in Buffer.from(base64String, 'base64')
Expand Down
2 changes: 2 additions & 0 deletions packages/transformers/js/src/global_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl<'a> Fold for GlobalReplacer<'a> {
specifier,
attributes: None,
is_optional: false,
is_helper: false,
});
}
"Buffer" => {
Expand All @@ -93,6 +94,7 @@ impl<'a> Fold for GlobalReplacer<'a> {
specifier,
attributes: None,
is_optional: false,
is_helper: false,
});
}
"__filename" => {
Expand Down