【问题标题】:Webpack 2 issues with Semantic UIWebpack 2 的语义 UI 问题
【发布时间】:2017-07-08 08:56:46
【问题描述】:

一直在努力使用 Webpack 2 设置语义用户界面。我遇到了一些与默认语义用户界面主题中的字体相关的错误,以及另一个关于 image-webpack-loader 的错误:

ERROR in ./~/css-loader?{"lessPlugins":[{"options":{"paths":{"../../theme.config":"/Users/djthomps/Desktop/demo/theme.config"}}}]}!./~/less-loader!./semantic/src/semantic.less
Module not found: Error: Can't resolve './themes/themes/default/assets/fonts/icons.eot' in '/Users/djthomps/Desktop/demo/semantic/src'
 @ ./~/css-loader?{"lessPlugins":[{"options":{"paths":{"../../theme.config":"/Users/djthomps/Desktop/demo/theme.config"}}}]}!./~/less-loader!./semantic/src/semantic.less 6:285117-285174 6:285197-285254
 @ ./semantic/src/semantic.less
 @ ./app/index.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/index.js

# same for icons.woff2

# same for icons.woff

# same for icons.ttf

# same for icons.svg

ERROR in ./~/css-loader?{"lessPlugins":[{"options":{"paths":{"../../theme.config":"/Users/djthomps/Desktop/demo/theme.config"}}}]}!./~/less-loader!./semantic/src/semantic.less
Module not found: Error: Can't resolve 'image-webpack' in '/Users/djthomps/Desktop/demo'
BREAKING CHANGE: It's no longer allowed to omit the '-loader' suffix when using loaders.
                 You need to specify 'image-webpack-loader' instead of 'image-webpack'.
 @ ./~/css-loader?{"lessPlugins":[{"options":{"paths":{"../../theme.config":"/Users/djthomps/Desktop/demo/theme.config"}}}]}!./~/less-loader!./semantic/src/semantic.less 6:218646-218697
 @ ./semantic/src/semantic.less
 @ ./app/index.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/index.js

最终目标是使用带有自定义主题的 react semantic-ui 组件,我可以简单地将其导入到我的 .jsx 文件中,如 this example 所示。

我一直在使用 Webpack 2 关注 this guide to get semantic-ui setup with Webpack 1,并在此过程中修复了 less-loader 的差异。尽管如此,在搜索了 font-awesome-webpack2 等其他项目并筛选了 github cmets 之后,我似乎无法解决这些问题。这是一个非常小的、可验证的示例:

webpack.config.js

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const RewriteImportPlugin = require('less-plugin-rewrite-import');

const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: './app/index.html',
    filename: 'index.html',
    inject: 'body' // inject scripts before closing body tag
});

module.exports = {
    entry: './app/index.js', // where the bundler starts the bundling process
    output: { // where the bundled code is saved
        path: path.resolve('dist'),
        filename: 'index_bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.(png|jpg|gif|woff|svg|eot|ttf|woff2)$/,
                loader: 'url-loader?limit=1024&name=[name]-[hash:8].[ext]!image-webpack'
            },
            {
                test: /\.less$/, // import css from 'foo.less';
                use: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            // importLoaders: 1,
                            lessPlugins: [
                                new RewriteImportPlugin({
                                    paths: {
                                        '../../theme.config':  __dirname + '/theme.config',
                                    },
                                })
                            ]
                        }
                    },
                    'less-loader'
                ]
            },
            {
                test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'file-loader'
            }
        ]
    },
    devtool: 'eval-source-map',
    devServer: { compress: true },
    plugins: [ HtmlWebpackPluginConfig ]
};

package.json

{
    "name": "demo",
    "version": "1.0.0",
    "main": "index.js",
    "license": "MIT",
    "scripts": {
        "start": "webpack-dev-server"
    },
    "devDependencies": {
        "css-loader": "^0.26.1",
        "html-webpack-plugin": "^2.28.0",
        "image-webpack-loader": "^3.2.0",
        "less-loader": "^2.2.3",
        "less-plugin-rewrite-import": "^0.1.1",
        "semantic-ui": "^2.2.7",
        "style-loader": "^0.13.1",
        "url-loader": "^0.5.7",
        "webpack": "^2.2.1",
        "webpack-dev-server": "^2.3.0"
    }
}

app/index.js

import css from '../semantic/src/semantic.less';

