JavaScriptServices icon indicating copy to clipboard operation
JavaScriptServices copied to clipboard

Importing CSS from node_modules

Open nklt opened this issue 10 years ago • 1 comments

Will the project include capability to import css files from npm modules. There are a list of npm projects for UI controls that is bundled in npm modules, and will be very useful to be able to import the css files from the node_modules location, inside the typescript definition files.

for example

@Component({ selector: 'counter', styleUrls: ['/node_modules/company-a/component-b/main.css'], // load the button theme template: require('./counter.component.html') })

Currently this does not work (whether we specify ./../ or any relative path to help the TSC find the file location. In the webpack.config.js module, current it only looks at ClientApp folder. We tried to add node_modules path in webpack.config.js but the webpack engine does not like compoents with "@" sign on the module name, such as @angular/xyz.

nklt avatar Sep 21 '16 21:09 nklt

Don't use styleUrls, because that actually sends URLs to the client so the browser will make HTTP requests for those files. That's not going to work, because you're not serving node_modules publicly (or at least you shouldn't).

The syntax you want is like this:

styles: [require('company-a/component-b/main.css')]

NPM allows you to reference arbitrary files within modules like this, and Webpack will see the require statement and will include that file (and anything else it depends on, which might include other .css files or images) within the compiled bundle under wwwroot/dist.

SteveSandersonMS avatar Sep 22 '16 11:09 SteveSandersonMS