FAQ and Common Issues

How Do I Deploy My Encore Assets?

There are two important things to remember when deploying your assets.

アセットをデプロイする際に覚えておくべき重要なことが 2 つあります。

1) Compile Assets for Production

1) プロダクション用のアセットをコンパイルする

Optimize your assets for production by running:

以下を実行して、本番用にアセットを最適化します。
1
$ ./node_modules/.bin/encore production

That will minify your assets and make other performance optimizations. Yay!

これにより、アセットが縮小され、他のパフォーマンスが最適化されます。わーい!

But, what server should you run this command on? That depends on how you deploy. For example, you could execute this locally (or on a build server), and use rsync or something else to transfer the generated files to your production server. Or, you could put your files on your production server first (e.g. via git pull) and then run this command on production (ideally, before traffic hits your code). In this case, you'll need to install Node.js on your production server.

しかし、どのサーバーでこのコマンドを実行する必要がありますか?これは、デプロイ方法によって異なります。たとえば、これをローカルで (またはビルド サーバーで) 実行し、usersync などを使用して、生成されたファイルを運用サーバーに転送できます。または、最初に本番サーバーにファイルを配置し (git pull など)、本番環境でこのコマンドを実行することもできます (理想的には、コードをトラフィックする前に)。この場合、本番サーバーに Node.js をインストールする必要があります。

2) Only Deploy the Built Assets

2) ビルドされたアセットのみを展開する

The only files that need to be deployed to your production servers are the final, built assets (e.g. the public/build directory). You do not need to install Node.js, deploy webpack.config.js, the node_modules directory or even your source asset files, unless you plan on running encore production on your production machine. Once your assets are built, these are the only thing that need to live on the production server.

運用サーバーにデプロイする必要があるファイルは、最終的なビルド済みアセット (public/build ディレクトリなど) だけです。プロダクション マシンで encore プロダクションを実行する予定がない限り、Node.js のインストール、webpack.config.js、node_modules ディレクトリ、さらには sourceasset ファイルのデプロイを行う必要はありません。アセットが構築されると、これらは運用サーバー上でライブする必要がある唯一のものです。

Do I Need to Install Node.js on My Production Server?

No, unless you plan to build your production assets on your production server, which is not recommended. See How Do I Deploy my Encore Assets?.

いいえ、推奨されません。 Encore アセットをデプロイするにはどうすればよいですか? を参照してください。

What Files Should I Commit to git? And which Should I Ignore?

You should commit all of your files to git, except for the node_modules/ directory and the built files. Your .gitignore file should include:

node_modules/ ディレクトリとビルドされたファイルを除いて、すべてのファイルを git にコミットする必要があります。 .gitignore ファイルには以下が含まれている必要があります。
1
2
3
/node_modules/
# whatever path you're passing to Encore.setOutputPath()
/public/build

You should commit all of your source asset files, package.json and yarn.lock or package-lock.json.

すべてのソース アセット ファイル、package.json および yarn.lock または package-lock.json をコミットする必要があります。

My App Lives under a Subdirectory

If your app does not live at the root of your web server (i.e. it lives under a subdirectory, like /myAppSubdir), you will need to configure that when calling Encore.setPublicPath():

アプリが Web サーバーのルートに存在しない場合 (つまり、/myAppSubdir のようなサブディレクトリの下に存在する場合)、Encore.setPublicPath() を呼び出すときにそのように構成する必要があります。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// webpack.config.js
  Encore
      // ...

      .setOutputPath('public/build/')

-     .setPublicPath('/build')
+     // this is your *true* public path
+     .setPublicPath('/myAppSubdir/build')

+     // this is now needed so that your manifest.json keys are still `build/foo.js`
+     // (which is a file that's used by Symfony's `asset()` function)
+     .setManifestKeyPrefix('build')
  ;

If you're using the encore_entry_script_tags() and encore_entry_link_tags() Twig shortcuts (or are processing your assets through entrypoints.json in some other way) you're done! These shortcut methods read from an entrypoints.json file that will now contain the subdirectory.

encore_entry_script_tags() および encore_entry_link_tags() Twig ショートカットを使用している場合 (または、別の方法で entrypoints.json を介してアセットを処理している場合) は完了です!これらのショートカット メソッドは、サブディレクトリを含む entrypoints.json ファイルから読み取ります。

"jQuery is not defined" or "$ is not defined"

This error happens when your code (or some library that you are using) expects $ or jQuery to be a global variable. But, when you use Webpack and require('jquery'), no global variables are set.

このエラーは、コード (または使用しているライブラリ) が $or jQuery がグローバル変数であると想定している場合に発生します。ただし、Webpack と require('jquery') を使用する場合、グローバル変数は設定されません。

The fix depends on if the error is happening in your code or inside some third-party code that you're using. See jQuery Plugins and Legacy Applications for the fix.

