UNPKG

3.52 kBJavaScriptView Raw
1///@ts-check
2'use strict';
3
4const path = require('path');
5const fs = require('fs');
6const npm = require('./npm-dependency');
7
8/**
9 * 加载npm 默认导出 scss/css 样式
10 * @param {string} moduleName
11 * @returns {string|null}
12 */
13function importNpmModule(moduleName) {
14 // 直接引用包名
15 const modulePath = path.join(process.cwd(), 'node_modules', moduleName);
16
17 // 自动提取 sass/wxss/style 字段
18 const pkg = npm.loadPackage(modulePath);
19 const key = [
20 'sass',
21 'scss',
22 // 'wxss',
23 'style'
24 ].find(function (key) {
25 return typeof pkg[key] === "string";
26 });
27 if (key || (pkg.main && !pkg.main.endsWith('.js'))) {
28 // console.log(key, pkg.main, path.join(modulePath, pkg[key || 'main']));
29 return path.join(modulePath, pkg[key || 'main']);
30 }
31
32 // 无有效字段自动搜索 index|style.[scss|sass|wxss|csss]
33 const name = [
34 'index.scss', 'index.sass', 'index.wxss', 'index.css',
35 'style.scss', 'style.sass', 'style.wxss', 'style.css',
36 ].find(name => fs.existsSync(path.join(modulePath, name)));
37 if (name) {
38 return path.join(modulePath, name);
39 }
40
41 // 尝试搜索 bower.json
42 const bower = fs.existsSync(path.join(modulePath, 'bower.json')) && npm.loadPackage(modulePath, 'bower.json');
43 if (bower) {
44 const key = ['sass', 'scss', 'wxss', 'css', 'style'].find(key => typeof bower[key] === "string");
45 if (key || (bower.main && !bower.main.endsWith('.js'))) {
46 return path.join(modulePath, bower[key || 'main'])
47 }
48 }
49}
50
51/**
52 * 自动创建scss文件
53 * @param {string} file
54 */
55function syncFile(file) {
56 // console.log(file);
57 if (
58 !['.css', '.scss', '.sass'].includes(path.extname(file)) &&
59 !fs.existsSync(file + '.scss') &&
60 !fs.existsSync(file + '.css') &&
61 !fs.existsSync(file + '.sass')
62 ) {
63 fs.copyFileSync(file, file + '.scss');
64 }
65 return file;
66
67}
68
69/**
70 * @param {string} url,
71 * @param {string} prev,
72 * @param {function} done,
73 */
74module.exports = function importer(url, prev, done) {
75 // console.log('import', url, prev);
76 if (url[0] === '~' && url[1] !== '/') {
77 // import from node_modules
78 const npmModule = url.substr(1);
79 const n = npmModule.split('/').length;
80 if (n === 1 || (n === 2 && npmModule[0] === '@')) {
81 // 直接引用包名
82 const file = importNpmModule(npmModule);
83 if (file) {
84 return {
85 file: syncFile(file),
86 }
87 }
88 } else {
89 const file = path.join(process.cwd(), 'node_modules', npmModule);
90 return { file: syncFile(file) }
91 }
92 } else if (prev && prev.indexOf('node_modules') >= 0) {
93 // inner import of node_modules
94 // 引用的 node_modules 再次引用其他文件
95 const file = path.resolve(path.dirname(prev), url);
96 return { file: syncFile(file) }
97 } else if (url.endsWith(".wxss") && !fs.existsSync(url + '.scss') && !fs.existsSync(url + '.css') && !fs.existsSync(url + '.sass')) {
98 // 引用本地wxss 并保持不变
99 // keep import
100 // console.log(url);
101 // dart-sass not support
102 if (url.startsWith("file://")) {
103 // fixed dart-sass root file name
104 url = url.substr(7)
105 }
106 return { contents: '@import url("' + url.trim() + '");' };
107
108 } else {
109 return null;
110 }
111};
\No newline at end of file