[英]babel es5 to es6 converting less extension to css on imports
I wrote some npm scripts that builds the ./lib
directory before publish to npm. 在发布到npm之前,我编写了一些构建./lib
目录的npm脚本。
1. script is responsible to convert all es6
*.js
files in ./src/components/
to es5
syntax and then copy the files to ./lib
(same structure). 1.脚本负责将es6
所有es6
*.js
文件./src/components/
为es5
语法,然后将文件复制到./lib
(相同的结构)。
This is the script: 这是脚本:
"cross-env NODE_ENV=production babel ./src/components --out-dir ./lib --ignore spec.js --copy-files"
And this is the .babelrc
file: 这是.babelrc
文件:
{
"presets": [
"react",
"stage-1"
],
"env": {
"development": {
"presets": [
"latest",
"react-hmre"
]
},
"production": {
"presets": [
[
"latest",
"es2015"
]
],
"plugins": [
"transform-react-constant-elements",
"transform-react-remove-prop-types",
[
"transform-imports",
{
"react-bootstrap": {
"transform": "react-bootstrap/lib/${member}",
"preventFullImport": true
},
"lodash": {
"transform": "lodash/${member}",
"preventFullImport": true
}
}
]
]
},
"test": {
"presets": [
"latest"
]
}
}
}
I have another script that responsible to convert .less
files to .css
and copy them to ./lib
(same structure): 我还有另一个脚本负责将.less
文件转换为.css
并将它们复制到./lib
(相同结构):
"lessc-glob ./src/components/**/*.less lib"
Everything works well as expected, but i have one problem now. 一切都按预期工作,但我现在有一个问题。 The import
that i have inside the .js
files are referring to .less
files, but i need it to change to .css
extensions. 我在.js
文件中拥有的import
是指.less
文件,但我需要将其更改为.css
扩展名。
To make things clear, 为了清楚起见,
What i have now is: 我现在所拥有的是:
import css from './styles.less';
Converted into this: 转换为:
var _styles = require('./styles.less');
But i want it to convert to this: 但我希望将其转换为:
var _styles = require('./styles.css');
replace can be installed and utilized to find instances of .less and replace them with .css in your resultant ES5 .js
file/s. 可以安装replace并将其用于查找.less实例,然后在最终的ES5 .js
文件中将它们替换为.css 。
npm script npm脚本
Add a replace
script to your package.json
as follows: 如下所示将replace
脚本添加到package.json
中:
...
"scripts": {
...
"replace": "replace \".less\" \".css\" ./lib/components/ -r --include=\"*.js\""
},
...
A call to the replace
script can then be chained to the end your script that is responsible for converting all es6 *.js
files. 然后可以将对replace
脚本的调用链接到负责转换所有es6 *.js
文件的脚本的末尾。 Eg 例如
...
"scripts": {
...
"quux": "cross-env NODE_ENV=production babel ./src/components --out-dir ./lib --ignore spec.js --copy-files && npm run replace",
"replace": "replace \".less\" \".css\" ./lib/components/ -r --include=\"*.js\""
...
},
...
Note the && npm run replace
part added to the end of your current quux
script. 请注意 && npm run replace
部分已添加到当前quux
脚本的末尾。
I've assumed the components
folder is copied to the lib
folder too. 我假设components
文件夹也被复制到lib
文件夹。 If it's not then the ./lib/components/
part in the replace
script will need to be changed to ./lib/
. 如果不是,那么replace
脚本中的./lib/components/
部分将需要更改为./lib/
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.