Skip to content

A platform for community discussion. Free, open, simple.

DEV: transpile certain ES6 files without producing a module (#6460)

Kyle Zhao8b2a379e4e48
diff --git a/app/assets/javascripts/google-tag-manager.js b/app/assets/javascripts/google-tag-manager.js.no-module.es6
similarity index 100%
rename from app/assets/javascripts/google-tag-manager.js
rename to app/assets/javascripts/google-tag-manager.js.no-module.es6
diff --git a/app/assets/javascripts/google-universal-analytics.js b/app/assets/javascripts/google-universal-analytics.js.no-module.es6
similarity index 100%
rename from app/assets/javascripts/google-universal-analytics.js
rename to app/assets/javascripts/google-universal-analytics.js.no-module.es6
diff --git a/app/assets/javascripts/preload-application-data.js b/app/assets/javascripts/preload-application-data.js.no-module.es6
similarity index 100%
rename from app/assets/javascripts/preload-application-data.js
rename to app/assets/javascripts/preload-application-data.js.no-module.es6
diff --git a/lib/es6_module_transpiler/sprockets.rb b/lib/es6_module_transpiler/sprockets.rb
index 9da657f03c2..aefccbcb9af 100644
--- a/lib/es6_module_transpiler/sprockets.rb
+++ b/lib/es6_module_transpiler/sprockets.rb
@@ -1,4 +1,4 @@
require 'sprockets'
-Sprockets.register_mime_type 'application/ecmascript6', extensions: ['.es6', '.js.es6'], charset: :unicode
+Sprockets.register_mime_type 'application/ecmascript6', extensions: ['.es6', '.js.es6', '.js.no-module.es6'], charset: :unicode
Sprockets.register_transformer 'application/ecmascript6', 'application/javascript', Tilt::ES6ModuleTranspilerTemplate
diff --git a/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb b/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb
index 704a95f1f92..10802feaac9 100644
--- a/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb
+++ b/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb
@@ -178,7 +178,7 @@ JS
js_source = ::JSON.generate(source, quirks_mode: true)
- if opts[:module_name]
+ if opts[:module_name] && transpile_into_module?
filename = opts[:filename] || 'unknown'
"Babel.transform(#{js_source}, { moduleId: '#{opts[:module_name]}', filename: '#{filename}', ast: false, presets: ['es2015'], plugins: [['transform-es2015-modules-amd', {noInterop: true}], 'transform-decorators-legacy', exports.WidgetHbsCompiler] }).code"
else
@@ -188,6 +188,10 @@ JS
private
+ def transpile_into_module?
+ file.nil? || file.exclude?('.no-module')
+ end
+
def module_name(root_path, logical_path)
path = nil