Skip to content

Commit

Permalink
Resolve helpers relative to JS transformer (#6278)
Browse files Browse the repository at this point in the history
Fixes T-1031
  • Loading branch information
devongovett committed May 13, 2021
1 parent ae4f6f8 commit 27d89ef
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
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);
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

0 comments on commit 27d89ef

Please sign in to comment.