app/index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Demo</title>
</head>
<body>
    <button class="ui button">Follow</button>
</body>
</html>

theme.config

 // truncated for brevity
 @button     : 'gmail';

我的项目结构如下:

.
├── app
│   ├── index.html
│   └── index.js
├── package.json
├── semantic
│   ├── gulpfile.js
│   ├── src
│   └── tasks
├── semantic.json
├── theme.config
└── webpack.config.js

更新 1

我一直在考虑可能的解决方案:

  1. postinstall 脚本将我的 theme.config 移动到 semantic 文件夹中,然后构建 semantic 有点像 this tutorial
  2. postinstall 脚本用我的版本替换所有 theme.config 导入(RewriteImportPlugin 应该处理的内容)
  3. 设置单独的 gulp 任务来处理文件的移动和语义 UI 的构建
  4. 端到端使用 webpack 2(首选)

更新 2

ERROR in ./~/css-loader?{"lessPlugins":[{"options":{"paths":{"../../theme.config":"/Users/djthomps/Desktop/demo/theme.config"}}}]}!./~/less-loader!./semantic/src/semantic.less
Module not found: Error: Can't resolve 'image-webpack' in '/Users/djthomps/Desktop/demo'
BREAKING CHANGE: It's no longer allowed to omit the '-loader' suffix when using loaders.
                 You need to specify 'image-webpack-loader' instead of 'image-webpack'.
 @ ./~/css-loader?{"lessPlugins":[{"options":{"paths":{"../../theme.config":"/Users/djthomps/Desktop/demo/theme.config"}}}]}!./~/less-loader!./semantic/src/semantic.less 6:218646-218697
 @ ./semantic/src/semantic.less
 @ ./app/index.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/index.js

通过调整配置文件修复:

 loader: 'url-loader?limit=1024&name=[name]-[hash:8].[ext]!image-webpack-loader' // note the loader at the end

【问题讨论】:

  • 如果你想在 React 中使用语义,他们正在开发一个不错的组件库:react.semantic-ui.com/introduction。您仍然必须手动包含 css。使用任何自定义 css/sass ......祝你好运,我浪费了很多时间让它工作。
  • @BalázsÉdes 我知道组件库。这个想法是我希望能够注入自己的主题并将.less文件直接导入组件中。
  • 所以我可以告诉你我在这方面也花了很长时间(用于 VueJS),最后我只是使用他们的 gulp 任务构建了语义 ui,并包含了从语义 dist 生成的 CSS 和 JS 文件.它实际上工作得很好,gulp watch 获取主题的更改并重新构建,然后 webpack 热重新加载,
  • @Justin 我实际上是从这条路开始的......很高兴知道其他人正在为同样的事情而苦苦挣扎并思考同样的事情。
  • @Justin 你能详细说明你是如何使用 Webpack 2 来实现这个功能的吗?当我使用常规的旧 css 时,我仍然遇到同样的错误

标签: javascript webpack less semantic-ui webpack-2


【解决方案1】:

希望这将为您指明正确的方向,即使它不是一个完整的解决方案。正如我所提到的,我花了很多时间试图让 Semantic-UI 与 Webpack 2 一起工作。我正在使用来自 vue-cli 的 Webpack template 进行 VueJS 项目。我尝试将 Vue 配置从模板中剥离出来,以获得一个与框架无关但效果不佳的示例。

看起来你可能只是想设置 Semantic-UI CSS,而不是他们的 JS 组件。我对 Vue Webpack 模板所做的所有添加都与 JS 相关,基本上只是为了包含用于 Semantic-UI 的 jQuery。因此,如果您只想让 CSS 正常工作,则无需添加这些内容。

为了让模板的配置与 Semantic-UI JS 一起工作,我将其添加到 module-exports

alias: {
  ...
  jquery: "jquery/src/jquery",
},
...
plugins: [
  new webpack.ProvidePlugin({
    $: "jquery",
    jQuery: "jquery",
    "window.jQuery": "jquery"
  })
]  

我运行 Semantic 的 Gulp 任务来构建它自己的 dist 文件夹,然后我可以简单地将这些文件包含在我的 webpack 的 main.js 条目中。

import '../semantic/dist/semantic.css'
import '../semantic/dist/semantic'

