Gutenberg块开发:尝试通过修改webpack.config将自定义js脚本添加到NPM启动命令

时间:2021-01-28 作者:Grégoire Sailland

“需要帮助了解”;提供您自己的网页配置;https://developer.wordpress.org/block-editor/packages/packages-scripts/#provide-your-own-webpack-config

const defaultConfig = require( \'@wordpress/scripts/config/webpack.config\' );

module.exports = {
    ...defaultConfig,
    entry: {
        ...defaultConfig.entry,
        //index: path.resolve( process.cwd(), \'src\', \'index.js\' ),
        slider: path.resolve( process.cwd(), \'src\', \'slider.js\' ),

    },
};
我在这里尝试从src文件夹向buid文件夹添加一个新的scipt,同时开发一个gutenberg blockI get错误:

ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `wp-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
添加其他不同的css文件也是一样,我该怎么做?

1 个回复
SO网友:Grégoire Sailland

谢谢大家的帮助!我从几天开始就在上面!

我发现了我的问题:

https://webpack.js.org/concepts/entry-points/ 帮助我找到解决方案

显然地path.resolve( process.cwd(), \'src\', \'slider.js\' ) 无法工作:替换为简单字符串路径修复了问题:

const defaultConfig = require( \'@wordpress/scripts/config/webpack.config\' );

module.exports = {
    ...defaultConfig,
    entry: {
        ...defaultConfig.entry,
        slider: \'./src/slider.js\',
    },
};

相关推荐

Why won't my scripts load?

在函数中加载脚本时遇到问题。php我以前做过这件事,所以我肯定我忽略了一些东西,但就我所知,这是正确的,我尝试过从其他答案中复制和粘贴。我没有收到任何控制台错误。我的任何脚本都没有加载到源代码中。感谢您的帮助。functions.php/*SCRIPTS*/ add_action( \'wp_enqueue_scripts\', \'theme_js\' ); function theme_js(){ wp_register_script( \