通过Webpack Encore引入RequireJS AMD模块

时间:2018-12-11 05:21:19

标签: webpack requirejs frontend amd webpack-encore

我有很多RequireJS AMD模块,需要将它们包含在我的项目中并通过Webpack引入。

似乎有人设法在这里完成此操作,但是我不确定Webpack encore是否可以实现:Webpack with requirejs/AMD

这是我的webpack.config.js:

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

Encore
    // directory where compiled assets will be stored
    .setOutputPath('web/build/')

    // public path used by the web server to access the output path
    .setPublicPath('/app/build')

    // only needed for CDN's or sub-directory deploy
    .setManifestKeyPrefix('build/')

    /*
     * ENTRY CONFIG
     *
     * Add 1 entry for each "page" of your app
     * (including one that's included on every page - e.g. "app")
     *
     * Each entry will result in one JavaScript file (e.g. app.js)
     * and one CSS file (e.g. app.css) if you JavaScript imports CSS.
     */
    .addEntry('app', './web/assets/js/app.js')

    // will require an extra script tag for runtime.js
    // but, you probably want this, unless you're building a single-page app
    .enableSingleRuntimeChunk()

    .cleanupOutputBeforeBuild()

    .enableSourceMaps(!Encore.isProduction())

    // enables hashed filenames (e.g. app.abc123.css)
    .enableVersioning(Encore.isProduction())

    // uncomment if you use Sass/SCSS files
    .enableSassLoader()

    // uncomment if you're having problems with a jQuery plugin
    .autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

如何提取RequireJS模块?

1 个答案:

答案 0 :(得分:0)

总体而言,我对Webpack的requirejs / AMD没有任何经验。但是,在webpack.config.js文件的底部,您可以根据需要修改Webpack的配置。例如:

const config = Encore.getWebpackConfig();

// make changes
// I don't know if these are correct - just referencing the
// other SO you linked to
config.output.library = '[name]';
config.output.libraryTarget = 'amd';
config.output.umdNamedDefine = true;


module.exports = config;

祝你好运!