From ab49e9a74dd7e7d34e00beebab9b7f00feabda82 Mon Sep 17 00:00:00 2001 From: davidroeca Date: Thu, 17 Sep 2020 12:49:34 -0400 Subject: [PATCH 01/10] add initial moduleRootDir implementation --- src/Chunk.ts | 38 +++++++++++++-------- src/rollup/types.d.ts | 2 ++ src/utils/options/mergeOptions.ts | 1 + src/utils/options/normalizeOutputOptions.ts | 1 + src/utils/path.ts | 2 +- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/Chunk.ts b/src/Chunk.ts index 3276a25d15c..15cc618e078 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -49,7 +49,7 @@ import { isDefaultAProperty, namespaceInteropHelpersByInteropType } from './utils/interopHelpers'; -import { basename, dirname, extname, isAbsolute, normalize, resolve } from './utils/path'; +import { basename, dirname, extname, isAbsolute, join, normalize, resolve } from './utils/path'; import { PluginDriver } from './utils/PluginDriver'; import relativeId, { getAliasName } from './utils/relativeId'; import renderChunk from './utils/renderChunk'; @@ -441,20 +441,30 @@ export default class Chunk { ? '[name].js' : '[name][extname].js' : options.entryFileNames; - path = relative( - preserveModulesRelativeDir, - `${dirname(sanitizedId)}/${renderNamePattern( - pattern, - 'output.entryFileNames', - { - ext: () => extension.substr(1), - extname: () => extension, - format: () => options.format as string, - name: () => this.getChunkName() - }, - this.getChunkInfo.bind(this) - )}` + let currentDir = dirname(sanitizedId); + const fileName = renderNamePattern( + pattern, + 'output.entryFileNames', + { + ext: () => extension.substr(1), + extname: () => extension, + format: () => options.format as string, + name: () => this.getChunkName() + }, + this.getChunkInfo.bind(this) ); + if (options.moduleRootDir) { + const moduleRootDir = resolve(options.moduleRootDir); + const moduleRootDirRegExp = new RegExp(`^${moduleRootDir}`); + if (currentDir.match(moduleRootDirRegExp)) { + currentDir = join( + preserveModulesRelativeDir, + currentDir.replace(moduleRootDirRegExp, '') + ); + } + } + const currentPath = `${currentDir}/${fileName}`; + path = relative(preserveModulesRelativeDir, currentPath); } else { path = `_virtual/${basename(sanitizedId)}`; } diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 0066f1d7d53..993e54c0f37 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -575,6 +575,7 @@ export interface OutputOptions { intro?: string | (() => string | Promise); manualChunks?: ManualChunksOption; minifyInternalExports?: boolean; + moduleRootDir?: string; name?: string; namespaceToStringTag?: boolean; noConflict?: boolean; @@ -620,6 +621,7 @@ export interface NormalizedOutputOptions { intro: () => string | Promise; manualChunks: ManualChunksOption; minifyInternalExports: boolean; + moduleRootDir: string | undefined; name: string | undefined; namespaceToStringTag: boolean; noConflict: boolean; diff --git a/src/utils/options/mergeOptions.ts b/src/utils/options/mergeOptions.ts index 5da4dac9571..e9a688d0334 100644 --- a/src/utils/options/mergeOptions.ts +++ b/src/utils/options/mergeOptions.ts @@ -213,6 +213,7 @@ function mergeOutputOptions( intro: getOption('intro'), manualChunks: getOption('manualChunks'), minifyInternalExports: getOption('minifyInternalExports'), + moduleRootDir: getOption('moduleRootDir'), name: getOption('name'), namespaceToStringTag: getOption('namespaceToStringTag'), noConflict: getOption('noConflict'), diff --git a/src/utils/options/normalizeOutputOptions.ts b/src/utils/options/normalizeOutputOptions.ts index 747be02e9b0..0551ad67429 100644 --- a/src/utils/options/normalizeOutputOptions.ts +++ b/src/utils/options/normalizeOutputOptions.ts @@ -56,6 +56,7 @@ export function normalizeOutputOptions( intro: getAddon(config, 'intro'), manualChunks: getManualChunks(config, inlineDynamicImports, preserveModules, inputOptions), minifyInternalExports: getMinifyInternalExports(config, format, compact), + moduleRootDir: config.moduleRootDir as string | undefined, name: config.name as string | undefined, namespaceToStringTag: (config.namespaceToStringTag as boolean | undefined) || false, noConflict: (config.noConflict as boolean | undefined) || false, diff --git a/src/utils/path.ts b/src/utils/path.ts index d6480fb9819..a0576033b16 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -14,4 +14,4 @@ export function normalize(path: string) { return path.replace(/\\/g, '/'); } -export { basename, dirname, extname, relative, resolve } from 'path'; +export { basename, dirname, extname, relative, resolve, join } from 'path'; From bb3603a89e7c9e0ac6837d3f25601816415f3329 Mon Sep 17 00:00:00 2001 From: davidroeca Date: Fri, 18 Sep 2020 11:00:49 -0400 Subject: [PATCH 02/10] moduleRootDir -> preserveModulesRoot Also avoid regex, switch `path.resolve` to `path.join` due to lack of browser support for `path.join` --- src/Chunk.ts | 13 ++++++------- src/rollup/types.d.ts | 4 ++-- src/utils/options/mergeOptions.ts | 2 +- src/utils/options/normalizeOutputOptions.ts | 2 +- src/utils/path.ts | 2 +- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Chunk.ts b/src/Chunk.ts index 15cc618e078..6d81142d379 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -49,7 +49,7 @@ import { isDefaultAProperty, namespaceInteropHelpersByInteropType } from './utils/interopHelpers'; -import { basename, dirname, extname, isAbsolute, join, normalize, resolve } from './utils/path'; +import { basename, dirname, extname, isAbsolute, normalize, resolve } from './utils/path'; import { PluginDriver } from './utils/PluginDriver'; import relativeId, { getAliasName } from './utils/relativeId'; import renderChunk from './utils/renderChunk'; @@ -453,13 +453,12 @@ export default class Chunk { }, this.getChunkInfo.bind(this) ); - if (options.moduleRootDir) { - const moduleRootDir = resolve(options.moduleRootDir); - const moduleRootDirRegExp = new RegExp(`^${moduleRootDir}`); - if (currentDir.match(moduleRootDirRegExp)) { - currentDir = join( + if (options.preserveModulesRoot) { + const preserveModulesRoot = resolve(options.preserveModulesRoot); + if (currentDir.startsWith(preserveModulesRoot)) { + currentDir = resolve( preserveModulesRelativeDir, - currentDir.replace(moduleRootDirRegExp, '') + currentDir.slice(preserveModulesRoot.length).replace(/^\//, '') ); } } diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 993e54c0f37..bbdc30bcce4 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -575,7 +575,6 @@ export interface OutputOptions { intro?: string | (() => string | Promise); manualChunks?: ManualChunksOption; minifyInternalExports?: boolean; - moduleRootDir?: string; name?: string; namespaceToStringTag?: boolean; noConflict?: boolean; @@ -584,6 +583,7 @@ export interface OutputOptions { plugins?: OutputPlugin[]; preferConst?: boolean; preserveModules?: boolean; + preserveModulesRoot?: string; sourcemap?: boolean | 'inline' | 'hidden'; sourcemapExcludeSources?: boolean; sourcemapFile?: string; @@ -621,7 +621,6 @@ export interface NormalizedOutputOptions { intro: () => string | Promise; manualChunks: ManualChunksOption; minifyInternalExports: boolean; - moduleRootDir: string | undefined; name: string | undefined; namespaceToStringTag: boolean; noConflict: boolean; @@ -630,6 +629,7 @@ export interface NormalizedOutputOptions { plugins: OutputPlugin[]; preferConst: boolean; preserveModules: boolean; + preserveModulesRoot: string | undefined; sourcemap: boolean | 'inline' | 'hidden'; sourcemapExcludeSources: boolean; sourcemapFile: string | undefined; diff --git a/src/utils/options/mergeOptions.ts b/src/utils/options/mergeOptions.ts index e9a688d0334..cb6cd097027 100644 --- a/src/utils/options/mergeOptions.ts +++ b/src/utils/options/mergeOptions.ts @@ -213,7 +213,6 @@ function mergeOutputOptions( intro: getOption('intro'), manualChunks: getOption('manualChunks'), minifyInternalExports: getOption('minifyInternalExports'), - moduleRootDir: getOption('moduleRootDir'), name: getOption('name'), namespaceToStringTag: getOption('namespaceToStringTag'), noConflict: getOption('noConflict'), @@ -222,6 +221,7 @@ function mergeOutputOptions( plugins: ensureArray(config.plugins) as Plugin[], preferConst: getOption('preferConst'), preserveModules: getOption('preserveModules'), + preserveModulesRoot: getOption('preserveModulesRoot'), sourcemap: getOption('sourcemap'), sourcemapExcludeSources: getOption('sourcemapExcludeSources'), sourcemapFile: getOption('sourcemapFile'), diff --git a/src/utils/options/normalizeOutputOptions.ts b/src/utils/options/normalizeOutputOptions.ts index 0551ad67429..655e682beef 100644 --- a/src/utils/options/normalizeOutputOptions.ts +++ b/src/utils/options/normalizeOutputOptions.ts @@ -56,7 +56,6 @@ export function normalizeOutputOptions( intro: getAddon(config, 'intro'), manualChunks: getManualChunks(config, inlineDynamicImports, preserveModules, inputOptions), minifyInternalExports: getMinifyInternalExports(config, format, compact), - moduleRootDir: config.moduleRootDir as string | undefined, name: config.name as string | undefined, namespaceToStringTag: (config.namespaceToStringTag as boolean | undefined) || false, noConflict: (config.noConflict as boolean | undefined) || false, @@ -65,6 +64,7 @@ export function normalizeOutputOptions( plugins: ensureArray(config.plugins) as Plugin[], preferConst: (config.preferConst as boolean | undefined) || false, preserveModules, + preserveModulesRoot: config.preserveModulesRoot as string | undefined, sourcemap: (config.sourcemap as boolean | 'inline' | 'hidden' | undefined) || false, sourcemapExcludeSources: (config.sourcemapExcludeSources as boolean | undefined) || false, sourcemapFile: config.sourcemapFile as string | undefined, diff --git a/src/utils/path.ts b/src/utils/path.ts index a0576033b16..d6480fb9819 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -14,4 +14,4 @@ export function normalize(path: string) { return path.replace(/\\/g, '/'); } -export { basename, dirname, extname, relative, resolve, join } from 'path'; +export { basename, dirname, extname, relative, resolve } from 'path'; From 45c75161a20d2dc56984988c6170a06e9529680a Mon Sep 17 00:00:00 2001 From: davidroeca Date: Fri, 18 Sep 2020 11:02:54 -0400 Subject: [PATCH 03/10] update cli help + docs --- cli/help.md | 1 + docs/01-command-line-reference.md | 2 ++ docs/02-javascript-api.md | 3 ++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/help.md b/cli/help.md index 027dd6f4a6b..06593c6fde6 100644 --- a/cli/help.md +++ b/cli/help.md @@ -46,6 +46,7 @@ Basic options: --preferConst Use `const` instead of `var` for exports --no-preserveEntrySignatures Avoid facade chunks for entry points --preserveModules Preserve module structure +--preserveModulesRootDir Preserved modules under this path are rooted in output `dir` --preserveSymlinks Do not follow symlinks when resolving files --shimMissingExports Create shim variables for missing exports --silent Don't print warnings diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index 21951106ade..4dc78e1d5ca 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -78,6 +78,7 @@ export default { // can be an array (for multiple inputs) outro, paths, preserveModules, + preserveModulesRootDir, sourcemap, sourcemapExcludeSources, sourcemapFile, @@ -303,6 +304,7 @@ Many options have command line equivalents. In those cases, any arguments passed --preferConst Use `const` instead of `var` for exports --no-preserveEntrySignatures Avoid facade chunks for entry points --preserveModules Preserve module structure +--preserveModulesRootDir Preserved modules under this path are rooted in output `dir` --preserveSymlinks Do not follow symlinks when resolving files --shimMissingExports Create shim variables for missing exports --silent Don't print warnings diff --git a/docs/02-javascript-api.md b/docs/02-javascript-api.md index 7357ee457b6..c6171e8331b 100755 --- a/docs/02-javascript-api.md +++ b/docs/02-javascript-api.md @@ -138,6 +138,7 @@ const outputOptions = { outro, paths, preserveModules, + preserveModulesRootDir, sourcemap, sourcemapExcludeSources, sourcemapFile, @@ -235,4 +236,4 @@ loadConfigFile(path.resolve(__dirname, 'rollup.config.js'), { format: 'es' }).th rollup.watch(options); } ); -``` \ No newline at end of file +``` From b8e5e5a87089558a03615ebe5ece94006a81fc91 Mon Sep 17 00:00:00 2001 From: davidroeca Date: Fri, 18 Sep 2020 11:03:14 -0400 Subject: [PATCH 04/10] fix optionList test --- test/misc/optionList.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/misc/optionList.js b/test/misc/optionList.js index 92c928c182c..2e471411ed9 100644 --- a/test/misc/optionList.js +++ b/test/misc/optionList.js @@ -1,6 +1,6 @@ exports.input = 'acorn, acornInjectPlugins, cache, context, experimentalCacheExpiry, external, inlineDynamicImports, input, manualChunks, moduleContext, onwarn, perf, plugins, preserveEntrySignatures, preserveModules, preserveSymlinks, shimMissingExports, strictDeprecations, treeshake, watch'; exports.flags = - 'acorn, acornInjectPlugins, amd, assetFileNames, banner, c, cache, chunkFileNames, compact, config, context, d, dir, dynamicImportFunction, e, entryFileNames, environment, esModule, experimentalCacheExpiry, exports, extend, external, externalLiveBindings, f, failAfterWarnings, file, footer, format, freeze, g, globals, h, hoistTransitiveImports, i, indent, inlineDynamicImports, input, interop, intro, m, manualChunks, minifyInternalExports, moduleContext, n, name, namespaceToStringTag, noConflict, o, onwarn, outro, p, paths, perf, plugin, plugins, preferConst, preserveEntrySignatures, preserveModules, preserveSymlinks, shimMissingExports, silent, sourcemap, sourcemapExcludeSources, sourcemapFile, stdin, strict, strictDeprecations, systemNullSetters, treeshake, v, w, waitForBundleInput, watch'; + 'acorn, acornInjectPlugins, amd, assetFileNames, banner, c, cache, chunkFileNames, compact, config, context, d, dir, dynamicImportFunction, e, entryFileNames, environment, esModule, experimentalCacheExpiry, exports, extend, external, externalLiveBindings, f, failAfterWarnings, file, footer, format, freeze, g, globals, h, hoistTransitiveImports, i, indent, inlineDynamicImports, input, interop, intro, m, manualChunks, minifyInternalExports, moduleContext, n, name, namespaceToStringTag, noConflict, o, onwarn, outro, p, paths, perf, plugin, plugins, preferConst, preserveEntrySignatures, preserveModules, preserveModulesRootDir, preserveSymlinks, shimMissingExports, silent, sourcemap, sourcemapExcludeSources, sourcemapFile, stdin, strict, strictDeprecations, systemNullSetters, treeshake, v, w, waitForBundleInput, watch'; exports.output = - 'amd, assetFileNames, banner, chunkFileNames, compact, dir, dynamicImportFunction, entryFileNames, esModule, exports, extend, externalLiveBindings, file, footer, format, freeze, globals, hoistTransitiveImports, indent, inlineDynamicImports, interop, intro, manualChunks, minifyInternalExports, name, namespaceToStringTag, noConflict, outro, paths, plugins, preferConst, preserveModules, sourcemap, sourcemapExcludeSources, sourcemapFile, sourcemapPathTransform, strict, systemNullSetters'; + 'amd, assetFileNames, banner, chunkFileNames, compact, dir, dynamicImportFunction, entryFileNames, esModule, exports, extend, externalLiveBindings, file, footer, format, freeze, globals, hoistTransitiveImports, indent, inlineDynamicImports, interop, intro, manualChunks, minifyInternalExports, name, namespaceToStringTag, noConflict, outro, paths, plugins, preferConst, preserveModules, preserveModulesRootDir, sourcemap, sourcemapExcludeSources, sourcemapFile, sourcemapPathTransform, strict, systemNullSetters'; From c6628e56b29e0d6155f1ca8e8bb272e5c89b851f Mon Sep 17 00:00:00 2001 From: davidroeca Date: Fri, 18 Sep 2020 11:03:37 -0400 Subject: [PATCH 05/10] add preserveModulesRoot test --- .../samples/preserve-modules-root/_config.js | 21 +++++++++++++++ .../amd/_virtual/_commonjsHelpers.js | 22 +++++++++++++++ .../amd/_virtual/index.js_commonjs-proxy | 7 +++++ .../_expected/amd/below/module.js | 9 +++++++ .../@my-scope/my-base-pkg/index.js | 16 +++++++++++ .../_expected/amd/under-build.js | 9 +++++++ .../cjs/_virtual/_commonjsHelpers.js | 20 ++++++++++++++ .../cjs/_virtual/index.js_commonjs-proxy | 7 +++++ .../_expected/cjs/below/module.js | 9 +++++++ .../@my-scope/my-base-pkg/index.js | 16 +++++++++++ .../_expected/cjs/under-build.js | 9 +++++++ .../_expected/es/_virtual/_commonjsHelpers.js | 15 +++++++++++ .../es/_virtual/index.js_commonjs-proxy | 2 ++ .../_expected/es/below/module.js | 7 +++++ .../@my-scope/my-base-pkg/index.js | 12 +++++++++ .../_expected/es/under-build.js | 7 +++++ .../system/_virtual/_commonjsHelpers.js | 27 +++++++++++++++++++ .../system/_virtual/index.js_commonjs-proxy | 15 +++++++++++ .../_expected/system/below/module.js | 16 +++++++++++ .../@my-scope/my-base-pkg/index.js | 21 +++++++++++++++ .../_expected/system/under-build.js | 16 +++++++++++ .../@my-scope/my-base-pkg/index.js | 7 +++++ .../@my-scope/my-base-pkg/package.json | 6 +++++ .../preserve-modules-root/src/below/module.js | 5 ++++ .../preserve-modules-root/src/under-build.js | 5 ++++ 25 files changed, 306 insertions(+) create mode 100644 test/chunking-form/samples/preserve-modules-root/_config.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/amd/_virtual/_commonjsHelpers.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/amd/_virtual/index.js_commonjs-proxy create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/amd/below/module.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/amd/custom_modules/@my-scope/my-base-pkg/index.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/amd/under-build.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/cjs/_virtual/_commonjsHelpers.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/cjs/_virtual/index.js_commonjs-proxy create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/cjs/below/module.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/cjs/custom_modules/@my-scope/my-base-pkg/index.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/cjs/under-build.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/es/_virtual/_commonjsHelpers.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/es/_virtual/index.js_commonjs-proxy create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/es/below/module.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/es/custom_modules/@my-scope/my-base-pkg/index.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/es/under-build.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/system/_virtual/_commonjsHelpers.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/system/_virtual/index.js_commonjs-proxy create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/system/below/module.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/system/custom_modules/@my-scope/my-base-pkg/index.js create mode 100644 test/chunking-form/samples/preserve-modules-root/_expected/system/under-build.js create mode 100644 test/chunking-form/samples/preserve-modules-root/custom_modules/@my-scope/my-base-pkg/index.js create mode 100644 test/chunking-form/samples/preserve-modules-root/custom_modules/@my-scope/my-base-pkg/package.json create mode 100644 test/chunking-form/samples/preserve-modules-root/src/below/module.js create mode 100644 test/chunking-form/samples/preserve-modules-root/src/under-build.js diff --git a/test/chunking-form/samples/preserve-modules-root/_config.js b/test/chunking-form/samples/preserve-modules-root/_config.js new file mode 100644 index 00000000000..85a19e67dfd --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_config.js @@ -0,0 +1,21 @@ +const commonjs = require('@rollup/plugin-commonjs'); +const resolve = require('@rollup/plugin-node-resolve').default; + +module.exports = { + description: 'confirm preserveModulesRoot restructures src appropriately', + options: { + input: ['src/under-build.js', 'src/below/module.js'], + plugins: [ + resolve({ + customResolveOptions: { + moduleDirectory: ['custom_modules'] + } + }), + commonjs() + ], + output: { + preserveModules: true, + preserveModulesRoot: 'src' + } + } +}; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/amd/_virtual/_commonjsHelpers.js b/test/chunking-form/samples/preserve-modules-root/_expected/amd/_virtual/_commonjsHelpers.js new file mode 100644 index 00000000000..1e811d97014 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/amd/_virtual/_commonjsHelpers.js @@ -0,0 +1,22 @@ +define(['exports'], function (exports) { 'use strict'; + + function createCommonjsModule(fn, basedir, module) { + return module = { + path: basedir, + exports: {}, + require: function (path, base) { + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base); + } + }, fn(module, module.exports), module.exports; + } + + function commonjsRequire () { + throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs'); + } + + exports.commonjsRequire = commonjsRequire; + exports.createCommonjsModule = createCommonjsModule; + + Object.defineProperty(exports, '__esModule', { value: true }); + +}); diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/amd/_virtual/index.js_commonjs-proxy b/test/chunking-form/samples/preserve-modules-root/_expected/amd/_virtual/index.js_commonjs-proxy new file mode 100644 index 00000000000..c0ae15db897 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/amd/_virtual/index.js_commonjs-proxy @@ -0,0 +1,7 @@ +define(['../custom_modules/@my-scope/my-base-pkg/index'], function (index) { 'use strict'; + + + + return index.myBasePkg; + +}); diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/amd/below/module.js b/test/chunking-form/samples/preserve-modules-root/_expected/amd/below/module.js new file mode 100644 index 00000000000..1e667ae03f7 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/amd/below/module.js @@ -0,0 +1,9 @@ +define(['../custom_modules/@my-scope/my-base-pkg/index'], function (index) { 'use strict'; + + var module = { + base2: index.myBasePkg, + }; + + return module; + +}); diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/amd/custom_modules/@my-scope/my-base-pkg/index.js b/test/chunking-form/samples/preserve-modules-root/_expected/amd/custom_modules/@my-scope/my-base-pkg/index.js new file mode 100644 index 00000000000..fb2660a7ee1 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/amd/custom_modules/@my-scope/my-base-pkg/index.js @@ -0,0 +1,16 @@ +define(['exports', '../../../_virtual/_commonjsHelpers'], function (exports, _commonjsHelpers) { 'use strict'; + + var myBasePkg = _commonjsHelpers.createCommonjsModule(function (module, exports) { + + Object.defineProperty(exports, '__esModule', { value: true }); + + var hello = 'world'; + + exports.hello = hello; + }); + + exports.myBasePkg = myBasePkg; + + Object.defineProperty(exports, '__esModule', { value: true }); + +}); diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/amd/under-build.js b/test/chunking-form/samples/preserve-modules-root/_expected/amd/under-build.js new file mode 100644 index 00000000000..dedbcba3e08 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/amd/under-build.js @@ -0,0 +1,9 @@ +define(['./custom_modules/@my-scope/my-base-pkg/index'], function (index) { 'use strict'; + + var underBuild = { + base: index.myBasePkg + }; + + return underBuild; + +}); diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/cjs/_virtual/_commonjsHelpers.js b/test/chunking-form/samples/preserve-modules-root/_expected/cjs/_virtual/_commonjsHelpers.js new file mode 100644 index 00000000000..df27e979295 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/cjs/_virtual/_commonjsHelpers.js @@ -0,0 +1,20 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); + +function createCommonjsModule(fn, basedir, module) { + return module = { + path: basedir, + exports: {}, + require: function (path, base) { + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base); + } + }, fn(module, module.exports), module.exports; +} + +function commonjsRequire () { + throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs'); +} + +exports.commonjsRequire = commonjsRequire; +exports.createCommonjsModule = createCommonjsModule; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/cjs/_virtual/index.js_commonjs-proxy b/test/chunking-form/samples/preserve-modules-root/_expected/cjs/_virtual/index.js_commonjs-proxy new file mode 100644 index 00000000000..7afb9b78806 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/cjs/_virtual/index.js_commonjs-proxy @@ -0,0 +1,7 @@ +'use strict'; + +var index = require('../custom_modules/@my-scope/my-base-pkg/index.js'); + + + +module.exports = index.myBasePkg; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/cjs/below/module.js b/test/chunking-form/samples/preserve-modules-root/_expected/cjs/below/module.js new file mode 100644 index 00000000000..e8430ef8b6d --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/cjs/below/module.js @@ -0,0 +1,9 @@ +'use strict'; + +var index = require('../custom_modules/@my-scope/my-base-pkg/index.js'); + +var module$1 = { + base2: index.myBasePkg, +}; + +module.exports = module$1; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/cjs/custom_modules/@my-scope/my-base-pkg/index.js b/test/chunking-form/samples/preserve-modules-root/_expected/cjs/custom_modules/@my-scope/my-base-pkg/index.js new file mode 100644 index 00000000000..6af68b8ed1c --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/cjs/custom_modules/@my-scope/my-base-pkg/index.js @@ -0,0 +1,16 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); + +var _commonjsHelpers = require('../../../_virtual/_commonjsHelpers.js'); + +var myBasePkg = _commonjsHelpers.createCommonjsModule(function (module, exports) { + +Object.defineProperty(exports, '__esModule', { value: true }); + +var hello = 'world'; + +exports.hello = hello; +}); + +exports.myBasePkg = myBasePkg; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/cjs/under-build.js b/test/chunking-form/samples/preserve-modules-root/_expected/cjs/under-build.js new file mode 100644 index 00000000000..321474036e1 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/cjs/under-build.js @@ -0,0 +1,9 @@ +'use strict'; + +var index = require('./custom_modules/@my-scope/my-base-pkg/index.js'); + +var underBuild = { + base: index.myBasePkg +}; + +module.exports = underBuild; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/es/_virtual/_commonjsHelpers.js b/test/chunking-form/samples/preserve-modules-root/_expected/es/_virtual/_commonjsHelpers.js new file mode 100644 index 00000000000..646a42223df --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/es/_virtual/_commonjsHelpers.js @@ -0,0 +1,15 @@ +function createCommonjsModule(fn, basedir, module) { + return module = { + path: basedir, + exports: {}, + require: function (path, base) { + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base); + } + }, fn(module, module.exports), module.exports; +} + +function commonjsRequire () { + throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs'); +} + +export { commonjsRequire, createCommonjsModule }; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/es/_virtual/index.js_commonjs-proxy b/test/chunking-form/samples/preserve-modules-root/_expected/es/_virtual/index.js_commonjs-proxy new file mode 100644 index 00000000000..f2f4d5354b2 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/es/_virtual/index.js_commonjs-proxy @@ -0,0 +1,2 @@ +import { m as myBasePkg } from '../custom_modules/@my-scope/my-base-pkg/index.js'; +export { m as default } from '../custom_modules/@my-scope/my-base-pkg/index.js'; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/es/below/module.js b/test/chunking-form/samples/preserve-modules-root/_expected/es/below/module.js new file mode 100644 index 00000000000..1231dcf4b8e --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/es/below/module.js @@ -0,0 +1,7 @@ +import { m as myBasePkg } from '../custom_modules/@my-scope/my-base-pkg/index.js'; + +var module = { + base2: myBasePkg, +}; + +export default module; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/es/custom_modules/@my-scope/my-base-pkg/index.js b/test/chunking-form/samples/preserve-modules-root/_expected/es/custom_modules/@my-scope/my-base-pkg/index.js new file mode 100644 index 00000000000..0367167d121 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/es/custom_modules/@my-scope/my-base-pkg/index.js @@ -0,0 +1,12 @@ +import { createCommonjsModule } from '../../../_virtual/_commonjsHelpers.js'; + +var myBasePkg = createCommonjsModule(function (module, exports) { + +Object.defineProperty(exports, '__esModule', { value: true }); + +var hello = 'world'; + +exports.hello = hello; +}); + +export { myBasePkg as m }; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/es/under-build.js b/test/chunking-form/samples/preserve-modules-root/_expected/es/under-build.js new file mode 100644 index 00000000000..c26b9473a7e --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/es/under-build.js @@ -0,0 +1,7 @@ +import { m as myBasePkg } from './custom_modules/@my-scope/my-base-pkg/index.js'; + +var underBuild = { + base: myBasePkg +}; + +export default underBuild; diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/system/_virtual/_commonjsHelpers.js b/test/chunking-form/samples/preserve-modules-root/_expected/system/_virtual/_commonjsHelpers.js new file mode 100644 index 00000000000..20f6ac1a11b --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/system/_virtual/_commonjsHelpers.js @@ -0,0 +1,27 @@ +System.register([], function (exports) { + 'use strict'; + return { + execute: function () { + + exports({ + commonjsRequire: commonjsRequire, + createCommonjsModule: createCommonjsModule + }); + + function createCommonjsModule(fn, basedir, module) { + return module = { + path: basedir, + exports: {}, + require: function (path, base) { + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base); + } + }, fn(module, module.exports), module.exports; + } + + function commonjsRequire () { + throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs'); + } + + } + }; +}); diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/system/_virtual/index.js_commonjs-proxy b/test/chunking-form/samples/preserve-modules-root/_expected/system/_virtual/index.js_commonjs-proxy new file mode 100644 index 00000000000..1ce0fcaadfd --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/system/_virtual/index.js_commonjs-proxy @@ -0,0 +1,15 @@ +System.register(['../custom_modules/@my-scope/my-base-pkg/index.js'], function (exports) { + 'use strict'; + var myBasePkg; + return { + setters: [function (module) { + myBasePkg = module.m; + exports('default', module.m); + }], + execute: function () { + + + + } + }; +}); diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/system/below/module.js b/test/chunking-form/samples/preserve-modules-root/_expected/system/below/module.js new file mode 100644 index 00000000000..d2e566cb37d --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/system/below/module.js @@ -0,0 +1,16 @@ +System.register(['../custom_modules/@my-scope/my-base-pkg/index.js'], function (exports) { + 'use strict'; + var myBasePkg; + return { + setters: [function (module) { + myBasePkg = module.m; + }], + execute: function () { + + var module$1 = exports('default', { + base2: myBasePkg, + }); + + } + }; +}); diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/system/custom_modules/@my-scope/my-base-pkg/index.js b/test/chunking-form/samples/preserve-modules-root/_expected/system/custom_modules/@my-scope/my-base-pkg/index.js new file mode 100644 index 00000000000..5ad74391e02 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/system/custom_modules/@my-scope/my-base-pkg/index.js @@ -0,0 +1,21 @@ +System.register(['../../../_virtual/_commonjsHelpers.js'], function (exports) { + 'use strict'; + var createCommonjsModule; + return { + setters: [function (module) { + createCommonjsModule = module.createCommonjsModule; + }], + execute: function () { + + var myBasePkg = exports('m', createCommonjsModule(function (module, exports) { + + Object.defineProperty(exports, '__esModule', { value: true }); + + var hello = 'world'; + + exports.hello = hello; + })); + + } + }; +}); diff --git a/test/chunking-form/samples/preserve-modules-root/_expected/system/under-build.js b/test/chunking-form/samples/preserve-modules-root/_expected/system/under-build.js new file mode 100644 index 00000000000..7c3dbbfc09e --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/_expected/system/under-build.js @@ -0,0 +1,16 @@ +System.register(['./custom_modules/@my-scope/my-base-pkg/index.js'], function (exports) { + 'use strict'; + var myBasePkg; + return { + setters: [function (module) { + myBasePkg = module.m; + }], + execute: function () { + + var underBuild = exports('default', { + base: myBasePkg + }); + + } + }; +}); diff --git a/test/chunking-form/samples/preserve-modules-root/custom_modules/@my-scope/my-base-pkg/index.js b/test/chunking-form/samples/preserve-modules-root/custom_modules/@my-scope/my-base-pkg/index.js new file mode 100644 index 00000000000..c0de037564f --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/custom_modules/@my-scope/my-base-pkg/index.js @@ -0,0 +1,7 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); + +var hello = 'world'; + +exports.hello = hello; diff --git a/test/chunking-form/samples/preserve-modules-root/custom_modules/@my-scope/my-base-pkg/package.json b/test/chunking-form/samples/preserve-modules-root/custom_modules/@my-scope/my-base-pkg/package.json new file mode 100644 index 00000000000..0bcba39bc4a --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/custom_modules/@my-scope/my-base-pkg/package.json @@ -0,0 +1,6 @@ +{ + "name": "@my-scope/my-base-pkg", + "version": "1.0.0", + "main": "index.js", + "license": "MIT" +} diff --git a/test/chunking-form/samples/preserve-modules-root/src/below/module.js b/test/chunking-form/samples/preserve-modules-root/src/below/module.js new file mode 100644 index 00000000000..5c6aa2914b3 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/src/below/module.js @@ -0,0 +1,5 @@ +const base2 = require('@my-scope/my-base-pkg') + +module.exports = { + base2, +} diff --git a/test/chunking-form/samples/preserve-modules-root/src/under-build.js b/test/chunking-form/samples/preserve-modules-root/src/under-build.js new file mode 100644 index 00000000000..88b868795a3 --- /dev/null +++ b/test/chunking-form/samples/preserve-modules-root/src/under-build.js @@ -0,0 +1,5 @@ +const base = require('@my-scope/my-base-pkg'); + +module.exports = { + base +}; From 4b629a697c528b806d7fcbf6dee370de5c02874b Mon Sep 17 00:00:00 2001 From: davidroeca Date: Fri, 18 Sep 2020 11:12:48 -0400 Subject: [PATCH 06/10] fix preserveModulesRootDir -> preserveModulesRoot --- test/misc/optionList.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/misc/optionList.js b/test/misc/optionList.js index 2e471411ed9..3d013653035 100644 --- a/test/misc/optionList.js +++ b/test/misc/optionList.js @@ -1,6 +1,6 @@ exports.input = 'acorn, acornInjectPlugins, cache, context, experimentalCacheExpiry, external, inlineDynamicImports, input, manualChunks, moduleContext, onwarn, perf, plugins, preserveEntrySignatures, preserveModules, preserveSymlinks, shimMissingExports, strictDeprecations, treeshake, watch'; exports.flags = - 'acorn, acornInjectPlugins, amd, assetFileNames, banner, c, cache, chunkFileNames, compact, config, context, d, dir, dynamicImportFunction, e, entryFileNames, environment, esModule, experimentalCacheExpiry, exports, extend, external, externalLiveBindings, f, failAfterWarnings, file, footer, format, freeze, g, globals, h, hoistTransitiveImports, i, indent, inlineDynamicImports, input, interop, intro, m, manualChunks, minifyInternalExports, moduleContext, n, name, namespaceToStringTag, noConflict, o, onwarn, outro, p, paths, perf, plugin, plugins, preferConst, preserveEntrySignatures, preserveModules, preserveModulesRootDir, preserveSymlinks, shimMissingExports, silent, sourcemap, sourcemapExcludeSources, sourcemapFile, stdin, strict, strictDeprecations, systemNullSetters, treeshake, v, w, waitForBundleInput, watch'; + 'acorn, acornInjectPlugins, amd, assetFileNames, banner, c, cache, chunkFileNames, compact, config, context, d, dir, dynamicImportFunction, e, entryFileNames, environment, esModule, experimentalCacheExpiry, exports, extend, external, externalLiveBindings, f, failAfterWarnings, file, footer, format, freeze, g, globals, h, hoistTransitiveImports, i, indent, inlineDynamicImports, input, interop, intro, m, manualChunks, minifyInternalExports, moduleContext, n, name, namespaceToStringTag, noConflict, o, onwarn, outro, p, paths, perf, plugin, plugins, preferConst, preserveEntrySignatures, preserveModules, preserveModulesRoot, preserveSymlinks, shimMissingExports, silent, sourcemap, sourcemapExcludeSources, sourcemapFile, stdin, strict, strictDeprecations, systemNullSetters, treeshake, v, w, waitForBundleInput, watch'; exports.output = - 'amd, assetFileNames, banner, chunkFileNames, compact, dir, dynamicImportFunction, entryFileNames, esModule, exports, extend, externalLiveBindings, file, footer, format, freeze, globals, hoistTransitiveImports, indent, inlineDynamicImports, interop, intro, manualChunks, minifyInternalExports, name, namespaceToStringTag, noConflict, outro, paths, plugins, preferConst, preserveModules, preserveModulesRootDir, sourcemap, sourcemapExcludeSources, sourcemapFile, sourcemapPathTransform, strict, systemNullSetters'; + 'amd, assetFileNames, banner, chunkFileNames, compact, dir, dynamicImportFunction, entryFileNames, esModule, exports, extend, externalLiveBindings, file, footer, format, freeze, globals, hoistTransitiveImports, indent, inlineDynamicImports, interop, intro, manualChunks, minifyInternalExports, name, namespaceToStringTag, noConflict, outro, paths, plugins, preferConst, preserveModules, preserveModulesRoot, sourcemap, sourcemapExcludeSources, sourcemapFile, sourcemapPathTransform, strict, systemNullSetters'; From 181397a995e91a95cdf5be8d7d6721f21f0dcd5f Mon Sep 17 00:00:00 2001 From: davidroeca Date: Fri, 18 Sep 2020 11:16:29 -0400 Subject: [PATCH 07/10] fix preserveModulesRootDir -> preserveModulesRoot --- cli/help.md | 2 +- docs/01-command-line-reference.md | 4 ++-- docs/02-javascript-api.md | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/help.md b/cli/help.md index 06593c6fde6..e3a5d9bbad1 100644 --- a/cli/help.md +++ b/cli/help.md @@ -46,7 +46,7 @@ Basic options: --preferConst Use `const` instead of `var` for exports --no-preserveEntrySignatures Avoid facade chunks for entry points --preserveModules Preserve module structure ---preserveModulesRootDir Preserved modules under this path are rooted in output `dir` +--preserveModulesRoot Preserved modules under this path are rooted in output `dir` --preserveSymlinks Do not follow symlinks when resolving files --shimMissingExports Create shim variables for missing exports --silent Don't print warnings diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index 4dc78e1d5ca..4f6ec13708f 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -78,7 +78,7 @@ export default { // can be an array (for multiple inputs) outro, paths, preserveModules, - preserveModulesRootDir, + preserveModulesRoot, sourcemap, sourcemapExcludeSources, sourcemapFile, @@ -304,7 +304,7 @@ Many options have command line equivalents. In those cases, any arguments passed --preferConst Use `const` instead of `var` for exports --no-preserveEntrySignatures Avoid facade chunks for entry points --preserveModules Preserve module structure ---preserveModulesRootDir Preserved modules under this path are rooted in output `dir` +--preserveModulesRoot Preserved modules under this path are rooted in output `dir` --preserveSymlinks Do not follow symlinks when resolving files --shimMissingExports Create shim variables for missing exports --silent Don't print warnings diff --git a/docs/02-javascript-api.md b/docs/02-javascript-api.md index c6171e8331b..9704b3f1fa1 100755 --- a/docs/02-javascript-api.md +++ b/docs/02-javascript-api.md @@ -138,7 +138,7 @@ const outputOptions = { outro, paths, preserveModules, - preserveModulesRootDir, + preserveModulesRoot, sourcemap, sourcemapExcludeSources, sourcemapFile, From bb16a351baa6f888d67b5a641cc95d21dc012e2e Mon Sep 17 00:00:00 2001 From: davidroeca Date: Fri, 18 Sep 2020 12:11:32 -0400 Subject: [PATCH 08/10] Fix cli help styling --- cli/help.md | 2 +- docs/01-command-line-reference.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/help.md b/cli/help.md index e3a5d9bbad1..396ad70bc59 100644 --- a/cli/help.md +++ b/cli/help.md @@ -46,7 +46,7 @@ Basic options: --preferConst Use `const` instead of `var` for exports --no-preserveEntrySignatures Avoid facade chunks for entry points --preserveModules Preserve module structure ---preserveModulesRoot Preserved modules under this path are rooted in output `dir` +--preserveModulesRoot Preserved modules under this path are rooted in output `dir` --preserveSymlinks Do not follow symlinks when resolving files --shimMissingExports Create shim variables for missing exports --silent Don't print warnings diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index 4f6ec13708f..7ea215bf3e0 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -304,7 +304,7 @@ Many options have command line equivalents. In those cases, any arguments passed --preferConst Use `const` instead of `var` for exports --no-preserveEntrySignatures Avoid facade chunks for entry points --preserveModules Preserve module structure ---preserveModulesRoot Preserved modules under this path are rooted in output `dir` +--preserveModulesRoot Preserved modules under this path are rooted in output `dir` --preserveSymlinks Do not follow symlinks when resolving files --shimMissingExports Create shim variables for missing exports --silent Don't print warnings From e5360771bf901d97758861db0c81f57b746f949d Mon Sep 17 00:00:00 2001 From: davidroeca Date: Sun, 20 Sep 2020 19:03:39 -0400 Subject: [PATCH 09/10] Refactor code --- src/Chunk.ts | 18 +++++++----------- src/utils/options/normalizeOutputOptions.ts | 11 ++++++++++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Chunk.ts b/src/Chunk.ts index 6d81142d379..bcdb0cc2567 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -441,7 +441,7 @@ export default class Chunk { ? '[name].js' : '[name][extname].js' : options.entryFileNames; - let currentDir = dirname(sanitizedId); + const currentDir = dirname(sanitizedId); const fileName = renderNamePattern( pattern, 'output.entryFileNames', @@ -453,17 +453,13 @@ export default class Chunk { }, this.getChunkInfo.bind(this) ); - if (options.preserveModulesRoot) { - const preserveModulesRoot = resolve(options.preserveModulesRoot); - if (currentDir.startsWith(preserveModulesRoot)) { - currentDir = resolve( - preserveModulesRelativeDir, - currentDir.slice(preserveModulesRoot.length).replace(/^\//, '') - ); - } - } const currentPath = `${currentDir}/${fileName}`; - path = relative(preserveModulesRelativeDir, currentPath); + const { preserveModulesRoot } = options; + if (preserveModulesRoot && currentPath.startsWith(preserveModulesRoot)) { + return currentPath.slice(preserveModulesRoot.length).replace(/^\//, ''); + } else { + return relative(preserveModulesRelativeDir, currentPath); + } } else { path = `_virtual/${basename(sanitizedId)}`; } diff --git a/src/utils/options/normalizeOutputOptions.ts b/src/utils/options/normalizeOutputOptions.ts index 655e682beef..e28a1c27ab5 100644 --- a/src/utils/options/normalizeOutputOptions.ts +++ b/src/utils/options/normalizeOutputOptions.ts @@ -13,6 +13,7 @@ import { } from '../../rollup/types'; import { ensureArray } from '../ensureArray'; import { errInvalidExportOptionValue, error, warnDeprecation } from '../error'; +import { resolve } from '../path'; import { GenericConfigObject, warnUnknownOptions } from './options'; export function normalizeOutputOptions( @@ -64,7 +65,7 @@ export function normalizeOutputOptions( plugins: ensureArray(config.plugins) as Plugin[], preferConst: (config.preferConst as boolean | undefined) || false, preserveModules, - preserveModulesRoot: config.preserveModulesRoot as string | undefined, + preserveModulesRoot: getPreserveModulesRoot(config), sourcemap: (config.sourcemap as boolean | 'inline' | 'hidden' | undefined) || false, sourcemapExcludeSources: (config.sourcemapExcludeSources as boolean | undefined) || false, sourcemapFile: config.sourcemapFile as string | undefined, @@ -170,6 +171,14 @@ const getPreserveModules = ( return preserveModules; }; +const getPreserveModulesRoot = (config: GenericConfigObject): string | undefined => { + const preserveModulesRoot = config.preserveModulesRoot as string | null | undefined; + if (preserveModulesRoot === null || preserveModulesRoot === undefined) { + return undefined; + } + return resolve(preserveModulesRoot); +}; + const getAmd = ( config: GenericConfigObject ): { From 1f6d7a958ee66a78a9511c1979e90c00a51270d7 Mon Sep 17 00:00:00 2001 From: davidroeca Date: Sun, 20 Sep 2020 19:15:15 -0400 Subject: [PATCH 10/10] early return -> path = --- src/Chunk.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Chunk.ts b/src/Chunk.ts index bcdb0cc2567..ed5b309ff38 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -456,9 +456,9 @@ export default class Chunk { const currentPath = `${currentDir}/${fileName}`; const { preserveModulesRoot } = options; if (preserveModulesRoot && currentPath.startsWith(preserveModulesRoot)) { - return currentPath.slice(preserveModulesRoot.length).replace(/^\//, ''); + path = currentPath.slice(preserveModulesRoot.length).replace(/^\//, ''); } else { - return relative(preserveModulesRelativeDir, currentPath); + path = relative(preserveModulesRelativeDir, currentPath); } } else { path = `_virtual/${basename(sanitizedId)}`;