Class: Condenser::BabelProcessor
- Inherits:
-
NodeProcessor
- Object
- NodeProcessor
- Condenser::BabelProcessor
- Defined in:
- lib/condenser/processors/babel_processor.rb
Defined Under Namespace
Classes: Error
Class Method Summary collapse
-
.call(environment, input) ⇒ Object
npm install @babel/core @babel/runtime-corejs3 @babel/plugin-transform-runtime @babel/preset-env rollup rollup-plugin-commonjs rollup-plugin-node-resolve @babel/plugin-proposal-class-properties babel-plugin-transform-class-extended-hook.
Instance Method Summary collapse
- #call(environment, input) ⇒ Object
-
#initialize(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, #exec_runtime, #exec_runtime_error, setup
Constructor Details
#initialize(options = {}) ⇒ BabelProcessor
Returns a new instance of BabelProcessor.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/condenser/processors/babel_processor.rb', line 15 def initialize( = {}) @options = .merge({ ast: false, compact: false, plugins: [ ["#{File.('../node_modules', __FILE__)}/babel-plugin-transform-class-extended-hook", {}], ["#{File.('../node_modules', __FILE__)}/@babel/plugin-proposal-class-properties", {}], ["#{File.('../node_modules', __FILE__)}/@babel/plugin-transform-runtime", { corejs: 3 }], ], presets: [["#{File.('../node_modules', __FILE__)}/@babel/preset-env", { "modules": false, "targets": { "browsers": "> 1% and not dead" } } ]], sourceMap: true }).freeze end |
Class Method Details
.call(environment, input) ⇒ Object
npm install @babel/core @babel/runtime-corejs3 @babel/plugin-transform-runtime @babel/preset-env rollup rollup-plugin-commonjs rollup-plugin-node-resolve @babel/plugin-proposal-class-properties babel-plugin-transform-class-extended-hook
11 12 13 |
# File 'lib/condenser/processors/babel_processor.rb', line 11 def self.call(environment, input) new.call(environment, input) end |
Instance Method Details
#call(environment, input) ⇒ Object
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 61 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 |
# File 'lib/condenser/processors/babel_processor.rb', line 32 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) result = exec_runtime(<<-JS) const babel = require("#{File.('../node_modules', __FILE__)}/@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); }, ExportDefaultDeclaration(path, state) { hasExports = true; defaultExport = true; }, ExportDefaultSpecifier(path, state) { hasExports = true; defaultExport = true; }, ExportAllDeclaration(path, state) { hasExports = true; }, ExportNamedDeclaration(path, state) { hasExports = true; }, ExportSpecifier(path, state) { hasExports = 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'] raise exec_runtime_error(result['error'][0] + ': ' + result['error'][1]) else input[:source] = result['code'] input[:map] = result['map'] input[: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.
112 113 114 115 116 117 118 119 120 |
# File 'lib/condenser/processors/babel_processor.rb', line 112 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 |