Skip to content

Commit

Permalink
Fix: not import @swc/helpers in script tag without type="module" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinyaigeek committed Jan 26, 2022
1 parent 74fcc3f commit a930ba5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
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

0 comments on commit a930ba5

Please sign in to comment.