diff --git a/packages/commonjs/src/generate-exports.js b/packages/commonjs/src/generate-exports.js index 1c5faa41d..ce54bdc28 100644 --- a/packages/commonjs/src/generate-exports.js +++ b/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) { @@ -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(', ')}));`); } diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index 45507aa23..0d619194d 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -97,6 +97,7 @@ export default async function transformCommonjs( const replacedGlobal = []; const replacedDynamicRequires = []; const importedVariables = new Set(); + const indentExclusionRanges = []; walk(ast, { enter(node, parent) { @@ -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]); + } } }, @@ -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}; diff --git a/packages/commonjs/test/fixtures/function/indent-multiline-strings/_config.js b/packages/commonjs/test/fixtures/function/indent-multiline-strings/_config.js new file mode 100644 index 000000000..bdb057976 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/indent-multiline-strings/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'indent does not affect multi-line string content', + pluginOptions: { + defaultIsModuleExports: false + } +}; diff --git a/packages/commonjs/test/fixtures/function/indent-multiline-strings/dep.js b/packages/commonjs/test/fixtures/function/indent-multiline-strings/dep.js new file mode 100644 index 000000000..a3ff3b43a --- /dev/null +++ b/packages/commonjs/test/fixtures/function/indent-multiline-strings/dep.js @@ -0,0 +1,8 @@ +const main = require('./main.js'); + +exports.value = ` +dep +multi +line +string +`; diff --git a/packages/commonjs/test/fixtures/function/indent-multiline-strings/main.js b/packages/commonjs/test/fixtures/function/indent-multiline-strings/main.js new file mode 100644 index 000000000..1b8ba7b78 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/indent-multiline-strings/main.js @@ -0,0 +1,8 @@ +const dep = require('./dep.js'); + +module.exports = ` +main +multi +line +string +`; diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index adb5ac68a..c55d02f1d 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -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 diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 0115b4d03..51a89778b 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