@tarojs/rn-style-transformer 4.0.0-beta.75 → 4.0.0-beta.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/LICENSE +1 -8
  2. package/dist/index.d.ts +8 -0
  3. package/dist/index.js +3 -3
  4. package/dist/index.js.map +1 -1
  5. package/dist/transforms/StyleSheet/ColorPropType.d.ts +10 -0
  6. package/dist/transforms/StyleSheet/ColorPropType.js.map +1 -1
  7. package/dist/transforms/StyleSheet/ImageResizeMode.d.ts +6 -0
  8. package/dist/transforms/StyleSheet/ImageStylePropTypes.d.ts +128 -0
  9. package/dist/transforms/StyleSheet/ImageStylePropTypes.js +1 -1
  10. package/dist/transforms/StyleSheet/ImageStylePropTypes.js.map +1 -1
  11. package/dist/transforms/StyleSheet/LayoutPropTypes.d.ts +388 -0
  12. package/dist/transforms/StyleSheet/ShadowPropTypesIOS.d.ts +45 -0
  13. package/dist/transforms/StyleSheet/ShadowPropTypesIOS.js +1 -1
  14. package/dist/transforms/StyleSheet/ShadowPropTypesIOS.js.map +1 -1
  15. package/dist/transforms/StyleSheet/StyleSheetValidation.d.ts +15 -0
  16. package/dist/transforms/StyleSheet/StyleSheetValidation.js +1 -1
  17. package/dist/transforms/StyleSheet/StyleSheetValidation.js.map +1 -1
  18. package/dist/transforms/StyleSheet/TextStylePropTypes.d.ts +191 -0
  19. package/dist/transforms/StyleSheet/TextStylePropTypes.js +1 -1
  20. package/dist/transforms/StyleSheet/TextStylePropTypes.js.map +1 -1
  21. package/dist/transforms/StyleSheet/TransformPropTypes.d.ts +67 -0
  22. package/dist/transforms/StyleSheet/TransformPropTypes.js.map +1 -1
  23. package/dist/transforms/StyleSheet/ViewStylePropTypes.d.ts +136 -0
  24. package/dist/transforms/StyleSheet/ViewStylePropTypes.js +1 -1
  25. package/dist/transforms/StyleSheet/ViewStylePropTypes.js.map +1 -1
  26. package/dist/transforms/StyleSheet/deprecatedPropType.d.ts +13 -0
  27. package/dist/transforms/StyleSheet/deprecatedPropType.js.map +1 -1
  28. package/dist/transforms/StyleSheet/index.d.ts +2 -0
  29. package/dist/transforms/StyleSheet/normalizeColor.d.ts +10 -0
  30. package/dist/transforms/StyleSheet/normalizeColor.js.map +1 -1
  31. package/dist/transforms/index.d.ts +56 -0
  32. package/dist/transforms/index.js +5 -5
  33. package/dist/transforms/index.js.map +1 -1
  34. package/dist/transforms/less.d.ts +2 -0
  35. package/dist/transforms/less.js.map +1 -1
  36. package/dist/transforms/postcss.d.ts +13 -0
  37. package/dist/transforms/postcss.js +5 -1
  38. package/dist/transforms/postcss.js.map +1 -1
  39. package/dist/transforms/sass.d.ts +2 -0
  40. package/dist/transforms/sass.js.map +1 -1
  41. package/dist/transforms/stylus.d.ts +3 -0
  42. package/dist/transforms/stylus.js +1 -1
  43. package/dist/transforms/stylus.js.map +1 -1
  44. package/dist/types/index.d.ts +206 -0
  45. package/dist/utils/index.d.ts +21 -0
  46. package/dist/utils/index.js.map +1 -1
  47. package/dist/utils/lessImport.d.ts +5 -0
  48. package/dist/utils/reporterSkip.d.ts +7 -0
  49. package/dist/utils/reporterSkip.js.map +1 -1
  50. package/package.json +32 -31
  51. package/.eslintrc.js +0 -8
  52. package/__tests__/config.spec.js +0 -203
  53. package/__tests__/index.spec.js +0 -252
  54. package/__tests__/platform.spec.js +0 -78
  55. package/__tests__/styles/b.css +0 -3
  56. package/__tests__/styles/b.less +0 -3
  57. package/__tests__/styles/b.rn.css +0 -3
  58. package/__tests__/styles/b.scss +0 -3
  59. package/__tests__/styles/b.styl +0 -3
  60. package/__tests__/styles/c.css +0 -3
  61. package/__tests__/styles/c.less +0 -5
  62. package/__tests__/styles/c.rn.scss +0 -5
  63. package/__tests__/styles/c.scss +0 -5
  64. package/__tests__/styles/d.rn.scss +0 -3
  65. package/__tests__/styles/d.scss +0 -3
  66. package/__tests__/styles/mixins.scss +0 -18
  67. package/__tests__/styles/nest.rn.less +0 -3
  68. package/__tests__/styles/variable.scss +0 -1
  69. package/babel.config.js +0 -12
  70. package/jest.config.js +0 -8
  71. package/src/config/rn-stylelint.json +0 -5
  72. package/src/index.ts +0 -52
  73. package/src/transforms/StyleSheet/ColorPropType.ts +0 -76
  74. package/src/transforms/StyleSheet/ImageResizeMode.ts +0 -50
  75. package/src/transforms/StyleSheet/ImageStylePropTypes.ts +0 -62
  76. package/src/transforms/StyleSheet/LayoutPropTypes.ts +0 -559
  77. package/src/transforms/StyleSheet/ShadowPropTypesIOS.ts +0 -48
  78. package/src/transforms/StyleSheet/StyleSheetValidation.ts +0 -81
  79. package/src/transforms/StyleSheet/TextStylePropTypes.ts +0 -123
  80. package/src/transforms/StyleSheet/TransformPropTypes.ts +0 -109
  81. package/src/transforms/StyleSheet/ViewStylePropTypes.ts +0 -61
  82. package/src/transforms/StyleSheet/deprecatedPropType.ts +0 -31
  83. package/src/transforms/StyleSheet/index.ts +0 -5
  84. package/src/transforms/StyleSheet/normalizeColor.ts +0 -369
  85. package/src/transforms/index.ts +0 -229
  86. package/src/transforms/less.ts +0 -50
  87. package/src/transforms/postcss.ts +0 -95
  88. package/src/transforms/sass.ts +0 -148
  89. package/src/transforms/stylus.ts +0 -124
  90. package/src/types/index.ts +0 -242
  91. package/src/utils/index.ts +0 -229
  92. package/src/utils/lessImport.ts +0 -50
  93. package/src/utils/reporterSkip.ts +0 -27
  94. package/tsconfig.json +0 -12
