Webpack Encore - 如何导入任何库(如果可能的话缩小版本)?

时间:2018-06-18 20:57:36

标签: javascript webpack webpack-encore blazy

我在webpack Encore上遇到了一些问题。 我正在尝试将此库添加到我的项目中:https://github.com/dinbror/blazy但是当我对此库中的js文件(缩小或不缩放)执行require时,我收到此错误

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)

我的要求是这样的:require('../vendor/bLazy/blazy');(供应商是我的Bower dep安装的文件夹)

库IS在生成的文件中。

所以我想知道是否有办法要求任何带有webPack Encore的库?

ps:有关信息,它适用于historyjs库(仅限非缩小版)

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

这是我的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();

编辑解决方案:我看到了一条帮助我的评论(感谢你)

import Blazy from '../vendor/bLazy/blazy.min'效果很好。 我认为required()方法相当于import,但似乎没有。

1 个答案:

答案 0 :(得分:0)

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

感谢发表评论的人。

ps:我没有看到如何在没有任何答案的情况下将其标记为已解决,所以我选择了此选项来将其选择为答案