Skip to content

Commit

Permalink
Start introducing new blockBindings option
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 1, 2021
1 parent bc23d49 commit 02e3432
Show file tree
Hide file tree
Showing 27 changed files with 158 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/finalisers/amd.ts
Expand Up @@ -22,7 +22,6 @@ export default function amd(
namedExportsMode,
outro,
snippets,
varOrConst,
warn
}: FinaliserOptions,
{
Expand Down Expand Up @@ -64,7 +63,6 @@ export default function amd(
magicString.prepend(
`${intro}${getInteropBlock(
dependencies,
varOrConst,
interop,
externalLiveBindings,
freeze,
Expand Down
14 changes: 5 additions & 9 deletions src/finalisers/cjs.ts
Expand Up @@ -19,8 +19,7 @@ export default function cjs(
isModuleFacade,
namedExportsMode,
outro,
snippets,
varOrConst
snippets
}: FinaliserOptions,
{
compact,
Expand All @@ -45,10 +44,9 @@ export default function cjs(
if (namespaceMarkers) {
namespaceMarkers += n + n;
}
const importBlock = getImportBlock(dependencies, snippets, compact, varOrConst);
const importBlock = getImportBlock(dependencies, snippets, compact);
const interopBlock = getInteropBlock(
dependencies,
varOrConst,
interop,
externalLiveBindings,
freeze,
Expand Down Expand Up @@ -76,9 +74,8 @@ export default function cjs(

function getImportBlock(
dependencies: ChunkDependencies,
{ n, _ }: GenerateCodeSnippets,
compact: boolean,
varOrConst: string
{ _, cnst, n }: GenerateCodeSnippets,
compact: boolean
): string {
let importBlock = '';
let definingVariable = false;
Expand All @@ -90,8 +87,7 @@ function getImportBlock(
definingVariable = false;
importBlock += `require('${id}')`;
} else {
importBlock +=
compact && definingVariable ? ',' : `${importBlock ? `;${n}` : ''}${varOrConst} `;
importBlock += compact && definingVariable ? ',' : `${importBlock ? `;${n}` : ''}${cnst} `;
definingVariable = true;
importBlock += `${name}${_}=${_}require('${id}')`;
}
Expand Down
1 change: 0 additions & 1 deletion src/finalisers/iife.ts
Expand Up @@ -80,7 +80,6 @@ export default function iife(
const useStrict = strict ? `${t}'use strict';${n}` : '';
const interopBlock = getInteropBlock(
dependencies,
varOrConst,
interop,
externalLiveBindings,
freeze,
Expand Down
5 changes: 2 additions & 3 deletions src/finalisers/shared/getInteropBlock.ts
Expand Up @@ -10,7 +10,6 @@ import {

export default function getInteropBlock(
dependencies: ModuleDeclarationDependency[],
varOrConst: string,
interop: GetInterop,
externalLiveBindings: boolean,
freeze: boolean,
Expand All @@ -19,7 +18,7 @@ export default function getInteropBlock(
indent: string,
snippets: GenerateCodeSnippets
): string {
const { _, n } = snippets;
const { _, cnst, n } = snippets;
const neededInteropHelpers = new Set<string>();
const interopStatements: string[] = [];
const addInteropStatement = (
Expand All @@ -29,7 +28,7 @@ export default function getInteropBlock(
): void => {
neededInteropHelpers.add(helper);
interopStatements.push(
`${varOrConst} ${helperVariableName}${_}=${_}/*#__PURE__*/${helper}(${dependencyVariableName});`
`${cnst} ${helperVariableName}${_}=${_}/*#__PURE__*/${helper}(${dependencyVariableName});`
);
};
for (const {
Expand Down
1 change: 0 additions & 1 deletion src/finalisers/system.ts
Expand Up @@ -155,7 +155,6 @@ const getStarExcludes = ({ dependencies, exports }: ModuleDeclarations): Set<str
return starExcludes;
};

// TODO Lukas _starExcludes must not conflict with local variables.
const getStarExcludesBlock = (
starExcludes: Set<string> | null,
varOrConst: string,
Expand Down
2 changes: 0 additions & 2 deletions src/finalisers/umd.ts
Expand Up @@ -42,7 +42,6 @@ export default function umd(
namedExportsMode,
outro,
snippets,
varOrConst,
warn
}: FinaliserOptions,
{
Expand Down Expand Up @@ -181,7 +180,6 @@ export default function umd(
magicString.prepend(
`${intro}${getInteropBlock(
dependencies,
varOrConst,
interop,
externalLiveBindings,
freeze,
Expand Down
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Expand Up @@ -583,6 +583,7 @@ type GeneratedCodePreset = 'es5' | 'es2015';

interface NormalizedGeneratedCodeOptions {
arrowFunctions: boolean;
blockBindings: boolean;
objectShorthand: boolean;
reservedNamesAsProps: boolean;
}
Expand Down
10 changes: 7 additions & 3 deletions src/utils/generateCodeSnippets.ts
Expand Up @@ -3,6 +3,7 @@ import { RESERVED_NAMES } from './reservedNames';

export interface GenerateCodeSnippets {
_: string;
cnst: string;
directReturnFunctionRight: string;
n: string;
namedDirectReturnFunctionRight: string;
Expand All @@ -27,18 +28,20 @@ export interface GenerateCodeSnippets {
getPropertyAccess(name: string): string;
}

// TODO Lukas deprecate preferConst
export function getGenerateCodeSnippets({
compact,
generatedCode: { arrowFunctions, objectShorthand, reservedNamesAsProps }
generatedCode: { arrowFunctions, blockBindings, objectShorthand, reservedNamesAsProps },
preferConst
}: NormalizedOutputOptions): GenerateCodeSnippets {
const { _, n, s } = compact ? { _: '', n: '', s: '' } : { _: ' ', n: '\n', s: ';' };
const cnst = blockBindings || preferConst ? 'const' : 'var';

// TODO Lukas use const
const getFunctionIntro: GenerateCodeSnippets['getFunctionIntro'] = arrowFunctions
? (params, { isAsync, name }) => {
const singleParam = params.length === 1;
const asyncString = isAsync ? `async${singleParam ? ' ' : _}` : '';
return `${name ? `var ${name}${_}=${_}` : ''}${asyncString}${
return `${name ? `${cnst} ${name}${_}=${_}` : ''}${asyncString}${
singleParam ? params[0] : `(${params.join(`,${_}`)})`
}${_}=>${_}`;
}
Expand All @@ -64,6 +67,7 @@ export function getGenerateCodeSnippets({

return {
_,
cnst,
directReturnFunctionRight,
getDirectReturnFunctionLeft,
getDirectReturnIifeLeft: (params, returned, { needsArrowReturnParens, needsWrappedFunction }) =>
Expand Down
1 change: 1 addition & 0 deletions src/utils/options/normalizeOutputOptions.ts
Expand Up @@ -324,6 +324,7 @@ const getGeneratedCode = (config: OutputOptions): NormalizedOutputOptions['gener
);
return {
arrowFunctions: configWithPreset.arrowFunctions === true,
blockBindings: configWithPreset.blockBindings === true,
objectShorthand: configWithPreset.objectShorthand === true,
reservedNamesAsProps: configWithPreset.reservedNamesAsProps === true
};
Expand Down
2 changes: 2 additions & 0 deletions src/utils/options/options.ts
Expand Up @@ -78,11 +78,13 @@ export const generatedCodePresets: {
} = {
es2015: {
arrowFunctions: true,
blockBindings: true,
objectShorthand: true,
reservedNamesAsProps: true
},
es5: {
arrowFunctions: false,
blockBindings: false,
objectShorthand: false,
reservedNamesAsProps: false
}
Expand Down
10 changes: 10 additions & 0 deletions test/form/samples/generated-code/block-bindings-false/_config.js
@@ -0,0 +1,10 @@
module.exports = {
description: 'does not use block bindings',
options: {
external: ['external'],
output: {
generatedCode: { arrowFunctions: true, blockBindings: false },
name: 'bundle'
}
}
};
@@ -0,0 +1,9 @@
define(['external'], (foo => { 'use strict';

var _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { 'default': e };

var foo__default = /*#__PURE__*/_interopDefaultLegacy(foo);

console.log(foo__default["default"]);

}));
@@ -0,0 +1,9 @@
'use strict';

var foo = require('external');

var _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { 'default': e };

var foo__default = /*#__PURE__*/_interopDefaultLegacy(foo);

console.log(foo__default["default"]);
@@ -0,0 +1,3 @@
import foo from 'external';

console.log(foo);
@@ -0,0 +1,10 @@
(foo => {
'use strict';

var _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { 'default': e };

var foo__default = /*#__PURE__*/_interopDefaultLegacy(foo);

console.log(foo__default["default"]);

})(foo);
@@ -0,0 +1,14 @@
System.register('bundle', ['external'], (() => {
'use strict';
var foo;
return {
setters: [(module => {
foo = module["default"];
})],
execute: (() => {

console.log(foo);

})
};
}));
@@ -0,0 +1,13 @@
((global, factory) => {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('external')) :
typeof define === 'function' && define.amd ? define(['external'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.foo));
})(this, (foo => { 'use strict';

var _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { 'default': e };

var foo__default = /*#__PURE__*/_interopDefaultLegacy(foo);

console.log(foo__default["default"]);

}));
2 changes: 2 additions & 0 deletions test/form/samples/generated-code/block-bindings-false/main.js
@@ -0,0 +1,2 @@
import foo from 'external';
console.log(foo);
10 changes: 10 additions & 0 deletions test/form/samples/generated-code/block-bindings-true/_config.js
@@ -0,0 +1,10 @@
module.exports = {
description: 'uses block bindings',
options: {
external: ['external'],
output: {
generatedCode: { arrowFunctions: true, blockBindings: true },
name: 'bundle'
}
}
};
@@ -0,0 +1,9 @@
define(['external'], (foo => { 'use strict';

const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { 'default': e };

const foo__default = /*#__PURE__*/_interopDefaultLegacy(foo);

console.log(foo__default["default"]);

}));
@@ -0,0 +1,9 @@
'use strict';

const foo = require('external');

const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { 'default': e };

const foo__default = /*#__PURE__*/_interopDefaultLegacy(foo);

console.log(foo__default["default"]);
@@ -0,0 +1,3 @@
import foo from 'external';

console.log(foo);
@@ -0,0 +1,10 @@
(foo => {
'use strict';

const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { 'default': e };

const foo__default = /*#__PURE__*/_interopDefaultLegacy(foo);

console.log(foo__default["default"]);

})(foo);
@@ -0,0 +1,14 @@
System.register('bundle', ['external'], (() => {
'use strict';
var foo;
return {
setters: [(module => {
foo = module["default"];
})],
execute: (() => {

console.log(foo);

})
};
}));
@@ -0,0 +1,13 @@
((global, factory) => {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('external')) :
typeof define === 'function' && define.amd ? define(['external'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.foo));
})(this, (foo => { 'use strict';

const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { 'default': e };

const foo__default = /*#__PURE__*/_interopDefaultLegacy(foo);

console.log(foo__default["default"]);

}));
1 change: 1 addition & 0 deletions test/function/samples/output-options-hook/_config.js
Expand Up @@ -32,6 +32,7 @@ module.exports = {
freeze: true,
generatedCode: {
arrowFunctions: false,
blockBindings: false,
objectShorthand: false,
reservedNamesAsProps: false
},
Expand Down

0 comments on commit 02e3432

Please sign in to comment.