R
R
rst6302020-04-30 08:43:33
JavaScript
rst630, 2020-04-30 08:43:33

How to register your JS in laravel mix/webpack?

I'm not sure about the correct approach, but I want to import my js code into the main app.js when building via npm run dev

I do this - in app.js I add it to plugins/filters.js
require('./plugins/filters');

function test() {
    console.log('aaa');
}


I collect and the output is:
/***/ "./resources/js/plugins/filters.js":
/*!*****************************************!*\
  !*** ./resources/js/plugins/filters.js ***!
  \*****************************************/
/*! no static exports found */
/***/ (function(module, exports) {

function test() {
  console.log('aaa');
}

^^^ this is part of the generated app.js after npm run dev

How do I get to my test() function now? Will it see $ from jQuery?

Putting it all in app.js is a better approach than stuffing it across pages in blade templates, isn't it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2020-05-01
@alexey-m-ukolov

How can I access my test() function now?

window.test = function () {
    console.log('aaa');
}

Now they don’t do this in development, but if you really want to, then please.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question