Skip to content

Commit a930ba5

Browse files
authoredJan 26, 2022
Fix: not import @swc/helpers in script tag without type="module" (#7599)
1 parent 74fcc3f commit a930ba5

File tree

3 files changed

+39
-1
lines changed
  • packages
    • core/integration-tests/test
    • transformers/js/core/src

3 files changed

+39
-1
lines changed
 

‎packages/core/integration-tests/test/html.js

+24
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,30 @@ describe('html', function () {
14081408
assert(errored);
14091409
});
14101410

1411+
it('should not import swc/helpers without type="module"', async function () {
1412+
await bundle(
1413+
path.join(
1414+
__dirname,
1415+
'/integration/html-js-not-import-swc-helpers-without-module/index.html',
1416+
),
1417+
{
1418+
defaultTargetOptions: {
1419+
engines: {
1420+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#browser_compatibility
1421+
browsers: ['Chrome 48'],
1422+
},
1423+
},
1424+
},
1425+
);
1426+
1427+
let html = await outputFS.readFile(
1428+
path.join(distDir, 'index.html'),
1429+
'utf8',
1430+
);
1431+
assert(!html.includes('swc/helpers'));
1432+
assert(html.includes('slicedToArray'));
1433+
});
1434+
14111435
it('should allow imports and requires in inline <script> tags', async function () {
14121436
let b = await bundle(
14131437
path.join(__dirname, '/integration/html-inline-js-require/index.html'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<script>
3+
function arr() {
4+
return [1, 2, 3];
5+
}
6+
const [a, b, c] = arr();
7+
</script>
8+
</html>

‎packages/transformers/js/core/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,15 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
223223
let should_inline_fs = config.inline_fs
224224
&& config.source_type != SourceType::Script
225225
&& code.contains("readFileSync");
226+
let should_import_swc_helpers = match config.source_type {
227+
SourceType::Module => true,
228+
SourceType::Script => false,
229+
};
226230
swc_common::GLOBALS.set(&Globals::new(), || {
227231
helpers::HELPERS.set(
228-
&helpers::Helpers::new(/* external helpers from @swc/helpers */ true),
232+
&helpers::Helpers::new(
233+
/* external helpers from @swc/helpers */ should_import_swc_helpers,
234+
),
229235
|| {
230236
let mut react_options = react::Options::default();
231237
if config.is_jsx {

0 commit comments

Comments
 (0)
Please sign in to comment.