window.$ 在 svelte 中保留,因此您必须修补您的库
使用jQuery in noconflict mode,比如
sed -E 's/(\W?)\$(\W)/\1jQuery\2/g' library.js >library.noconflict.js
用jQuery(替换$(和用jQuery.替换$.
(\W?) 匹配可选的非单词字符(空格、括号),
避免将func_$( 替换为func_jQuery(
\$(\W) 匹配 $( 或 $. 或 $) 或 $ 或 ....
在我的情况下,我必须修补 goldenlayout.js 版本 1.5.9,比如
sed -E 's/(\W?)\$(\W)/\1jQuery\2/g' \
node_modules/golden-layout/dist/goldenlayout.js \
>src/goldenlayout-1.5.9.noconflict.js
也不得不换
(function($){
// ....
define( [ 'jquery' ], function( jquery ) {
$ = jquery;
return lm.LayoutManager;
} ); // jshint ignore:line
// ....
} );})(window.$);
与
(function(){
// ....
define( [], function() {
return lm.LayoutManager;
} ); // jshint ignore:line
// ....
} );})();
在运行 sed 之前避免变量阴影
在这种情况下,本地 jQuery 与全局 window.jQuery
sed 可能会替换太多(字符串内容)
所以理想情况下做一个像 AST 变换一样
// tools/patch-goldenlayout.js
// pnpm i -D acorn estree-walker magic-string
const input_file = "../node_modules/golden-layout/dist/goldenlayout.js";
const output_file = "../src/goldenlayout.noconflict.js";
const id_search = '$';
const id_replace = 'jQuery';
const acorn_parse = require("acorn").parse;
const estree_walk = require("estree-walker").walk;
const magicString = require("magic-string");
const fs = require("fs");
const code_old = fs.readFileSync(input_file, 'utf8');
let code = new magicString(code_old);
const ast = acorn_parse( code_old, {
sourceType: 'module', ecmaVersion: 11 /* year 2020 */ });
estree_walk( ast, {
enter: function ( node, parent, prop, index ) {
if (node.type == 'Identifier' && node.name == id_search) {
code.overwrite(node.start, node.end, id_replace);
}
}});
fs.writeFileSync(output_file, code.toString());