Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

小程序用到的静态资源走CDN 和 防止node_modules/xxxx包被taro再次编译的bug #3508

Merged
merged 7 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/taro-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"chokidar": "^2.0.3",
"commander": "^2.19.0",
"cross-spawn": "^6.0.5",
"crypto": "^1.0.1",
"css-to-react-native-transform": "^1.4.0",
"ejs": "^2.6.1",
"envinfo": "^6.0.1",
Expand Down Expand Up @@ -98,6 +99,7 @@
"through2": "^2.0.3",
"vinyl": "^2.1.0",
"vinyl-fs": "^3.0.2",
"xxhashjs": "^0.2.2",
"yauzl": "2.10.0"
},
"devDependencies": {
Expand Down
25 changes: 18 additions & 7 deletions packages/taro-cli/src/mini/astProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import generate from 'babel-generator'
import traverse, { NodePath } from 'babel-traverse'
import * as _ from 'lodash'
import { Config as IConfig } from '@tarojs/taro'
import getHashName from '../util/hash';

const template = require('babel-template')

Expand Down Expand Up @@ -96,8 +97,10 @@ function analyzeImportUrl ({
npmOutputDir,
sourceDir,
outputDir,
npmConfig
npmConfig,
projectConfig
} = getBuildData()
const publicPath = (projectConfig.weapp || ({} as any)).publicPath;
if (value.indexOf('.') === 0) {
let importPath = path.resolve(path.dirname(sourceFilePath), value)
importPath = resolveScriptPath(importPath)
Expand Down Expand Up @@ -159,8 +162,11 @@ function analyzeImportUrl ({
showPath = vpath.replace(nodeModulesPath, `/${npmConfig.name}`)
} else {
showPath = vpath.replace(sourceDir, '')
if (publicPath) {
const hashName = getHashName(vpath);
showPath = (/\/$/.test(publicPath) ? publicPath : publicPath + '/') + hashName;
}
}

if (defaultSpecifier) {
astPath.replaceWith(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(defaultSpecifier), t.stringLiteral(showPath.replace(/\\/g, '/')))]))
} else {
Expand Down Expand Up @@ -243,9 +249,10 @@ export function parseAst (
isProduction,
npmConfig,
alias: pathAlias,
compileInclude,
compileConfig,
projectConfig
} = getBuildData()
const publicPath = (projectConfig.weapp || {} as any).publicPath;
const notExistNpmList = getNotExistNpmList()
const taroMiniAppFramework = `@tarojs/taro-${buildAdapter}`
let configObj: IConfig = {}
Expand Down Expand Up @@ -433,7 +440,7 @@ export function parseAst (
// alias 替换
if (isAliasPath(value, pathAlias)) {
value = replaceAliasPath(sourceFilePath, value, pathAlias)
source.value = value
source.value = value;
}
if (isNpmPkg(value) && !isQuickAppPkg(value) && !notExistNpmList.has(value)) {
if (value === taroJsComponents) {
Expand Down Expand Up @@ -491,7 +498,7 @@ export function parseAst (
buildAdapter,
root: appPath,
npmOutputDir,
compileInclude,
compileConfig,
env: projectConfig.env || {},
uglify: projectConfig!.plugins!.uglify || { enable: true },
babelConfig: getBabelConfig(projectConfig!.plugins!.babel) || {}
Expand Down Expand Up @@ -597,7 +604,7 @@ export function parseAst (
buildAdapter,
root: appPath,
npmOutputDir,
compileInclude,
compileConfig,
env: projectConfig.env || {},
uglify: projectConfig!.plugins!.uglify || { enable: true },
babelConfig: getBabelConfig(projectConfig!.plugins!.babel) || {}
Expand Down Expand Up @@ -840,6 +847,10 @@ export function parseAst (
showPath = vpath.replace(nodeModulesPath, `/${npmConfig.name}`)
} else {
showPath = vpath.replace(sourceDir, '')
if (publicPath) {
const hashName = getHashName(vpath);
showPath = (/\/$/.test(publicPath) ? publicPath : publicPath + '/') + hashName;
}
}
astPath.replaceWith(t.stringLiteral(showPath.replace(/\\/g, '/')))
} else {
Expand Down Expand Up @@ -893,7 +904,7 @@ export function parseAst (
buildAdapter,
root: appPath,
npmOutputDir,
compileInclude,
compileConfig,
env: projectConfig.env || {},
uglify: projectConfig!.plugins!.uglify || { enable: true },
babelConfig: getBabelConfig(projectConfig!.plugins!.babel) || {}
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli/src/mini/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function compileDepScripts (scriptFiles: string[], needUseBabel?: boolean
}
const weappConf = Object.assign({}, projectConfig.weapp)
const useCompileConf = Object.assign({}, weappConf.compile)
const compileExclude = useCompileConf.exclude || []
const compileExclude = (useCompileConf.exclude || []).filter(item => !/(?:\/|^)node_modules(\/|$)/.test(item));
let isInCompileExclude = false
compileExclude.forEach(excludeItem => {
if (item.indexOf(path.join(appPath, excludeItem)) >= 0) {
Expand Down
52 changes: 35 additions & 17 deletions packages/taro-cli/src/mini/compileStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from 'path'
import * as autoprefixer from 'autoprefixer'
import * as postcss from 'postcss'
import * as pxtransform from 'postcss-pxtransform'

import getHashName from '../util/hash';
import browserList from '../config/browser_list'
import {
resolveNpmPkgMainPath,
Expand Down Expand Up @@ -106,7 +106,7 @@ export function processStyleUseCssModule (styleObj: IStyleObj): any {

async function processStyleWithPostCSS (styleObj: IStyleObj): Promise<string> {
const { appPath, projectConfig, npmConfig, isProduction, buildAdapter } = getBuildData()
const weappConf = Object.assign({}, projectConfig.weapp)
const weappConf = Object.assign({}, projectConfig[buildAdapter])
const useModuleConf = weappConf.module || {}
const customPostcssConf = useModuleConf.postcss || {}
const customCssModulesConf = Object.assign({
Expand All @@ -119,12 +119,13 @@ async function processStyleWithPostCSS (styleObj: IStyleObj): Promise<string> {
enable: true,
config: {}
}, customPostcssConf.pxtransform || {})
const customUrlConf = Object.assign({
const customUrlConf = {
enable: true,
config: {
limit: 10240
}
}, customPostcssConf.url || {})
} as any,
...customPostcssConf.url
};
const customAutoprefixerConf = Object.assign({
enable: true,
config: {
Expand All @@ -139,8 +140,8 @@ async function processStyleWithPostCSS (styleObj: IStyleObj): Promise<string> {
if (projectConfig.hasOwnProperty(DEVICE_RATIO_NAME)) {
postcssPxtransformOption[DEVICE_RATIO_NAME] = projectConfig.deviceRatio
}
const cssUrlConf = Object.assign({ limit: 10240 }, customUrlConf)
const maxSize = Math.round((customUrlConf.config.limit || cssUrlConf.limit) / 1024)

const maxSize = (customUrlConf.config.limit || 1024) / 1024
const postcssPxtransformConf = Object.assign({}, postcssPxtransformOption, customPxtransformConf, customPxtransformConf.config)
const processors: any[] = []
if (customAutoprefixerConf.enable) {
Expand All @@ -149,15 +150,33 @@ async function processStyleWithPostCSS (styleObj: IStyleObj): Promise<string> {
if (customPxtransformConf.enable && buildAdapter !== BUILD_TYPES.QUICKAPP) {
processors.push(pxtransform(postcssPxtransformConf))
}
if (cssUrlConf.enable) {
const cssUrlParseConf = {
url: 'inline',
maxSize,
encodeType: 'base64'
if (customUrlConf.enable) {
let inlineOpts = {};
const url = customUrlConf.config.url || 'inline';
if (url === 'inline' && !weappConf.publicPath) {
inlineOpts = {
encodeType: 'base64',
maxSize,
url
};
}
processors.push(cssUrlParse(cssUrlConf.config.basePath ? Object.assign(cssUrlParseConf, {
basePath: cssUrlConf.config.basePath
}) : cssUrlParseConf))

if (weappConf.publicPath && typeof url !== 'function') {
customUrlConf.config.url = (assets) => {
if (/\./.test(assets.url)) {
const publicPath = weappConf.publicPath;
const hashName = getHashName(assets.absolutePath);
assets.url = (/\/$/.test(publicPath) ? publicPath : publicPath + '/') + hashName;
}
return assets.url;
}
}

const cssUrlParseConf = {
...inlineOpts,
...customUrlConf.config
};
processors.push(cssUrlParse(cssUrlParseConf))
}

const defaultPostCSSPluginNames = ['autoprefixer', 'pxtransform', 'url', 'cssModules']
Expand Down Expand Up @@ -202,7 +221,6 @@ export function compileDepStyles (outputFilePath: string, styleFiles: string[])
const pluginsConfig = projectConfig.plugins || {}
const weappConf = projectConfig.weapp || {} as IMiniAppConfig
const useCompileConf = Object.assign({}, weappConf.compile)
const compileInclude = useCompileConf.include || []
isBuildingStyles.set(outputFilePath, true)
return Promise.all(styleFiles.map(async p => {
const filePath = path.join(p)
Expand All @@ -221,7 +239,7 @@ export function compileDepStyles (outputFilePath: string, styleFiles: string[])
root: appPath,
rootNpm: nodeModulesPath,
npmOutputDir,
compileInclude,
compileConfig: useCompileConf,
env: projectConfig.env || {},
uglify: projectConfig!.plugins!.uglify || { enable: true },
babelConfig: getBabelConfig(projectConfig!.plugins!.babel) || {}
Expand Down
17 changes: 12 additions & 5 deletions packages/taro-cli/src/mini/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path'
import * as _ from 'lodash'
import { Config } from '@tarojs/taro'
import * as wxTransformer from '@tarojs/transformer-wx'
import getHashName from '../util/hash';

import {
BUILD_TYPES,
Expand Down Expand Up @@ -69,7 +70,7 @@ export interface IBuildData {
appConfig: Config,
pageConfigs: Map<string, Config>,
alias: IOption,
compileInclude: string[],
compileConfig: {[k: string]: any},
isProduction: boolean,
buildAdapter: BUILD_TYPES,
outputFilesTypes: IMINI_APP_FILE_TYPE,
Expand Down Expand Up @@ -117,7 +118,6 @@ export function setBuildData (appPath: string, adapter: BUILD_TYPES): IBuildData
dir: null
}, weappConf.npm)
const useCompileConf = Object.assign({}, weappConf.compile)
const compileInclude = useCompileConf.include || []
BuildData = {
appPath,
configDir,
Expand All @@ -134,7 +134,7 @@ export function setBuildData (appPath: string, adapter: BUILD_TYPES): IBuildData
isProduction: false,
appConfig: {},
pageConfigs: new Map<string, Config>(),
compileInclude,
compileConfig: useCompileConf,
buildAdapter: adapter,
outputFilesTypes: MINI_APP_FILES[adapter],
constantsReplaceList: Object.assign({}, generateEnvList(projectConfig.env || {}), generateConstantsList(projectConfig.defineConstants || {}), {
Expand Down Expand Up @@ -295,13 +295,20 @@ export function initCopyFiles () {
}

export function copyFilesFromSrcToOutput (files: string[], cb?: (sourceFilePath: string, outputFilePath: string) => void) {
const { nodeModulesPath, npmOutputDir, sourceDir, outputDir, appPath } = BuildData
const { nodeModulesPath, npmOutputDir, sourceDir, outputDir, appPath, projectConfig, buildAdapter } = BuildData
const adapterConfig = projectConfig[buildAdapter];
files.forEach(file => {
let outputFilePath
if (NODE_MODULES_REG.test(file)) {
outputFilePath = file.replace(nodeModulesPath, npmOutputDir)
} else {
outputFilePath = file.replace(sourceDir, outputDir)
if (adapterConfig.publicPath) {
const hashName = getHashName(file);
const staticPath = path.join(appPath, adapterConfig.staticDirectory, projectConfig.projectName || '');
outputFilePath = `${staticPath}/${hashName}`;
} else {
outputFilePath = file.replace(sourceDir, outputDir);
}
}
if (isCopyingFiles.get(outputFilePath)) {
return
Expand Down
62 changes: 62 additions & 0 deletions packages/taro-cli/src/util/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import crypto from 'crypto';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as xxh from 'xxhashjs';

const HEXBASE = 16;

const defaultHashOptions = {
method: 'xxhash32',
shrink: 8,
append: false
};

const getxxhash = (content, options) => {
const hashFunc = options.method === 'xxhash32' ? xxh.h32 : xxh.h64;
const seed = 0;

return hashFunc(seed)
.update(content)
.digest()
.toString(HEXBASE);
};

const getHash = (content, options) => {
if (options.method && typeof options.method === 'function') {
return options.method(content);
}

if (options.method && options.method.indexOf('xxhash') === 0) {
return getxxhash(content, options);
}

try {
const hashFunc = crypto.createHash(options.method);

return hashFunc.update(content)
.digest('hex');
} catch (e) {
return null;
}
};

function hash(content, options) {

options = options || defaultHashOptions;

let hash = getHash(content, options);

if (hash == null) {
// bad hash method; fallback to defaults
// TODO: warning/error reporting?
hash = getHash(content, defaultHashOptions);
}

return options.shrink ? hash.substr(0, options.shrink) : hash;
};

export default (file, options? : any) => {
const content = fs.readFileSync(file);
const ext = path.extname(file);
return hash(content, options) + ext;
}
6 changes: 3 additions & 3 deletions packages/taro-cli/src/util/npmExact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface IArgs {
buildAdapter: BUILD_TYPES,
root: string,
npmOutputDir: string,
compileInclude: string[],
compileConfig: {[k: string]: any},
env: object,
uglify: TogglableOptions,
babelConfig: object
Expand All @@ -41,7 +41,7 @@ export function getExactedNpmFilePath ({
buildAdapter,
root,
npmOutputDir,
compileInclude,
compileConfig,
env,
uglify,
babelConfig
Expand All @@ -56,7 +56,7 @@ export function getExactedNpmFilePath ({
root,
rootNpm: nodeModulesPath,
npmOutputDir,
compileInclude,
compileConfig,
env,
uglify,
babelConfig
Expand Down
Loading