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



我有一个文件: 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);
}
/***/ })
/******/ });

所以我在我的视图中有一个:

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

说:未定的参考:未定义测试。我如何避免所有这些模块导出东西,而只留下我的代码。

您的云使用ES6导入和导出。

修改webpack.mix.js:

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

修改myScript.js

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

添加JS/testing.js

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

最新更新