javascript - 如何指定哪些 Require.js 模块是用优化器压缩的,哪些不是?

标签 javascript node.js requirejs

将 Require.js 优化器与 Node.js 结合使用,我只想压缩部分项目文件。如何指定要压缩的文件?这是我当前的 node.js 脚本:

var requirejs = require('requirejs');

var config = {
baseUrl: '/common',
name: 'main',
out: '/compressed'
};

requirejs.optimize(config, function (buildResponse) {
//buildResponse is just a text output of the modules
//included. Load the built file for the contents.
//Use config.out to get the optimized file contents.
var contents = fs.readFileSync(config.out, 'utf8');
});

我只是想压缩公共(public)文件。主要也是共同点。有一些不常见的文件是我不想压缩的依赖项。优化器如何选择要合并的文件?

最佳答案

我认为 Require.js 构建系统不是这样工作的;这是一种全有或全无的操作。来自网站 ( http://requirejs.org/docs/optimization.html ):

The build system will then trace the dependencies for main.js and inject them into the appdirectory-build/scripts/main.js file.

但是,您可以提供大量选项:

https://github.com/jrburke/r.js/blob/master/build/example.build.js

基于此,您最好的选择似乎是将不需要的模块标记为“ stub ”模块。来自页面:

Specify modules to stub out in the optimized file. The optimizer will use the source version of these modules for dependency tracing and for plugin use, but when writing the text into an optimized layer, these modules will get the following text instead

关于javascript - 如何指定哪些 Require.js 模块是用优化器压缩的,哪些不是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11442088/

相关文章:

RequireJS 在加载所需模块之前不运行数据主脚本

javascript - 如何在 ReactJS 中创建动态函数名称并在 onBlur 上使用?

javascript - 如何将 ng-app 与 RequireJs 一起使用?

javascript - 禁用双击以单击触摸屏 iOS 设备

node.js - 如何在 Node Js 中使用和发布来自 Amazon MQ 的消息?

Node.js 和 MongoDB 如果文档完全匹配存在,则忽略插入

sql-server - Node.js MSSQL tedius ConnectionError : Failed to connect to localhost:1433 - connect ECONNREFUSED

webpack - 当 block 加载失败时,Webpack 是否提供全局错误 Hook ?

javascript - 动态调用方法 | JavaScript

php - 这是 jQuery 的错误吗? $.post 与 $.ajax