修正は、エラーがコード内で発生しているか、使用しているサードパーティ コード内で発生しているかによって異なります。修正については、jQuery プラグインとレガシー アプリケーションを参照してください。

Uncaught ReferenceError: webpackJsonp is not defined

If you get this error, it's probably because you've forgotten to add a script tag for the runtime.js file that contains Webpack's runtime. If you're using the encore_entry_script_tags() Twig function, this should never happen: the file script tag is rendered automatically.

このエラーが発生した場合は、Webpack のランタイムを含む runtime.js ファイルの scripttag を追加するのを忘れている可能性があります。 encore_entry_script_tags() Twig 関数を使用している場合、これは決して起こらないはずです: ファイル スクリプト タグは自動的にレンダリングされます。

This dependency was not found: some-module in ./path/to/file.js

Usually, after you install a package via yarn or npm, you can require / import it to use it. For example, after running yarn add respond.js or npm install respond.js, you try to require that module:

通常、yarn または npm を介してパッケージをインストールした後、require / importit を使用して使用できます。たとえば、yarn add Respond.js または npm install Respond.js を実行した後、そのモジュールを要求しようとします。
1
require('respond.js');

But, instead of working, you see an error:

ただし、機能する代わりに、次のエラーが表示されます。

This dependency was not found:

この依存関係は見つかりませんでした:
  • respond.js in ./assets/app.js
    ./assets/app.js 内の Respond.js

Typically, a package will "advertise" its "main" file by adding a main key to its package.json. But sometimes, old libraries won't have this. Instead, you'll need to specifically require the file you need. In this case, the file you should use is located at node_modules/respond.js/dest/respond.src.js. You can require this via:

通常、パッケージは、メイン キーをその package.json に追加することによって、「メイン」ファイルを「アドバタイズ」します。ただし、古いライブラリにはこれがない場合があります。代わりに、必要なファイルを明確に要求する必要があります。この場合、使用する必要があるファイルは node_modules/respond.js/dest/respond.src.js にあります。次の方法でrequirethisを行うことができます:
1
2
// require a non-minified file whenever possible
require('respond.js/dest/respond.src.js');

I need to execute Babel on a third-party Module

For performance, Encore does not process libraries inside node_modules/ through Babel. But, you can change that via the configureBabel() method. See Configuring Babel for details.

パフォーマンスのために、Encore は node_modules/throughBabel 内のライブラリを処理しません。ただし、configureBabel() メソッドを使用して変更できます。詳細については、Babel の設定を参照してください。

How Do I Integrate my Encore Configuration with my IDE?

Webpack integration in PhpStorm and other IDEs makes your development more productive (for example by resolving aliases). However, you may face this error:

PhpStorm およびその他の IDE での Webpack の統合により、開発の生産性が向上します (たとえば、エイリアスの解決による)。ただし、次のエラーに直面する場合があります。
1
2
3
4
Encore.setOutputPath() cannot be called yet because the runtime environment
doesn't appear to be configured. Make sure you're using the encore executable
or call Encore.configureRuntimeEnvironment() first if you're purposely not
calling Encore directly.

It fails because the Encore Runtime Environment is only configured when you are running it (e.g. when executing yarn encore dev). Fix this issue calling to Encore.isRuntimeEnvironmentConfigured() and Encore.configureRuntimeEnvironment() methods:

Encore ランタイム環境は実行中 (yarn encore dev の実行時など) にのみ設定されるため、失敗します。 toEncore.isRuntimeEnvironmentConfigured() および Encore.configureRuntimeEnvironment() メソッドを呼び出して、この問題を修正します。
1
2
3
4
5
6
7
8
// webpack.config.js
const Encore = require('@symfony/webpack-encore')

if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}

// ... the rest of the Encore configuration

My Tests are Failing Because of entrypoints.json File

After installing Encore, you might see the following error when running tests locally or on your Continuous Integration server:

Encore をインストールした後、ローカルまたは継続的インテグレーション サーバーでテストを実行すると、次のエラーが表示される場合があります。
1
2
3
4
Uncaught PHP Exception Twig\Error\RuntimeError:
"An exception has been thrown during the rendering of a template
("Could not find the entrypoints file from Webpack:
the file "/var/www/html/public/build/entrypoints.json" does not exist.

This is happening because you did not build your Encore assets, hence no entrypoints.json file. To solve this error, either build Encore assets or set the strict_mode option to false (this prevents Encore's Twig functions to trigger exceptions when there's no entrypoints.json file):

これは、Encore アセットをビルドしていないために発生しているため、noentrypoints.json ファイルはありません。このエラーを解決するには、Encore アセットをビルドするか、strict_mode オプションを false に設定します (これにより、entrypoints.json ファイルがない場合に Encore の Twigfunctions が例外をトリガーするのを防ぎます)。
1
2
3
4
# config/packages/test/webpack_encore.yaml
webpack_encore:
    strict_mode: false
    # ...