简体   繁体   English

Webpack concat输出js文件以将其包装在库中?

[英]Webpack concat output js file to wrap it in a library?

I need to concat a webpack output with other files, like pre.js + bundle.js(the webpack output file) + after.js . 我需要将webpack输出与其他文件连接起来,例如pre.js + bundle.js(the webpack output file) + after.js I've tried the webpack concat plugin and also combining webpack with gulp, but I just can't figure out how to get the output file and pipeline it to concat task. 我已经尝试过webpack concat插件,并且也将webpack与gulp结合使用,但是我只是不知道如何获取输出文件并将其通过管道传递给concat任务。 The purpose of this is to wrap the output in a custom library. 这样做的目的是将输出包装在自定义库中。

webpack.config.js : webpack.config.js

module.exports = {
    context: path.resolve(__dirname, 'src'),
    entry: './app.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [{
            test: /\.js$/,
            include: path.resolve(__dirname, 'src'),
            use: [{
                loader: 'babel-loader',
                options: {
                    presets: [
                        ['es2015', {modules: false}]
                    ]
                }
            }]
        }
    ]},
     plugins: [
        new ConcatPlugin({
            useHash: true,
            sourceMap: true,
            name: 'flxeible',
            fileName: '[name].bundle.js',
            filesToConcat: ['pre.js','dist/bundle.js','after.js']
        })
    ]
};

This concatenates the bundle.js that's been already been built, not the one that's being generated and updated by the webpack dev server. 这将连接已经构建的bundle.js ,而不是由webpack开发服务器生成和更新的bundle.js。

using gulp : 使用gulp

var gulp = require('gulp');
var gutil = require('gulp-util')
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server')
var webpackConfig = require('./webpack.config.js')

gulp.task('default', [ "webpack-dev-server"])

var myDevConfig = Object.create(webpackConfig);

gulp.task("webpack-dev-server", function(callback){

    new WebpackDevServer(webpack(myDevConfig), {
        stats: {
            colors: true
        }
    }).listen(8080, "localhost", function(err){
         if(err) throw new gutil.PluginError("webpack-dev-server", err);
         gutil.log("[webpack-dev-server]", 
         "http://localhost:8080/webpack-dev-server/index.html");
     })
})

in the second case, not sure how to get the webpack output in gulp. 在第二种情况下,不确定如何在gulp中获取webpack输出。

You're using gulp solely to run the webpack-dev-server ? 您正在使用gulp只运行webpack-dev-server This is not necessary: https://webpack.js.org/guides/development/#using-webpack-dev-server 这不是必需的: https : //webpack.js.org/guides/development/#using-webpack-dev-server

Without knowing the content+purpose of pre.js and after.js , it's hard to give good advice. 不了解pre.jsafter.js的内容和目的,很难给出好的建议。 You might want to adapt the BannerPlugin to also output a footer= after.js , or configure webpack to generate multiple targets (first dist/bundle.js , and pre.js + dist/bundle.js + after.js afterwards), or check the various ways for shimming , or combine the tree files to a new entry file via three subsequent imports ( import 'pre.js'; import 'bundle.js'; import 'after.js' ). 您可能想要使BannerPlugin也适应以输出footer = after.js ,或将webpack配置为生成多个目标 (第一个dist/bundle.jspre.js + dist/bundle.js +之后的after.js ),或者检查import 'pre.js'; import 'bundle.js'; import 'after.js'各种方法 ,或通过三个后续导入将树文件合并到新的条目文件( import 'pre.js'; import 'bundle.js'; import 'after.js' )。 Good luck. 祝好运。

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

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