@@ -1,229 +0,0 @@
1
- import { printLog, processTypeEnum, recursiveMerge } from '@tarojs/helper'
2
- import * as path from 'path'
3
- import transformCSS from 'taro-css-to-react-native'
4
-
5
- import { Config, PostcssConfig, RenderAdditionalResult, TransformOptions } from '../types'
6
- import { normalizeSourceMap } from '../utils'
7
- import lessTransform from './less'
8
- import postcssTransform, { makePostcssPlugins } from './postcss'
9
- import sassTransform from './sass'
10
- import { StyleSheetValidation } from './StyleSheet'
11
- import stylusTransform, { defaultOptions as stylusDefaultOptions } from './stylus'
12
-
13
- export function getWrapedCSS (css) {
14
- return `
15
- import { StyleSheet } from 'react-native'
16
- import { scalePx2dp, scaleVu2dp } from '@tarojs/runtime-rn'
17
-
18
- // 用来标识 rn-runner transformer 是否读写缓存
19
- function ignoreStyleFileCache() {}
20
-
21
- export default StyleSheet.create(${css})
22
- `
23
- }
24
-
25
- /**
26
- * 校验样式
27
- */
28
- function validateStyle ({ styleObject, filename }) {
29
- for (const name in styleObject) {
30
- try {
31
- StyleSheetValidation.validateStyle(name, styleObject)
32
- } catch (err) {
33
- // 先忽略掉 scalePx2dp 的报错
34
- if (/Invalid prop `.*` of type `string` supplied to `.*`, expected `number`[^]*/g.test(err.message)) return
35
- printLog(processTypeEnum.WARNING, err.message, `entry file: ${filename}`)
36
- }
37
- }
38
- }
39
- interface PostcssParam {
40
- css: string
41
- map: any
42
- filename: string
43
- additionalData: string
44
- postcssConfig: PostcssConfig
45
- transformOptions: TransformOptions
46
- }
47
-
48
- const designWidth = 750
49
-
50
- const deviceRatio = {
51
- 640: 2.34 / 2,
52
- 750: 1,
53
- 828: 1.81 / 2
54
- }
55
-
56
- const enum ProcessTypes {
57
- POSTCSS = 'postcss',
58
- SASS = 'sass',
59
- LESS = 'less',
60
- STYLUS = 'stylus',
61
- }
62
-
63
- const DEFAULT_RN_CONFIG = {
64
- [ProcessTypes.POSTCSS]: {
65
- options: {},
66
- scalable: true
67
- },
68
- [ProcessTypes.SASS]: {
69
- options: {}
70
- },
71
- [ProcessTypes.LESS]: {
72
- options: {}
73
- },
74
- [ProcessTypes.STYLUS]: {
75
- options: stylusDefaultOptions
76
- }
77
- }
78
-
79
- export default class StyleTransform {
80
- config: Config
81
-
82
- processConfigMap = new Map()
83
-
84
- constructor (config: Config) {
85
- this.init(config)
86
- }
87
-
88
- init = (config: Config) => {
89
- this.config = {
90
- designWidth: config.designWidth || designWidth,
91
- deviceRatio: config.deviceRatio || deviceRatio,
92
- sass: config.sass || {},
93
- alias: config.alias ?? {},
94
- rn: recursiveMerge({}, DEFAULT_RN_CONFIG, config.rn)
95
- }
96
-
97
- Reflect.ownKeys(this.config.rn).forEach((key: string) => {
98
- if (
99
- [
100
- ProcessTypes.SASS,
101
- ProcessTypes.LESS,
102
- ProcessTypes.STYLUS,
103
- ProcessTypes.POSTCSS
104
- ].includes(key.toLocaleLowerCase() as any)
105
- ) {
106
- const processConfig = {
107
- ...this.config.rn[key],
108
- alias: config.alias ?? {}
109
- }
110
- if (key.toLocaleLowerCase() === ProcessTypes.SASS) {
111
- processConfig.sass = this.config.sass
112
- }
113
- this.processConfigMap.set(key, processConfig)
114
- }
115
- })
116
- }
117
-
118
- /**
119
- * @description 样式文件处理
120
- * @param {string} src
121
- * @param {string} filename
122
- * @param {object} options
123
- */
124
- async processStyle (src: string, filename: string, options: TransformOptions) {
125
- let result: undefined | RenderAdditionalResult
126
- let css = src
127
- let map: undefined | string
128
- let additionalData = ''
129
- const ext = path.extname(filename)
130
- if (/.less$/i.test(ext)) {
131
- result = await lessTransform(src, filename, this.processConfigMap.get(ProcessTypes.LESS))
132
- } else if (/.s(c|a)ss$/i.test(ext)) {
133
- result = await sassTransform(src, filename, this.processConfigMap.get(ProcessTypes.SASS), options)
134
- } else if (/.styl(us)?$/i.test(ext)) {
135
- result = await stylusTransform(src, filename, this.processConfigMap.get(ProcessTypes.STYLUS))
136
- }
137
-
138
- if (result) {
139
- css = Buffer.isBuffer(result.css) ? result.css.toString() : result.css
140
- map = Buffer.isBuffer(result.map) ? result.map.toString() : result.map
141
- additionalData = result.additionalData
142
- }
143
-
144
- // postcss 插件,比如处理平台特有样式,单位转换
145
- return await this.postCSS({
146
- css,
147
- map,
148
- filename,
149
- additionalData,
150
- transformOptions: options,
151
- postcssConfig: this.processConfigMap.get(ProcessTypes.POSTCSS)
152
- })
153
- }
154
-
155
- /**
156
- * postcss处理
157
- * @param param0 PostcssParam
158
- * @returns {Promise | any}
159
- */
160
- postCSS ({
161
- css,
162
- map,
163
- filename,
164
- postcssConfig,
165
- transformOptions,
166
- additionalData
167
- }: PostcssParam) {
168
- const plugins = makePostcssPlugins({
169
- filename,
170
- postcssConfig,
171
- transformOptions,
172
- designWidth: this.config.designWidth,
173
- deviceRatio: this.config.deviceRatio,
174
- additionalData: additionalData
175
- })
176
-
177
- return postcssTransform(
178
- css,
179
- filename,
180
- {
181
- plugins,
182
- options: {
183
- ...postcssConfig.options,
184
- map: map && {
185
- prev: normalizeSourceMap(map, filename),
186
- inline: false,
187
- annotation: false
188
- }
189
- }
190
- }
191
- ).then(result => {
192
- return {
193
- ...result,
194
- filename
195
- }
196
- })
197
- }
198
-
199
- /**
200
- * @description 处理样式入口
201
- * @param {string} src
202
- * @param {string} filename
203
- * @param {object} transform
204
- * @return {string} JSONString
205
- */
206
- async transform (src: string, filename: string, options: TransformOptions) {
207
- // printLog(processTypeEnum.START, '样式文件处理开始', filename)
208
- const result = await this.processStyle(src, filename, options)
209
-
210
- // 把 css 转换成对象 rn 的样式,接入 taro 的 css-to-react-native,比如有单位的处理
211
- const styleObject = transformCSS(
212
- result.css,
213
- {
214
- parseMediaQueries: true,
215
- scalable: this.config.rn.postcss?.scalable
216
- }
217
- )
218
-
219
- // stylelint,转换成对象,对对象进行校验
220
- validateStyle({ styleObject, filename })
221
- const css = JSON.stringify(styleObject, null, 2)
222
- .replace(/"(scalePx2dp\(.*?\))"/g, '$1')
223
- .replace(/"(scaleVu2dp\(.*?\))"/g, '$1')
224
- .replace(/\\\\/g, '\\')
225
-
226
- // 注入自适应方法 scalePx2dp
227
- return getWrapedCSS(css)
228
- }
229
- }
@@ -1,50 +0,0 @@
1
- import less from 'less'
2
- import * as path from 'path'
3
-
4
- import { LessConfig, RenderAdditionalResult, RenderResult } from '../types'
5
- import { getAdditionalData, insertBefore } from '../utils'
6
- import makeLessImport from '../utils/lessImport'
7
-
8
- function renderToCSS (src, filename, options = {} as any) {
9
- // default plugins
10
- const plugins = [makeLessImport(options)]
11
- // default paths set current filePath
12
- const paths = [path.dirname(path.resolve(process.cwd(), filename))]
13
- return new Promise((resolve, reject) => {
14
- less
15
- .render(src, {
16
- ...options,
17
- filename,
18
- plugins: plugins.concat(options.plugins || []),
19
- paths: paths.concat(options.paths || [])
20
- }, (err, result) => {
21
- if (err) {
22
- return reject(err.message)
23
- }
24
- resolve(result)
25
- })
26
- })
27
- }
28
-
29
- export default function transform (
30
- src: string,
31
- filename: string,
32
- config: LessConfig
33
- ) {
34
- const additionalData = getAdditionalData(src, config.additionalData)
35
- const data = insertBefore(src, additionalData)
36
-
37
- return renderToCSS(
38
- data,
39
- filename,
40
- {
41
- sourceMap: {
42
- outputFilename: `${filename}.map`
43
- },
44
- alias: config.alias,
45
- ...config.options
46
- }
47
- ).then((result: RenderResult) => {
48
- return { ...result, additionalData } as RenderAdditionalResult
49
- })
50
- }
@@ -1,95 +0,0 @@
1
- import { isNpmPkg, printLog, processTypeEnum, recursiveMerge, resolveSync } from '@tarojs/helper'
2
- import * as path from 'path'
3
- import postcss from 'postcss'
4
- import postcssImport from 'postcss-import'
5
- import pxtransform from 'postcss-pxtransform'
6
-
7
- import stylelintConfig from '../config/rn-stylelint.json'
8
- import { resolveStyle } from '../utils'
9
- import reporterSkip from '../utils/reporterSkip'
10
-
11
- const defaultPxtransformOption: {
12
- [key: string]: any
13
- } = {
14
- enable: true,
15
- config: {
16
- platform: 'rn'
17
- }
18
- }
19
-
20
- export function makePostcssPlugins ({
21
- filename,
22
- designWidth,
23
- deviceRatio,
24
- postcssConfig,
25
- transformOptions,
26
- additionalData
27
- }) {
28
- const optionsWithDefaults = ['pxtransform', 'postcss-import', 'postcss-reporter', 'stylelint', 'cssModules']
29
-
30
- if (designWidth) {
31
- defaultPxtransformOption.config.designWidth = designWidth
32
- }
33
-
34
- if (deviceRatio) {
35
- defaultPxtransformOption.config.deviceRatio = deviceRatio
36
- }
37
- const pxtransformOption = recursiveMerge({}, defaultPxtransformOption, postcssConfig.pxtransform)
38
-
39
- const plugins = [
40
- postcssImport({
41
- resolve: function resolve (id: string, basedir: string, options: postcssImport.AtImportOptions) {
42
- return resolveStyle(
43
- id,
44
- {
45
- ...options,
46
- basedir,
47
- defaultExt: '.css', // 省略后缀则默认 `.css`
48
- alias: postcssConfig.alias,
49
- platform: transformOptions.platform
50
- }
51
- )
52
- }
53
- })
54
- ]
55
-
56
- if (pxtransformOption.enable) {
57
- plugins.push(pxtransform(pxtransformOption.config))
58
- }
59
-
60
- const skipRows = additionalData ? additionalData.split('\n').length : 0
61
-
62
- plugins.push(
63
- require('stylelint')(stylelintConfig),
64
- // @ts-ignore
65
- reporterSkip({ skipRows, filename }),
66
- require('postcss-reporter')({ clearReportedMessages: true })
67
- )
68
-
69
- Object.entries(postcssConfig).forEach(([pluginName, pluginOption]) => {
70
- if (optionsWithDefaults.indexOf(pluginName) > -1) return
71
- if (!pluginOption || !(pluginOption as any).enable) return
72
-
73
- if (!isNpmPkg(pluginName)) { // local plugin
74
- pluginName = path.join(process.cwd(), pluginName)
75
- }
76
-
77
- try {
78
- const pluginPath = resolveSync(pluginName, { basedir: process.cwd() }) || ''
79
- plugins.push(require(pluginPath)((pluginOption as any).config || {}))
80
- } catch (e) {
81
- const msg = e.code === 'MODULE_NOT_FOUND' ? `缺少postcss插件${pluginName}, 已忽略` : e
82
- printLog(msg, processTypeEnum.WARNING)
83
- }
84
- })
85
-
86
- return plugins
87
- }
88
-
89
- export default function transform (src: string, filename: string, { options, plugins }) {
90
- return postcss(plugins)
91
- .process(src, { from: filename, ...options })
92
- .then(result => {
93
- return result
94
- })
95
- }
@@ -1,148 +0,0 @@
1
- import * as fs from 'fs'
2
- import * as path from 'path'
3
-
4
- import { RenderAdditionalResult, RenderResult, SassConfig, SassGlobalConfig, TransformOptions } from '../types'
5
- import { getAdditionalData, insertAfter, insertBefore, resolveStyle, sortStyle } from '../utils'
6
-
7
- /**
8
- * 用过用户手动安装了 node-sass,启用node-sass,默认使用 sass
9
- */
10
- function getSassImplementation () {
11
- let sassImplPkg = 'node-sass'
12
-
13
- try {
14
- require.resolve('node-sass')
15
- } catch (error) {
16
- try {
17
- require.resolve('sass')
18
- sassImplPkg = 'sass'
19
- } catch (ignoreError) {
20
- sassImplPkg = 'sass'
21
- }
22
- }
23
-
24
- return require(sassImplPkg)
25
- }
26
-
27
- const sassImplementation = getSassImplementation()
28
-
29
- function makeURL (resource: string, rootDir: string) {
30
- const url = path.resolve(rootDir, resource)
31
- if (!fs.existsSync(url)) {
32
- throw new Error(`全局注入 scss 文件路径错误: ${url}`)
33
- }
34
- return url
35
- }
36
-
37
- function makeImportStatement (filePath: string, resource: string, rootDir: string) {
38
- const url = makeURL(resource, rootDir)
39
- const relativePath = path.relative(filePath, url).replace(/\\/g, '/') // fix window path error
40
- return `@import './${relativePath}'`
41
- }
42
-
43
- function getGlobalResource (filename: string, config: SassGlobalConfig) {
44
- let resource = ''
45
- const projectDirectory = config.projectDirectory || process.cwd()
46
- const filePath = path.dirname(path.resolve(projectDirectory, filename))
47
- if (typeof config.resource === 'string') {
48
- resource = makeImportStatement(filePath, config.resource, projectDirectory)
49
- }
50
- if (Array.isArray(config.resource)) {
51
- const resources = config.resource?.map(source => makeImportStatement(filePath, source, projectDirectory)) || []
52
- resource = resources.join(';\n')
53
- }
54
- // https://taro-docs.jd.com/taro/docs/config-detail/#sassdata, data 覆盖 resurce 配置
55
- return insertAfter(resource, config?.data)
56
- }
57
-
58
- function combineResource (src: string, filename: string, config: SassConfig) {
59
- // sass config
60
- const globalResource = getGlobalResource(filename, config.sass || {})
61
-
62
- // sass tranform config
63
- const additionalData = getAdditionalData(src, config.additionalData)
64
-
65
- return insertAfter(globalResource, additionalData)
66
- }
67
-
68
- function renderToCSS (src, filename, options, transformOptions: TransformOptions) {
69
- const defaultOpts = {
70
- importer: function (...params) { /* url, prev, done */
71
- let [url, prev] = params
72
- // url is the path in import as is, which LibSass encountered.
73
- // prev is the previously resolved path.
74
- // done is an optional callback, either consume it or return value synchronously.
75
- // this.options contains this options hash, this.callback contains the node-style callback
76
- let basedir = ''
77
- let defaultExt = ''
78
- if (path.isAbsolute(prev)) {
79
- ({ dir: basedir, ext: defaultExt } = path.parse(prev))
80
- } else {
81
- ({ dir: basedir, ext: defaultExt } = path.parse(path.resolve(process.cwd(), filename)))
82
- }
83
-
84
- // 外部 sass importer 配置
85
- if (typeof options.importer === 'function') {
86
- ({ file: url } = options.importer(params))
87
- }
88
-
89
- try {
90
- const file = resolveStyle(
91
- url,
92
- {
93
- basedir,
94
- defaultExt,
95
- alias: options.alias,
96
- platform: transformOptions.platform
97
- }
98
- )
99
- const contents = fs.readFileSync(file, 'utf8')
100
- return { file, contents }
101
- } catch (err) {
102
- return err
103
- }
104
- }
105
- }
106
-
107
- const opts = { ...options, ...defaultOpts, data: src }
108
-
109
- return new Promise((resolve, reject) => {
110
- sassImplementation.render(opts, (err, result) => {
111
- if (err) {
112
- reject(err)
113
- } else {
114
- resolve(result)
115
- }
116
- })
117
- })
118
- }
119
-
120
- export default function transform (
121
- src: string,
122
- filename: string,
123
- config: SassConfig,
124
- transformOptions: TransformOptions
125
- ) {
126
- const additionalData = combineResource(src, filename, config)
127
- let data = insertBefore(src, additionalData)
128
- data = sortStyle(data)
129
-
130
- if (!data) {
131
- data = `\n${data}` // fix empty file error. reference https://github.com/sass/node-sass/blob/91c40a0bf0a3923ab9f91b82dcd479c25486235a/lib/index.js#L430
132
- }
133
-
134
- return renderToCSS(
135
- data,
136
- filename,
137
- {
138
- // file: filename, // fix[issues/11983]: with file option,dart-sass importer donot execute。
139
- outFile: `${filename}.map`,
140
- sourceMap: true, // If no outFile is set, sourceMap parameter is ignored.
141
- alias: config.alias,
142
- ...config.options
143
- },
144
- transformOptions
145
- ).then((result: RenderResult) => {
146
- return { ...result, additionalData } as RenderAdditionalResult
147
- })
148
- }
@@ -1,124 +0,0 @@
1
- import stylus from 'stylus'
2
-
3
- import { RenderAdditionalResult, RenderResult, StylusConfig, StylusRenderOptions } from '../types'
4
- import { getAdditionalData, insertBefore } from '../utils'
5
-
6
- export const defaultOptions: StylusRenderOptions = {
7
- use: [],
8
- include: [],
9
- import: [],
10
- define: {},
11
- includeCSS: false,
12
- // resolveURL: { nocheck: false },
13
- lineNumbers: false,
14
- hoistAtrules: false,
15
- disableCache: false
16
- }
17
-
18
- function renderToCSS (src, filename, options = {} as StylusRenderOptions) {
19
- const stylusOptions = { filename, ...options }
20
- const styl = stylus(src, stylusOptions)
21
-
22
- styl.set(
23
- 'sourcemap',
24
- {
25
- comment: true,
26
- sourceRoot: '.', // stylusOptions.dest
27
- basePath: '.'
28
- }
29
- )
30
-
31
- // include regular CSS on @import
32
- if (stylusOptions.includeCSS) {
33
- styl.set('include css', true)
34
- }
35
-
36
- if (stylusOptions.hoistAtrules) {
37
- styl.set('hoist atrules', true)
38
- }
39
-
40
- if (stylusOptions.lineNumbers) {
41
- styl.set('linenos', true)
42
- }
43
-
44
- if (stylusOptions.disableCache) {
45
- styl.set('cache', false)
46
- }
47
-
48
- if (
49
- typeof stylusOptions.use !== 'undefined' &&
50
- stylusOptions.use.length > 0
51
- ) {
52
- let { length } = stylusOptions.use
53
-
54
- // eslint-disable-next-line no-plusplus
55
- while (length--) {
56
- const [item] = stylusOptions.use.splice(length, 1)
57
- let useIn: any = item
58
- if (typeof item === 'string') {
59
- try {
60
- const resolved = require.resolve(item)
61
-
62
- // eslint-disable-next-line import/no-dynamic-require, global-require
63
- useIn = require(resolved)(stylusOptions)
64
- } catch (error) {
65
- const msg = `Failed to load "${item}" Stylus plugin.Are you sure it's installed?\n${error}`
66
- throw new Error(msg)
67
- }
68
- }
69
- styl.use(useIn)
70
- }
71
- }
72
-
73
- if (typeof stylusOptions.import !== 'undefined') {
74
- for (const imported of stylusOptions.import) {
75
- styl.import(imported)
76
- }
77
- }
78
-
79
- if (typeof stylusOptions.include !== 'undefined') {
80
- for (const included of stylusOptions.include) {
81
- styl.include(included)
82
- }
83
- }
84
-
85
- // if (stylusOptions.resolveURL !== false) {
86
- // styl.define('url', urlResolver(stylusOptions.resolveURL))
87
- // }
88
-
89
- if (typeof stylusOptions.define !== 'undefined') {
90
- const definitions = Array.isArray(stylusOptions.define)
91
- ? stylusOptions.define
92
- : Object.entries(stylusOptions.define)
93
-
94
- for (const defined of definitions) {
95
- styl.define.apply(null, defined)
96
- }
97
- }
98
-
99
- return new Promise((resolve, reject) => {
100
- styl.render((err, css) => {
101
- if (err) {
102
- reject(err)
103
- } else {
104
- resolve({
105
- css,
106
- map: styl.sourcemap
107
- })
108
- }
109
- })
110
- })
111
- }
112
-
113
- export default function transform (src: string, filename: string, config: StylusConfig) {
114
- const additionalData = getAdditionalData(src, config.additionalData)
115
- const data = insertBefore(src, additionalData)
116
-
117
- return renderToCSS(
118
- data,
119
- filename,
120
- config.options
121
- ).then((result: RenderResult) => {
122
- return { ...result, additionalData } as RenderAdditionalResult
123
- })
124
- }