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

Remove this exports tracking for files with module syntax #9330

Merged
merged 7 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
41 changes: 41 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
overlayFS,
run,
runBundle,
fsFixture,
} from '@parcel/test-utils';

const bundle = (name, opts = {}) => {
Expand Down Expand Up @@ -2487,6 +2488,23 @@ describe('scope hoisting', function () {
assert.equal(await output, 42);
});

it('should handle TSC polyfills', async () => {
await fsFixture(overlayFS, __dirname)`
tsc-polyfill-es6
library.js:
var __polyfill = (this && this.__polyfill) || function (a) {return a;};
export default __polyfill('es6')

index.js:
import value from './library';
output = value;`;

let b = await bundle(path.join(__dirname, 'tsc-polyfill-es6/index.js'), {
inputFS: overlayFS,
});
assert.equal(await run(b), 'es6');
});

describe("considers an asset's closest package.json for sideEffects, not the package through which it found the asset", () => {
it('handles redirects up the tree', async () => {
let b = await bundle(
Expand Down Expand Up @@ -5265,6 +5283,29 @@ describe('scope hoisting', function () {

assert.deepEqual(await run(b), {test: 2});
});

it('should handle TSC polyfills', async () => {
await fsFixture(overlayFS, __dirname)`
tsc-polyfill-commonjs
library.js:
"use strict";
var __polyfill = (this && this.__polyfill) || function (a) {return a;};
exports.value = __polyfill('cjs')

index.js:
const value = require('./library');
output = value;
`;

let b = await bundle(
path.join(__dirname, 'tsc-polyfill-commonjs/index.js'),
{
inputFS: overlayFS,
},
);

assert.deepEqual(await run(b), {value: 'cjs'});
});
});

it('should not throw with JS included from HTML', async function () {
Expand Down
5 changes: 4 additions & 1 deletion packages/transformers/js/core/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct Collect {
in_function: bool,
in_assign: bool,
in_class: bool,
is_module: bool,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -120,12 +121,14 @@ impl Collect {
ignore_mark: Mark,
global_mark: Mark,
trace_bailouts: bool,
is_module: bool,
) -> Self {
Collect {
source_map,
decls,
ignore_mark,
global_mark,
is_module,
static_cjs_exports: true,
has_cjs_exports: false,
is_esm: false,
Expand Down Expand Up @@ -768,7 +771,7 @@ impl Visit for Collect {
}
}
Expr::Bin(bin_expr) => {
if self.in_module_this {
if self.is_module & self.in_module_this {
// Some TSC polyfills use a pattern like below.
// We want to avoid marking these modules as CJS
// e.g. var _polyfill = (this && this.polyfill) || function () {}
Expand Down
2 changes: 2 additions & 0 deletions packages/transformers/js/core/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn inline_fs<'a>(
global_mark: Mark,
project_root: &'a str,
deps: &'a mut Vec<DependencyDescriptor>,
is_module: bool,
) -> impl Fold + 'a {
InlineFS {
filename: Path::new(filename).to_path_buf(),
Expand All @@ -26,6 +27,7 @@ pub fn inline_fs<'a>(
Mark::fresh(Mark::root()),
global_mark,
false,
is_module,
),
global_mark,
project_root,
Expand Down
1 change: 1 addition & 0 deletions packages/transformers/js/core/src/hoist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,7 @@ mod tests {
Mark::fresh(Mark::root()),
global_mark,
true,
true,
);
module.visit_with(&mut collect);

Expand Down
3 changes: 3 additions & 0 deletions packages/transformers/js/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
),
));

let is_module = module.is_module();
Copy link
Member

Choose a reason for hiding this comment

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

What does this do exactly? What is the definition of a "module" here? Anything with an import/export? We also compute this within collect itself already.

Copy link
Member

Choose a reason for hiding this comment

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

// If it's a script, convert into module. This needs to happen after
// the resolver (which behaves differently for non-/strict mode).
let module = match module {
Expand Down Expand Up @@ -342,6 +343,7 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
global_mark,
&config.project_root,
&mut fs_deps,
is_module
),
should_inline_fs
),
Expand Down Expand Up @@ -448,6 +450,7 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
ignore_mark,
global_mark,
config.trace_bailouts,
is_module,
);
module.visit_with(&mut collect);
if let Some(bailouts) = &collect.bailouts {
Expand Down