Skip to content

Instantly share code, notes, and snippets.

@ksuzushima
Created October 31, 2017 03:05
Show Gist options
  • Save ksuzushima/34277896740d1b68071f8fea6082626e to your computer and use it in GitHub Desktop.
Save ksuzushima/34277896740d1b68071f8fea6082626e to your computer and use it in GitHub Desktop.
WordPress + Webpack + Select2(jQuery dependencies)
  1. WordPressがデフォルトで読み込んでるjQueryは活用(プラグインなど使うとどうしてもjQuery依存してるものが出てくる)
  2. それでもバンドルツール入れないとシンドイ
  3. jQueryに依存してるけど、便利なSelect2をadmin画面に導入したい
// webpack.config.js

externals: {
  jquery: 'jQuery'
}
// entry.js
import jQuery from 'jquery'; //or import $ from 'jquery';
import 'select2';
import '../node_modules/select2/dist/css/select2.css';

jQuery(document).ready($ => {
  const $select = $('select');
  $select.select2();
});

externals を使って外部依存を解決。 参考: externals

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment