webpackã®inline syntax
誰å¾ãããããªãã§ãããããã®è³ªåãæ¥ãã®ã§ããã§èª¬æãããã¨æãã¾ãã
webpackã¯ããã¼ãã¼ãã§ã¤ã³ã¨å¼ã°ããä»çµã¿ã§åãã¦ãã¾ãã
ããã«ã¯ãinlineã®æ¸ãæ¹ãåå¨ãã¾ããéçºè
ã§ãã使ãã®ã¯éæ¨å¥¨ã§ãã
ããããwebpackã®ãã°ã«ã¯ãã®è¡¨è¨ãå¤ã
åºã¦ããããçåããã人ã¯å¤ãã¨æãã¾ãã
è¨å®ã¯ä»¥ä¸ã®ãã¡ã¤ã«ç¾¤ã§ãã
// webpack.config.js module.exports = { mode: 'development', module: { rules: [ { test: /\.css$/i, use: [ 'style-loader', 'css-loader', ], }, { test: /\.css$/i, use: [require.resolve('./loader')], enforce: 'post', } ], }, };
// loader.js module.exports = function postLoader(res) { console.log(this.resource); return res; }
ãã®å ´åã¯ãæ¡å¼µå.cssãããå ´åã«css-loader -> style-loader -> ./loaderã¨ãããã¼ãã¼ãã§ã¤ã³ãçºçãã¾ãã
åºæ¬
webpackã®ãã¼ãã¼ã®èªã¿æ¹ã¯å³ããã¨ãªããoptionsã®ã¤ãªãã¯queryã§ãã
ã¾ããinlineã¯webpack.config.jsã«æ¸ããããã®ãããåªå
度ãé«ãä¸æ¸ãå¯è½ã§ãã
以ä¸ã®å ´åã ã¨ãcss-modulesã¨sourceMapãä¸æ¸ãããæå¹åããã¾ãã
ãã¼ãã¼éã¯!
ã§ã»ãã¬ã¼ããã¾ãã
import style from 'style-loader!css-loader?modules&sourceMap!./style.css';
æ¥é è¾
webpackã§ã¯prefixã«ã¤ãããã®ã«ãã£ã¦å 容ãå¤åãã¾ãã
!
import style from '!style-loader!css-loader?modules&sourceMap!./style.css';
å
é ã«!
ãã¤ãããã¨ã«ãããwebpack.config.jsã«æ¸ããããã¼ãã¼ã®è¨å®ç¾¤ãç¡å¹ã«ãã¾ãã
!!
import style from '!!style-loader!css-loader?modules&sourceMap!./style.css';
å
é ã«!!
ãã¤ãããã¨ã«ããã!
ã«å ãenforce
ã®pre
, post
ãç¡å¹ã«ãã¾ãã
ãªã®ã§ããã®è¨äºã®ä¾ã ã¨./loader
ã®å¼ã³åºãã!!
ã«ãããã¨ã«ããç¡å¹ååºæ¥ã¾ãã
!!!...
ãã以éã¯ã¾ã äºç´ãªãã
-!
import style from '-!style-loader!css-loader?modules&sourceMap!./style.css';
å
é ã«-!
ãã¤ãããã¨ã«ããã!
ã«å ãenforce
ã®pre
ãç¡å¹åãã¾ãã(post
ã®ã¿åã)
ãã®è¨äºã®å ´åã./loader
ã¯post
ã§è¨å®ãããããå¼ã³åºããè¡ããã¾ãã
Match Resource
!=!
ã使ããã¨ã«ããããªã½ã¼ã¹ã®matchãè¡ãã¾ãã
import style from './style.js!=!style-loader!css-loader?modules&sourceMap!./style.css'
ãã®å ´åã¯ãcssã§å¦çãã¦ããã®ãå¼·å¶çã«jsã¨èªèããã¾ãã
ãªã®ã§ãå®éã«ã¯style.jsã¨ãããã¡ã¤ã«åã¯åå¨ãã¾ããã
ããã¯ä¸»ã«Module Typeãå¤æ´ããã¨ãã«ä½¿ããã¾ãã
ä¾ãã°ãjsonãã¡ã¤ã«ãjsã«å¤æãã¦ãªã«ãå¦çãç¶è¡ããå ´åçã
ä¸ã®ä¾ã§ä¸ããwebpack.config.jsãè¦ã¦ã¿ã¾ãã
module.exports = { mode: 'development', module: { ... { test: /\.css$/i, use: [require.resolve('./loader')], enforce: 'post', } ], }, };
// loader module.exports = function postLoader(res) { console.log(this.resource); return res; }
Match Resouceã¯å¤æå ã®æ¡å¼µåãwebpack.config.jsã¸æ¸¡ãããããã®å ´åã®ãã¼ãã¼ã®åºåã¯ä½ãåºã¾ããã
ãã¦ã以ä¸ã®ããã«ãã£ããããã®ãcssã§ã¯ãªãjsã«å¤æ´ãã¾ãã
module.exports = { mode: 'development', module: { ... { - test: /\.css$/i, + test: /\.js$/i, use: [require.resolve('./loader')], enforce: 'post', } ], }, };
ãã¼ãã¼ã®åºåã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
/webpack/src/index.js /webpack/src/style.css <----- !!!!! /webpack/node_modules/style-loader/dist/runtime/injectStylesIntoStyleTag.js /webpack/node_modules/css-loader/dist/runtime/api.js
ã§ã¦ãã¾ããããããä¸æè°ã ã¨ã¯æãã¾ãããtestã®æ¡ä»¶ã«ããããã¦ããªãããã«è¦ãã¾ãã
importã®ãã¡ã¤ã«åã¯.css
ãªã®ã«test: /\.js$/i
ã®ãã¼ãã«å
¥ã£ã¦ãããã¨ã¨ãªãã¾ãã
ãã®å ´åã¯ãmatch resouceã«ãã£ã¦å¤æããã¦ãããã¨ãèªã¿åãã¾ãã
æå¾ã«
è¦ããªãã¦ããã§ãã