简体   繁体   English

babel es5到es6在导入时将较少的扩展名转换为CSS

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM