我想在Gatsby网站中内嵌https://zizzamia.github.io/perfume/
的整个UMD模块源代码,如何从node_modules
目录中读取它并将其放入以下内容的<head>
文件吗?
答案 0 :(得分:0)
我想出了以下解决方案:
npm install raw-loader --save-dev
import rawPerfume from 'raw-loader!./node_modules/perfume.js/dist/perfume.umd.min.js';
plugins/<PLUGIN_NAME>
添加到项目的根目录),其中包含
gatsby-ssr.js
文件(看看SSR guide of
Gatsby)import React from "react";
export const onRenderBody = ({
setHeadComponents,
}) => {
setHeadComponents([
React.createElement('script', {
dangerouslySetInnerHTML: {
__html: rawPerfume.default || rawPerfume
}
})
]);
}
最终的代码是这样的(plugins/gatsby-plugin-perfume.js/gatsby-ssr.js
文件)
import rawPerfume from 'raw-loader!./node_modules/perfume.js/dist/perfume.umd.min.js';
import React from "react";
export const onRenderBody = ({
setHeadComponents,
}) => {
setHeadComponents([
React.createElement('script', {
dangerouslySetInnerHTML: {
__html: rawPerfume.default || rawPerfume
}
})
]);
}
您也可以在我也分享过的the gatsby-plugin-perfume.js plugin上看到代码