Merge branch '2.x' into dev

This commit is contained in:
chenjiahan
2020-12-03 17:19:17 +08:00
7 changed files with 39 additions and 5 deletions

View File

@@ -38,7 +38,7 @@ export function getCssBaseFile() {
const IMPORT_STYLE_RE = /import\s+?(?:(?:".*?")|(?:'.*?'))[\s]*?(?:;|$|)/g;
// "import 'a.less';" => "import 'a.css';"
export function replaceCssImport(code: string) {
export function replaceCssImportExt(code: string) {
return code.replace(IMPORT_STYLE_RE, str =>
str.replace(`.${CSS_LANG}`, '.css')
);

View File

@@ -1,16 +1,18 @@
import { transformAsync } from '@babel/core';
import { readFileSync, removeSync, outputFileSync } from 'fs-extra';
import { replaceExt } from '../common';
import { replaceCssImport } from '../common/css';
import { replaceCssImportExt } from '../common/css';
import { replaceScriptImportExt } from './get-deps';
export function compileJs(filePath: string): Promise<void> {
return new Promise((resolve, reject) => {
let code = readFileSync(filePath, 'utf-8');
code = replaceCssImport(code);
code = replaceCssImportExt(code);
code = replaceScriptImportExt(code, '.vue', '');
transformAsync(code, { filename: filePath })
.then(result => {
.then((result) => {
if (result) {
const jsFilePath = replaceExt(filePath, '.js');

View File

@@ -6,7 +6,7 @@ let depsMap: Record<string, string[]> = {};
let existsCache: Record<string, boolean> = {};
// https://regexr.com/47jlq
const IMPORT_RE = /import\s+?(?:(?:(?:[\w*\s{},]*)\s+from\s+?)|)(?:(?:".*?")|(?:'.*?'))[\s]*?(?:;|$|)/g;
const IMPORT_RE = /import\s+?(?:(?:(?:[\w*\s{},]*)\s+from(\s+)?)|)(?:(?:".*?")|(?:'.*?'))[\s]*?(?:;|$|)/g;
function matchImports(code: string): string[] {
return code.match(IMPORT_RE) || [];
@@ -71,3 +71,15 @@ export function getDeps(filePath: string) {
return paths;
}
// "import App from 'App.vue';" => "import App from 'App.xxx';"
export function replaceScriptImportExt(code: string, from: string, to: string) {
const importLines = matchImports(code);
importLines.forEach(importLine => {
const result = importLine.replace(from, to);
code = code.replace(importLine, result);
});
return code;
}

View File

@@ -39,6 +39,7 @@ export function getSiteDevBaseConfig(): WebpackConfig {
const siteConfig = getSiteConfig();
const title = getTitle(siteConfig);
const { htmlPluginOptions } = vantConfig.site;
return merge(baseConfig as any, {
entry: {
@@ -88,6 +89,7 @@ export function getSiteDevBaseConfig(): WebpackConfig {
template: join(__dirname, '../../site/desktop/index.html'),
filename: 'index.html',
baiduAnalytics,
...htmlPluginOptions,
}),
new HtmlWebpackPlugin({
title,
@@ -97,6 +99,7 @@ export function getSiteDevBaseConfig(): WebpackConfig {
template: join(__dirname, '../../site/mobile/index.html'),
filename: 'mobile.html',
baiduAnalytics,
...htmlPluginOptions,
}),
],
});