Welcome to mirror list, hosted at ThFree Co, Russian Federation.

js.rb « updater « tasks - github.com/twbs/bootstrap-rubygem.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 973e233e5299d9e756c66d75935a7e394c6524d1 (plain)
1
2
3
4
5
6
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'tsort'

class Updater
  module Js
    INLINED_SRCS = %w[util/index.js util/sanitizer.js util/scrollbar.js].freeze

    def update_javascript_assets
      log_status 'Updating javascripts...'
      save_to  = @save_to[:js]
      read_files('js/dist', bootstrap_js_files).each do |name, content|
        save_file("#{save_to}/#{name}", remove_source_mapping_url(content))
      end
      log_processed "#{bootstrap_js_files * ' '}"

      log_status 'Updating javascript manifest'
      manifest = "//= require ./bootstrap-global-this-define\n"
      bootstrap_js_files.each do |name|
        name = name.gsub(/\.js$/, '')
        manifest << "//= require ./bootstrap/#{name}\n"
      end
      manifest << "//= require ./bootstrap-global-this-undefine\n"
      dist_js = read_files('dist/js', %w(bootstrap.js bootstrap.min.js))
      {
          'assets/javascripts/bootstrap-global-this-define.js' => <<~JS,
            // Set a `globalThis` so that bootstrap components are defined on window.bootstrap instead of window.
            window['bootstrap'] = {
              Popper: window.Popper,
              _originalGlobalThis: window['globalThis']
            };
            window['globalThis'] = window['bootstrap'];
          JS
          'assets/javascripts/bootstrap-global-this-undefine.js' => <<~JS,
            window['globalThis'] = window['bootstrap']._originalGlobalThis;
            window['bootstrap']._originalGlobalThis = null;
          JS
          'assets/javascripts/bootstrap-sprockets.js' => manifest,
          'assets/javascripts/bootstrap.js'           => dist_js['bootstrap.js'],
          'assets/javascripts/bootstrap.min.js'       => dist_js['bootstrap.min.js'],
      }.each do |path, content|
        save_file path, remove_source_mapping_url(content)
        log_processed path
      end
    end

    def bootstrap_js_files
      @bootstrap_js_files ||= begin
        src_files = get_paths_by_type('js/src', /\.js$/) - INLINED_SRCS
        puts "src_files: #{src_files.inspect}"
        imports = Deps.new
        # Get the imports from the ES6 files to order requires correctly.
        read_files('js/src', src_files).each do |name, content|
          file_imports = content.scan(%r{import *(?:[a-zA-Z]*|\{[a-zA-Z ,]*\}) *from '\./([\w/-]+)}).flatten(1).map { |f| "#{f}.js" }.uniq
          imports.add name, *(file_imports - INLINED_SRCS)
        end
        imports.tsort
      end
    end

    def remove_source_mapping_url(content)
      content.sub(%r{^//# sourceMappingURL=.*\n?\z}, '')
    end

    class Deps
      include TSort

      def initialize
        @imports = {}
      end

      def add(from, *tos)
        (@imports[from] ||= []).push(*tos.sort)
      end

      def tsort_each_child(node, &block)
        @imports[node].each(&block)
      end

      def tsort_each_node(&block)
        @imports.each_key(&block)
      end
    end
  end
end