Webpack将文件输出到不同的目录

时间:2015-07-30 08:35:33

标签: webpack

在构建项目之后,我有index.html,bundle.js和一些.css文件,我希望它们被放置在这样的目录结构中:

dist/
    - css/all.my.css
    - js/bundle.js
    - index.html

以下是我为.js和.css所做的事情:

entry: {
    app: path.resolve(__dirname, 'app/src/index.js'),
},
output: {
    path: path.resolve(__dirname, 'dist', 'css'),
    filename: '../js/[name].js'
},

以下是相应的模块配置:

module: {
    loaders: [{
        test: /\.jsx?$/,
        loaders: ['react-hot', 'babel-loader'],
        exclude: /node_modules/
    },
    {
      test: /\.css$/,
      loader: ExtractTextPlugin.extract("style-loader", "css-loader")
    },
    {
      test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
      loader: "url-loader?limit=10000&mimetype=application/font-woff"
    },
    {
      test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
      loader: "file-loader"
    }]
},

我不知道如何从文件夹中复制index.html并将其置于dist /下。我知道需要file-loader,但我应该在entryoutput下撰写什么?

谢谢!

我明白了,这是解决方案:

output: {
        path: path.resolve(__dirname, '../dist'),   // this is the output directory, everything will be placed under here
        filename: 'js/bundle.js', // move the bundle.js file under the js/ folder, the dir structure will be like this /dist/js/bundle.js
}

要移动dist/css/下的css文件:

module: {
    loaders: [{
        test: /\.css$/,
            loader: ExtractTextPlugin.extract('style-loader', 'css-loader!cssnext-loader')
     }]
}

我使用了ExtractTextPlugin,所以要配置css文件的输出路径,我必须在plugins部分定义它:

plugins: [
    new ExtractTextPlugin('./css/bundle.css'), // bundle.css will be put under /dist/css/
]

将图像和字体移动到自己的文件夹中:

module: {
    loaders: [{
        test: /\.(png|jpg|svg)$/,
        loader: 'url-loader?limit=8192&name=img/[name].[ext]'
    }, {
        test: /\.(woff|woff2|eot|ttf)$/,
        loader: 'url-loader?limit=8192&name=fonts/[name].[ext]'
    }]
}

注意loader字符串的定义方式:&name=img/[name].[ext]表示使用原始文件的名称和扩展名,并将其放在img /文件夹下。与字体文件相同。

这是完整的配置文件:

var webpack = require('webpack'),
    path = require('path'),
    ExtractTextPlugin = require('extract-text-webpack-plugin'),
    Clean = require('clean-webpack-plugin')

module.exports = {
    devtool: 'cheap-module-source-map',
    entry: {
        app: path.resolve(__dirname, '../app/index.js'),
    },
    output: {
        path: path.resolve(__dirname, '../dist'),
        filename: 'js/bundle.js',
        publicPath: '/static/'
    },
    module: {
        loaders: [{
            test: /\.js$/,
            loaders: ['babel?presets[]=react,presets[]=es2015,presets[]=stage-0,plugins[]=transform-decorators-legacy'],
            include: path.join(__dirname, '../app'),
        },{
            test: /\.css$/,
            loader: ExtractTextPlugin.extract('style-loader', 'css-loader!cssnext-loader')
        },{
            test: /\.(woff|woff2|eot|ttf)$/,
            loader: 'url-loader?limit=8192&name=fonts/[name].[ext]'
        },{
            test: /\.(png|jpg|svg)$/,
            loader: 'url-loader?limit=8192&name=img/[name].[ext]'
        }]
    },
    cssnext: {
        browsers: 'last 2 versions'
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    },
    plugins: [
        new Clean([path.join(__dirname, '../dist')], {
            root: path.join(__dirname, '..'),
            verbose: true
        }),
        new ExtractTextPlugin('./css/bundle.css', {allChunks: true}),
        new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]), //ignore locale files from moment.js, saves 300k
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify('production')
            },
            '__DEVTOOLS__': false
        }),
        new webpack.optimize.UglifyJsPlugin({
            compressor: {
                warnings: false
            },
            mangle: false
        })
    ]
}

你不需要那里的所有东西,我只想展示一切到位的大局。只关注outputloadersplugins

2 个答案:

答案 0 :(得分:1)

我认为使用此 npm软件包

https://github.com/gregnb/filemanager-webpack-plugin

,然后在webpack配置文件中使用类似的内容将已编译的应用程序文件移至所需的位置。

new FileManagerPlugin({
    onEnd: {
        move: [
            {source: 'dist/index.html', destination: '../api/modules/web/views/init-build.phtml'},
            {source: 'dist/js', destination: '../api/public/js'},
            {source: 'dist/images', destination: '../api/public/images'}
        ]
    }
})

例如,上面的代码将已编译的ng5应用程序移动到模块化Falcon php应用程序中,并使SPA索引文件成为视图。因此,猎鹰可能会使用它来渲染SPA种子。

答案 1 :(得分:-4)

查看webpack的html-webpack-plugin,它将为您动态构建index.html文件,并将其放在给定的文件夹中。