gpt4 book ai didi

javascript - Laravel mix 编译自定义 JS 文件

转载 作者:行者123 更新时间:2023-11-28 04:08:48 24 4
gpt4 key购买 nike

我有一个文件: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>

它说: Uncaught ReferenceError :测试未定义。我怎样才能避免所有模块导出内容并只留下我的代码。

最佳答案

您云使用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();

关于javascript - Laravel mix 编译自定义 JS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46479042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com