活动介绍
file-type

深入理解copy-webpack-plugin-hash:前端文件复制与哈希处理

ZIP文件

下载需积分: 50 | 53KB | 更新于2025-02-22 | 26 浏览量 | 0 下载量 举报 收藏
download 立即下载
标题中提到的知识点是关于一个前端开源库,名为"copy-webpack-plugin-hash"。这个库是针对Webpack打包工具的插件,它扩展了Webpack的功能,允许开发者在构建过程中复制文件和目录。 Webpack是一个广泛使用的现代JavaScript应用程序的静态模块打包器(bundler)。它通过分析项目结构,找到JavaScript模块以及其他一些浏览器不能直接运行的扩展语言(如SASS、TypeScript等),然后将它们转换和打包为合适的格式供浏览器使用。 在使用Webpack进行开发时,很多时候需要将特定的文件或整个目录复制到输出目录中,以便在生产环境中使用。copy-webpack-plugin是一个专门用来实现这一功能的Webpack插件。通过使用copy-webpack-plugin,开发者可以避免手动将文件从源目录复制到目标目录,从而简化了工作流程。 "copy-webpack-plugin-hash"是在copy-webpack-plugin的基础上的一个变体,它在复制文件的过程中添加了哈希值。这通常用于实现长时间缓存策略,即在文件内容未发生变化时,通过哈希值保持文件名不变,以便客户端能够利用缓存。而当文件内容发生变化时,文件名中的哈希值也会相应改变,从而触发浏览器下载新的文件。这种做法既可以减少服务器的请求次数,又可以加快页面的加载速度。 描述中提到的Webpack复制文件和目录的具体操作,是指在Webpack的配置文件中,通常会有一个`plugins`数组,用于配置各种插件。使用copy-webpack-plugin时,需要在`plugins`数组中添加对应的插件实例,并指定需要复制的文件或目录,以及复制的目标路径。在使用"copy-webpack-plugin-hash"时,开发者需要按照其特有的配置规则来设置哈希生成的条件和方式。 具体到"copy-webpack-plugin-hash"的使用方法,可能涉及到以下几个方面: 1. 安装:通常可以通过npm或yarn等包管理工具安装所需的copy-webpack-plugin-hash库。 2. 配置:在Webpack配置文件中配置插件,需要引入插件并创建插件实例。在实例化时,可以通过配置选项指定哪些文件或目录需要被复制,复制到哪里,以及如何处理哈希值。 3. 哈希值的生成:需要了解该插件如何根据文件内容生成哈希值,以及这个哈希值如何与文件名结合。 4. 文件管理:学习如何在构建过程中有效地管理文件的复制,包括如何处理文件的版本更新和缓存失效。 5. 构建优化:了解如何通过哈希化复制文件来优化构建结果,提高项目的构建效率和运行时的性能。 这个插件对于前端项目构建过程中确保文件的正确复制、管理和缓存优化都十分重要,它使得开发者可以更加专注于业务逻辑的开发,而不是手动处理文件复制等繁琐的工作。

相关推荐

filetype

