diff --git a/.github/workflows/js.yml b/.github/workflows/js.yml index a3c9cc04..e60bf211 100644 --- a/.github/workflows/js.yml +++ b/.github/workflows/js.yml @@ -9,24 +9,23 @@ jobs: strategy: matrix: os: [ubuntu-latest] - node: [14.x] + node: [14] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - - name: Use Node.js ${{ matrix.node }} - uses: actions/setup-node@v1 + - uses: pnpm/action-setup@v2.2.2 with: - node-version: ${{ matrix.node }} + version: 7.4.1 - - uses: pnpm/action-setup@v1.2.1 + - uses: actions/setup-node@v3 with: - version: 5.13.6 - run_install: | - - cwd: vite-plugin-ruby - - recursive: false + cache: 'pnpm' + node-version: ${{ matrix.node }} + + - run: pnpm -C vite-plugin-ruby install --frozen-lockfile - name: Build run: pnpm -C vite-plugin-ruby build diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml index e17ff5b6..41458b83 100644 --- a/.github/workflows/rubocop.yml +++ b/.github/workflows/rubocop.yml @@ -6,9 +6,6 @@ jobs: build: name: Rubocop runs-on: ${{ matrix.os }} - env: - BUNDLE_JOBS: 4 - BUNDLE_RETRY: 3 strategy: matrix: os: [ubuntu-latest] @@ -17,23 +14,12 @@ jobs: ] steps: - - uses: actions/checkout@v2 - - uses: actions/cache@v2 - with: - path: /home/runner/bundle - key: bundle-use-ruby-gems-${{ hashFiles('**/Gemfile.lock') }} - restore-keys: | - bundle-use-ruby-gems- + - uses: actions/checkout@v3 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} + bundler-cache: true - - name: Bundle install - run: | - gem install bundler -v 2.1.4 - bundle config path /home/runner/bundle - bundle install - - - name: Ruby linter - run: bundle exec rubocop + - name: Rubocop + run: bin/rubocop diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index b9723f01..5ef630dc 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -7,70 +7,38 @@ jobs: name: build runs-on: ${{ matrix.os }} continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' || matrix.experimental }} - env: - BUNDLE_JOBS: 4 - BUNDLE_RETRY: 3 strategy: fail-fast: false matrix: os: [ubuntu-latest] - bundler: ["2.1.4"] ruby: [ - "2.5", "2.7", "3.0", + "3.1", ] gemfile: [ - "gemfiles/Gemfile-rails.5.2.x", "gemfiles/Gemfile-rails.6.0.x", - "gemfiles/Gemfile-rails.6.1.x" + "gemfiles/Gemfile-rails.6.1.x", + "gemfiles/Gemfile-rails.7.0.x" ] experimental: [false] - exclude: - - ruby: "2.5" - gemfile: gemfiles/Gemfile-rails-edge - - ruby: "3.0" - gemfile: gemfiles/Gemfile-rails.5.2.x include: - - ruby: "2.4" - bundler: "1.17.3" - os: ubuntu-latest - gemfile: gemfiles/Gemfile-rails.4.2.x - experimental: true - - ruby: "2.7" - bundler: "2.1.4" - os: ubuntu-latest - gemfile: gemfiles/Gemfile-rails-edge - experimental: true - ruby: "3.1" - bundler: "2.1.4" os: ubuntu-latest gemfile: gemfiles/Gemfile-rails-edge experimental: true steps: - - uses: actions/checkout@v2 - - uses: actions/cache@v2 - with: - path: /home/runner/bundle - key: bundle-use-ruby-${{ matrix.ruby }}-${{ matrix.gemfile }}-gems-${{ hashFiles(matrix.gemfile) }}-${{ hashFiles('**/*.gemspec') }} - restore-keys: | - bundle-use-ruby-${{ matrix.ruby }}-${{ matrix.gemfile }}-gems- + - uses: actions/checkout@v3 - - uses: actions/setup-node@v2 + - uses: actions/setup-node@v3 with: node-version: '14' - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} - - - name: Bundle install - run: | - gem install bundler -v ${{ matrix.bundler }} - bundle _${{ matrix.bundler }}_ config path /home/runner/bundle - bundle _${{ matrix.bundler }}_ config --global gemfile ${{ matrix.gemfile }} - bundle _${{ matrix.bundler }}_ install # --jobs 4 --retry 3 + bundler-cache: true - name: Setup Code Climate test-reporter run: | @@ -78,8 +46,8 @@ jobs: chmod +x ./cc-test-reporter ./cc-test-reporter before-build - - name: Ruby specs - run: bundle _${{ matrix.bundler }}_ exec rake test + - name: Ruby Tests + run: bin/rake test - name: Upload code coverage to Code Climate if: ${{ contains(github.ref, 'main') }} diff --git a/.rubocop.yml b/.rubocop.yml index 5511d671..ae1a8aeb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,6 +4,10 @@ require: - rubocop-performance - rubocop-minitest +inherit_mode: + merge: + - Exclude + AllCops: Exclude: - "node_modules/**/*" diff --git a/docs/src/guide/migration.md b/docs/src/guide/migration.md index 59ded5c2..82ba202a 100644 --- a/docs/src/guide/migration.md +++ b/docs/src/guide/migration.md @@ -25,6 +25,7 @@ [resolve.alias]: https://vitejs.dev/config/#resolve-alias [sprockets]: https://github.com/rails/sprockets-rails [sprockets example]: https://github.com/ElMassimo/vite_ruby/pull/165 +[stimulus-vite-helpers]: https://github.com/ElMassimo/stimulus-vite-helpers # Migrating to Vite @@ -71,6 +72,8 @@ Proceed to fix any errors that occur (i.e. differences between Webpack and Vite. + import TextInput from '@/components/TextInput.vue' ``` + The same is true, when importing .svelte or .scss files from Javascript. + - Replace usages of [tag helpers] as you move the [entrypoints]. ```diff @@ -94,15 +97,52 @@ Proceed to fix any errors that occur (i.e. differences between Webpack and Vite. + const controllers = import.meta.globEager('./**/*_controller.js') ``` -- If importing code that is located outside of the [sourceCodeDir], make sure to add a [glob expression] in [watchAdditionalPaths], so that changes to these files are detected, and trigger a recompilation. + If you want to automatically register the Stimulus Controllers, have a look at [stimulus-vite-helpers] as a replacement for @hotwired/stimulus-webpack-helpers + +- If importing code that is located outside of the [sourceCodeDir], make sure to add a [glob expression] in [watchAdditionalPaths], so that changes to these files are detected, and trigger a recompilation. - If you were using [rails-erb-loader], you might want to check [vite-plugin-erb] to ease the transition, but it's better to avoid mixing ERB in frontend assets. - Make sure [npx] is available (comes by default in most node.js installations), or [clear][clear rake] the [vite:install_dependencies] rake task and provide your own implementation. +- If you are importing your own source code without absolute path prefix (such as ``@/`` or ``~/``) you can either prefix all imports with ``@/``: + + ```diff + + - import MyModule from "admin/components/MyModule.vue" + + import MyModule from "@/admin/components/MyModule.vue" + ``` + + Or you can define an alias for every folder under [sourceCodeDir] (``app/javascript``): + + ```javascript + // vite.config.js + import path from 'path'; + import fs from 'fs' + + const sourceCodeDir = "app/javascript" + const items = fs.readdirSync(sourceCodeDir) + const directories = items.filter(item => fs.lstatSync(path.join(sourceCodeDir, item)).isDirectory()) + const aliasesFromJavascriptRoot = {} + directories.forEach(directory => { + aliasesFromJavascriptRoot[directory] = path.resolve(__dirname, sourceCodeDir, directory) + }) + export default defineConfig({ + resolve: { + alias: { + ...aliasesFromJavascriptRoot, + // can add more aliases, as "old" images or "@assets", see below + images: path.resolve(__dirname, './app/assets/images'), + }, + }, + ``` + + ::: tip Loaders to Plugins Vite provides many features [out of the box], which reduce the -need for configuration. +need for configuration. You may just need to install the required +package, for example sass, typescript, pug +just need to be available, so Vite can pick them up. In complex setups, the app might depend on specific webpack loaders, which can't be used in Vite, which uses [Rollup] under the hood. diff --git a/gemfiles/Gemfile-rails.7.0.x b/gemfiles/Gemfile-rails.7.0.x new file mode 100644 index 00000000..2e2c2fc1 --- /dev/null +++ b/gemfiles/Gemfile-rails.7.0.x @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gem 'rails', '~> 7.0.3' + +gemspec path: '../vite_ruby' +gemspec path: '../vite_rails' +gemspec path: '../vite_plugin_legacy' diff --git a/vite_ruby/CHANGELOG.md b/vite_ruby/CHANGELOG.md index 6f16b914..1e90717a 100644 --- a/vite_ruby/CHANGELOG.md +++ b/vite_ruby/CHANGELOG.md @@ -1,10 +1,10 @@ -# [](https://github.com/ElMassimo/vite_ruby/compare/vite_ruby@3.1.6...vite_ruby@) (2022-07-01) - +## [3.1.7](https://github.com/ElMassimo/vite_ruby/compare/vite_ruby@3.1.6...vite_ruby@3.1.7) (2022-07-13) -### Features -* allow starting an ESM SSR server from a CJS app ([17c8926](https://github.com/ElMassimo/vite_ruby/commit/17c89265f2f5e291ed76784aad647c94e2915d96)) +### Bug Fixes +* npm deprecation warning on assets:precompile ([#226](https://github.com/ElMassimo/vite_ruby/issues/226)) ([e4f4b75](https://github.com/ElMassimo/vite_ruby/commit/e4f4b7540ef34296f1a8a4d8f1d2838549ee8460)), closes [#220](https://github.com/ElMassimo/vite_ruby/issues/220) +# [](https://github.com/ElMassimo/vite_ruby/compare/vite_ruby@3.1.6...vite_ruby@) (2022-07-01) ## [3.1.6](https://github.com/ElMassimo/vite_ruby/compare/vite_ruby@3.1.5...vite_ruby@3.1.6) (2022-06-02) diff --git a/vite_ruby/lib/tasks/vite.rake b/vite_ruby/lib/tasks/vite.rake index 2394ad37..aedc3e9c 100644 --- a/vite_ruby/lib/tasks/vite.rake +++ b/vite_ruby/lib/tasks/vite.rake @@ -42,8 +42,7 @@ namespace :vite do desc 'Ensure build dependencies like Vite are installed before bundling' task :install_dependencies do - legacy_flag = `npm --version`.to_i < 7 rescue false - cmd = legacy_flag ? 'npx ci --yes' : 'npx --yes ci' + cmd = ViteRuby.commands.legacy_npm_version? ? 'npx ci --yes' : 'npx --yes ci' system({ 'NODE_ENV' => 'development' }, cmd) end @@ -82,6 +81,10 @@ end # Any prerequisite task that installs packages should also install build dependencies. if ARGV.include?('assets:precompile') - ENV['NPM_CONFIG_PRODUCTION'] = 'false' + if ViteRuby.commands.legacy_npm_version? + ENV['NPM_CONFIG_PRODUCTION'] = 'false' + else + ENV['NPM_CONFIG_INCLUDE'] = 'dev' + end ENV['YARN_PRODUCTION'] = 'false' end diff --git a/vite_ruby/lib/vite_ruby/commands.rb b/vite_ruby/lib/vite_ruby/commands.rb index 771cfa6d..0674cd52 100644 --- a/vite_ruby/lib/vite_ruby/commands.rb +++ b/vite_ruby/lib/vite_ruby/commands.rb @@ -66,6 +66,11 @@ def install_binstubs `bundle config --delete bin` end + # Internal: Checks if the npm version is 6 or lower. + def legacy_npm_version? + `npm --version`.to_i < 7 rescue false + end + # Internal: Verifies if ViteRuby is properly installed. def verify_install unless File.exist?(config.root.join('bin/vite')) diff --git a/vite_ruby/lib/vite_ruby/manifest.rb b/vite_ruby/lib/vite_ruby/manifest.rb index 5c5877af..daad7c21 100644 --- a/vite_ruby/lib/vite_ruby/manifest.rb +++ b/vite_ruby/lib/vite_ruby/manifest.rb @@ -7,7 +7,7 @@ # lookup_entrypoint('calendar', type: :javascript) # => { "file" => "/vite/assets/calendar-1016838bab065ae1e314.js", "imports" => [] } # -# NOTE: Using "autoBuild": true` in `config/vite.json` file will trigger a build +# NOTE: Using `"autoBuild": true` in `config/vite.json` file will trigger a build # on demand as needed, before performing any lookup. class ViteRuby::Manifest def initialize(vite_ruby)