Webpack Encore:选择性或部分版本控制

发布于 2025-01-16 14:26:37 字数 1222 浏览 5 评论 0原文

我通常在 Webpack 中启用版本控制,但是有没有更有选择性的方法?

我需要保持一个或多个文件不受版本控制。
如果我可以同时拥有这些文件的版本化和非版本化版本,那就更好了。

我当前的配置:

let Encore = require('@symfony/webpack-encore');
let UglifyJsPlugin = require('uglifyjs-webpack-plugin');

Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .addEntry('app', './assets/js/app.js')
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .enableSassLoader()
    .autoProvidejQuery()

    // I enable the versioning
    .enableVersioning(Encore.isProduction())

    // But i want an unversioned build of this file
    .addStyleEntry('blog_article', './assets/css/blog_article.scss')
;

const config = Encore.getWebpackConfig();
if(Encore.isProduction()) {
    config.plugins.push(new UglifyJsPlugin({
        uglifyOptions: {
            output: {
                comments: false
            }
        }
    }));
}

module.exports = [config];

当前输出:

  • app.XXX.js(版本化)
  • blog_article.XXX.css(版本化)

所需输出:

  • app.XXX.js(版本化)
  • blog_article.XXX.css(版本化)
  • blog_article.css(未版本化)

I usually enable the versioning in Webpack, but is there to do it more selectively?

I need to keep one or more files unversioned.
Even better if I could have both, versioned and unversioned builds of these files.

My current config:

let Encore = require('@symfony/webpack-encore');
let UglifyJsPlugin = require('uglifyjs-webpack-plugin');

Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .addEntry('app', './assets/js/app.js')
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .enableSassLoader()
    .autoProvidejQuery()

    // I enable the versioning
    .enableVersioning(Encore.isProduction())

    // But i want an unversioned build of this file
    .addStyleEntry('blog_article', './assets/css/blog_article.scss')
;

const config = Encore.getWebpackConfig();
if(Encore.isProduction()) {
    config.plugins.push(new UglifyJsPlugin({
        uglifyOptions: {
            output: {
                comments: false
            }
        }
    }));
}

module.exports = [config];

Current output :

  • app.XXX.js (versioned)
  • blog_article.XXX.css (versioned)

Desired output :

  • app.XXX.js (versioned)
  • blog_article.XXX.css (versioned)
  • blog_article.css (unversioned)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2025-01-23 14:26:37

一年过去了,这个问题可能已经解决了,但我也遇到了类似的问题。我需要从资产中的特定目录复制文件而不添加哈希。根据 Symfony webpack 文档Webpack 文档,这是我的解决方案。


if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}

Encore
    // directory where compiled assets will be stored
    .setOutputPath('public_html/build/')
    // public path used by the web server to access the output path
    .setPublicPath('/build')
    .addEntry('app', './assets/app.js')
    .addLoader({
        test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        type: 'asset/resource',
    })
    .splitEntryChunks()
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    // enables hashed filenames (e.g. app.abc123.css)
    .enableVersioning(Encore.isProduction())
    // enables Sass/SCSS support
    .enableSassLoader(function(options) {
        // https://github.com/sass/node-sass#options
        options.sassOptions = {outputStyle: 'compressed'}
    })
    .enablePostCssLoader()
    // Copy files with hash
    .copyFiles({
        from: './assets/images',
        to: 'images/[path][name].[hash:8].[ext]'
    })
;

// build the second configuration
const mainConfig = Encore.getWebpackConfig();

// reset Encore to build the second config
Encore.reset();

Encore
    .setOutputPath('public_html/build_editor/')
    .setPublicPath('/build_editor')
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .enableVersioning(false)
    // copy files without hash
    .copyFiles({
        from: './assets/editor_images',
        to: 'images/[path][name].[ext]'
    })

const editorAssetsConfig = Encore.getWebpackConfig();
editorAssetsConfig.name = 'editorAssetsConfig';

module.exports = [mainConfig, editorAssetsConfig];

A year has passed, the question is probably solved, but I had a similar problem. I need to copy files from specific dir in assets without adding hash. According to Symfony webpack docs and Webpack docs, here is my solution.


if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}

Encore
    // directory where compiled assets will be stored
    .setOutputPath('public_html/build/')
    // public path used by the web server to access the output path
    .setPublicPath('/build')
    .addEntry('app', './assets/app.js')
    .addLoader({
        test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        type: 'asset/resource',
    })
    .splitEntryChunks()
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    // enables hashed filenames (e.g. app.abc123.css)
    .enableVersioning(Encore.isProduction())
    // enables Sass/SCSS support
    .enableSassLoader(function(options) {
        // https://github.com/sass/node-sass#options
        options.sassOptions = {outputStyle: 'compressed'}
    })
    .enablePostCssLoader()
    // Copy files with hash
    .copyFiles({
        from: './assets/images',
        to: 'images/[path][name].[hash:8].[ext]'
    })
;

// build the second configuration
const mainConfig = Encore.getWebpackConfig();

// reset Encore to build the second config
Encore.reset();

Encore
    .setOutputPath('public_html/build_editor/')
    .setPublicPath('/build_editor')
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .enableVersioning(false)
    // copy files without hash
    .copyFiles({
        from: './assets/editor_images',
        to: 'images/[path][name].[ext]'
    })

const editorAssetsConfig = Encore.getWebpackConfig();
editorAssetsConfig.name = 'editorAssetsConfig';

module.exports = [mainConfig, editorAssetsConfig];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文