Skip to content

Commit

Permalink
fix(commonjs): Exclude multi-line template strings from indent (#1229)
Browse files Browse the repository at this point in the history
Fixes #1228
  • Loading branch information
simonbuchan authored Aug 5, 2022
1 parent df3d73f commit 54c1a7c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/commonjs/src/generate-exports.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function wrapCode(magicString, uses, moduleName, exportsName) {
export function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges) {
const args = [];
const passedArgs = [];
if (uses.module) {
Expand All @@ -11,7 +11,7 @@ export function wrapCode(magicString, uses, moduleName, exportsName) {
}
magicString
.trim()
.indent('\t')
.indent('\t', { exclude: indentExclusionRanges })
.prepend(`(function (${args.join(', ')}) {\n`)
.append(`\n} (${passedArgs.join(', ')}));`);
}
Expand Down
12 changes: 10 additions & 2 deletions packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export default async function transformCommonjs(
const replacedGlobal = [];
const replacedDynamicRequires = [];
const importedVariables = new Set();
const indentExclusionRanges = [];

walk(ast, {
enter(node, parent) {
Expand Down Expand Up @@ -395,6 +396,11 @@ export default async function transformCommonjs(
if (!scope.parent) {
topLevelDeclarations.push(node);
}
return;
case 'TemplateElement':
if (node.value.raw.includes('\n')) {
indentExclusionRanges.push([node.start, node.end]);
}
}
},

Expand Down Expand Up @@ -527,11 +533,13 @@ export default async function transformCommonjs(
);

if (shouldWrap) {
wrapCode(magicString, uses, moduleName, exportsName);
wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges);
}

if (usesRequireWrapper) {
magicString.trim().indent('\t');
magicString.trim().indent('\t', {
exclude: indentExclusionRanges
});
magicString.prepend(
`var ${isRequiredName};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
description: 'indent does not affect multi-line string content',
pluginOptions: {
defaultIsModuleExports: false
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const main = require('./main.js');

exports.value = `
dep
multi
line
string
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const dep = require('./dep.js');

module.exports = `
main
multi
line
string
`;
56 changes: 56 additions & 0 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -4834,6 +4834,62 @@ Generated by [AVA](https://avajs.dev).
`,
}

## indent-multiline-strings

> Snapshot 1
{
'main.js': `'use strict';␊
␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
var main$1 = {exports: {}};␊
␊
var dep = {};␊
␊
var hasRequiredDep;␊
␊
function requireDep () {␊
if (hasRequiredDep) return dep;␊
hasRequiredDep = 1;␊
requireMain();␊
␊
dep.value = `␊
dep␊
multi␊
line␊
string␊
`;␊
return dep;␊
}␊
␊
var hasRequiredMain;␊
␊
function requireMain () {␊
if (hasRequiredMain) return main$1.exports;␊
hasRequiredMain = 1;␊
(function (module) {␊
requireDep();␊
␊
module.exports = `␊
main␊
multi␊
line␊
string␊
`;␊
} (main$1));␊
return main$1.exports;␊
}␊
␊
var mainExports = requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
␊
module.exports = main;␊
`,
}

## index

> Snapshot 1
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 54c1a7c

Please sign in to comment.