简体   繁体   English

Laravel混合以编译自定义JS文件

[英]Laravel mix to compile custom JS file

I have a file: myscript.js and I'm using laravel mix to compile it to public/js/myscript.js and it works, but it compiles it like this: 我有一个文件: myscript.js ,我正在使用laravel mix将其编译为public/js/myscript.js ,它可以工作,但它的编译方式如下:

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId]) {
/******/            return installedModules[moduleId].exports;
/******/        }
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 31);
/******/ })
/************************************************************************/
/******/ ({

/***/ 31:
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(32);


/***/ }),

/***/ 32:
/***/ (function(module, exports) {

var a = 0:

function testing(){
    console.log(a);
}

/***/ })

/******/ });

So then I have this in my view: 所以我认为:

<script src="{{ asset('js/myscript.js') }}></script>
<script>testing();</script>

And it says: Uncaught ReferenceError: testing is not defined. 它说:Uncaught ReferenceError:未定义测试。 How can I avoid all that module exports stuff and only leave my code. 如何避免所有模块导出内容而只留下我的代码。

you cloud use ES6 import and export. 您的云使用ES6导入和导出。

modify webpack.mix.js: 修改webpack.mix.js:

mix
    .js('js/myscript.js', 'public/js')
    .js('js/testing.js', 'public/js')

modify myscript.js 修改myscript.js

var a = 0;
var testing = function() {
    console.log(a);
};
export default testing

add js/testting.js 添加js / testting.js

import testing from './myscript';
testing();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM