简体   繁体   English

如何使用Webpack 2从node_modules导入CSS

[英]How to import css from node_modules using webpack 2

I want to import a css file (lightgallery.css) from node_modules to public_html. 我想将一个CSS文件(lightgallery.css)从node_modules导入public_html。 My project has this structure 我的项目具有这种结构

root
    app/
        main.jsx
        ...
    public_html/
        index.html
        ...
    node_modules/
        lightgallery/
        ...
    webpacj.config.js

webpack.config.js webpack.config.js

let config = {    

    entry: {
        main: './app/main.jsx'
    },

    output: {
        path: path.resolve(__dirname, 'public_html')
    },         

    resolve: {
        extensions: ['.js', '.json', '.css'],
        alias: {
          "lightgallery-css": path.resolve(__dirname, "./node_modules/lightgallery/src/css/lightgallery.css")
        }
    },

    module: {

        rules: [

        {
            test: /\.css$/,
            // include: /node_modules/,
            use:  ExtractTextPlugin.extract({
                fallback: "style-loader",
                use: "css-loader"
            })
        },

        {
            test: /\.(gif|png|jpg|jpeg|svg)$/,
            use: {
                loader: 'url-loader',
                options: {
                    limit: 25000,
                },
            },
        },

        {
            test: /\.(ttf|eot|woff|woff2)$/,
            loader: 'file-loader',
            options: {
                limit: 50000,
                name: 'fonts/[name].[ext]',
            },
        },

        ]
    },

    plugins: [

        new HtmlWebpackPlugin({
            template: './app/index.html'
        }),

        new  ExtractTextPlugin(path.resolve(__dirname, 'public_html') + '[name].bundle.css'),  
    ]

}  


module.exports = config;

I tried to load the css in .jsx file in this way 我试图以这种方式在.jsx文件中加载CSS

import 'lightgallery/src/css/lightgallery.css'

but I can't find *.bundle.css in public_html (I get no errors after compiling my app) 但我在public_html中找不到* .bundle.css(编译我的应用程序后没有错误)

What I wrong? 我怎么了

EDIT2: The webpack's list of compiled files EDIT2:Webpack的已编译文件列表

Version: webpack 2.6.1
Time: 11314ms
                Asset       Size  Chunks                    Chunk Names
                14.js      11 kB      14  [emitted]         CompaniesArchived
         fonts/lg.eot    4.02 kB          [emitted]
        fonts/lg.woff    3.96 kB          [emitted]
                 0.js     354 kB       0  [emitted]  [big]  ProjectActivitiesPagination
                 1.js     194 kB       1  [emitted]         ProjectActivitiesShared
                 2.js    13.5 kB       2  [emitted]
                 3.js    14.1 kB       3  [emitted]
                 4.js    32.9 kB       4  [emitted]         Account
                 5.js    36.2 kB       5  [emitted]         Project
                 6.js    36.2 kB       6  [emitted]         MyProject
                 7.js    34.7 kB       7  [emitted]         Customer
                 8.js    33.5 kB       8  [emitted]         Company
                 9.js    20.7 kB       9  [emitted]         ProjectsArchived
                10.js    20.7 kB      10  [emitted]         MyProjectsArchived
                11.js    14.2 kB      11  [emitted]         CustomersArchived
                12.js    22.3 kB      12  [emitted]         Projects
                13.js    7.85 kB      13  [emitted]         PasswordChange
         fonts/lg.ttf    3.88 kB          [emitted]
                15.js    9.26 kB      15  [emitted]         Signup
                16.js    27.8 kB      16  [emitted]         AccountUpgrade
                17.js    6.27 kB      17  [emitted]         Login
                18.js    4.29 kB      18  [emitted]         AccountActivation
                19.js    3.08 kB      19  [emitted]         Search
                20.js    5.56 kB      20  [emitted]         ProjectsWithDeadline
                21.js    4.12 kB      21  [emitted]         ProjectsWithActivitiesToComplete
                22.js    5.63 kB      22  [emitted]         ProjectsForViewer
                23.js    3.41 kB      23  [emitted]         PasswordReset
                24.js     6.9 kB      24  [emitted]         MyProjects
                25.js    1.36 kB      25  [emitted]         Logout
                26.js    7.82 kB      26  [emitted]         Customers
              main.js    1.06 MB      27  [emitted]  [big]  main
            vendor.js    2.05 MB      28  [emitted]  [big]  vendor
           index.html    2.66 kB          [emitted]
nls/it/translation.js    7.88 kB          [emitted]
           nls/nls.js  182 bytes          [emitted]

Open the project in editor like Visual Studio Code, locate the file 在类似Visual Studio Code的编辑器中打开项目,找到文件

.angular-cli.json .angular-cli.json

under the apps configuration array, add styles array. 在apps配置数组下,添加styles数组。

"apps": [{
        "root": "src",
        "outDir": "dist",
        "assets": [
            "assets",
            "favicon.ico"
        ],
        "index": "index.html",
        "main": "main.ts",
        "polyfills": "polyfills.ts",
        "test": "test.ts",
        "tsconfig": "tsconfig.app.json",
        "testTsconfig": "tsconfig.spec.json",
        "prefix": "app",
        "styles": [
            "../node_modules/bootstrap/dist/css/bootstrap.css",
            "styles.css"
        ],
        "scripts": [],

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

相关问题 使用webpack目标节点从node_modules导入css - import css from node_modules with webpack target node 使用 require() 从 node_modules 导入 css 文件 - import css file from node_modules using require() 如何使用webpack从node_modules加载静态CSS文件? - Example of how to load static CSS files from node_modules using webpack? Sapper:如何从 node_modules 导入 CSS? - Sapper: How to import CSS from node_modules? Webpack不会从node_modules导入包(仅适用于js) - Webpack does not import bundles from node_modules (js only) React/Webpack Css 从 node_modules 导入未正确加载 - React/Webpack Css imports from node_modules are not loading correctly 如何使用 Laravel 和 Z424516CA53B4AD4BEF37ED04F8795A 从 node_modules 将 javascript object 导入 Vue - How to import javascript object from node_modules into Vue with Laravel and webpack Webpack 正在从我机器的全局“node_modules”导入模块。 如何让它只从我的项目的 `node_modules` 导入? - Webpack is importing modules from my machine's global `node_modules`. How do I get it to only import from my project's `node_modules`? 如何在vuejs中导入node_modules的所有css - how to import all css of node_modules in vuejs 使用〜从Angular的使用合同的node_modules中导入带有Angular的CSS吗? - Is using ~ to import CSS with Angular from node_modules part of Angular's usage contract?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM