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

Fix: not import @swc/helpers in script tag without type="module" #7599

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
24 changes: 24 additions & 0 deletions packages/core/integration-tests/test/html.js
Expand Up @@ -1408,6 +1408,30 @@ describe('html', function () {
assert(errored);
});

it('should not import swc/helpers without type="module"', async function () {
await bundle(
path.join(
__dirname,
'/integration/html-js-not-import-swc-helpers-without-module/index.html',
),
{
defaultTargetOptions: {
engines: {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#browser_compatibility
browsers: ['Chrome 48'],
},
},
},
);

let html = await outputFS.readFile(
path.join(distDir, 'index.html'),
'utf8',
);
assert(!html.includes('swc/helpers'));
assert(html.includes('slicedToArray'));
});

it('should allow imports and requires in inline <script> tags', async function () {
let b = await bundle(
path.join(__dirname, '/integration/html-inline-js-require/index.html'),
Expand Down
@@ -0,0 +1,8 @@
<html>
<script>
function arr() {
return [1, 2, 3];
}
const [a, b, c] = arr();
</script>
</html>
8 changes: 7 additions & 1 deletion packages/transformers/js/core/src/lib.rs
Expand Up @@ -223,9 +223,15 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
let should_inline_fs = config.inline_fs
&& config.source_type != SourceType::Script
&& code.contains("readFileSync");
let should_import_swc_helpers = match config.source_type {
SourceType::Module => true,
SourceType::Script => false,
};
swc_common::GLOBALS.set(&Globals::new(), || {
helpers::HELPERS.set(
&helpers::Helpers::new(/* external helpers from @swc/helpers */ true),
&helpers::Helpers::new(
/* external helpers from @swc/helpers */ should_import_swc_helpers,
),
|| {
let mut react_options = react::Options::default();
if config.is_jsx {
Expand Down