Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commonjs): Exclude multi-line template strings from indent #1229

Merged
merged 1 commit into from Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/commonjs/src/generate-exports.js
@@ -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
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
@@ -0,0 +1,6 @@
module.exports = {
description: 'indent does not affect multi-line string content',
pluginOptions: {
defaultIsModuleExports: false
}
};
@@ -0,0 +1,8 @@
const main = require('./main.js');

exports.value = `
dep
multi
line
string
`;
@@ -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
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.