Skip to content

Commit

Permalink
migrate @babel/preset-env to babel-preset-modules (#24780)
Browse files Browse the repository at this point in the history
* modern preset

* switch language_out to be NO_TRANSPILE when esm flag is used

* fix formatting issue
  • Loading branch information
erwinmombay committed Sep 30, 2019
1 parent 8370cee commit 587f0b4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
27 changes: 15 additions & 12 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ const noModuleTarget = {
: ['Last 2 versions'],
};

const moduleTarget = {
'esmodules': true,
};

// eslint-disable-next-line local/no-module-exports
module.exports = function(api) {
api.cache(true);
Expand All @@ -51,14 +47,21 @@ module.exports = function(api) {
}
return {
'presets': [
[
'@babel/preset-env',
{
'modules': isDist ? false : 'commonjs',
'loose': true,
'targets': esm ? moduleTarget : noModuleTarget,
},
],
esm
? [
'babel-preset-modules',
{
'loose': true,
},
]
: [
'@babel/preset-env',
{
'modules': isDist ? false : 'commonjs',
'loose': true,
'targets': noModuleTarget,
},
],
],
'compact': false,
'sourceType': 'module',
Expand Down
2 changes: 1 addition & 1 deletion build-system/compile/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function compile(entryModuleFilenames, outputDir, outputFilename, options) {
assume_function_wrapper: true,
// Transpile from ES6 to ES5 if not running with `--esm`
// otherwise transpilation is done by Babel
language_in: 'ECMASCRIPT6',
language_in: argv.esm ? 'ECMASCRIPT_2017' : 'ECMASCRIPT6',
language_out: argv.esm ? 'NO_TRANSPILE' : 'ECMASCRIPT5',
// We do not use the polyfills provided by closure compiler.
// If you need a polyfill. Manually include them in the
Expand Down
7 changes: 5 additions & 2 deletions build-system/compile/single-pass.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,16 @@ exports.getFlags = function(config) {
source_map_include_content: !!argv.full_sourcemaps,
source_map_location_mapping: ['|/'],
//new_type_inf: true,
language_in: 'ES6',
// By default closure puts all of the public exports on the global, but
// because of the wrapper modules (to mitigate async loading of scripts)
// that we add to the js binaries this prevents other js binaries from
// accessing the symbol, we remedy this by attaching all public exports
// to `_` and everything imported across modules is is accessed through `_`.
rename_prefix_namespace: '_',
language_out: config.language_out || 'ES5',
language_in: config.esm ? 'ECMASCRIPT_2017' : 'ECMASCRIPT6',
language_out: config.esm
? 'NO_TRANSPILE'
: config.language_out || 'ECMASCRIPT5',
chunk_output_path_prefix: config.writeTo || 'out/',
module_resolution: 'NODE',
process_common_js_modules: true,
Expand Down Expand Up @@ -551,6 +553,7 @@ exports.singlePassCompile = async function(entryModule, options) {
.getFlags({
modules: [entryModule].concat(extensions),
writeTo: singlePassDest,
esm: options.esm,
define: options.define,
externs: options.externs,
hideWarningsFor: options.hideWarningsFor,
Expand Down
4 changes: 2 additions & 2 deletions build-system/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ exports.transpileTs = function(srcDir, srcFilename) {
const tsEntry = path.join(srcDir, srcFilename).replace(/\.js$/, '.ts');
const tsConfig = ts.convertCompilerOptionsFromJson(
{
'module': 'ES6',
'target': 'ES6',
'module': 'esnext',
'target': 'esnext',
},
srcDir
);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"@ampproject/animations": "0.2.0",
"@ampproject/worker-dom": "0.19.0",
"babel-preset-modules": "0.0.2",
"document-register-element": "1.5.0",
"dompurify": "2.0.2",
"moment": "2.24.0",
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,16 @@ babel-preset-jest@^24.9.0:
"@babel/plugin-syntax-object-rest-spread" "^7.0.0"
babel-plugin-jest-hoist "^24.9.0"

babel-preset-modules@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/babel-preset-modules/-/babel-preset-modules-0.0.2.tgz#3ece9871bb022784a76749114e7b8bb109a6ec1e"
integrity sha512-YWjLfgPRGrQNPEZITnyIgsMITrPjA4ZvLS6hVyTCYgqymR0+l2P1jTd3lMHZ+hBDqAHDSGGKD20oxUmQvvvubg==
dependencies:
"@babel/plugin-proposal-unicode-property-regex" "^7.4.4"
"@babel/plugin-transform-dotall-regex" "^7.4.4"
"@babel/types" "^7.4.4"
esutils "^2.0.2"

babel-runtime@^6.22.0, babel-runtime@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe"
Expand Down

0 comments on commit 587f0b4

Please sign in to comment.