【讨论】:

    【解决方案2】:

    打了三天的头,我终于能够弄清楚大部分。

    webpack.config.js

    const path = require('path');
    const webpack = require('webpack');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    
    module.exports = {
        entry: './app/index.js', // where the bundler starts the bundling process
        output: { // where the bundled code is saved
            path: path.resolve('dist'),
            filename: 'index_bundle.js'
        },
        resolve: {
            alias: {
                semantic: path.resolve(__dirname, 'semantic/src/'),
                jquery: path.resolve(__dirname, 'node_modules/jquery/src/jquery')
            }
        },
        module: {
            loaders: [
                {
                    test: /\.(png|gif)$/,
                    loader: 'url-loader?limit=1024&name=[name]-[hash:8].[ext]!image-webpack-loader'
                },
                {
                    test: /\.jpg$/,
                    loader: 'file-loader'
                },
                {
                    test: /\.less$/, // import css from 'foo.less';
                    use: [
                        'style-loader',
                        'css-loader',
                        'less-loader'
                    ]
                },
                {
                    test: /\.(ttf|eot|svg|woff2?)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                    loader: 'file-loader'
                }
            ]
        },
        devtool: 'eval-source-map',
        devServer: { compress: true },
        plugins: [
            new HtmlWebpackPlugin({
                template: './app/index.html',
                filename: 'index.html',
                inject: 'body' // inject scripts before closing body tag
            }),
            new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery'
        })
        ]
    };
    

    但问题是,如果你想使用捆绑的字体,你需要修复路径,因为它们被错误解析之后我们执行less-loader 加载器(错误仍然是神秘)。我创建了一个 handy template 作为一个非常最小的示例,其中包含一些额外的细节。

    【讨论】:

      【解决方案3】:

      这应该是为 webpack2 制作语义 ui 主题的最优雅的方式。

      感谢issue 的想法,我已经更新了我的教程React+Webpack1/2/3+Semantic UI and how to do themingdemo project

      请按照教程或滚动到底部查看您需要进行的主要更改。这两个与 Webpack1 的主要区别是:

      • 默认情况下,less-loader 会使用 webpack 的解析器来解析所有的 less 文件,使得像 less-plugin-rewrite-import 这样的插件无法处理 less 文件。这就是为什么您会发现该插件不适用于 webpack2。为了让 less-loader 使用自己的解析器,你需要手动指定选项路径供它搜索(看看下面粘贴的 webpack 配置)
      • 由于现在我们使用较少的解析器,我们不能再使用~ 来引用node_modules 中的模块,所以打开你的theme.config 并将@import "~semantic-ui-less/theme.less"; 更改为@import "semantic-ui-less/theme.less";
      常量路径 = 要求('路径'); 常量 webpack = require('webpack'); const RewriteImportPlugin = require("less-plugin-rewrite-import"); 常量 HtmlWebpackPlugin = require('html-webpack-plugin'); const ROOT_DIR = path.resolve(__dirname); const SRC_DIR = path.resolve(__dirname, 'app'); const BUILD_DIR = path.resolve(__dirname, 'build'); const NODE_MODULES_DIR = path.resolve(__dirname, 'node_modules'); var webpackConfig = { 开发工具:'评估', 入口: { 索引:path.resolve(SRC_DIR,'index.js'), }, 输出: { 路径:BUILD_DIR, 文件名:'[name].[hash:8].js', }, 解决: { 模块:[ROOT_DIR,'node_modules'], }, 模块: { 规则:[ { 测试:/\.(less|config)/, 采用: [ '样式加载器', 'css 加载器', { 装载机:“少装载机”, 选项: { 路径:[ROOT_DIR,NODE_MODULES_DIR], 插件:[ 新的 RewriteImportPlugin({ 路径:{ '../../theme.config': __dirname + '/app/semantic-ui/theme.config', }, }), ], }, }, ], }, { 测试:/\.(png|jpg|gif|woff|svg|eot|ttf|woff2)$/, 采用: [ {加载器:'文件加载器'}, ], }, { 测试:/\.html$/, 加载器:'html-loader', }, { 测试:/\.jsx?$/, 排除:/(node_modules|bower_components)/, 加载器:'babel-loader', 查询:{预设:['es2015']} }, ], }, 插件:[ 新的 HtmlWebpackPlugin({ 注入:'身体', 模板:'app/index.html', 文件名:'index.html', 块:['索引'], chunksSortMode: '依赖', 环境:进程.env, }), ], }; 模块.exports = webpackConfig;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-05
        • 2018-11-18
        相关资源
        最近更新 更多