diff --git a/packages/babel-cli/src/babel/util.js b/packages/babel-cli/src/babel/util.js index b2d22a3f77..ad972fd0cd 100644 --- a/packages/babel-cli/src/babel/util.js +++ b/packages/babel-cli/src/babel/util.js @@ -49,9 +49,14 @@ export function addSourceMappingUrl(code, loc) { return code + "\n//# sourceMappingURL=" + path.basename(loc); } +const CALLER = { + name: "@babel/cli", +}; + export function transform(filename, code, opts) { opts = { ...opts, + caller: CALLER, filename, }; @@ -64,6 +69,11 @@ export function transform(filename, code, opts) { } export function compile(filename, opts) { + opts = { + ...opts, + caller: CALLER, + }; + return new Promise((resolve, reject) => { babel.transformFile(filename, opts, (err, result) => { if (err) reject(err); diff --git a/packages/babel-core/src/config/caching.js b/packages/babel-core/src/config/caching.js index 7c3a28bda2..c8db5a166c 100644 --- a/packages/babel-core/src/config/caching.js +++ b/packages/babel-core/src/config/caching.js @@ -206,12 +206,29 @@ function makeSimpleConfigurator( return; } - return cache.using(val); + return cache.using(() => assertSimpleType(val())); } cacheFn.forever = () => cache.forever(); cacheFn.never = () => cache.never(); - cacheFn.using = cb => cache.using(() => cb()); - cacheFn.invalidate = cb => cache.invalidate(() => cb()); + cacheFn.using = cb => cache.using(() => assertSimpleType(cb())); + cacheFn.invalidate = cb => cache.invalidate(() => assertSimpleType(cb())); return (cacheFn: any); } + +// Types are limited here so that in the future these values can be used +// as part of Babel's caching logic. +type SimpleType = string | boolean | number | null | void; +export function assertSimpleType(value: mixed): SimpleType { + if ( + value != null && + typeof value !== "string" && + typeof value !== "boolean" && + typeof value !== "number" + ) { + throw new Error( + "Cache keys must be either string, boolean, number, null, or undefined.", + ); + } + return value; +} diff --git a/packages/babel-core/src/config/config-chain.js b/packages/babel-core/src/config/config-chain.js index e81ca81f74..258b2c725c 100644 --- a/packages/babel-core/src/config/config-chain.js +++ b/packages/babel-core/src/config/config-chain.js @@ -8,6 +8,7 @@ import { type IgnoreList, type ConfigApplicableTest, type BabelrcSearch, + type CallerMetadata, } from "./validation/options"; import pathPatternToRegex from "./pattern-to-regex"; @@ -50,12 +51,27 @@ export type ConfigContext = { cwd: string, root: string, envName: string, + caller: CallerMetadata | void, }; /** * Build a config chain for a given preset. */ -export const buildPresetChain: ( +export function buildPresetChain( + arg: PresetInstance, + context: *, +): ConfigChain | null { + const chain = buildPresetChainWalker(arg, context); + if (!chain) return null; + + return { + plugins: dedupDescriptors(chain.plugins), + presets: dedupDescriptors(chain.presets), + options: chain.options, + }; +} + +export const buildPresetChainWalker: ( arg: PresetInstance, context: *, ) => * = makeChainWalker({ @@ -128,9 +144,14 @@ export function buildRootChain( let configFile; if (typeof opts.configFile === "string") { - configFile = loadConfig(opts.configFile, context.cwd, context.envName); + configFile = loadConfig( + opts.configFile, + context.cwd, + context.envName, + context.caller, + ); } else if (opts.configFile !== false) { - configFile = findRootConfig(context.root, context.envName); + configFile = findRootConfig(context.root, context.envName, context.caller); } let { babelrc, babelrcRoots } = opts; @@ -234,7 +255,7 @@ function babelrcLoadEnabled( if (typeof pat === "string") pat = pathPatternToRegex(pat, context.cwd); return pkgData.directories.some(directory => { - return matchPattern(pat, context.cwd, directory); + return matchPattern(pat, context.cwd, directory, context); }); }); } @@ -449,7 +470,12 @@ function mergeExtendsChain( ): boolean { if (opts.extends === undefined) return true; - const file = loadConfig(opts.extends, dirname, context.envName); + const file = loadConfig( + opts.extends, + dirname, + context.envName, + context.caller, + ); if (files.has(file)) { throw new Error( @@ -629,12 +655,23 @@ function matchesPatterns( dirname: string, ): boolean { return patterns.some(pattern => - matchPattern(pattern, dirname, context.filename), + matchPattern(pattern, dirname, context.filename, context), ); } -function matchPattern(pattern, dirname, pathToTest): boolean { - if (typeof pattern === "function") return !!pattern(pathToTest); +function matchPattern( + pattern, + dirname, + pathToTest, + context: ConfigContext, +): boolean { + if (typeof pattern === "function") { + return !!pattern(pathToTest, { + dirname, + envName: context.envName, + caller: context.caller, + }); + } if (typeof pathToTest !== "string") { throw new Error( diff --git a/packages/babel-core/src/config/files/configuration.js b/packages/babel-core/src/config/files/configuration.js index 66b39cb2c0..b31519b43f 100644 --- a/packages/babel-core/src/config/files/configuration.js +++ b/packages/babel-core/src/config/files/configuration.js @@ -14,6 +14,7 @@ import makeAPI from "../helpers/config-api"; import { makeStaticFileCache } from "./utils"; import pathPatternToRegex from "../pattern-to-regex"; import type { FilePackageData, RelativeConfig, ConfigFile } from "./types"; +import type { CallerMetadata } from "../validation/options"; const debug = buildDebug("babel:config:loading:files:configuration"); @@ -26,6 +27,7 @@ const BABELIGNORE_FILENAME = ".babelignore"; export function findRelativeConfig( packageData: FilePackageData, envName: string, + caller: CallerMetadata | void, ): RelativeConfig { let config = null; let ignore = null; @@ -37,7 +39,7 @@ export function findRelativeConfig( config = [BABELRC_FILENAME, BABELRC_JS_FILENAME].reduce( (previousConfig: ConfigFile | null, name) => { const filepath = path.join(loc, name); - const config = readConfig(filepath, envName); + const config = readConfig(filepath, envName, caller); if (config && previousConfig) { throw new Error( @@ -91,10 +93,11 @@ export function findRelativeConfig( export function findRootConfig( dirname: string, envName: string, + caller: CallerMetadata | void, ): ConfigFile | null { const filepath = path.resolve(dirname, BABEL_CONFIG_JS_FILENAME); - const conf = readConfig(filepath, envName); + const conf = readConfig(filepath, envName, caller); if (conf) { debug("Found root config %o in $o.", BABEL_CONFIG_JS_FILENAME, dirname); } @@ -105,10 +108,11 @@ export function loadConfig( name: string, dirname: string, envName: string, + caller: CallerMetadata | void, ): ConfigFile { const filepath = resolve.sync(name, { basedir: dirname }); - const conf = readConfig(filepath, envName); + const conf = readConfig(filepath, envName, caller); if (!conf) { throw new Error(`Config file ${filepath} contains no configuration data`); } @@ -121,16 +125,22 @@ export function loadConfig( * Read the given config file, returning the result. Returns null if no config was found, but will * throw if there are parsing errors while loading a config. */ -function readConfig(filepath, envName): ConfigFile | null { +function readConfig(filepath, envName, caller): ConfigFile | null { return path.extname(filepath) === ".js" - ? readConfigJS(filepath, { envName }) + ? readConfigJS(filepath, { envName, caller }) : readConfigJSON5(filepath); } const LOADING_CONFIGS = new Set(); const readConfigJS = makeStrongCache( - (filepath, cache: CacheConfigurator<{ envName: string }>) => { + ( + filepath, + cache: CacheConfigurator<{ + envName: string, + caller: CallerMetadata | void, + }>, + ) => { if (!fs.existsSync(filepath)) { cache.forever(); return null; diff --git a/packages/babel-core/src/config/files/index-browser.js b/packages/babel-core/src/config/files/index-browser.js index cbc8b69a98..3305c346e4 100644 --- a/packages/babel-core/src/config/files/index-browser.js +++ b/packages/babel-core/src/config/files/index-browser.js @@ -7,6 +7,8 @@ import type { FilePackageData, } from "./types"; +import type { CallerMetadata } from "../validation/options"; + export type { ConfigFile, IgnoreFile, RelativeConfig, FilePackageData }; export function findPackageData(filepath: string): FilePackageData { @@ -21,6 +23,7 @@ export function findPackageData(filepath: string): FilePackageData { export function findRelativeConfig( pkgData: FilePackageData, // eslint-disable-line no-unused-vars envName: string, // eslint-disable-line no-unused-vars + caller: CallerMetadata | void, // eslint-disable-line no-unused-vars ): RelativeConfig { return { pkg: null, config: null, ignore: null }; } @@ -28,6 +31,7 @@ export function findRelativeConfig( export function findRootConfig( dirname: string, // eslint-disable-line no-unused-vars envName: string, // eslint-disable-line no-unused-vars + caller: CallerMetadata | void, // eslint-disable-line no-unused-vars ): ConfigFile | null { return null; } @@ -36,6 +40,7 @@ export function loadConfig( name: string, dirname: string, envName: string, // eslint-disable-line no-unused-vars + caller: CallerMetadata | void, // eslint-disable-line no-unused-vars ): ConfigFile { throw new Error(`Cannot load ${name} relative to ${dirname} in a browser`); } diff --git a/packages/babel-core/src/config/full.js b/packages/babel-core/src/config/full.js index ffdb71b02d..af4212953f 100644 --- a/packages/babel-core/src/config/full.js +++ b/packages/babel-core/src/config/full.js @@ -13,7 +13,7 @@ import { import type { UnloadedDescriptor } from "./config-descriptors"; import traverse from "@babel/traverse"; import { makeWeakCache, type CacheConfigurator } from "./caching"; -import { validate } from "./validation/options"; +import { validate, type CallerMetadata } from "./validation/options"; import { validatePluginObject } from "./validation/plugins"; import makeAPI from "./helpers/config-api"; @@ -41,6 +41,7 @@ export type PluginPasses = Array; // process 'ignore'/'only' and other filename-based logic. type SimpleContext = { envName: string, + caller: CallerMetadata | void, }; export default function loadFullConfig( diff --git a/packages/babel-core/src/config/helpers/config-api.js b/packages/babel-core/src/config/helpers/config-api.js index da14c22eeb..fa106f0ce2 100644 --- a/packages/babel-core/src/config/helpers/config-api.js +++ b/packages/babel-core/src/config/helpers/config-api.js @@ -2,7 +2,13 @@ import semver from "semver"; import { version as coreVersion } from "../../"; -import type { CacheConfigurator, SimpleCacheConfigurator } from "../caching"; +import { + assertSimpleType, + type CacheConfigurator, + type SimpleCacheConfigurator, +} from "../caching"; + +import type { CallerMetadata } from "../validation/options"; type EnvFunction = { (): string, @@ -20,12 +26,14 @@ export type PluginAPI = { }; export default function makeAPI( - cache: CacheConfigurator<{ envName: string }>, + cache: CacheConfigurator<{ envName: string, caller: CallerMetadata | void }>, ): PluginAPI { const env: any = value => cache.using(data => { if (typeof value === "undefined") return data.envName; - if (typeof value === "function") return value(data.envName); + if (typeof value === "function") { + return assertSimpleType(value(data.envName)); + } if (!Array.isArray(value)) value = [value]; return value.some(entry => { @@ -36,12 +44,16 @@ export default function makeAPI( }); }); + const caller: any = cb => + cache.using(data => assertSimpleType(cb(data.caller))); + return { version: coreVersion, cache: cache.simple(), // Expose ".env()" so people can easily get the same env that we expose using the "env" key. env, async: () => false, + caller, assertVersion, }; } diff --git a/packages/babel-core/src/config/partial.js b/packages/babel-core/src/config/partial.js index 206f7c35de..31b2aa0cf4 100644 --- a/packages/babel-core/src/config/partial.js +++ b/packages/babel-core/src/config/partial.js @@ -28,7 +28,7 @@ export default function loadPrivatePartialConfig( const args = inputOpts ? validate("arguments", inputOpts) : {}; - const { envName = getEnv(), cwd = ".", root: rootDir = "." } = args; + const { envName = getEnv(), cwd = ".", root: rootDir = ".", caller } = args; const absoluteCwd = path.resolve(cwd); const absoluteRootDir = path.resolve(absoluteCwd, rootDir); @@ -40,6 +40,7 @@ export default function loadPrivatePartialConfig( cwd: absoluteCwd, root: absoluteRootDir, envName, + caller, }; const configChain = buildRootChain(args, context); diff --git a/packages/babel-core/src/config/validation/option-assertions.js b/packages/babel-core/src/config/validation/option-assertions.js index a00f430b8c..e8f51d2d12 100644 --- a/packages/babel-core/src/config/validation/option-assertions.js +++ b/packages/babel-core/src/config/validation/option-assertions.js @@ -14,6 +14,7 @@ import type { CompactOption, RootInputSourceMapOption, NestingPath, + CallerMetadata, } from "./options"; export type ValidatorSet = { @@ -103,6 +104,41 @@ export function assertSourceType( return value; } +export function assertCallerMetadata( + loc: OptionPath, + value: mixed, +): CallerMetadata | void { + const obj = assertObject(loc, value); + if (obj) { + if (typeof obj[("name": string)] !== "string") { + throw new Error( + `${msg(loc)} set but does not contain "name" property string`, + ); + } + + for (const prop of Object.keys(obj)) { + const propLoc = access(loc, prop); + const value = obj[prop]; + if ( + value != null && + typeof value !== "boolean" && + typeof value !== "string" && + typeof value !== "number" + ) { + // NOTE(logan): I'm limiting the type here so that we can guarantee that + // the "caller" value will serialize to JSON nicely. We can always + // allow more complex structures later though. + throw new Error( + `${msg( + propLoc, + )} must be null, undefined, a boolean, a string, or a number.`, + ); + } + } + } + return (value: any); +} + export function assertInputSourceMap( loc: OptionPath, value: mixed, diff --git a/packages/babel-core/src/config/validation/options.js b/packages/babel-core/src/config/validation/options.js index 085f0d3608..8497c9661b 100644 --- a/packages/babel-core/src/config/validation/options.js +++ b/packages/babel-core/src/config/validation/options.js @@ -11,6 +11,7 @@ import { assertBoolean, assertObject, assertArray, + assertCallerMetadata, assertInputSourceMap, assertIgnoreList, assertPluginList, @@ -33,6 +34,9 @@ const ROOT_VALIDATORS: ValidatorSet = { $PropertyType, >), + caller: (assertCallerMetadata: Validator< + $PropertyType, + >), filename: (assertString: Validator< $PropertyType, >), @@ -176,6 +180,7 @@ export type ValidatedOptions = { ast?: boolean, inputSourceMap?: RootInputSourceMapOption, envName?: string, + caller?: CallerMetadata, extends?: string, env?: EnvSet, @@ -225,6 +230,11 @@ export type ValidatedOptions = { generatorOpts?: {}, }; +export type CallerMetadata = { + // If 'caller' is specified, require that the name is given for debugging + // messages. + name: string, +}; export type EnvSet = { [string]: ?T, }; diff --git a/packages/babel-core/test/config-loading.js b/packages/babel-core/test/config-loading.js index 7b8d116234..b31eac8ed2 100644 --- a/packages/babel-core/test/config-loading.js +++ b/packages/babel-core/test/config-loading.js @@ -323,4 +323,51 @@ describe("@babel/core config loading", () => { } }); }); + + describe("caller metadata", () => { + it("should pass caller data through", () => { + const options1 = loadConfig({ + ...makeOpts(), + caller: { + name: "babel-test", + someFlag: true, + }, + }).options; + + expect(options1.caller.name).toBe("babel-test"); + expect(options1.caller.someFlag).toBe(true); + }); + + it("should pass unknown caller data through", () => { + const options1 = loadConfig({ + ...makeOpts(), + caller: undefined, + }).options; + + expect(options1.caller).toBeUndefined(); + }); + + it("should pass caller data to test functions", () => { + const options1 = loadConfig({ + ...makeOpts(), + caller: { + name: "babel-test", + someFlag: true, + }, + overrides: [ + { + test: (filename, { caller }) => caller.name === "babel-test", + comments: false, + }, + { + test: (filename, { caller }) => caller.name !== "babel-test", + ast: false, + }, + ], + }).options; + + expect(options1.comments).toBe(false); + expect(options1.ast).not.toBe(false); + }); + }); }); diff --git a/packages/babel-node/src/_babel-node.js b/packages/babel-node/src/_babel-node.js index 27f94a3d73..42ebf774d1 100644 --- a/packages/babel-node/src/_babel-node.js +++ b/packages/babel-node/src/_babel-node.js @@ -60,6 +60,9 @@ program.usage("[options] [ -e script | script.js ] [arguments]"); program.parse(process.argv); register({ + caller: { + name: "@babel/node", + }, extensions: program.extensions, ignore: program.ignore, only: program.only, diff --git a/packages/babel-preset-env/src/index.js b/packages/babel-preset-env/src/index.js index bed18c728b..338a25a6ab 100644 --- a/packages/babel-preset-env/src/index.js +++ b/packages/babel-preset-env/src/index.js @@ -155,6 +155,10 @@ const filterItems = ( return result; }; +function supportsStaticESM(caller) { + return !!(caller && caller.supportsStaticESM); +} + export default declare((api, opts) => { api.assertVersion(7); @@ -232,9 +236,15 @@ export default declare((api, opts) => { const plugins = []; const pluginUseBuiltIns = useBuiltIns !== false; - // NOTE: not giving spec here yet to avoid compatibility issues when - // transform-modules-commonjs gets its spec mode - if (modules !== false && moduleTransformations[modules]) { + if ( + modules !== false && + moduleTransformations[modules] && + // TODO: Remove the 'api.caller' check eventually. Just here to prevent + // unnecessary breakage in the short term for users on older betas/RCs. + (modules !== "auto" || !api.caller || !api.caller(supportsStaticESM)) + ) { + // NOTE: not giving spec here yet to avoid compatibility issues when + // transform-modules-commonjs gets its spec mode plugins.push([getPlugin(moduleTransformations[modules]), { loose }]); } diff --git a/packages/babel-preset-env/src/module-transformations.js b/packages/babel-preset-env/src/module-transformations.js index ad50bdc263..a98f8dd044 100644 --- a/packages/babel-preset-env/src/module-transformations.js +++ b/packages/babel-preset-env/src/module-transformations.js @@ -1,4 +1,5 @@ export default { + auto: "transform-modules-commonjs", amd: "transform-modules-amd", commonjs: "transform-modules-commonjs", cjs: "transform-modules-commonjs", diff --git a/packages/babel-preset-env/src/normalize-options.js b/packages/babel-preset-env/src/normalize-options.js index 838625ecc2..6d1e58ffce 100644 --- a/packages/babel-preset-env/src/normalize-options.js +++ b/packages/babel-preset-env/src/normalize-options.js @@ -128,13 +128,16 @@ export const validateIgnoreBrowserslistConfig = ( ); export const validateModulesOption = ( - modulesOpt: ModuleOption = ModulesOption.commonjs, + modulesOpt: ModuleOption = ModulesOption.auto, ) => { invariant( ModulesOption[modulesOpt] || ModulesOption[modulesOpt] === ModulesOption.false, - `Invalid Option: The 'modules' option must be either 'false' to indicate no modules, or a - module type which can be be one of: 'commonjs' (default), 'amd', 'umd', 'systemjs'.`, + `Invalid Option: The 'modules' option must be one of \n` + + ` - 'false' to indicate no module processing\n` + + ` - a specific module type: 'commonjs', 'amd', 'umd', 'systemjs'` + + ` - 'auto' (default) which will automatically select 'false' if the current\n` + + ` process is known to support ES module syntax, or "commonjs" otherwise\n`, ); return modulesOpt; diff --git a/packages/babel-preset-env/src/options.js b/packages/babel-preset-env/src/options.js index b201bac971..e1f08ccd9b 100644 --- a/packages/babel-preset-env/src/options.js +++ b/packages/babel-preset-env/src/options.js @@ -15,6 +15,7 @@ export const TopLevelOptions = { export const ModulesOption = { false: false, + auto: "auto", amd: "amd", commonjs: "commonjs", cjs: "cjs", diff --git a/packages/babel-preset-env/test/debug-fixtures/android/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/android/stdout.txt index b5ec3f027f..f74f6151c9 100644 --- a/packages/babel-preset-env/test/debug-fixtures/android/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/android/stdout.txt @@ -5,7 +5,7 @@ Using targets: "android": "4" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-template-literals { "android":"4" } @@ -149,4 +149,4 @@ Using polyfills with `entry` option: web.timers { "android":"4" } web.immediate { "android":"4" } web.dom.iterable { "android":"4" } -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/builtins-no-import/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/builtins-no-import/stdout.txt index 4dc3eb3de1..6f6c512833 100644 --- a/packages/babel-preset-env/test/debug-fixtures/builtins-no-import/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/builtins-no-import/stdout.txt @@ -5,7 +5,7 @@ Using targets: "node": "6" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-function-name { "node":"6" } @@ -23,4 +23,4 @@ Using plugins: Using polyfills with `entry` option: [/src/in.js] `import '@babel/polyfill'` was not found. -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/builtins/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/builtins/stdout.txt index c3d1d33a78..2f431dd2ab 100644 --- a/packages/babel-preset-env/test/debug-fixtures/builtins/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/builtins/stdout.txt @@ -7,7 +7,7 @@ Using targets: "node": "6" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-template-literals { "ie":"10" } @@ -165,4 +165,4 @@ Using polyfills with `entry` option: web.timers { "chrome":"54", "ie":"10", "node":"6" } web.immediate { "chrome":"54", "ie":"10", "node":"6" } web.dom.iterable { "chrome":"54", "ie":"10", "node":"6" } -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/electron/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/electron/stdout.txt index 551dbe28da..c0b392313f 100644 --- a/packages/babel-preset-env/test/debug-fixtures/electron/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/electron/stdout.txt @@ -12,7 +12,7 @@ Using targets: "electron": "0.36" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-function-name { "electron":"0.36" } @@ -125,4 +125,4 @@ Using polyfills with `entry` option: web.timers { "electron":"0.36" } web.immediate { "electron":"0.36" } web.dom.iterable { "electron":"0.36" } -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/plugins-only/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/plugins-only/stdout.txt index 03b791a6ce..460d1d4440 100644 --- a/packages/babel-preset-env/test/debug-fixtures/plugins-only/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/plugins-only/stdout.txt @@ -13,7 +13,7 @@ Using targets: "node": "7.4" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-literals { "firefox":"52" } @@ -28,4 +28,4 @@ Using plugins: proposal-optional-catch-binding { "firefox":"52", "node":"7.4" } Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set. -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/shippedProposals-chrome60/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/shippedProposals-chrome60/stdout.txt index 50ffbc89a9..951d93305d 100644 --- a/packages/babel-preset-env/test/debug-fixtures/shippedProposals-chrome60/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/shippedProposals-chrome60/stdout.txt @@ -5,7 +5,7 @@ Using targets: "chrome": "60" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-dotall-regex { "chrome":"60" } @@ -29,4 +29,4 @@ Using polyfills with `entry` option: web.timers { "chrome":"60" } web.immediate { "chrome":"60" } web.dom.iterable { "chrome":"60" } -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/shippedProposals/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/shippedProposals/stdout.txt index 737cf67d3e..7afc25b3b6 100644 --- a/packages/babel-preset-env/test/debug-fixtures/shippedProposals/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/shippedProposals/stdout.txt @@ -3,7 +3,7 @@ Using targets: {} -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-template-literals {} @@ -183,4 +183,4 @@ Using polyfills with `entry` option: web.timers {} web.immediate {} web.dom.iterable {} -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/specific-targets/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/specific-targets/stdout.txt index bd9ebdd8b8..3752cdf46a 100644 --- a/packages/babel-preset-env/test/debug-fixtures/specific-targets/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/specific-targets/stdout.txt @@ -10,7 +10,7 @@ Using targets: "safari": "7" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-template-literals { "ie":"10", "safari":"7" } @@ -169,4 +169,4 @@ Using polyfills with `entry` option: web.timers { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" } web.immediate { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" } web.dom.iterable { "chrome":"54", "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" } -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/usage-none/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/usage-none/stdout.txt index a676e90e8a..b53b349d9c 100644 --- a/packages/babel-preset-env/test/debug-fixtures/usage-none/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/usage-none/stdout.txt @@ -7,7 +7,7 @@ Using targets: "ie": "11" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-template-literals { "ie":"11" } @@ -43,4 +43,4 @@ Using polyfills with `usage` option: [/src/in.js] Based on your code and targets, none were added. [/src/in2.js] Based on your code and targets, none were added. -Successfully compiled 2 files with Babel. \ No newline at end of file +Successfully compiled 2 files with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/usage-with-import/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/usage-with-import/stdout.txt index ac3f472444..c898cba790 100644 --- a/packages/babel-preset-env/test/debug-fixtures/usage-with-import/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/usage-with-import/stdout.txt @@ -5,7 +5,7 @@ Using targets: "chrome": "55" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-dotall-regex { "chrome":"55" } @@ -18,4 +18,4 @@ Using plugins: Using polyfills with `usage` option: [/src/in.js] Based on your code and targets, none were added. -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/usage/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/usage/stdout.txt index 15817ea3f5..423d048ecb 100644 --- a/packages/babel-preset-env/test/debug-fixtures/usage/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/usage/stdout.txt @@ -7,7 +7,7 @@ Using targets: "ie": "11" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-template-literals { "ie":"11" } @@ -49,4 +49,4 @@ Using polyfills with `usage` option: [/src/in2.js] Added following polyfills: regenerator-runtime { "chrome":"52", "firefox":"50", "ie":"11" } web.dom.iterable { "chrome":"52", "firefox":"50", "ie":"11" } -Successfully compiled 2 files with Babel. \ No newline at end of file +Successfully compiled 2 files with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/versions-decimals/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/versions-decimals/stdout.txt index bce66a7bd0..9edbf1b625 100644 --- a/packages/babel-preset-env/test/debug-fixtures/versions-decimals/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/versions-decimals/stdout.txt @@ -16,7 +16,7 @@ Using targets: "node": "6.1" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-template-literals { "ie":"10" } @@ -195,4 +195,4 @@ Using polyfills with `entry` option: web.timers { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" } web.immediate { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" } web.dom.iterable { "chrome":"54", "electron":"0.36", "ie":"10", "node":"6.1" } -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/debug-fixtures/versions-strings/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/versions-strings/stdout.txt index caf39ec6a3..ded27be01c 100644 --- a/packages/babel-preset-env/test/debug-fixtures/versions-strings/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/versions-strings/stdout.txt @@ -7,7 +7,7 @@ Using targets: "node": "6.10" } -Using modules transform: commonjs +Using modules transform: auto Using plugins: transform-template-literals { "ie":"10" } @@ -165,4 +165,4 @@ Using polyfills with `entry` option: web.timers { "chrome":"54", "ie":"10", "node":"6.10" } web.immediate { "chrome":"54", "ie":"10", "node":"6.10" } web.dom.iterable { "chrome":"54", "ie":"10", "node":"6.10" } -Successfully compiled 1 file with Babel. \ No newline at end of file +Successfully compiled 1 file with Babel. diff --git a/packages/babel-preset-env/test/fixtures/modules/auto-cjs/input.mjs b/packages/babel-preset-env/test/fixtures/modules/auto-cjs/input.mjs new file mode 100644 index 0000000000..e5a2b28c2f --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/modules/auto-cjs/input.mjs @@ -0,0 +1,2 @@ +import mod from "mod"; +console.log(mod); diff --git a/packages/babel-preset-env/test/fixtures/modules/auto-cjs/options.json b/packages/babel-preset-env/test/fixtures/modules/auto-cjs/options.json new file mode 100644 index 0000000000..e35c724b5a --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/modules/auto-cjs/options.json @@ -0,0 +1,9 @@ +{ + "caller": { + "name": "test-fixture", + "supportsStaticESM": false + }, + "presets": [ + "env" + ] +} diff --git a/packages/babel-preset-env/test/fixtures/modules/auto-cjs/output.js b/packages/babel-preset-env/test/fixtures/modules/auto-cjs/output.js new file mode 100644 index 0000000000..d79cb698b5 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/modules/auto-cjs/output.js @@ -0,0 +1,7 @@ +"use strict"; + +var _mod = _interopRequireDefault(require("mod")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +console.log(_mod.default); diff --git a/packages/babel-preset-env/test/fixtures/modules/auto-esm/input.mjs b/packages/babel-preset-env/test/fixtures/modules/auto-esm/input.mjs new file mode 100644 index 0000000000..e5a2b28c2f --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/modules/auto-esm/input.mjs @@ -0,0 +1,2 @@ +import mod from "mod"; +console.log(mod); diff --git a/packages/babel-preset-env/test/fixtures/modules/auto-esm/options.json b/packages/babel-preset-env/test/fixtures/modules/auto-esm/options.json new file mode 100644 index 0000000000..a4e6bf5cde --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/modules/auto-esm/options.json @@ -0,0 +1,9 @@ +{ + "caller": { + "name": "test-fixture", + "supportsStaticESM": true + }, + "presets": [ + "env" + ] +} diff --git a/packages/babel-preset-env/test/fixtures/modules/auto-esm/output.mjs b/packages/babel-preset-env/test/fixtures/modules/auto-esm/output.mjs new file mode 100644 index 0000000000..e5a2b28c2f --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/modules/auto-esm/output.mjs @@ -0,0 +1,2 @@ +import mod from "mod"; +console.log(mod); diff --git a/packages/babel-preset-env/test/fixtures/modules/auto-unknown/input.mjs b/packages/babel-preset-env/test/fixtures/modules/auto-unknown/input.mjs new file mode 100644 index 0000000000..e5a2b28c2f --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/modules/auto-unknown/input.mjs @@ -0,0 +1,2 @@ +import mod from "mod"; +console.log(mod); diff --git a/packages/babel-preset-env/test/fixtures/modules/auto-unknown/options.json b/packages/babel-preset-env/test/fixtures/modules/auto-unknown/options.json new file mode 100644 index 0000000000..b207aefdde --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/modules/auto-unknown/options.json @@ -0,0 +1,5 @@ +{ + "presets": [ + "env" + ] +} diff --git a/packages/babel-preset-env/test/fixtures/modules/auto-unknown/output.js b/packages/babel-preset-env/test/fixtures/modules/auto-unknown/output.js new file mode 100644 index 0000000000..d79cb698b5 --- /dev/null +++ b/packages/babel-preset-env/test/fixtures/modules/auto-unknown/output.js @@ -0,0 +1,7 @@ +"use strict"; + +var _mod = _interopRequireDefault(require("mod")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +console.log(_mod.default); diff --git a/packages/babel-preset-env/test/normalize-options.spec.js b/packages/babel-preset-env/test/normalize-options.spec.js index abe163e0cf..7a0f481f7b 100644 --- a/packages/babel-preset-env/test/normalize-options.spec.js +++ b/packages/babel-preset-env/test/normalize-options.spec.js @@ -134,16 +134,20 @@ describe("normalize-options", () => { }); describe("validateModulesOption", () => { - it("`undefined` option returns commonjs", () => { - expect(validateModulesOption()).toBe("commonjs"); + it("`undefined` option returns auto", () => { + expect(validateModulesOption()).toBe("auto"); }); - it("`false` option returns commonjs", () => { + it("`false` option returns false", () => { expect(validateModulesOption(false)).toBe(false); }); + it("auto option is valid", () => { + expect(validateModulesOption("auto")).toBe("auto"); + }); + it("commonjs option is valid", () => { - expect(validateModulesOption()).toBe("commonjs"); + expect(validateModulesOption("commonjs")).toBe("commonjs"); }); it("systemjs option is valid", () => { diff --git a/packages/babel-register/src/node.js b/packages/babel-register/src/node.js index c73ddef062..84dbe8b5cd 100644 --- a/packages/babel-register/src/node.js +++ b/packages/babel-register/src/node.js @@ -125,6 +125,10 @@ export default function register(opts?: Object = {}) { transformOpts = { ...opts, + caller: { + name: "@babel/register", + ...(opts.caller || {}), + }, }; let { cwd = "." } = transformOpts;