简体   繁体   English

Webpack 编译的 CSS 文件包含 Javascript 变量和函数

[英]Webpack-compiled CSS file is including Javascript variables and functions

We use a simple application of webpack/mix:我们使用一个简单的 webpack/mix 应用:

mix.js('resources/js/app.js', 'public/js')
.js('resources/js/cpg.js', 'public/js')
.js('resources/js/editor.js', 'public/js')
.copy('resources/js/ckeditor/dist', 'public/js/editor')
.sass('resources/sass/app.scss', 'public/css')
.sass('resources/sass/cpg.scss', 'public/css')

With webpack.config.js:使用 webpack.config.js:

module.exports = {
resolve: {
    alias: {
        '@': path.resolve('resources/js'),
    },
},

// https://webpack.js.org/configuration/entry-context/
entry: './resources/js/editor.js',

// https://webpack.js.org/configuration/output/
output: {
    path: path.resolve(__dirname, 'public/js/editor'),
    filename: 'editor.js'
},

module: {
    rules: [
        {
            test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,

            use: ['raw-loader']
        },
        {
            test: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css$/,

            use: [
                {
                    loader: 'style-loader',
                    options: {
                        injectType: 'singletonStyleTag',
                        attributes: {
                            'data-cke': true
                        }
                    }
                },
                {
                    loader: 'postcss-loader',
                    options: styles.getPostCssConfig({
                        themeImporter: {
                            themePath: require.resolve('@ckeditor/ckeditor5-theme-lark')
                        },
                        minify: true
                    })
                }
            ]
        }
    ]
},

// Useful for debugging.
devtool: 'source-map',

// By default webpack logs warnings if the bundle is bigger than 200kb.
performance: { hints: false }
}

Prior to the addition of ckeditor, we had no troubles.在添加ckeditor之前,我们没有遇到任何麻烦。 But now that ckeditor has been added, the following JS now appears in our compiled cpg.css file:但是现在已经添加了 ckeditor,下面的 JS 现在出现在我们编译的 cpg.css 文件中:

 function webpackContext(req) {
    var id = webpackContextResolve(req);
    return __webpack_require__(id);
}
function webpackContextResolve(req) {
    if(!__webpack_require__.o(map, req)) {
        var e = new Error("Cannot find module '" + req + "'");
        e.code = 'MODULE_NOT_FOUND';
        throw e;
    }
    return map[req];
}
webpackContext.keys = function webpackContextKeys() {
    return Object.keys(map);
};
webpackContext.resolve = webpackContextResolve;
module.exports = webpackContext;
webpackContext.id = "./node_modules/moment/locale sync recursive ^\\.\\/.*$";

Obviously, this is a problem.显然,这是一个问题。 JS code does not belong in CSS files, and it trips up our SonarCloud quality gate (for good reason) so we can't deploy anything that's been compiled unless we manually edit the compiled files. JS 代码不属于 CSS 文件,它会触发我们的 SonarCloud 质量门(有充分的理由),因此我们无法部署任何已编译的内容,除非我们手动编辑已编译的文件。 Which mostly defeats the purpose of having them.这在很大程度上违背了拥有它们的目的。

Further backstory: the section of our project that uses CKEditor was completed by a contractor.进一步的背景故事:我们项目中使用 CKEditor 的部分是由承包商完成的。 So, all of this was merged into our project before we saw that compiled files were improper.所以,在我们看到编译的文件不正确之前,所有这些都被合并到我们的项目中。 The contractor is no longer with the company, so I'm trying to debug on my own and getting nowhere.承包商不再与公司合作,所以我试图自己调试但无济于事。 It seems to be an exceedingly rare bug for Webpack to place JS code in a CSS file. Webpack 将 JS 代码放在 CSS 文件中似乎是一个非常罕见的错误。

Progress update: Removing the ckeditor references has no impact.进度更新:删除 ckeditor 引用没有影响。 The Webpack just seems to be broken now. Webpack 现在似乎坏了。 Comprehensive node_modules re-install had no effect.综合 node_modules 重新安装没有效果。 It's just broken.它只是坏了。

Might be related to this https://github.com/ckeditor/ckeditor5/issues/8112 A solution there suggests this change可能与此有关https://github.com/ckeditor/ckeditor5/issues/8112那里的解决方案表明此更改

        use: [
            {
                loader: 'style-loader',
                options: {
                    injectType: 'singletonStyleTag',
                    attributes: {
                        'data-cke': true
                    }
                }
            },
            'css-loader', // added this line
            {
                loader: 'postcss-loader',
                options: styles.getPostCssConfig({
                    themeImporter: {
                        themePath: require.resolve('@ckeditor/ckeditor5-theme-lark')
                    },
                    minify: true
                })
            }
        ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM