From e9bd92ace78f85c86d71225df6dbdc17245b2189 Mon Sep 17 00:00:00 2001 From: Shinyaigeek Date: Sun, 23 Jan 2022 19:59:45 +0900 Subject: [PATCH 1/3] Update: when transforming javascript is Script, not include importing swc/helpers --- packages/transformers/js/core/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/transformers/js/core/src/lib.rs b/packages/transformers/js/core/src/lib.rs index 4022a0467de..58ba9bd4cea 100644 --- a/packages/transformers/js/core/src/lib.rs +++ b/packages/transformers/js/core/src/lib.rs @@ -223,9 +223,15 @@ pub fn transform(config: Config) -> Result { 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 { From 31b456bb36444ad62607a8c5eec37aebf53273e5 Mon Sep 17 00:00:00 2001 From: Shinyaigeek Date: Sun, 23 Jan 2022 20:04:39 +0900 Subject: [PATCH 2/3] Chore: add test case for importing swc/helpers in script without module --- packages/core/integration-tests/test/html.js | 22 +++++++++++++++++++ .../index.html | 8 +++++++ 2 files changed, 30 insertions(+) create mode 100644 packages/core/integration-tests/test/integration/html-js-not-import-swc-helpers-without-module/index.html diff --git a/packages/core/integration-tests/test/html.js b/packages/core/integration-tests/test/html.js index ec1c8e74ef7..a846691865b 100644 --- a/packages/core/integration-tests/test/html.js +++ b/packages/core/integration-tests/test/html.js @@ -1408,6 +1408,28 @@ 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: { + // 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 + \ No newline at end of file From b4362ce9794ecc2d0bf8ae4d4127947ce26c6ae8 Mon Sep 17 00:00:00 2001 From: Shinyaigeek Date: Sun, 23 Jan 2022 20:39:46 +0900 Subject: [PATCH 3/3] Chore: fix how to specify browserslist --- packages/core/integration-tests/test/html.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core/integration-tests/test/html.js b/packages/core/integration-tests/test/html.js index a846691865b..fd42fc4006d 100644 --- a/packages/core/integration-tests/test/html.js +++ b/packages/core/integration-tests/test/html.js @@ -1416,8 +1416,10 @@ describe('html', function () { ), { defaultTargetOptions: { - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#browser_compatibility - browsers: ['Chrome 48'], + engines: { + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#browser_compatibility + browsers: ['Chrome 48'], + }, }, }, );