Skip to content

Commit

Permalink
allow disabling "use strict" emit in SWC when you reeeally want to (#…
Browse files Browse the repository at this point in the history
…1537)

* allow disabling "use strict" emit in SWC when you reeeally want to

* lint-fix
  • Loading branch information
cspotcode committed Nov 3, 2021
1 parent 2385967 commit d44c1ec
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 56 deletions.
125 changes: 70 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion src/transpilers/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
module,
jsxFactory,
jsxFragmentFactory,
strict,
alwaysStrict,
noImplicitUseStrict,
} = compilerOptions;
const nonTsxOptions = createSwcOptions(false);
const tsxOptions = createSwcOptions(true);
Expand All @@ -71,21 +74,39 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
}
swcTarget = swcTargets[swcTargetIndex];
const keepClassNames = target! >= /* ts.ScriptTarget.ES2016 */ 3;
// swc only supports these 4x module options
const moduleType =
module === ModuleKind.CommonJS
? 'commonjs'
: module === ModuleKind.AMD
? 'amd'
: module === ModuleKind.UMD
? 'umd'
: undefined;
: 'es6';
// In swc:
// strictMode means `"use strict"` is *always* emitted for non-ES module, *never* for ES module where it is assumed it can be omitted.
// (this assumption is invalid, but that's the way swc behaves)
// tsc is a bit more complex:
// alwaysStrict will force emitting it always unless `import`/`export` syntax is emitted which implies it per the JS spec.
// if not alwaysStrict, will emit implicitly whenever module target is non-ES *and* transformed module syntax is emitted.
// For node, best option is to assume that all scripts are modules (commonjs or esm) and thus should get tsc's implicit strict behavior.

// Always set strictMode, *unless* alwaysStrict is disabled and noImplicitUseStrict is enabled
const strictMode =
// if `alwaysStrict` is disabled, remembering that `strict` defaults `alwaysStrict` to true
(alwaysStrict === false || (alwaysStrict !== true && strict !== true)) &&
// if noImplicitUseStrict is enabled
noImplicitUseStrict === true
? false
: true;
return {
sourceMaps: sourceMap,
// isModule: true,
module: moduleType
? ({
noInterop: !esModuleInterop,
type: moduleType,
strictMode,
} as swcTypes.ModuleConfig)
: undefined,
swcrc: false,
Expand Down

0 comments on commit d44c1ec

Please sign in to comment.