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

`.js.no-module.es6` files will be transpiled without producing a module.

import/export statements are deliberately not supported, so one would still need to use 
`const module = require('moduleName').default.`
This commit is contained in:
Kyle Zhao 2018-10-09 09:18:23 +08:00 committed by GitHub
parent e55f220b33
commit 8b2a379e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 6 additions and 2 deletions

View File

@ -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

View File

@ -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