简体   繁体   English

Webpack Encore-如何导入任何库(如果可能,则为最小版本)?

[英]Webpack Encore - How to import any library (minified version if possible)?

I have some problems with webpack Encore. 我在使用webpack Encore时遇到一些问题。 I'm crrently trying to add this library to my project : https://github.com/dinbror/blazy but when i do a require on the js files(minified or not) from this library, I got this error 我正在努力尝试将此库添加到我的项目中: https : //github.com/dinbror/blazy但是当我对这个库中的js文件(无论是否缩小)进行要求时,我得到了这个错误

jquery.js:3827 Uncaught ReferenceError: Blazy is not defined
    at HTMLDocument.<anonymous> (reader_init.js:215)
    at mightThrow (jquery.js:3534)
    at process (jquery.js:3602)

I do my require like this : require('../vendor/bLazy/blazy'); 我这样执行我的要求: require('../vendor/bLazy/blazy'); (vendor is the folder where my Bower dep are installed) (供应商是我的Bower dep的安装文件夹)

The library IS in the generated file. 库IS在生成的文件中。

So I would like to know if there is a way to require any library with webPack Encore ? 所以我想知道是否有一种方法可以使用webPack Encore来要求任何库?

ps : For info, it worked great with a historyjs library (non minified version only, though) ps:有关信息,它与historyjs库(尽管非缩小版)配合使用非常好

require('../vendor/history.js/scripts/bundled-uncompressed/html5/jquery.history');

Here is my webpack.config.js if it can help 这是我的webpack.config.js,如果可以帮助的话

// webpack.config.js
var Encore = require('@symfony/webpack-encore');

Encore
// the project directory where all compiled assets will be stored
    .setOutputPath('public/build/')

    // the public path used by the web server to access the previous directory
    .setPublicPath((!Encore.isProduction())?'/rapp/public/build':'/build')

    // this is now needed so that your manifest.json keys are still `build/foo.js`
    // i.e. you won't need to change anything in your Symfony app
    .setManifestKeyPrefix('build')

    // will create public/build/main.js and public/build/main.css
    .addEntry('main', './assets/js/main.js')
    //Add entry if other js/css needed. first parameter is the generated filename.
    //require scss file in js. (if you addEntry for scss file only, it will create a js file with same name)
    .addEntry('reader', './assets/js/reader.js')

    //file upload with dropzone
    .addEntry('dropzone', './assets/js/dropzone.js')

    //Admin chapter js
    .addEntry('admin-chapter', './assets/js/chapter.js')

    //addStyleEntry : for css file only

    // allow sass/scss files to be processed
    .enableSassLoader()

    // allow legacy applications to use $/jQuery as a global variable
    .autoProvidejQuery()

    .enableSourceMaps(!Encore.isProduction())

    // empty the outputPath dir before each build
    .cleanupOutputBeforeBuild()

// create hashed filenames (e.g. app.abc123.css)
    .enableVersioning()

    .createSharedEntry('vendor', [
        'jquery',
    ])

    .autoProvideVariables({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery',
    })

    .configureFilenames({
        images: '[path][name].[hash:8].[ext]'
    })
;

// export the final configuration
module.exports = Encore.getWebpackConfig();

EDIT SOLUTION : I saw a comment that helped me (thanks to you) 编辑解决方案:我看到一条对我有所帮助的评论(感谢您)

import Blazy from '../vendor/bLazy/blazy.min' works great. import Blazy from '../vendor/bLazy/blazy.min'效果很好。 I thought the required() method was an equivalent of import but seems not. 我以为required()方法等效于import但似乎不是。

import Blazy from '../vendor/bLazy/blazy.min'

thanks to the one that commented. 感谢发表评论的人。

ps : I didn't see how to mark as solved without any answer so I did this one to select it as answer ps:我没有看到如何在没有任何答案的情况下将其标记为已解决,所以我选择了此选项来将其选择为答案

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

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