将上述修改整合到我提供的文件中 const path = require('path'); const fs = require('fs'); const WEB_SITE = 'mobile'; const { ZipUpload } = require("@hyfe/hy-utils").default.fs; const TYPE_ENV = process.env.TYPE_ENV; const envSelect = process.env.envSelect; let destPath = "../../s2b/s2b/mobile/dist_mobile/h5" const buildAnalyzeFlag = process.env.analyze; const prodFlag = TYPE_ENV === 'prod'; // ==================== Webpack 5 兼容性修复 ==================== const webpackSourcesPath = require.resolve('webpack-sources'); const webpackSourcesDir = path.dirname(webpackSourcesPath); // ============================================================ const config = { projectName: 'mini-program-taro', date: '2019-5-22', designWidth: 750, deviceRatio: { 640: 2.34 / 2, 750: 1, 828: 1.81 / 2, }, sourceRoot: 'src', outputRoot: `dist/${process.env.TARO_ENV}`, // ==================== 持久化缓存配置 ==================== cache: { enable: true, type: 'filesystem', buildDependencies: { config: [__filename] } }, // ===================================================== alias: { '@/assets': path.resolve(__dirname, '..', 'src/assets'), '@/tpl-assets': path.resolve(__dirname, '..', 'node_modules/@hyfe/ui-taro/lib/assets/templates'), '@/pages': path.resolve(__dirname, '..', 'src/pages'), '@/service': path.resolve(__dirname, '..', 'src/service'), '@/redux': path.resolve(__dirname, '..', 'src/redux'), '@/utils': path.resolve(__dirname, '..', 'src/utils'), '@/libs': path.resolve(__dirname, '..', 'src/libs'), 'taro-ui$': 'taro-ui/lib/index', api: path.resolve(__dirname, '..', 'src/webapi'), wmkit: path.resolve(__dirname, '..', 'src/wmkit/common'), config: path.resolve(__dirname, '..', 'src/wmkit/config'), immutable: path.resolve(__dirname, '..', 'src/wmkit/common/immutable.min.js'), '@/*': path.resolve(__dirname, '..', 'src/*'), // ==================== Webpack 兼容性别名 ==================== 'webpack/sources': webpackSourcesDir }, terser: { enable: true, config: { warnings: false, compress: { drop_debugger: true, drop_console: false, }, }, }, csso: { enable: true, config: { restructure: true, }, }, sass: { resource: [ path.resolve(__dirname, '..', 'src/pages/common/style/swipe.scss'), path.resolve(__dirname, '..', 'node_modules/taro-ui/dist/style/components/modal.scss'), path.resolve(__dirname, '..', 'node_modules/taro-ui/dist/style/components/switch.scss'), path.resolve(__dirname, '..', 'node_modules/taro-ui/dist/style/components/tab-bar.scss'), path.resolve(__dirname, '..', 'node_modules/taro-ui/dist/style/components/badge.scss'), path.resolve(__dirname, '..', 'node_modules/taro-ui/dist/style/components/noticebar.scss'), ], projectDirectory: path.resolve(__dirname, '..'), data: '$nav-height: 48px;', }, defineConstants: { process: { env: { TYPE_ENV: JSON.stringify(process.env.TYPE_ENV), envSelect: JSON.stringify(process.env.envSelect), TARO_ENV: JSON.stringify(process.env.TARO_ENV), analyze: JSON.stringify(process.env.analyze), } }, }, compiler: { type: 'webpack5', prebundle: { enable: false } }, copy: { patterns: [], options: {}, }, plugins: [ [ "@tarojs/plugin-framework-react", { reactRefresh: true // 确保启用热更新 } ], ["babel-plugin-import", { libraryName: "taro-ui", customName: (name, file) => { const nameSection = name.split('-') if (nameSection.length === 4) { nameSection.pop() } const pathMap = { 'tabs/pane': 'tabs-pane', 'modal-action': 'modal/action', 'modal-content': 'modal/content', 'modal-header': 'modal/header', 'badge': 'badge', 'switch': 'switch', 'tab-bar': "tab-bar", 'progress': 'progress', 'icon': 'icon' } const path = nameSection.slice(1).join('-'); return `taro-ui/lib/components/${pathMap[path] || path}`; }, style: (name) => { if (name.includes('/modal')) { return 'taro-ui/dist/style/components/modal.scss' } const wholePath = name.split('/') const compName = wholePath[wholePath.length - 1] const fix = { 'tabs-pane': 'tabs', 'modal/action': 'modal', 'modal/header': 'modal', 'modal/content': 'modal', 'badge': 'badge', 'switch': 'switch', 'tab-bar': "tab-bar", 'progress': 'progress', 'icon': 'icon' }[compName] return `taro-ui/dist/style/components/${fix || compName}.scss` } }] ], framework: 'react', mini: { enableSourceMap: !prodFlag, miniCssExtractPluginOption: { ignoreOrder: true }, compile: {}, postcss: { pxtransform: { enable: true, config: { selectorBlackList: ['body'] }, }, url: { enable: true, config: { limit: 1024, }, }, cssModules: { enable: false, config: { namingPattern: 'module', generateScopedName: '[name]__[local]___[hash:base64:5]', }, }, }, webpackChain(chain, webpack) { // ==================== Webpack 兼容性修复 ==================== chain.resolve.alias.set('webpack/sources', webpackSourcesDir); chain.optimization.minimize(true); buildAnalyzeFlag && chain.plugin('analyzer') .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, []); chain.plugin('ignore').use(webpack.IgnorePlugin, [{ resourceRegExp: /^\.\/locale$/, contextRegExp: /moment$/ }]); chain.merge({ optimization: { splitChunks: { cacheGroups: { // 保留原有缓存组配置... }, }, }, }); }, lessLoaderOption: { paths: [path.resolve(__dirname, 'node_modules'), path.resolve(__dirname, '..', 'src')], }, imageUrlLoaderOption: { esModule: false, }, }, h5: { publicPath: `/${WEB_SITE}/`, staticDirectory: 'static', esnextModules: ['taro-ui', '@hyfe/ui-taro', '@hyfe'], output: { filename: 'js/[name].[hash].js', chunkFilename: 'js/[name].[chunkhash].js', }, miniCssExtractPluginOption: { filename: 'css/[name].[hash].css', chunkFilename: 'css/[name].[chunkhash].css', ignoreOrder: true }, router: { basename: `/${WEB_SITE}/`, mode: 'browser', }, enableSourceMap: !prodFlag, postcss: { autoprefixer: { enable: true, config: {}, }, url: { enable: true, config: { limit: 1024, }, }, cssModules: { enable: false, config: { namingPattern: 'module', generateScopedName: '[name]__[local]___[hash:base64:5]', }, }, }, webpackChain(chain) { // ==================== Webpack 兼容性修复 ==================== chain.resolve.alias.set('webpack/sources', webpackSourcesDir); chain.plugin('htmlWebpackPlugin').tap((args) => { args[0].publicPath = `/${WEB_SITE}/`; return args; }); chain.module.rule('script') .exclude .add(/app\.config\.ts$/) chain.merge({ optimization: { splitChunks: { cacheGroups: { lodash: { name: 'lodash', priority: 1000, test(module) { return /[\\/]node_modules[\\/]lodash/.test(module.resource); }, }, }, }, } }); if (prodFlag) { chain.plugin('after-build-plugin') .use(new ZipUpload({ envCode: envSelect, scriptPath: path.resolve(__dirname, './zip_upload.sh'), src: path.resolve(__dirname, '../dist/h5'), dest: path.resolve(__dirname, destPath) })); } }, commonChunks(commonChunks) { commonChunks.push('lodash'); return commonChunks; }, lessLoaderOption: { paths: [path.resolve(__dirname, 'node_modules'), path.resolve(__dirname, '..', 'src')] }, imageUrlLoaderOption: { esModule: false, }, devServer: { host: '0.0.0.0', port: '8080', open: `/${WEB_SITE}/`, }, }, }; module.exports = function (merge) { return merge({}, config, require('./build')); };

