【发布时间】:2017-09-08 15:07:27
【问题描述】:
我正在尝试向 npm 发布一个反应库,但在我的本地 npm 构建中,由于 scss 文件而出现错误。我正在使用 babel 转译为 es5,但最终文件仍然需要 scss 文件。
一个例子是我原来的console.js文件有以下导入:
import './console.scss'
导致错误的我转译的console.js 文件包含以下内容:
require('./console.scss')
我收到一个错误:Error: Cannot find module './console.scss'
我正在通过创建一个本地 npm 包来测试它。我的流程是:
npm run build
npm run build:babel
npm pack
然后我将包安装到另一个项目中以对其进行测试,但我在其中遇到了错误。
这是我的 package.json:
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"start": "nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"build-watch": "webpack -w",
"build-babel": "babel src --presets babel-preset-es2015 --out-dir dist"
},
"author": "",
"license": "ISC",
"babel": {
"presets": [
"es2015",
"react"
]
},
"dependencies": {
"react": "^15.5.4",
"react-dom": "^15.5.4"
},
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.24.1",
"babel-core": "^6.24.1",
"babel-loader": "^6.4.1",
"babel-plugin-css-modules-transform": "^1.2.7",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.0",
"express": "^4.15.2",
"html-loader": "^0.4.5",
"node-sass": "^4.5.2",
"node-sass-loader": "^0.1.7",
"nodemon": "^1.11.0",
"path": "^0.12.7",
"sass-loader": "^6.0.3",
"style-loader": "^0.16.1",
"webpack": "^2.3.3"
}
}
这是我的 webpack 配置:
'use strict';
const webpack = require('webpack')
const path = require('path')
module.exports = {
entry: "./src/indexLocal.js",
output: {
path: path.join(__dirname, 'src'),
filename: 'bundle.js',
},
module: {
loaders: [
{
test: /.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015'],
},
},
{
test: /\.scss$/,
loaders: ['style-loader', 'css-loader', 'sass-loader'],
}
,{
test: /.html$/,
loader: 'html-loader',
query: {
minimize: true
}
}
]
}
};
如何确保我的 npm 包包含原始样式,而不会因导入 scss 文件而出错? 干杯
【问题讨论】:
-
你的 npm 包是用来发布 Webpack 包的,还是一个普通的结构化目录?你有两个独立的构建工具,不清楚为什么两者都会影响这个问题。如果您使用
babel的CLI 进行发布,那么没有任何东西可以为Node 编译.scss文件。 -
我的 npm 包应该发布一个普通的结构化目录。 webpack 构建是针对本地开发的,但我想如果有人想看的话,我会把它包含在问题中。我将使用什么来编译 Node 的 .scss 文件?
标签: npm sass webpack ecmascript-6 babeljs