From 831e159bbef4f3943671edeace97d2acb7bc68f7 Mon Sep 17 00:00:00 2001 From: syler Date: Fri, 23 Oct 2020 13:02:47 +0200 Subject: [PATCH 01/17] add typescript config support --- cli/run/loadConfigFile.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index c4c37f0717b..92d57c8b757 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -11,6 +11,7 @@ import relativeId from '../../src/utils/relativeId'; import { stderr } from '../logging'; import batchWarnings, { BatchWarnings } from './batchWarnings'; import { addCommandPluginsToInputOptions } from './commandPlugins'; +const typescript = require('rollup-plugin-typescript'); function supportsNativeESM() { return Number(/^v(\d+)/.exec(process.version)![1]) >= 13; @@ -67,6 +68,7 @@ async function getDefaultFromTranspiledConfigFile( (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5, id.length) === '.json', input: fileName, onwarn: warnings.add, + plugins: [typescript()], treeshake: false }); if (!silent && warnings.count > 0) { From ebc3af265234a5b4aa0cd688a3c0a6a3aabdd89f Mon Sep 17 00:00:00 2001 From: syler Date: Sat, 24 Oct 2020 12:42:35 +0200 Subject: [PATCH 02/17] change require to import for loading ts config file --- .../transform-ts-plugin-config-import.js | 18 ++++++++++ cli/run/import-ts-plugin.ts | 11 +++++++ cli/run/loadConfigFile.ts | 33 ++++++++++++++----- package.json | 2 ++ rollup.config.js | 6 ++-- 5 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 build-plugins/transform-ts-plugin-config-import.js create mode 100644 cli/run/import-ts-plugin.ts diff --git a/build-plugins/transform-ts-plugin-config-import.js b/build-plugins/transform-ts-plugin-config-import.js new file mode 100644 index 00000000000..7c62ecadc59 --- /dev/null +++ b/build-plugins/transform-ts-plugin-config-import.js @@ -0,0 +1,18 @@ +import { transform } from '@babel/core' + +export default function transformTsPluginConfigImport () { + return { + name: 'transform-ts-plugin-config-import', + transform (code, id) { + if (id.endsWith('import-ts-plugin.ts')) { + const transformedCode = transform(code, { + plugins: ['babel-plugin-dynamic-import-node'], + babelrc: false, + configFile: false, + sourceMaps: true + }); + return { code: transformedCode.code, map: transformedCode.map } + } + }, + }; +} diff --git a/cli/run/import-ts-plugin.ts b/cli/run/import-ts-plugin.ts new file mode 100644 index 00000000000..318db18e130 --- /dev/null +++ b/cli/run/import-ts-plugin.ts @@ -0,0 +1,11 @@ +import { resolve } from 'path'; + +export default async (configPath: string) => { + try { + return ( + await import(resolve(configPath, '../', 'node_modules/@rollup/plugin-typescript')) + ).default(); + } catch { + throw new Error('Please install @rollup/plugin-typescript or use a .js rollup config file.'); + } +}; diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index 92d57c8b757..26660b0786e 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -11,7 +11,7 @@ import relativeId from '../../src/utils/relativeId'; import { stderr } from '../logging'; import batchWarnings, { BatchWarnings } from './batchWarnings'; import { addCommandPluginsToInputOptions } from './commandPlugins'; -const typescript = require('rollup-plugin-typescript'); +import importTypescriptPlugin from './import-ts-plugin'; function supportsNativeESM() { return Number(/^v(\d+)/.exec(process.version)![1]) >= 13; @@ -45,12 +45,26 @@ async function loadConfigFile( commandOptions: any ): Promise { const extension = path.extname(fileName); - const configFileExport = - extension === '.mjs' && supportsNativeESM() - ? (await import(pathToFileURL(fileName).href)).default - : extension === '.cjs' - ? getDefaultFromCjs(require(fileName)) - : await getDefaultFromTranspiledConfigFile(fileName, commandOptions.silent); + let configFileExport; + + switch (extension) { + case '.mjs': + if (supportsNativeESM()) { + configFileExport = (await import(pathToFileURL(fileName).href)).default; + break; + } + case '.cjs': + configFileExport = getDefaultFromCjs(require(fileName)); + break; + default: + configFileExport = await getDefaultFromTranspiledConfigFile( + fileName, + commandOptions.silent, + extension === '.ts' + ); + break; + } + return getConfigList(configFileExport, commandOptions); } @@ -60,7 +74,8 @@ function getDefaultFromCjs(namespace: GenericConfigObject) { async function getDefaultFromTranspiledConfigFile( fileName: string, - silent: boolean + silent: boolean, + typescript: boolean ): Promise { const warnings = batchWarnings(); const bundle = await rollup.rollup({ @@ -68,7 +83,7 @@ async function getDefaultFromTranspiledConfigFile( (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5, id.length) === '.json', input: fileName, onwarn: warnings.add, - plugins: [typescript()], + plugins: typescript ? [await importTypescriptPlugin(fileName)] : [], treeshake: false }); if (!silent && warnings.count > 0) { diff --git a/package.json b/package.json index 0158ba24ce8..4d13850c417 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "fsevents": "~2.3.1" }, "devDependencies": { + "@babel/core": "^7.12.3", "@rollup/plugin-alias": "^3.1.2", "@rollup/plugin-buble": "^0.21.3", "@rollup/plugin-commonjs": "18.0.0-1", @@ -74,6 +75,7 @@ "acorn": "^8.2.4", "acorn-jsx": "^5.3.1", "acorn-walk": "^8.1.0", + "babel-plugin-dynamic-import-node": "^2.3.3", "buble": "^0.20.0", "chokidar": "^3.5.1", "colorette": "^1.2.2", diff --git a/rollup.config.js b/rollup.config.js index 53f056b9610..e3a6285d1a1 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -13,6 +13,7 @@ import emitModulePackageFile from './build-plugins/emit-module-package-file.js'; import esmDynamicImport from './build-plugins/esm-dynamic-import.js'; import getLicenseHandler from './build-plugins/generate-license-file'; import replaceBrowserModules from './build-plugins/replace-browser-modules.js'; +import transformTsPluginConfigImport from './build-plugins/transform-ts-plugin-config-import'; import pkg from './package.json'; const commitHash = (function () { @@ -41,7 +42,7 @@ const onwarn = warning => { // eslint-disable-next-line no-console console.error( 'Building Rollup produced warnings that need to be resolved. ' + - 'Please keep in mind that the browser build may never have external dependencies!' + 'Please keep in mind that the browser build may never have external dependencies!' ); throw new Error(warning.message); }; @@ -68,7 +69,8 @@ const nodePlugins = [ conditionalFsEventsImport(), string({ include: '**/*.md' }), commonjs({ include: 'node_modules/**' }), - typescript() + typescript(), + transformTsPluginConfigImport(), ]; export default command => { From 5d81ec690598af77e2137dceb2c823680e63248a Mon Sep 17 00:00:00 2001 From: syler Date: Sat, 24 Oct 2020 12:43:49 +0200 Subject: [PATCH 03/17] add .ts to config search. --- cli/run/getConfigPath.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/run/getConfigPath.ts b/cli/run/getConfigPath.ts index 5d42f3e81a0..3c8dc2c6ea8 100644 --- a/cli/run/getConfigPath.ts +++ b/cli/run/getConfigPath.ts @@ -33,7 +33,7 @@ export function getConfigPath(commandConfig: string | true): string { function findConfigFileNameInCwd(): string { const filesInWorkingDir = new Set(readdirSync(process.cwd())); - for (const extension of ['mjs', 'cjs']) { + for (const extension of ['mjs', 'cjs', 'ts']) { const fileName = `${DEFAULT_CONFIG_BASE}.${extension}`; if (filesInWorkingDir.has(fileName)) return fileName; } From 2a90f4993db8a23e71e87f2e723819eb1f8f8e8e Mon Sep 17 00:00:00 2001 From: syler Date: Sat, 24 Oct 2020 13:08:36 +0200 Subject: [PATCH 04/17] update lock and remove switch in loadConfigFile --- cli/run/loadConfigFile.ts | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index 26660b0786e..14da1be3f3a 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -45,25 +45,17 @@ async function loadConfigFile( commandOptions: any ): Promise { const extension = path.extname(fileName); - let configFileExport; - switch (extension) { - case '.mjs': - if (supportsNativeESM()) { - configFileExport = (await import(pathToFileURL(fileName).href)).default; - break; - } - case '.cjs': - configFileExport = getDefaultFromCjs(require(fileName)); - break; - default: - configFileExport = await getDefaultFromTranspiledConfigFile( - fileName, - commandOptions.silent, - extension === '.ts' - ); - break; - } + const configFileExport = + extension === '.mjs' && supportsNativeESM() + ? (await import(pathToFileURL(fileName).href)).default + : extension === '.cjs' + ? getDefaultFromCjs(require(fileName)) + : await getDefaultFromTranspiledConfigFile( + fileName, + commandOptions.silent, + extension === '.ts' + ); return getConfigList(configFileExport, commandOptions); } From 955a6f73a4c2b1d197aaf34b6c596b1783d29c68 Mon Sep 17 00:00:00 2001 From: syler Date: Sat, 24 Oct 2020 13:25:35 +0200 Subject: [PATCH 05/17] add test for ts config file --- test/cli/samples/config-ts-no-plugin/_config.js | 14 ++++++++++++++ .../samples/config-ts-no-plugin/rollup.config.ts | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/cli/samples/config-ts-no-plugin/_config.js create mode 100644 test/cli/samples/config-ts-no-plugin/rollup.config.ts diff --git a/test/cli/samples/config-ts-no-plugin/_config.js b/test/cli/samples/config-ts-no-plugin/_config.js new file mode 100644 index 00000000000..e3f055e629a --- /dev/null +++ b/test/cli/samples/config-ts-no-plugin/_config.js @@ -0,0 +1,14 @@ +const assert = require('assert'); + +module.exports = { + description: 'throw error if @rollup/plugin-typescript is not installed.', + command: 'rollup --config rollup.config.ts', + execute: true, + error(err) { + assert.ok( + /Please install @rollup\/plugin-typescript or use a \.js rollup config file\./.test( + err.message + ) + ); + } +}; diff --git a/test/cli/samples/config-ts-no-plugin/rollup.config.ts b/test/cli/samples/config-ts-no-plugin/rollup.config.ts new file mode 100644 index 00000000000..f4cf3170b3a --- /dev/null +++ b/test/cli/samples/config-ts-no-plugin/rollup.config.ts @@ -0,0 +1,14 @@ +import replace from '@rollup/plugin-replace'; + +export const ignoresNonDefaultExports = true; + +export default { + input: 'main.js', + output: { + format: 'cjs' + }, + plugins: [ + //@ts-ignore + replace({ ANSWER: 42 }) + ] +}; From e922eea75cca9313e520fc913ef0b4f612ea544a Mon Sep 17 00:00:00 2001 From: syler Date: Sat, 24 Oct 2020 13:47:50 +0200 Subject: [PATCH 06/17] add typescript config note. --- docs/01-command-line-reference.md | 256 +++++++++++++++--------------- 1 file changed, 131 insertions(+), 125 deletions(-) diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index 919ddc591c2..9d0408bc010 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -2,7 +2,7 @@ title: Command Line Interface --- -Rollup should typically be used from the command line. You can provide an optional Rollup configuration file to simplify command line usage and enable advanced Rollup functionality. +Rollup should typically be used from the command line. You can provide an optional Rollup configuration file to simplify command line usage and enable advanced Rollup functionality. ### Configuration Files @@ -10,11 +10,11 @@ Rollup configuration files are optional, but they are powerful and convenient an ```javascript export default { - input: 'src/main.js', - output: { - file: 'bundle.js', - format: 'cjs' - } + input: 'src/main.js', + output: { + file: 'bundle.js', + format: 'cjs' + } }; ``` @@ -22,92 +22,96 @@ Typically, it is called `rollup.config.js` and sits in the root directory of you If you want to write your config as a CommonJS module using `require` and `module.exports`, you should change the file extension to `.cjs`, which will prevent Rollup from trying to transpile the file. Furthermore if you are on Node 13+, changing the file extension to `.mjs` will also prevent Rollup from transpiling it but import the file as an ES module instead. See [using untranspiled config files](guide/en/#using-untranspiled-config-files) for more details and why you might want to do this. +You can also use a typescript config by using the `.ts` extension and installing `@rollup/plugin-typescript` + Config files support the options listed below. Consult the [big list of options](guide/en/#big-list-of-options) for details on each option: ```javascript // rollup.config.js -export default { // can be an array (for multiple inputs) - // core input options - external, - input, // conditionally required - plugins, - - // advanced input options - cache, - onwarn, - preserveEntrySignatures, - strictDeprecations, - - // danger zone - acorn, - acornInjectPlugins, - context, - moduleContext, - preserveSymlinks, - shimMissingExports, - treeshake, - - // experimental - experimentalCacheExpiry, - perf, - - output: { // required (can be an array, for multiple outputs) - // core output options - dir, - file, - format, // required - globals, - name, - plugins, - - // advanced output options - assetFileNames, - banner, - chunkFileNames, - compact, - entryFileNames, - extend, - footer, - hoistTransitiveImports, - inlineDynamicImports, - interop, - intro, - manualChunks, - minifyInternalExports, - outro, - paths, - preserveModules, - preserveModulesRoot, - sourcemap, - sourcemapExcludeSources, - sourcemapFile, - sourcemapPathTransform, +export default { + // can be an array (for multiple inputs) + // core input options + external, + input, // conditionally required + plugins, + + // advanced input options + cache, + onwarn, + preserveEntrySignatures, + strictDeprecations, + + // danger zone + acorn, + acornInjectPlugins, + context, + moduleContext, + preserveSymlinks, + shimMissingExports, + treeshake, + + // experimental + experimentalCacheExpiry, + perf, + + output: { + // required (can be an array, for multiple outputs) + // core output options + dir, + file, + format, // required + globals, + name, + plugins, + + // advanced output options + assetFileNames, + banner, + chunkFileNames, + compact, + entryFileNames, + extend, + footer, + hoistTransitiveImports, + inlineDynamicImports, + interop, + intro, + manualChunks, + minifyInternalExports, + outro, + paths, + preserveModules, + preserveModulesRoot, + sourcemap, + sourcemapExcludeSources, + sourcemapFile, + sourcemapPathTransform, validate, - // danger zone - amd, - esModule, - exports, - externalLiveBindings, - freeze, - indent, - namespaceToStringTag, - noConflict, - preferConst, - sanitizeFileName, - strict, - systemNullSetters - }, - - watch: { - buildDelay, - chokidar, - clearScreen, - skipWrite, - exclude, - include - } | false + // danger zone + amd, + esModule, + exports, + externalLiveBindings, + freeze, + indent, + namespaceToStringTag, + noConflict, + preferConst, + sanitizeFileName,strict, + systemNullSetters + }, + + watch: + { + buildDelay, + chokidar, + clearScreen, + skipWrite, + exclude, + include + } | false }; ``` @@ -116,25 +120,28 @@ You can export an **array** from your config file to build bundles from several ```javascript // rollup.config.js (building more than one bundle) -export default [{ - input: 'main-a.js', - output: { - file: 'dist/bundle-a.js', - format: 'cjs' - } -}, { - input: 'main-b.js', - output: [ - { - file: 'dist/bundle-b1.js', - format: 'cjs' - }, - { - file: 'dist/bundle-b2.js', - format: 'es' - } - ] -}]; +export default [ + { + input: 'main-a.js', + output: { + file: 'dist/bundle-a.js', + format: 'cjs' + } + }, + { + input: 'main-b.js', + output: [ + { + file: 'dist/bundle-b1.js', + format: 'cjs' + }, + { + file: 'dist/bundle-b2.js', + format: 'es' + } + ] + } +]; ``` If you want to create your config asynchronously, Rollup can also handle a `Promise` which resolves to an object or an array. @@ -149,10 +156,7 @@ Similarly, you can do this as well: ```javascript // rollup.config.js (Promise resolving an array) -export default Promise.all([ - fetch('get-config-1'), - fetch('get-config-2') -]) +export default Promise.all([fetch('get-config-1'), fetch('get-config-2')]); ``` To use Rollup with a configuration file, pass the `--config` or `-c` flags: @@ -175,11 +179,11 @@ import defaultConfig from './rollup.default.config.js'; import debugConfig from './rollup.debug.config.js'; export default commandLineArgs => { - if (commandLineArgs.configDebug === true) { - return debugConfig; - } - return defaultConfig; -} + if (commandLineArgs.configDebug === true) { + return debugConfig; + } + return defaultConfig; +}; ``` If you now run `rollup --config --configDebug`, the debug configuration will be used. @@ -254,11 +258,11 @@ By default, Rollup will expect config files to be ES modules and bundle and tran ```javascript // rollup.config.cjs module.exports = { - input: 'src/main.js', - output: { - file: 'bundle.js', - format: 'cjs' - } + input: 'src/main.js', + output: { + file: 'bundle.js', + format: 'cjs' + } }; ``` @@ -270,18 +274,19 @@ There are some potential gotchas when using `.mjs` on Node 13+: - You will only get a default export from CommonJS plugins - You may not be able to import JSON files such as your `package.json file`. There are two ways to go around this: + - run Rollup CLI via ``` node --experimental-json-modules ./node_modules/.bin/rollup --config ``` - + - create a CommonJS wrapper that requires the JSON file: - + ```js // load-package.cjs module.exports = require('./package.json'); - + // rollup.config.mjs import pkg from './load-package.cjs'; ... @@ -380,6 +385,7 @@ Use the specified plugin. There are several ways to specify plugins here: ``` The file should export a function returning a plugin object. + - Via the name of a plugin that is installed in a local or global `node_modules` folder: ``` @@ -441,9 +447,9 @@ will set `process.env.INCLUDE_DEPS === 'true'` and `process.env.BUILD === 'produ ```json // in package.json { - "scripts": { - "build": "rollup -c --environment INCLUDE_DEPS,BUILD:production" - } + "scripts": { + "build": "rollup -c --environment INCLUDE_DEPS,BUILD:production" + } } ``` @@ -478,7 +484,7 @@ echo "export const foo = 42;" | rollup --format cjs --file out.js When this file contains imports, Rollup will try to resolve them relative to the current working directory. When a config file is used, Rollup will only use `stdin` as an entry point if the file name of the entry point is `-`. To read a non-entry-point file from stdin, just call it `-`, which is the file name that is used internally to reference `stdin`. I.e. ```js -import foo from "-"; +import foo from '-'; ``` in any file will prompt Rollup to try to read the imported file from `stdin` and assign the default export to `foo`. You can pass the [`--no-stdin`](guide/en/#--no-stdin) CLI flag to Rollup to treat `-` as a regular file name instead. From 1e612f064bec8bf4f4dda14707ac66b4eee74eb6 Mon Sep 17 00:00:00 2001 From: syler Date: Sat, 24 Oct 2020 14:51:02 +0200 Subject: [PATCH 07/17] remove transform-ts-plugin-config-import. --- .../transform-ts-plugin-config-import.js | 18 ------------------ cli/run/import-ts-plugin.ts | 12 +++++------- cli/run/loadConfigFile.ts | 2 +- package-lock.json | 10 ++++++++++ package.json | 3 +-- rollup.config.js | 4 ++-- 6 files changed, 19 insertions(+), 30 deletions(-) delete mode 100644 build-plugins/transform-ts-plugin-config-import.js diff --git a/build-plugins/transform-ts-plugin-config-import.js b/build-plugins/transform-ts-plugin-config-import.js deleted file mode 100644 index 7c62ecadc59..00000000000 --- a/build-plugins/transform-ts-plugin-config-import.js +++ /dev/null @@ -1,18 +0,0 @@ -import { transform } from '@babel/core' - -export default function transformTsPluginConfigImport () { - return { - name: 'transform-ts-plugin-config-import', - transform (code, id) { - if (id.endsWith('import-ts-plugin.ts')) { - const transformedCode = transform(code, { - plugins: ['babel-plugin-dynamic-import-node'], - babelrc: false, - configFile: false, - sourceMaps: true - }); - return { code: transformedCode.code, map: transformedCode.map } - } - }, - }; -} diff --git a/cli/run/import-ts-plugin.ts b/cli/run/import-ts-plugin.ts index 318db18e130..b10f948ff5b 100644 --- a/cli/run/import-ts-plugin.ts +++ b/cli/run/import-ts-plugin.ts @@ -1,11 +1,9 @@ -import { resolve } from 'path'; - -export default async (configPath: string) => { +export default async () => { try { - return ( - await import(resolve(configPath, '../', 'node_modules/@rollup/plugin-typescript')) - ).default(); + return (await import('@rollup/plugin-typescript')).default(); } catch { - throw new Error('Please install @rollup/plugin-typescript or use a .js rollup config file.'); + throw new Error( + 'Please install @rollup/plugin-typescript or use another config file extension.' + ); } }; diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index 14da1be3f3a..893c8d0c0c8 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -75,7 +75,7 @@ async function getDefaultFromTranspiledConfigFile( (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5, id.length) === '.json', input: fileName, onwarn: warnings.add, - plugins: typescript ? [await importTypescriptPlugin(fileName)] : [], + plugins: typescript ? [await importTypescriptPlugin()] : [], treeshake: false }); if (!silent && warnings.count > 0) { diff --git a/package-lock.json b/package-lock.json index d0e9c0755b4..1696700e06b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -544,6 +544,16 @@ "magic-string": "^0.25.7" } }, + "@rollup/plugin-typescript": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-6.0.0.tgz", + "integrity": "sha512-Y5U2L4eaF3wUSgCZRMdvNmuzWkKMyN3OwvhAdbzAi5sUqedaBk/XbzO4T7RlViDJ78MOPhwAIv2FtId/jhMtbg==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^3.1.0", + "resolve": "^1.17.0" + } + }, "@rollup/pluginutils": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-3.1.0.tgz", diff --git a/package.json b/package.json index 4d13850c417..7f2d65b29d9 100644 --- a/package.json +++ b/package.json @@ -58,13 +58,13 @@ "fsevents": "~2.3.1" }, "devDependencies": { - "@babel/core": "^7.12.3", "@rollup/plugin-alias": "^3.1.2", "@rollup/plugin-buble": "^0.21.3", "@rollup/plugin-commonjs": "18.0.0-1", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.0", "@rollup/plugin-replace": "^2.4.2", + "@rollup/plugin-typescript": "^6.0.0", "@types/micromatch": "^4.0.1", "@types/node": "^10.17.51", "@types/require-relative": "^0.8.0", @@ -75,7 +75,6 @@ "acorn": "^8.2.4", "acorn-jsx": "^5.3.1", "acorn-walk": "^8.1.0", - "babel-plugin-dynamic-import-node": "^2.3.3", "buble": "^0.20.0", "chokidar": "^3.5.1", "colorette": "^1.2.2", diff --git a/rollup.config.js b/rollup.config.js index e3a6285d1a1..59043d4ba7a 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -13,7 +13,6 @@ import emitModulePackageFile from './build-plugins/emit-module-package-file.js'; import esmDynamicImport from './build-plugins/esm-dynamic-import.js'; import getLicenseHandler from './build-plugins/generate-license-file'; import replaceBrowserModules from './build-plugins/replace-browser-modules.js'; -import transformTsPluginConfigImport from './build-plugins/transform-ts-plugin-config-import'; import pkg from './package.json'; const commitHash = (function () { @@ -70,7 +69,6 @@ const nodePlugins = [ string({ include: '**/*.md' }), commonjs({ include: 'node_modules/**' }), typescript(), - transformTsPluginConfigImport(), ]; export default command => { @@ -78,6 +76,8 @@ export default command => { const commonJSBuild = { // fsevents is a dependency of chokidar that cannot be bundled as it contains binary code external: [ + 'buffer', + '@rollup/plugin-typescript', 'assert', 'crypto', 'events', From e05096b047f904e7d7fab003ba2b4877393698ef Mon Sep 17 00:00:00 2001 From: syler Date: Sat, 24 Oct 2020 14:53:35 +0200 Subject: [PATCH 08/17] add config-ts test and remove config-ts-no-plugin test. --- test/cli/samples/config-ts-no-plugin/_config.js | 14 -------------- test/cli/samples/config-ts/_config.js | 5 +++++ test/cli/samples/config-ts/main.js | 1 + .../rollup.config.ts | 2 -- 4 files changed, 6 insertions(+), 16 deletions(-) delete mode 100644 test/cli/samples/config-ts-no-plugin/_config.js create mode 100644 test/cli/samples/config-ts/_config.js create mode 100644 test/cli/samples/config-ts/main.js rename test/cli/samples/{config-ts-no-plugin => config-ts}/rollup.config.ts (78%) diff --git a/test/cli/samples/config-ts-no-plugin/_config.js b/test/cli/samples/config-ts-no-plugin/_config.js deleted file mode 100644 index e3f055e629a..00000000000 --- a/test/cli/samples/config-ts-no-plugin/_config.js +++ /dev/null @@ -1,14 +0,0 @@ -const assert = require('assert'); - -module.exports = { - description: 'throw error if @rollup/plugin-typescript is not installed.', - command: 'rollup --config rollup.config.ts', - execute: true, - error(err) { - assert.ok( - /Please install @rollup\/plugin-typescript or use a \.js rollup config file\./.test( - err.message - ) - ); - } -}; diff --git a/test/cli/samples/config-ts/_config.js b/test/cli/samples/config-ts/_config.js new file mode 100644 index 00000000000..2a20be8e666 --- /dev/null +++ b/test/cli/samples/config-ts/_config.js @@ -0,0 +1,5 @@ +module.exports = { + description: 'uses config file (.ts)', + command: 'rollup --config rollup.config.ts', + execute: true +}; diff --git a/test/cli/samples/config-ts/main.js b/test/cli/samples/config-ts/main.js new file mode 100644 index 00000000000..df16c1b06b9 --- /dev/null +++ b/test/cli/samples/config-ts/main.js @@ -0,0 +1 @@ +assert.equal( ANSWER, 42 ); diff --git a/test/cli/samples/config-ts-no-plugin/rollup.config.ts b/test/cli/samples/config-ts/rollup.config.ts similarity index 78% rename from test/cli/samples/config-ts-no-plugin/rollup.config.ts rename to test/cli/samples/config-ts/rollup.config.ts index f4cf3170b3a..d8ff40bfaae 100644 --- a/test/cli/samples/config-ts-no-plugin/rollup.config.ts +++ b/test/cli/samples/config-ts/rollup.config.ts @@ -1,7 +1,5 @@ import replace from '@rollup/plugin-replace'; -export const ignoresNonDefaultExports = true; - export default { input: 'main.js', output: { From 061b753d024ee06a70c7e33f83fbd88708e157c1 Mon Sep 17 00:00:00 2001 From: syler Date: Sat, 24 Oct 2020 14:59:32 +0200 Subject: [PATCH 09/17] re add realative plugin path. --- cli/run/import-ts-plugin.ts | 8 ++++++-- cli/run/loadConfigFile.ts | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cli/run/import-ts-plugin.ts b/cli/run/import-ts-plugin.ts index b10f948ff5b..fbf8abbe236 100644 --- a/cli/run/import-ts-plugin.ts +++ b/cli/run/import-ts-plugin.ts @@ -1,6 +1,10 @@ -export default async () => { +import { resolve } from 'path'; + +export default async (configPath: string) => { try { - return (await import('@rollup/plugin-typescript')).default(); + return ( + await import(resolve(configPath, '../', 'node_modules/@rollup/plugin-typescript')) + ).default(); } catch { throw new Error( 'Please install @rollup/plugin-typescript or use another config file extension.' diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index 893c8d0c0c8..14da1be3f3a 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -75,7 +75,7 @@ async function getDefaultFromTranspiledConfigFile( (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5, id.length) === '.json', input: fileName, onwarn: warnings.add, - plugins: typescript ? [await importTypescriptPlugin()] : [], + plugins: typescript ? [await importTypescriptPlugin(fileName)] : [], treeshake: false }); if (!silent && warnings.count > 0) { From e196d21d346d255004afd608e78419ee0f1f8b90 Mon Sep 17 00:00:00 2001 From: syler Date: Sat, 24 Oct 2020 15:12:14 +0200 Subject: [PATCH 10/17] revert accidentall formating --- docs/01-command-line-reference.md | 253 +++++++++++++++--------------- 1 file changed, 124 insertions(+), 129 deletions(-) diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index 9d0408bc010..dfaa6b163e9 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -2,7 +2,7 @@ title: Command Line Interface --- -Rollup should typically be used from the command line. You can provide an optional Rollup configuration file to simplify command line usage and enable advanced Rollup functionality. +Rollup should typically be used from the command line. You can provide an optional Rollup configuration file to simplify command line usage and enable advanced Rollup functionality. ### Configuration Files @@ -10,11 +10,11 @@ Rollup configuration files are optional, but they are powerful and convenient an ```javascript export default { - input: 'src/main.js', - output: { - file: 'bundle.js', - format: 'cjs' - } + input: 'src/main.js', + output: { + file: 'bundle.js', + format: 'cjs' + } }; ``` @@ -29,89 +29,86 @@ Config files support the options listed below. Consult the [big list of options] ```javascript // rollup.config.js -export default { - // can be an array (for multiple inputs) - // core input options - external, - input, // conditionally required - plugins, - - // advanced input options - cache, - onwarn, - preserveEntrySignatures, - strictDeprecations, - - // danger zone - acorn, - acornInjectPlugins, - context, - moduleContext, - preserveSymlinks, - shimMissingExports, - treeshake, - - // experimental - experimentalCacheExpiry, - perf, - - output: { - // required (can be an array, for multiple outputs) - // core output options - dir, - file, - format, // required - globals, - name, - plugins, - - // advanced output options - assetFileNames, - banner, - chunkFileNames, - compact, - entryFileNames, - extend, - footer, - hoistTransitiveImports, - inlineDynamicImports, - interop, - intro, - manualChunks, - minifyInternalExports, - outro, - paths, - preserveModules, - preserveModulesRoot, - sourcemap, - sourcemapExcludeSources, - sourcemapFile, - sourcemapPathTransform, +export default { // can be an array (for multiple inputs) + // core input options + external, + input, // conditionally required + plugins, + + // advanced input options + cache, + onwarn, + preserveEntrySignatures, + strictDeprecations, + + // danger zone + acorn, + acornInjectPlugins, + context, + moduleContext, + preserveSymlinks, + shimMissingExports, + treeshake, + + // experimental + experimentalCacheExpiry, + perf, + + output: { // required (can be an array, for multiple outputs) + // core output options + dir, + file, + format, // required + globals, + name, + plugins, + + // advanced output options + assetFileNames, + banner, + chunkFileNames, + compact, + entryFileNames, + extend, + footer, + hoistTransitiveImports, + inlineDynamicImports, + interop, + intro, + manualChunks, + minifyInternalExports, + outro, + paths, + preserveModules, + preserveModulesRoot, + sourcemap, + sourcemapExcludeSources, + sourcemapFile, + sourcemapPathTransform, validate, - // danger zone - amd, - esModule, - exports, - externalLiveBindings, - freeze, - indent, - namespaceToStringTag, - noConflict, - preferConst, - sanitizeFileName,strict, - systemNullSetters - }, - - watch: - { - buildDelay, - chokidar, - clearScreen, - skipWrite, - exclude, - include - } | false + // danger zone + amd, + esModule, + exports, + externalLiveBindings, + freeze, + indent, + namespaceToStringTag, + noConflict, + preferConst, + sanitizeFileName,strict, + systemNullSetters + }, + + watch: { + buildDelay, + chokidar, + clearScreen, + skipWrite, + exclude, + include + } | false }; ``` @@ -120,28 +117,25 @@ You can export an **array** from your config file to build bundles from several ```javascript // rollup.config.js (building more than one bundle) -export default [ - { - input: 'main-a.js', - output: { - file: 'dist/bundle-a.js', - format: 'cjs' - } - }, - { - input: 'main-b.js', - output: [ - { - file: 'dist/bundle-b1.js', - format: 'cjs' - }, - { - file: 'dist/bundle-b2.js', - format: 'es' - } - ] - } -]; +export default [{ + input: 'main-a.js', + output: { + file: 'dist/bundle-a.js', + format: 'cjs' + } +}, { + input: 'main-b.js', + output: [ + { + file: 'dist/bundle-b1.js', + format: 'cjs' + }, + { + file: 'dist/bundle-b2.js', + format: 'es' + } + ] +}]; ``` If you want to create your config asynchronously, Rollup can also handle a `Promise` which resolves to an object or an array. @@ -156,7 +150,10 @@ Similarly, you can do this as well: ```javascript // rollup.config.js (Promise resolving an array) -export default Promise.all([fetch('get-config-1'), fetch('get-config-2')]); +export default Promise.all([ + fetch('get-config-1'), + fetch('get-config-2') +]) ``` To use Rollup with a configuration file, pass the `--config` or `-c` flags: @@ -179,11 +176,11 @@ import defaultConfig from './rollup.default.config.js'; import debugConfig from './rollup.debug.config.js'; export default commandLineArgs => { - if (commandLineArgs.configDebug === true) { - return debugConfig; - } - return defaultConfig; -}; + if (commandLineArgs.configDebug === true) { + return debugConfig; + } + return defaultConfig; +} ``` If you now run `rollup --config --configDebug`, the debug configuration will be used. @@ -258,11 +255,11 @@ By default, Rollup will expect config files to be ES modules and bundle and tran ```javascript // rollup.config.cjs module.exports = { - input: 'src/main.js', - output: { - file: 'bundle.js', - format: 'cjs' - } + input: 'src/main.js', + output: { + file: 'bundle.js', + format: 'cjs' + } }; ``` @@ -274,19 +271,18 @@ There are some potential gotchas when using `.mjs` on Node 13+: - You will only get a default export from CommonJS plugins - You may not be able to import JSON files such as your `package.json file`. There are two ways to go around this: - - run Rollup CLI via ``` node --experimental-json-modules ./node_modules/.bin/rollup --config ``` - + - create a CommonJS wrapper that requires the JSON file: - + ```js // load-package.cjs module.exports = require('./package.json'); - + // rollup.config.mjs import pkg from './load-package.cjs'; ... @@ -385,7 +381,6 @@ Use the specified plugin. There are several ways to specify plugins here: ``` The file should export a function returning a plugin object. - - Via the name of a plugin that is installed in a local or global `node_modules` folder: ``` @@ -447,9 +442,9 @@ will set `process.env.INCLUDE_DEPS === 'true'` and `process.env.BUILD === 'produ ```json // in package.json { - "scripts": { - "build": "rollup -c --environment INCLUDE_DEPS,BUILD:production" - } + "scripts": { + "build": "rollup -c --environment INCLUDE_DEPS,BUILD:production" + } } ``` @@ -484,7 +479,7 @@ echo "export const foo = 42;" | rollup --format cjs --file out.js When this file contains imports, Rollup will try to resolve them relative to the current working directory. When a config file is used, Rollup will only use `stdin` as an entry point if the file name of the entry point is `-`. To read a non-entry-point file from stdin, just call it `-`, which is the file name that is used internally to reference `stdin`. I.e. ```js -import foo from '-'; +import foo from "-"; ``` in any file will prompt Rollup to try to read the imported file from `stdin` and assign the default export to `foo`. You can pass the [`--no-stdin`](guide/en/#--no-stdin) CLI flag to Rollup to treat `-` as a regular file name instead. From 5f3d159b4d8b93612db65ff3fed8bf3b542e2621 Mon Sep 17 00:00:00 2001 From: syler Date: Sun, 25 Oct 2020 09:15:16 +0100 Subject: [PATCH 11/17] add ts config plugin option --- cli/run/import-ts-plugin.ts | 7 +++---- cli/run/loadConfigFile.ts | 4 +++- src/utils/options/mergeOptions.ts | 4 +++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cli/run/import-ts-plugin.ts b/cli/run/import-ts-plugin.ts index fbf8abbe236..7292191f609 100644 --- a/cli/run/import-ts-plugin.ts +++ b/cli/run/import-ts-plugin.ts @@ -1,10 +1,9 @@ import { resolve } from 'path'; -export default async (configPath: string) => { +export default async (configPath: string, tsPlugin: any) => { + const plugin = tsPlugin || '@rollup/plugin-typescript'; try { - return ( - await import(resolve(configPath, '../', 'node_modules/@rollup/plugin-typescript')) - ).default(); + return (await import(resolve(configPath, '../', 'node_modules/', plugin))).default(); } catch { throw new Error( 'Please install @rollup/plugin-typescript or use another config file extension.' diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index 14da1be3f3a..bca3838575c 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -53,6 +53,7 @@ async function loadConfigFile( ? getDefaultFromCjs(require(fileName)) : await getDefaultFromTranspiledConfigFile( fileName, + commandOptions.ts, commandOptions.silent, extension === '.ts' ); @@ -66,6 +67,7 @@ function getDefaultFromCjs(namespace: GenericConfigObject) { async function getDefaultFromTranspiledConfigFile( fileName: string, + tsConfigPlugin: boolean, silent: boolean, typescript: boolean ): Promise { @@ -75,7 +77,7 @@ async function getDefaultFromTranspiledConfigFile( (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5, id.length) === '.json', input: fileName, onwarn: warnings.add, - plugins: typescript ? [await importTypescriptPlugin(fileName)] : [], + plugins: typescript ? [await importTypescriptPlugin(fileName, tsConfigPlugin)] : [], treeshake: false }); if (!silent && warnings.count > 0) { diff --git a/src/utils/options/mergeOptions.ts b/src/utils/options/mergeOptions.ts index 4543df8d69a..86cd7a9782d 100644 --- a/src/utils/options/mergeOptions.ts +++ b/src/utils/options/mergeOptions.ts @@ -23,6 +23,7 @@ export const commandAliases: { [key: string]: string } = { n: 'name', o: 'file', p: 'plugin', + t: 'ts', v: 'version', w: 'watch' }; @@ -55,7 +56,8 @@ export function mergeOptions( 'silent', 'failAfterWarnings', 'stdin', - 'waitForBundleInput' + 'waitForBundleInput', + 'ts' ), 'CLI flags', warn, From cc66b34b1ef4255673b2a4496e32fa41db399a69 Mon Sep 17 00:00:00 2001 From: syler Date: Sun, 25 Oct 2020 09:28:04 +0100 Subject: [PATCH 12/17] rename ts option --- cli/run/loadConfigFile.ts | 2 +- src/utils/options/mergeOptions.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index bca3838575c..d5b22f356b3 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -53,7 +53,7 @@ async function loadConfigFile( ? getDefaultFromCjs(require(fileName)) : await getDefaultFromTranspiledConfigFile( fileName, - commandOptions.ts, + commandOptions.configPlugin, commandOptions.silent, extension === '.ts' ); diff --git a/src/utils/options/mergeOptions.ts b/src/utils/options/mergeOptions.ts index 86cd7a9782d..da07282f827 100644 --- a/src/utils/options/mergeOptions.ts +++ b/src/utils/options/mergeOptions.ts @@ -23,7 +23,6 @@ export const commandAliases: { [key: string]: string } = { n: 'name', o: 'file', p: 'plugin', - t: 'ts', v: 'version', w: 'watch' }; @@ -57,7 +56,7 @@ export function mergeOptions( 'failAfterWarnings', 'stdin', 'waitForBundleInput', - 'ts' + 'configPlugin' ), 'CLI flags', warn, From 061996afe1a93701fbc21c746f0f71317498fe35 Mon Sep 17 00:00:00 2001 From: syler Date: Sun, 25 Oct 2020 10:07:04 +0100 Subject: [PATCH 13/17] add custom config plugin option --- cli/run/getConfigPath.ts | 2 +- cli/run/import-ts-plugin.ts | 12 ------------ cli/run/importConfigPlugin.ts | 26 ++++++++++++++++++++++++++ cli/run/loadConfigFile.ts | 14 +++++++++----- 4 files changed, 36 insertions(+), 18 deletions(-) delete mode 100644 cli/run/import-ts-plugin.ts create mode 100644 cli/run/importConfigPlugin.ts diff --git a/cli/run/getConfigPath.ts b/cli/run/getConfigPath.ts index 3c8dc2c6ea8..655742d88d4 100644 --- a/cli/run/getConfigPath.ts +++ b/cli/run/getConfigPath.ts @@ -33,7 +33,7 @@ export function getConfigPath(commandConfig: string | true): string { function findConfigFileNameInCwd(): string { const filesInWorkingDir = new Set(readdirSync(process.cwd())); - for (const extension of ['mjs', 'cjs', 'ts']) { + for (const extension of ['mjs', 'cjs', 'ts', 'coffee']) { const fileName = `${DEFAULT_CONFIG_BASE}.${extension}`; if (filesInWorkingDir.has(fileName)) return fileName; } diff --git a/cli/run/import-ts-plugin.ts b/cli/run/import-ts-plugin.ts deleted file mode 100644 index 7292191f609..00000000000 --- a/cli/run/import-ts-plugin.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { resolve } from 'path'; - -export default async (configPath: string, tsPlugin: any) => { - const plugin = tsPlugin || '@rollup/plugin-typescript'; - try { - return (await import(resolve(configPath, '../', 'node_modules/', plugin))).default(); - } catch { - throw new Error( - 'Please install @rollup/plugin-typescript or use another config file extension.' - ); - } -}; diff --git a/cli/run/importConfigPlugin.ts b/cli/run/importConfigPlugin.ts new file mode 100644 index 00000000000..6d2902398fe --- /dev/null +++ b/cli/run/importConfigPlugin.ts @@ -0,0 +1,26 @@ +import { resolve } from 'path'; + +export default async function importCustomConfigPlugin( + configPath: string, + customPlugin: any, + extension: string +) { + let plugin = customPlugin; + if (!plugin || plugin === true) { + switch (extension) { + case '.ts': + plugin = '@rollup/plugin-typescript'; + break; + + default: + throw new Error('some error'); // TODO + } + } + try { + return (await import(resolve(configPath, '../', 'node_modules/', plugin))).default(); + } catch { + throw new Error( + 'Please install @rollup/plugin-typescript or use another config file extension.' + ); + } +} diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index d5b22f356b3..c3806e670be 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -11,7 +11,8 @@ import relativeId from '../../src/utils/relativeId'; import { stderr } from '../logging'; import batchWarnings, { BatchWarnings } from './batchWarnings'; import { addCommandPluginsToInputOptions } from './commandPlugins'; -import importTypescriptPlugin from './import-ts-plugin'; + +import importCustomConfigPlugin from './importConfigPlugin'; function supportsNativeESM() { return Number(/^v(\d+)/.exec(process.version)![1]) >= 13; @@ -55,7 +56,7 @@ async function loadConfigFile( fileName, commandOptions.configPlugin, commandOptions.silent, - extension === '.ts' + extension ); return getConfigList(configFileExport, commandOptions); @@ -67,9 +68,9 @@ function getDefaultFromCjs(namespace: GenericConfigObject) { async function getDefaultFromTranspiledConfigFile( fileName: string, - tsConfigPlugin: boolean, + configPlugin: boolean, silent: boolean, - typescript: boolean + extension: string ): Promise { const warnings = batchWarnings(); const bundle = await rollup.rollup({ @@ -77,7 +78,10 @@ async function getDefaultFromTranspiledConfigFile( (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5, id.length) === '.json', input: fileName, onwarn: warnings.add, - plugins: typescript ? [await importTypescriptPlugin(fileName, tsConfigPlugin)] : [], + plugins: + extension !== '.js' + ? [await importCustomConfigPlugin(fileName, configPlugin, extension)] + : [], treeshake: false }); if (!silent && warnings.count > 0) { From 738f32bf0e43bcd38f03570a07aeaf71fba8234e Mon Sep 17 00:00:00 2001 From: syler Date: Thu, 29 Oct 2020 08:40:04 +0100 Subject: [PATCH 14/17] use addCommandPluginsToInputOptions to load config plugin. --- cli/run/commandPlugins.ts | 8 +++++--- cli/run/getConfigPath.ts | 2 +- cli/run/importConfigPlugin.ts | 26 -------------------------- cli/run/loadConfigFile.ts | 26 ++++++++------------------ 4 files changed, 14 insertions(+), 48 deletions(-) delete mode 100644 cli/run/importConfigPlugin.ts diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index e2e6ea31a8d..2e5896347a9 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -5,15 +5,17 @@ import { waitForInputPlugin } from './waitForInput'; export function addCommandPluginsToInputOptions( inputOptions: InputOptions, - command: Record -): void { + command: Record, + pluginOption = 'plugin' +) { if (command.stdin !== false) { inputOptions.plugins!.push(stdinPlugin(command.stdin)); } if (command.waitForBundleInput === true) { inputOptions.plugins!.push(waitForInputPlugin()); } - const commandPlugin = command.plugin; + + const commandPlugin = command[pluginOption]; if (commandPlugin) { const plugins = Array.isArray(commandPlugin) ? commandPlugin : [commandPlugin]; for (const plugin of plugins) { diff --git a/cli/run/getConfigPath.ts b/cli/run/getConfigPath.ts index 655742d88d4..3c8dc2c6ea8 100644 --- a/cli/run/getConfigPath.ts +++ b/cli/run/getConfigPath.ts @@ -33,7 +33,7 @@ export function getConfigPath(commandConfig: string | true): string { function findConfigFileNameInCwd(): string { const filesInWorkingDir = new Set(readdirSync(process.cwd())); - for (const extension of ['mjs', 'cjs', 'ts', 'coffee']) { + for (const extension of ['mjs', 'cjs', 'ts']) { const fileName = `${DEFAULT_CONFIG_BASE}.${extension}`; if (filesInWorkingDir.has(fileName)) return fileName; } diff --git a/cli/run/importConfigPlugin.ts b/cli/run/importConfigPlugin.ts deleted file mode 100644 index 6d2902398fe..00000000000 --- a/cli/run/importConfigPlugin.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { resolve } from 'path'; - -export default async function importCustomConfigPlugin( - configPath: string, - customPlugin: any, - extension: string -) { - let plugin = customPlugin; - if (!plugin || plugin === true) { - switch (extension) { - case '.ts': - plugin = '@rollup/plugin-typescript'; - break; - - default: - throw new Error('some error'); // TODO - } - } - try { - return (await import(resolve(configPath, '../', 'node_modules/', plugin))).default(); - } catch { - throw new Error( - 'Please install @rollup/plugin-typescript or use another config file extension.' - ); - } -} diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index c3806e670be..4103a8a3655 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -12,8 +12,6 @@ import { stderr } from '../logging'; import batchWarnings, { BatchWarnings } from './batchWarnings'; import { addCommandPluginsToInputOptions } from './commandPlugins'; -import importCustomConfigPlugin from './importConfigPlugin'; - function supportsNativeESM() { return Number(/^v(\d+)/.exec(process.version)![1]) >= 13; } @@ -52,12 +50,7 @@ async function loadConfigFile( ? (await import(pathToFileURL(fileName).href)).default : extension === '.cjs' ? getDefaultFromCjs(require(fileName)) - : await getDefaultFromTranspiledConfigFile( - fileName, - commandOptions.configPlugin, - commandOptions.silent, - extension - ); + : await getDefaultFromTranspiledConfigFile(fileName, commandOptions); return getConfigList(configFileExport, commandOptions); } @@ -68,23 +61,20 @@ function getDefaultFromCjs(namespace: GenericConfigObject) { async function getDefaultFromTranspiledConfigFile( fileName: string, - configPlugin: boolean, - silent: boolean, - extension: string + commandOptions: any ): Promise { const warnings = batchWarnings(); - const bundle = await rollup.rollup({ + const inputOptions = { external: (id: string) => (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5, id.length) === '.json', input: fileName, onwarn: warnings.add, - plugins: - extension !== '.js' - ? [await importCustomConfigPlugin(fileName, configPlugin, extension)] - : [], + plugins: [], treeshake: false - }); - if (!silent && warnings.count > 0) { + }; + addCommandPluginsToInputOptions(inputOptions, commandOptions, 'configPlugin'); + const bundle = await rollup.rollup(inputOptions); + if (!commandOptions.silent && warnings.count > 0) { stderr(bold(`loaded ${relativeId(fileName)} with warnings`)); warnings.flush(); } From cef8b2d8d09e42d907811a613d2f81f4f7ba93d0 Mon Sep 17 00:00:00 2001 From: syler Date: Thu, 29 Oct 2020 08:56:52 +0100 Subject: [PATCH 15/17] autoresolve configPlugin if typescipt config file. --- cli/run/commandPlugins.ts | 18 ++++++++++++++++-- cli/run/loadConfigFile.ts | 7 ++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index 2e5896347a9..270f998a206 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -6,7 +6,8 @@ import { waitForInputPlugin } from './waitForInput'; export function addCommandPluginsToInputOptions( inputOptions: InputOptions, command: Record, - pluginOption = 'plugin' + pluginOption = 'plugin', + extension = '' ) { if (command.stdin !== false) { inputOptions.plugins!.push(stdinPlugin(command.stdin)); @@ -15,7 +16,7 @@ export function addCommandPluginsToInputOptions( inputOptions.plugins!.push(waitForInputPlugin()); } - const commandPlugin = command[pluginOption]; + const commandPlugin = resolveCommandPlugin(command, pluginOption, extension); if (commandPlugin) { const plugins = Array.isArray(commandPlugin) ? commandPlugin : [commandPlugin]; for (const plugin of plugins) { @@ -32,6 +33,19 @@ export function addCommandPluginsToInputOptions( } } +function resolveCommandPlugin(command: any, pluginOption: string, extension: string) { + if ( + (pluginOption === 'configPlugin' && !command[pluginOption]) || + command[pluginOption] === true + ) { + switch (extension) { + case '.ts': + return 'typescript'; + } + } + return command[pluginOption]; +} + function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string) { let plugin: any = null; let pluginArg: any = undefined; diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index 4103a8a3655..16a900f36af 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -50,7 +50,7 @@ async function loadConfigFile( ? (await import(pathToFileURL(fileName).href)).default : extension === '.cjs' ? getDefaultFromCjs(require(fileName)) - : await getDefaultFromTranspiledConfigFile(fileName, commandOptions); + : await getDefaultFromTranspiledConfigFile(fileName, commandOptions, extension); return getConfigList(configFileExport, commandOptions); } @@ -61,7 +61,8 @@ function getDefaultFromCjs(namespace: GenericConfigObject) { async function getDefaultFromTranspiledConfigFile( fileName: string, - commandOptions: any + commandOptions: any, + extension: string ): Promise { const warnings = batchWarnings(); const inputOptions = { @@ -72,7 +73,7 @@ async function getDefaultFromTranspiledConfigFile( plugins: [], treeshake: false }; - addCommandPluginsToInputOptions(inputOptions, commandOptions, 'configPlugin'); + addCommandPluginsToInputOptions(inputOptions, commandOptions, 'configPlugin', extension); const bundle = await rollup.rollup(inputOptions); if (!commandOptions.silent && warnings.count > 0) { stderr(bold(`loaded ${relativeId(fileName)} with warnings`)); From ffe2359e6bef0035cb4b5a11f05d63285969ccee Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 14 Jun 2021 07:56:21 +0200 Subject: [PATCH 16/17] Fix dependencies and test --- docs/01-command-line-reference.md | 3 +- package-lock.json | 315 +++++++++++++++++------------- package.json | 2 +- rollup.config.js | 4 +- test/misc/optionList.js | 2 +- 5 files changed, 181 insertions(+), 145 deletions(-) diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index dfaa6b163e9..fdd17064b4c 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -97,7 +97,8 @@ export default { // can be an array (for multiple inputs) namespaceToStringTag, noConflict, preferConst, - sanitizeFileName,strict, + sanitizeFileName, + strict, systemNullSetters }, diff --git a/package-lock.json b/package-lock.json index 1696700e06b..42f0e14727a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,32 +13,27 @@ "@babel/highlight": "^7.10.4" } }, - "@babel/compat-data": { - "version": "7.14.0", - "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.14.0.tgz", - "integrity": "sha512-vu9V3uMM/1o5Hl5OekMUowo3FqXLJSw+s+66nt0fSWVWTtmosdzn45JHOB3cPtZoe6CTBDzvSw0RdOY85Q37+Q==", - "dev": true - }, "@babel/core": { - "version": "7.14.0", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.14.0.tgz", - "integrity": "sha512-8YqpRig5NmIHlMLw09zMlPTvUVMILjqCOtVgu+TVNWEBvy9b5I3RRyhqnrV4hjgEK7n8P9OqvkWJAFmEL6Wwfw==", + "version": "7.12.3", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.12.3.tgz", + "integrity": "sha512-0qXcZYKZp3/6N2jKYVxZv0aNCsxTSVCiK72DTiTYZAu7sjg73W0/aynWjMbiGd87EQL4WyA8reiJVh92AVla9g==", "dev": true, "requires": { - "@babel/code-frame": "^7.12.13", - "@babel/generator": "^7.14.0", - "@babel/helper-compilation-targets": "^7.13.16", - "@babel/helper-module-transforms": "^7.14.0", - "@babel/helpers": "^7.14.0", - "@babel/parser": "^7.14.0", - "@babel/template": "^7.12.13", - "@babel/traverse": "^7.14.0", - "@babel/types": "^7.14.0", + "@babel/code-frame": "^7.10.4", + "@babel/generator": "^7.12.1", + "@babel/helper-module-transforms": "^7.12.1", + "@babel/helpers": "^7.12.1", + "@babel/parser": "^7.12.3", + "@babel/template": "^7.10.4", + "@babel/traverse": "^7.12.1", + "@babel/types": "^7.12.1", "convert-source-map": "^1.7.0", "debug": "^4.1.0", - "gensync": "^1.0.0-beta.2", + "gensync": "^1.0.0-beta.1", "json5": "^2.1.2", - "semver": "^6.3.0", + "lodash": "^4.17.19", + "resolve": "^1.3.2", + "semver": "^5.4.1", "source-map": "^0.5.0" }, "dependencies": { @@ -61,9 +56,9 @@ } }, "semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", "dev": true }, "source-map": { @@ -75,12 +70,12 @@ } }, "@babel/generator": { - "version": "7.14.1", - "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.14.1.tgz", - "integrity": "sha512-TMGhsXMXCP/O1WtQmZjpEYDhCYC9vFhayWZPJSZCGkPJgUqX0rF0wwtrYvnzVxIjcF80tkUertXVk5cwqi5cAQ==", + "version": "7.12.1", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.12.1.tgz", + "integrity": "sha512-DB+6rafIdc9o72Yc3/Ph5h+6hUjeOp66pF0naQBgUFFuPqzQwIlPTm3xZR7YNvduIMtkDIj2t21LSQwnbCrXvg==", "dev": true, "requires": { - "@babel/types": "^7.14.1", + "@babel/types": "^7.12.1", "jsesc": "^2.5.1", "source-map": "^0.5.0" }, @@ -99,26 +94,6 @@ } } }, - "@babel/helper-compilation-targets": { - "version": "7.13.16", - "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.13.16.tgz", - "integrity": "sha512-3gmkYIrpqsLlieFwjkGgLaSHmhnvlAYzZLlYVjlW+QwI+1zE17kGxuJGmIqDQdYp56XdmGeD+Bswx0UTyG18xA==", - "dev": true, - "requires": { - "@babel/compat-data": "^7.13.15", - "@babel/helper-validator-option": "^7.12.17", - "browserslist": "^4.14.5", - "semver": "^6.3.0" - }, - "dependencies": { - "semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", - "dev": true - } - } - }, "@babel/helper-function-name": { "version": "7.12.13", "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.12.13.tgz", @@ -128,6 +103,24 @@ "@babel/helper-get-function-arity": "^7.12.13", "@babel/template": "^7.12.13", "@babel/types": "^7.12.13" + }, + "dependencies": { + "@babel/helper-validator-identifier": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.14.5.tgz", + "integrity": "sha512-5lsetuxCLilmVGyiLEfoHBRX8UCFD+1m2x3Rj97WrW3V7H3u4RWRXA4evMjImCsin2J2YT0QaVDGf+z8ondbAg==", + "dev": true + }, + "@babel/types": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.14.5.tgz", + "integrity": "sha512-M/NzBpEL95I5Hh4dwhin5JlE7EzO5PHMAuzjxss3tiOBD46KfQvVedN/3jEPZvdRvtsK2222XfdHogNIttFgcg==", + "dev": true, + "requires": { + "@babel/helper-validator-identifier": "^7.14.5", + "to-fast-properties": "^2.0.0" + } + } } }, "@babel/helper-get-function-arity": { @@ -137,40 +130,59 @@ "dev": true, "requires": { "@babel/types": "^7.12.13" + }, + "dependencies": { + "@babel/helper-validator-identifier": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.14.5.tgz", + "integrity": "sha512-5lsetuxCLilmVGyiLEfoHBRX8UCFD+1m2x3Rj97WrW3V7H3u4RWRXA4evMjImCsin2J2YT0QaVDGf+z8ondbAg==", + "dev": true + }, + "@babel/types": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.14.5.tgz", + "integrity": "sha512-M/NzBpEL95I5Hh4dwhin5JlE7EzO5PHMAuzjxss3tiOBD46KfQvVedN/3jEPZvdRvtsK2222XfdHogNIttFgcg==", + "dev": true, + "requires": { + "@babel/helper-validator-identifier": "^7.14.5", + "to-fast-properties": "^2.0.0" + } + } } }, "@babel/helper-member-expression-to-functions": { - "version": "7.13.12", - "resolved": "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.13.12.tgz", - "integrity": "sha512-48ql1CLL59aKbU94Y88Xgb2VFy7a95ykGRbJJaaVv+LX5U8wFpLfiGXJJGUozsmA1oEh/o5Bp60Voq7ACyA/Sw==", + "version": "7.12.1", + "resolved": "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.12.1.tgz", + "integrity": "sha512-k0CIe3tXUKTRSoEx1LQEPFU9vRQfqHtl+kf8eNnDqb4AUJEy5pz6aIiog+YWtVm2jpggjS1laH68bPsR+KWWPQ==", "dev": true, "requires": { - "@babel/types": "^7.13.12" + "@babel/types": "^7.12.1" } }, "@babel/helper-module-imports": { - "version": "7.13.12", - "resolved": "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-7.13.12.tgz", - "integrity": "sha512-4cVvR2/1B693IuOvSI20xqqa/+bl7lqAMR59R4iu39R9aOX8/JoYY1sFaNvUMyMBGnHdwvJgUrzNLoUZxXypxA==", + "version": "7.12.1", + "resolved": "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-7.12.1.tgz", + "integrity": "sha512-ZeC1TlMSvikvJNy1v/wPIazCu3NdOwgYZLIkmIyAsGhqkNpiDoQQRmaCK8YP4Pq3GPTLPV9WXaPCJKvx06JxKA==", "dev": true, "requires": { - "@babel/types": "^7.13.12" + "@babel/types": "^7.12.1" } }, "@babel/helper-module-transforms": { - "version": "7.14.0", - "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.14.0.tgz", - "integrity": "sha512-L40t9bxIuGOfpIGA3HNkJhU9qYrf4y5A5LUSw7rGMSn+pcG8dfJ0g6Zval6YJGd2nEjI7oP00fRdnhLKndx6bw==", + "version": "7.12.1", + "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.12.1.tgz", + "integrity": "sha512-QQzehgFAZ2bbISiCpmVGfiGux8YVFXQ0abBic2Envhej22DVXV9nCFaS5hIQbkyo1AdGb+gNME2TSh3hYJVV/w==", "dev": true, "requires": { - "@babel/helper-module-imports": "^7.13.12", - "@babel/helper-replace-supers": "^7.13.12", - "@babel/helper-simple-access": "^7.13.12", - "@babel/helper-split-export-declaration": "^7.12.13", - "@babel/helper-validator-identifier": "^7.14.0", - "@babel/template": "^7.12.13", - "@babel/traverse": "^7.14.0", - "@babel/types": "^7.14.0" + "@babel/helper-module-imports": "^7.12.1", + "@babel/helper-replace-supers": "^7.12.1", + "@babel/helper-simple-access": "^7.12.1", + "@babel/helper-split-export-declaration": "^7.11.0", + "@babel/helper-validator-identifier": "^7.10.4", + "@babel/template": "^7.10.4", + "@babel/traverse": "^7.12.1", + "@babel/types": "^7.12.1", + "lodash": "^4.17.19" } }, "@babel/helper-optimise-call-expression": { @@ -180,27 +192,45 @@ "dev": true, "requires": { "@babel/types": "^7.12.13" + }, + "dependencies": { + "@babel/helper-validator-identifier": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.14.5.tgz", + "integrity": "sha512-5lsetuxCLilmVGyiLEfoHBRX8UCFD+1m2x3Rj97WrW3V7H3u4RWRXA4evMjImCsin2J2YT0QaVDGf+z8ondbAg==", + "dev": true + }, + "@babel/types": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.14.5.tgz", + "integrity": "sha512-M/NzBpEL95I5Hh4dwhin5JlE7EzO5PHMAuzjxss3tiOBD46KfQvVedN/3jEPZvdRvtsK2222XfdHogNIttFgcg==", + "dev": true, + "requires": { + "@babel/helper-validator-identifier": "^7.14.5", + "to-fast-properties": "^2.0.0" + } + } } }, "@babel/helper-replace-supers": { - "version": "7.13.12", - "resolved": "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.13.12.tgz", - "integrity": "sha512-Gz1eiX+4yDO8mT+heB94aLVNCL+rbuT2xy4YfyNqu8F+OI6vMvJK891qGBTqL9Uc8wxEvRW92Id6G7sDen3fFw==", + "version": "7.12.1", + "resolved": "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.12.1.tgz", + "integrity": "sha512-zJjTvtNJnCFsCXVi5rUInstLd/EIVNmIKA1Q9ynESmMBWPWd+7sdR+G4/wdu+Mppfep0XLyG2m7EBPvjCeFyrw==", "dev": true, "requires": { - "@babel/helper-member-expression-to-functions": "^7.13.12", - "@babel/helper-optimise-call-expression": "^7.12.13", - "@babel/traverse": "^7.13.0", - "@babel/types": "^7.13.12" + "@babel/helper-member-expression-to-functions": "^7.12.1", + "@babel/helper-optimise-call-expression": "^7.10.4", + "@babel/traverse": "^7.12.1", + "@babel/types": "^7.12.1" } }, "@babel/helper-simple-access": { - "version": "7.13.12", - "resolved": "https://registry.npmjs.org/@babel/helper-simple-access/-/helper-simple-access-7.13.12.tgz", - "integrity": "sha512-7FEjbrx5SL9cWvXioDbnlYTppcZGuCY6ow3/D5vMggb2Ywgu4dMrpTJX0JdQAIcRRUElOIxF3yEooa9gUb9ZbA==", + "version": "7.12.1", + "resolved": "https://registry.npmjs.org/@babel/helper-simple-access/-/helper-simple-access-7.12.1.tgz", + "integrity": "sha512-OxBp7pMrjVewSSC8fXDFrHrBcJATOOFssZwv16F3/6Xtc138GHybBfPbm9kfiqQHKhYQrlamWILwlDCeyMFEaA==", "dev": true, "requires": { - "@babel/types": "^7.13.12" + "@babel/types": "^7.12.1" } }, "@babel/helper-split-export-declaration": { @@ -210,6 +240,24 @@ "dev": true, "requires": { "@babel/types": "^7.12.13" + }, + "dependencies": { + "@babel/helper-validator-identifier": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.14.5.tgz", + "integrity": "sha512-5lsetuxCLilmVGyiLEfoHBRX8UCFD+1m2x3Rj97WrW3V7H3u4RWRXA4evMjImCsin2J2YT0QaVDGf+z8ondbAg==", + "dev": true + }, + "@babel/types": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.14.5.tgz", + "integrity": "sha512-M/NzBpEL95I5Hh4dwhin5JlE7EzO5PHMAuzjxss3tiOBD46KfQvVedN/3jEPZvdRvtsK2222XfdHogNIttFgcg==", + "dev": true, + "requires": { + "@babel/helper-validator-identifier": "^7.14.5", + "to-fast-properties": "^2.0.0" + } + } } }, "@babel/helper-validator-identifier": { @@ -218,21 +266,15 @@ "integrity": "sha512-V3ts7zMSu5lfiwWDVWzRDGIN+lnCEUdaXgtVHJgLb1rGaA6jMrtB9EmE7L18foXJIE8Un/A/h6NJfGQp/e1J4A==", "dev": true }, - "@babel/helper-validator-option": { - "version": "7.12.17", - "resolved": "https://registry.npmjs.org/@babel/helper-validator-option/-/helper-validator-option-7.12.17.tgz", - "integrity": "sha512-TopkMDmLzq8ngChwRlyjR6raKD6gMSae4JdYDB8bByKreQgG0RBTuKe9LRxW3wFtUnjxOPRKBDwEH6Mg5KeDfw==", - "dev": true - }, "@babel/helpers": { - "version": "7.14.0", - "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.14.0.tgz", - "integrity": "sha512-+ufuXprtQ1D1iZTO/K9+EBRn+qPWMJjZSw/S0KlFrxCw4tkrzv9grgpDHkY9MeQTjTY8i2sp7Jep8DfU6tN9Mg==", + "version": "7.12.1", + "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.12.1.tgz", + "integrity": "sha512-9JoDSBGoWtmbay98efmT2+mySkwjzeFeAL9BuWNoVQpkPFQF8SIIFUfY5os9u8wVzglzoiPRSW7cuJmBDUt43g==", "dev": true, "requires": { - "@babel/template": "^7.12.13", - "@babel/traverse": "^7.14.0", - "@babel/types": "^7.14.0" + "@babel/template": "^7.10.4", + "@babel/traverse": "^7.12.1", + "@babel/types": "^7.12.1" } }, "@babel/highlight": { @@ -247,9 +289,9 @@ } }, "@babel/parser": { - "version": "7.14.1", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.14.1.tgz", - "integrity": "sha512-muUGEKu8E/ftMTPlNp+mc6zL3E9zKWmF5sDHZ5MSsoTP9Wyz64AhEf9kD08xYJ7w6Hdcu8H550ircnPyWSIF0Q==", + "version": "7.12.3", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.12.3.tgz", + "integrity": "sha512-kFsOS0IbsuhO5ojF8Hc8z/8vEIOkylVBrjiZUbLTE3XFe0Qi+uu6HjzQixkFaqr0ZPAMZcBVxEwmsnsLPZ2Xsw==", "dev": true }, "@babel/template": { @@ -271,23 +313,46 @@ "requires": { "@babel/highlight": "^7.12.13" } + }, + "@babel/helper-validator-identifier": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.14.5.tgz", + "integrity": "sha512-5lsetuxCLilmVGyiLEfoHBRX8UCFD+1m2x3Rj97WrW3V7H3u4RWRXA4evMjImCsin2J2YT0QaVDGf+z8ondbAg==", + "dev": true + }, + "@babel/parser": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.14.5.tgz", + "integrity": "sha512-TM8C+xtH/9n1qzX+JNHi7AN2zHMTiPUtspO0ZdHflW8KaskkALhMmuMHb4bCmNdv9VAPzJX3/bXqkVLnAvsPfg==", + "dev": true + }, + "@babel/types": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.14.5.tgz", + "integrity": "sha512-M/NzBpEL95I5Hh4dwhin5JlE7EzO5PHMAuzjxss3tiOBD46KfQvVedN/3jEPZvdRvtsK2222XfdHogNIttFgcg==", + "dev": true, + "requires": { + "@babel/helper-validator-identifier": "^7.14.5", + "to-fast-properties": "^2.0.0" + } } } }, "@babel/traverse": { - "version": "7.14.0", - "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.14.0.tgz", - "integrity": "sha512-dZ/a371EE5XNhTHomvtuLTUyx6UEoJmYX+DT5zBCQN3McHemsuIaKKYqsc/fs26BEkHs/lBZy0J571LP5z9kQA==", + "version": "7.12.1", + "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.12.1.tgz", + "integrity": "sha512-MA3WPoRt1ZHo2ZmoGKNqi20YnPt0B1S0GTZEPhhd+hw2KGUzBlHuVunj6K4sNuK+reEvyiPwtp0cpaqLzJDmAw==", "dev": true, "requires": { - "@babel/code-frame": "^7.12.13", - "@babel/generator": "^7.14.0", - "@babel/helper-function-name": "^7.12.13", - "@babel/helper-split-export-declaration": "^7.12.13", - "@babel/parser": "^7.14.0", - "@babel/types": "^7.14.0", + "@babel/code-frame": "^7.10.4", + "@babel/generator": "^7.12.1", + "@babel/helper-function-name": "^7.10.4", + "@babel/helper-split-export-declaration": "^7.11.0", + "@babel/parser": "^7.12.1", + "@babel/types": "^7.12.1", "debug": "^4.1.0", - "globals": "^11.1.0" + "globals": "^11.1.0", + "lodash": "^4.17.19" }, "dependencies": { "@babel/code-frame": { @@ -308,12 +373,13 @@ } }, "@babel/types": { - "version": "7.14.1", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.14.1.tgz", - "integrity": "sha512-S13Qe85fzLs3gYRUnrpyeIrBJIMYv33qSTg1qoBwiG6nPKwUWAD9odSzWhEedpwOIzSEI6gbdQIWEMiCI42iBA==", + "version": "7.12.1", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.12.1.tgz", + "integrity": "sha512-BzSY3NJBKM4kyatSOWh3D/JJ2O3CVzBybHWxtgxnggaxEuaSTTDqeiSb/xk9lrkw2Tbqyivw5ZU4rT+EfznQsA==", "dev": true, "requires": { - "@babel/helper-validator-identifier": "^7.14.0", + "@babel/helper-validator-identifier": "^7.10.4", + "lodash": "^4.17.19", "to-fast-properties": "^2.0.0" } }, @@ -545,9 +611,9 @@ } }, "@rollup/plugin-typescript": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-6.0.0.tgz", - "integrity": "sha512-Y5U2L4eaF3wUSgCZRMdvNmuzWkKMyN3OwvhAdbzAi5sUqedaBk/XbzO4T7RlViDJ78MOPhwAIv2FtId/jhMtbg==", + "version": "8.2.1", + "resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-8.2.1.tgz", + "integrity": "sha512-Qd2E1pleDR4bwyFxqbjt4eJf+wB0UKVMLc7/BAFDGVdAXQMCsD4DUv5/7/ww47BZCYxWtJqe1Lo0KVNswBJlRw==", "dev": true, "requires": { "@rollup/pluginutils": "^3.1.0", @@ -1001,19 +1067,6 @@ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", "dev": true }, - "browserslist": { - "version": "4.16.6", - "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.16.6.tgz", - "integrity": "sha512-Wspk/PqO+4W9qp5iUTJsa1B/QrYn1keNCcEP5OvP7WBwT4KaDly0uONYmC6Xa3Z5IqnUgS0KcgLYu1l74x0ZXQ==", - "dev": true, - "requires": { - "caniuse-lite": "^1.0.30001219", - "colorette": "^1.2.2", - "electron-to-chromium": "^1.3.723", - "escalade": "^3.1.1", - "node-releases": "^1.1.71" - } - }, "buble": { "version": "0.20.0", "resolved": "https://registry.npmjs.org/buble/-/buble-0.20.0.tgz", @@ -1083,12 +1136,6 @@ "integrity": "sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==", "dev": true }, - "caniuse-lite": { - "version": "1.0.30001223", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001223.tgz", - "integrity": "sha512-k/RYs6zc/fjbxTjaWZemeSmOjO0JJV+KguOBA3NwPup8uzxM1cMhR2BD9XmO86GuqaqTCO8CgkgH9Rz//vdDiA==", - "dev": true - }, "chalk": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", @@ -1428,12 +1475,6 @@ "esutils": "^2.0.2" } }, - "electron-to-chromium": { - "version": "1.3.727", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.727.tgz", - "integrity": "sha512-Mfz4FIB4FSvEwBpDfdipRIrwd6uo8gUDoRDF4QEYb4h4tSuI3ov594OrjU6on042UlFHouIJpClDODGkPcBSbg==", - "dev": true - }, "emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", @@ -3595,12 +3636,6 @@ "process-on-spawn": "^1.0.0" } }, - "node-releases": { - "version": "1.1.71", - "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-1.1.71.tgz", - "integrity": "sha512-zR6HoT6LrLCRBwukmrVbHv0EpEQjksO6GmFcZQQuCAy139BEsoVKPYnf3jongYW83fAa1torLGYwxxky/p28sg==", - "dev": true - }, "normalize-package-data": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz", diff --git a/package.json b/package.json index 7f2d65b29d9..f3df747bfce 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.0.0", "@rollup/plugin-replace": "^2.4.2", - "@rollup/plugin-typescript": "^6.0.0", + "@rollup/plugin-typescript": "^8.2.1", "@types/micromatch": "^4.0.1", "@types/node": "^10.17.51", "@types/require-relative": "^0.8.0", diff --git a/rollup.config.js b/rollup.config.js index 59043d4ba7a..110e503a0ca 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -41,7 +41,7 @@ const onwarn = warning => { // eslint-disable-next-line no-console console.error( 'Building Rollup produced warnings that need to be resolved. ' + - 'Please keep in mind that the browser build may never have external dependencies!' + 'Please keep in mind that the browser build may never have external dependencies!' ); throw new Error(warning.message); }; @@ -68,7 +68,7 @@ const nodePlugins = [ conditionalFsEventsImport(), string({ include: '**/*.md' }), commonjs({ include: 'node_modules/**' }), - typescript(), + typescript() ]; export default command => { diff --git a/test/misc/optionList.js b/test/misc/optionList.js index b0cadbe0fc9..4f1ef129f06 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, makeAbsoluteExternalsRelative, 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, makeAbsoluteExternalsRelative, manualChunks, minifyInternalExports, moduleContext, n, name, namespaceToStringTag, noConflict, o, onwarn, outro, p, paths, perf, plugin, plugins, preferConst, preserveEntrySignatures, preserveModules, preserveModulesRoot, preserveSymlinks, sanitizeFileName, shimMissingExports, silent, sourcemap, sourcemapExcludeSources, sourcemapFile, stdin, strict, strictDeprecations, systemNullSetters, treeshake, v, validate, w, waitForBundleInput, watch'; + 'acorn, acornInjectPlugins, amd, assetFileNames, banner, c, cache, chunkFileNames, compact, config, configPlugin, 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, makeAbsoluteExternalsRelative, manualChunks, minifyInternalExports, moduleContext, n, name, namespaceToStringTag, noConflict, o, onwarn, outro, p, paths, perf, plugin, plugins, preferConst, preserveEntrySignatures, preserveModules, preserveModulesRoot, preserveSymlinks, sanitizeFileName, shimMissingExports, silent, sourcemap, sourcemapExcludeSources, sourcemapFile, stdin, strict, strictDeprecations, systemNullSetters, treeshake, v, validate, 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, preserveModulesRoot, sanitizeFileName, sourcemap, sourcemapExcludeSources, sourcemapFile, sourcemapPathTransform, strict, systemNullSetters, validate'; From ac35be24abe224796a936ff4ff03eef4af800b9d Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Tue, 15 Jun 2021 14:56:57 +0200 Subject: [PATCH 17/17] Remove TypeScript default, always transpile config if configPlugin option is used --- cli/run/commandPlugins.ts | 28 +++++++-------------- cli/run/loadConfigFile.ts | 16 ++++++------ docs/01-command-line-reference.md | 18 ++++++++++++- test/cli/samples/config-ts/_config.js | 4 +-- test/cli/samples/config-ts/main.js | 2 +- test/cli/samples/config-ts/rollup.config.ts | 12 ++++----- 6 files changed, 42 insertions(+), 38 deletions(-) diff --git a/cli/run/commandPlugins.ts b/cli/run/commandPlugins.ts index 270f998a206..0602437e628 100644 --- a/cli/run/commandPlugins.ts +++ b/cli/run/commandPlugins.ts @@ -5,18 +5,21 @@ import { waitForInputPlugin } from './waitForInput'; export function addCommandPluginsToInputOptions( inputOptions: InputOptions, - command: Record, - pluginOption = 'plugin', - extension = '' -) { + command: Record +): void { if (command.stdin !== false) { inputOptions.plugins!.push(stdinPlugin(command.stdin)); } if (command.waitForBundleInput === true) { inputOptions.plugins!.push(waitForInputPlugin()); } + addPluginsFromCommandOption(command.plugin, inputOptions); +} - const commandPlugin = resolveCommandPlugin(command, pluginOption, extension); +export function addPluginsFromCommandOption( + commandPlugin: unknown, + inputOptions: InputOptions +): void { if (commandPlugin) { const plugins = Array.isArray(commandPlugin) ? commandPlugin : [commandPlugin]; for (const plugin of plugins) { @@ -33,20 +36,7 @@ export function addCommandPluginsToInputOptions( } } -function resolveCommandPlugin(command: any, pluginOption: string, extension: string) { - if ( - (pluginOption === 'configPlugin' && !command[pluginOption]) || - command[pluginOption] === true - ) { - switch (extension) { - case '.ts': - return 'typescript'; - } - } - return command[pluginOption]; -} - -function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string) { +function loadAndRegisterPlugin(inputOptions: InputOptions, pluginText: string): void { let plugin: any = null; let pluginArg: any = undefined; if (pluginText[0] === '{') { diff --git a/cli/run/loadConfigFile.ts b/cli/run/loadConfigFile.ts index 16a900f36af..ad4d180c580 100644 --- a/cli/run/loadConfigFile.ts +++ b/cli/run/loadConfigFile.ts @@ -10,7 +10,7 @@ import { GenericConfigObject } from '../../src/utils/options/options'; import relativeId from '../../src/utils/relativeId'; import { stderr } from '../logging'; import batchWarnings, { BatchWarnings } from './batchWarnings'; -import { addCommandPluginsToInputOptions } from './commandPlugins'; +import { addCommandPluginsToInputOptions, addPluginsFromCommandOption } from './commandPlugins'; function supportsNativeESM() { return Number(/^v(\d+)/.exec(process.version)![1]) >= 13; @@ -41,16 +41,17 @@ export default async function loadAndParseConfigFile( async function loadConfigFile( fileName: string, - commandOptions: any + commandOptions: Record ): Promise { const extension = path.extname(fileName); const configFileExport = - extension === '.mjs' && supportsNativeESM() - ? (await import(pathToFileURL(fileName).href)).default + commandOptions.configPlugin || + !(extension === '.cjs' || (extension === '.mjs' && supportsNativeESM())) + ? await getDefaultFromTranspiledConfigFile(fileName, commandOptions) : extension === '.cjs' ? getDefaultFromCjs(require(fileName)) - : await getDefaultFromTranspiledConfigFile(fileName, commandOptions, extension); + : (await import(pathToFileURL(fileName).href)).default; return getConfigList(configFileExport, commandOptions); } @@ -61,8 +62,7 @@ function getDefaultFromCjs(namespace: GenericConfigObject) { async function getDefaultFromTranspiledConfigFile( fileName: string, - commandOptions: any, - extension: string + commandOptions: Record ): Promise { const warnings = batchWarnings(); const inputOptions = { @@ -73,7 +73,7 @@ async function getDefaultFromTranspiledConfigFile( plugins: [], treeshake: false }; - addCommandPluginsToInputOptions(inputOptions, commandOptions, 'configPlugin', extension); + addPluginsFromCommandOption(commandOptions.configPlugin, inputOptions); const bundle = await rollup.rollup(inputOptions); if (!commandOptions.silent && warnings.count > 0) { stderr(bold(`loaded ${relativeId(fileName)} with warnings`)); diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index fdd17064b4c..7244dc14bad 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -22,7 +22,11 @@ Typically, it is called `rollup.config.js` and sits in the root directory of you If you want to write your config as a CommonJS module using `require` and `module.exports`, you should change the file extension to `.cjs`, which will prevent Rollup from trying to transpile the file. Furthermore if you are on Node 13+, changing the file extension to `.mjs` will also prevent Rollup from transpiling it but import the file as an ES module instead. See [using untranspiled config files](guide/en/#using-untranspiled-config-files) for more details and why you might want to do this. -You can also use a typescript config by using the `.ts` extension and installing `@rollup/plugin-typescript` +You can also use other languages for your configuration files like TypeScript. To do that, install a corresponding Rollup plugin like `@rollup/plugin-typescript` and use the [`--configPlugin`](guide/en/#--configplugin-plugin) option: + +``` +rollup --config rollup.config.ts --configPlugin typescript +``` Config files support the options listed below. Consult the [big list of options](guide/en/#big-list-of-options) for details on each option: @@ -229,6 +233,8 @@ export default defineConfig({ }) ``` +See also the [`--configPlugin`](guide/en/#--configplugin-plugin) for how to write your config in TypeScript. + ### Differences to the JavaScript API While config files provide an easy way to configure Rollup, they also limit how Rollup can be invoked and where configuration is taken from. Especially if you are rebundling Rollup in another build tool or want to integrate it into an advanced build process, it may be better to directly invoke Rollup programmatically from your scripts. @@ -412,6 +418,16 @@ By default, plugin functions be called with no argument to create the plugin. Yo rollup -i input.js -f es -p 'terser={output: {beautify: true, indent_level: 2}}' ``` +#### `--configPlugin ` + +Allows to specify Rollup plugins to transpile or otherwise control the parsing of your configuration file. The main benefit is that it allows you to use non-JavaScript configuration files. For instance the following will allow you to write your configuration in TypeScript, provided you have `@rollup/plugin-typescript` installed: + +``` +rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript +``` + +It supports the same syntax as the [`--plugin`](guide/en/#-p-plugin---plugin-plugin) option i.e. you can spcify the option multiple times, you can omit the `@rollup/plugin-` prefix and just write `typescript` and you can specify plugin options via `={...}`. + #### `-v`/`--version` Print the installed version number. diff --git a/test/cli/samples/config-ts/_config.js b/test/cli/samples/config-ts/_config.js index 2a20be8e666..b4e2435de4a 100644 --- a/test/cli/samples/config-ts/_config.js +++ b/test/cli/samples/config-ts/_config.js @@ -1,5 +1,5 @@ module.exports = { - description: 'uses config file (.ts)', - command: 'rollup --config rollup.config.ts', + description: 'supports loading TypeScript config files via plugin option', + command: 'rollup --config rollup.config.ts --configPlugin typescript', execute: true }; diff --git a/test/cli/samples/config-ts/main.js b/test/cli/samples/config-ts/main.js index df16c1b06b9..e37ed30c344 100644 --- a/test/cli/samples/config-ts/main.js +++ b/test/cli/samples/config-ts/main.js @@ -1 +1 @@ -assert.equal( ANSWER, 42 ); +assert.ok( true ); diff --git a/test/cli/samples/config-ts/rollup.config.ts b/test/cli/samples/config-ts/rollup.config.ts index d8ff40bfaae..33f98d1b575 100644 --- a/test/cli/samples/config-ts/rollup.config.ts +++ b/test/cli/samples/config-ts/rollup.config.ts @@ -1,12 +1,10 @@ -import replace from '@rollup/plugin-replace'; +import { RollupOptions } from '../../../../dist/rollup'; -export default { +const options: RollupOptions = { input: 'main.js', output: { format: 'cjs' - }, - plugins: [ - //@ts-ignore - replace({ ANSWER: 42 }) - ] + } }; + +export { options as default };