You should not create your .js
files in the /priv/static/js
directory. Instead, use the /assets
directory.
Phoenix 1.4 uses Webpack to bundle js
files located in /assets
into a single app.js
file which will be placed into /priv/static/js
. As an entry point to your application, you can use /assets/js/app.js
where the following lines load Vue:
import Vue from 'vue';
new Vue({
// ...
});
(You can also put this code in a seperate .js
file, as long as you import it in app.js
)
This solution, however, won't help if you need static js files for different routes. You maybe want to always render priv/static/js/app.js
, but specific files just on specific layouts. Achieving multiple output files in priv/static/js
can be done by modifying your webpack.config.js
slightly and adding more entrypoints:
entry: {
app: ['./js/app.js'].concat(glob.sync('./vendor/**/*.js')),
special: ['./js/special.js']
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, '../priv/static/js')},
}
With this config, two files will be created:
priv/static/js/app.js
- Containing all vendor files and app.js
priv/static/js/special.js
- Containing just special.js
The special.js
file can be included within Phoenix then:
<script type="text/javascript" src="<%= Routes.static_path(@conn, "/js/special.js") %>"></script>