filetype

请使用[email protected]之后的版本提供的构造函数和API。全局安装@vue/cli,vue3创建cesium+three.js深度融合的项目,项目名称为vue-cesium,选择babel、vue-router、vuex、scss、Linter/F和vue3,其他的默认。删除views目录下的所有js文件和components目录下的文件,在views目录下新建CesiumView.vue文件,修改App.vue和router目录下的main.js文件,将router目录下的index.js文件里的路由器模型设置为hash模型。安装cesium和three,修改vue.config.js,在vue.config.js里添加const CesiumSourc = "node_modules/cesium/Source",添加const=CesiumWorker="../Build/Cesium/Workers",把CesiumSourc路径下的Assets、ThirdParty、Widgets复制到dist目录下。把CesiumSource路径下的CesiumWorker复制到dist目录下,把public目录下的models复制到dist目录下。在CesiumView.vue里添加 import * as Cesium from "cesium/Cesium";、 import * as THREE from "three"; 、import "cesium/Widgets/widgets.css,使用最新版本提供的构造函数或api编写代码,在CesiumView.vue中添加const czml = [ { id: "document", name: "CZML Model", version: "1.0", clock: { interval: "2025-08-07T09:30:00Z/2025-08-10T09:30:00Z", currentTime: "2025-08-07T09:30:00Z", multiplier: 1, range: "LOOP_STOP", step: "SYSTEM_CLOCK_MULTIPLIER" } }, { id: "aircraftModel", name: "Cesium Air", position: { epoch: "2025-08-07T09:30:00Z", cartographicDegrees: [ 0, 113.60194, 23.525223, 520, 1200, 113.578067, 23.541241, 1200, 2500, 113.487823, 23.594139, 2500 ], }, model: { gltf: "models/CesiumAir/Cesium_Air.glb", minimumPixelSize: 128, maximumScale: 200, }, }, ];,dataSource加载czml并监听czml获取czml模型的位置和自动调整czml模型的方向,创建particleCount数量为5000个的粒子系统,建立float32Array()缓冲区存储记录每个粒子的位置、大小、颜色、速度、随机值,使用glsl着色器渲染粒子冒烟着火的特效,把粒子系统的三维坐标转换成cesium的三维坐标添加到czml模型上显示出来。注意,此项目不添加额外的ui控制和ui显示。

weixin_38743506
  • 粉丝: 352
上传资源 快速赚钱