Class: Condenser::BabelProcessor
- Inherits:
-
NodeProcessor
- Object
- NodeProcessor
- Condenser::BabelProcessor
- Defined in:
- lib/condenser/processors/babel_processor.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Attributes inherited from NodeProcessor
Instance Method Summary collapse
- #call(environment, input) ⇒ Object
-
#initialize(dir = nil, **options) ⇒ BabelProcessor
constructor
A new instance of BabelProcessor.
-
#split_subpath(path, subpath) ⇒ Object
Internal: Get relative path for root path and subpath.
Methods inherited from NodeProcessor
#binary, call, #exec_runtime, #exec_runtime_error, #exec_syntax_error, #name, #npm_install, #npm_module_path, setup
Constructor Details
#initialize(dir = nil, **options) ⇒ BabelProcessor
Returns a new instance of BabelProcessor.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/condenser/processors/babel_processor.rb', line 7 def initialize(dir = nil, **) super(dir) [:plugins] ||= [ ["@babel/plugin-transform-runtime", { corejs: 3, useESModules: true }] ] [:presets] ||= [ ['@babel/preset-env', { modules: false, targets: { browsers: '> 1% and not dead' } }] ] packages = .slice(:plugins, :presets).values.reduce(&:+).map { |p| p.is_a?(Array) ? p[0] : p} packages.unshift('@babel/core') if packages.include?('@babel/plugin-transform-runtime') runtime = [:plugins].find { |i| i.is_a?(Array) ? i[0] == '@babel/plugin-transform-runtime' : i == '@babel/plugin-transform-runtime' } packages << if runtime.is_a?(Array) && runtime[1][:corejs] if runtime[1][:corejs].is_a?(Hash) "@babel/runtime-corejs#{runtime[1][:corejs][:version]}" else "@babel/runtime-corejs#{runtime[1][:corejs]}" end else '@babel/runtime' end end npm_install(*packages) [:plugins].map! do |plugin| if plugin.is_a?(Array) plugin[0] = npm_module_path(plugin[0]) plugin else npm_module_path(plugin) end end [:presets].map! do |plugin| if plugin.is_a?(Array) plugin[0] = npm_module_path(plugin[0]) plugin else npm_module_path(plugin) end end @options = { ast: false, compact: false, sourceMap: false }.merge() end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/condenser/processors/babel_processor.rb', line 5 def @options end |
Instance Method Details
#call(environment, input) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/condenser/processors/babel_processor.rb', line 62 def call(environment, input) opts = { # 'moduleRoot' => nil, 'filename' => input[:filename], 'moduleId' => input[:filename].sub(/(\..+)+/, ''), 'cwd' => '/assets/', 'filenameRelative' => input[:filename], 'sourceFileName' => input[:filename] # 'sourceMapTarget' => input[:filename] # 'inputSourceMap' }.merge(@options).select { |k,v| !v.nil? } if match = input[:source].match(/\A(\/\/[^\n]*(\n|\z))*/) directives = match.to_s.split(/\n/).map { |l| l.delete_prefix("//").strip } directives.each do |directive| if directive.start_with?('depends_on') input[:process_dependencies] << directive.sub(/\Adepends_on\s+/, '') end end end result = exec_runtime(<<-JS) const babel = require("#{File.join(npm_module_path('@babel/core'))}"); const source = #{JSON.generate(input[:source])}; const options = #{JSON.generate(opts).gsub(/"@?babel[\/-][^"]+"/) { |m| "require(#{m})"}}; let imports = []; let defaultExport = false; let hasExports = false; options['plugins'].push(function({ types: t }) { return { visitor: { ImportDeclaration(path, state) { imports.push(path.node.source.value); }, ExportDeclaration(path, state) { hasExports = true; if (path.node.source) { imports.push(path.node.source.value); } }, ExportDefaultDeclaration(path, state) { defaultExport = true; } } }; }); try { const result = babel.transform(source, options); result.imports = imports; result.exports = hasExports; result.defaultExport = defaultExport; console.log(JSON.stringify(result)); } catch(e) { console.log(JSON.stringify({'error': [e.name, e.message, e.stack]})); process.exit(0); } JS if result['error'] if result['error'][0] == 'SyntaxError' raise exec_syntax_error(result['error'][1], "/assets/#{input[:filename]}") else raise exec_runtime_error(result['error'][0] + ': ' + result['error'][1]) end else input[:source] = result['code'] input[:map] = result['map'] input[:export_dependencies] = result['imports'].map do |i| i.end_with?('.js') ? i : "#{i}.js" end input[:default_export] = result['defaultExport'] input[:exports] = result['exports'] end end |
#split_subpath(path, subpath) ⇒ Object
Internal: Get relative path for root path and subpath.
path - String path subpath - String subpath of path
Returns relative String path if subpath is a subpath of path, or nil if subpath is outside of path.
150 151 152 153 154 155 156 157 158 |
# File 'lib/condenser/processors/babel_processor.rb', line 150 def split_subpath(path, subpath) return "" if path == subpath path = File.join(path, ''.freeze) if subpath.start_with?(path) subpath[path.length..-1] else nil end end |