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

feat(mp\h5): 支持 output.clean 选项 #14069

Merged
merged 17 commits into from
Jun 29, 2023
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "taro",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "开放式跨端跨框架开发解决方案",
"homepage": "https://github.com/NervJS/taro#readme",
"author": "O2Team",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-transform-react-jsx-to-rn-stylesheet",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "Transform stylesheet selector to style in JSX Elements.",
"license": "MIT",
"main": "dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-taroapi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-transform-taroapi",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-taro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-preset-taro",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "Taro babel preset",
"author": "yuche <[email protected]>",
"homepage": "https://github.com/nervjs/taro/tree/master/packages/babel-preset-taro#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/create-app",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "create taro app with one command",
"author": "VincentW <[email protected]>",
"homepage": "https://github.com/nervjs/taro/tree/master/packages/create-app#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/css-to-react-native/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "taro-css-to-react-native",
"description": "Convert CSS text to a React Native stylesheet object",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"main": "dist/index.js",
"license": "MIT",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config-taro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-config-taro",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "Taro specific linting rules for ESLint",
"main": "index.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/postcss-html-transform/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-html-transform",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "transform html tag name selector",
"main": "index.js",
"author": "drchan",
Expand Down
2 changes: 1 addition & 1 deletion packages/postcss-plugin-constparse/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-plugin-constparse",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "parse constants defined in config",
"main": "index.js",
"author": "Simba",
Expand Down
2 changes: 1 addition & 1 deletion packages/postcss-pxtransform/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-pxtransform",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "PostCSS plugin px 转小程序 rpx及h5 rem 单位",
"main": "index.js",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/shared",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "Taro utils internal use.",
"author": "yuche <[email protected]>",
"homepage": "https://github.com/nervjs/taro/tree/master/packages/shared#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/stylelint-config-taro-rn/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stylelint-config-taro-rn",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "Shareable stylelint config for React Native CSS modules",
"main": "index.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/stylelint-taro-rn/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "stylelint-taro-rn",
"description": "A collection of React Native specific rules for stylelint",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"main": "dist/index.js",
"files": [
"dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-alipay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/plugin-platform-alipay",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "支付宝小程序平台插件",
"author": "Chen-jj",
"homepage": "https://github.com/nervjs/taro/tree/master/packages/taro-alipay#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/api",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "Taro common API",
"author": "yuche <[email protected]>",
"homepage": "https://github.com/nervjs/taro/tree/master/packages/api#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli-convertor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/cli-convertor",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "cli tool for taro-convert",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/cli",
"version": "3.6.9-alpha.5",
"version": "3.6.9-alpha.7",
"description": "cli tool for taro",
"main": "index.js",
"types": "dist/index.d.ts",
Expand Down
98 changes: 98 additions & 0 deletions packages/taro-cli/src/__tests__/build-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { emptyDirectory } from '@tarojs/helper'
import * as path from 'path'

import { run } from './utils'

const runBuild = run('build', [
'commands/build',
require.resolve('@tarojs/plugin-platform-weapp'),
require.resolve('@tarojs/plugin-platform-h5')
])

jest.mock('@tarojs/helper', () => {
const helper = jest.requireActual('@tarojs/helper')
const fs = helper.fs
return {
__esModule: true,
...helper,
emptyDirectory: jest.fn(),
fs: {
...fs
},
}
})

const APP_PATH = path.join(__dirname, 'fixtures/default')
const OUTPUT_PATH = path.join(__dirname, 'fixtures/default/dist')

describe('构建配置测试', () => {
const emptyDirectoryMocked = emptyDirectory as jest.Mock<any>

beforeEach(() => {
emptyDirectoryMocked.mockReset()
process.argv = []
})

afterEach(() => {
process.argv = []
emptyDirectoryMocked.mockReset()
})

describe('小程序', () => {
it(`项目 output.clean = clean: { keep: ['project.config.json'] } ==> 清空dist文件夹但保留指定文件`, async () => {
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
const logSpy = jest.spyOn(console, 'log')
const errorSpy = jest.spyOn(console, 'error')
logSpy.mockImplementation(() => {})
errorSpy.mockImplementation(() => {})
exitSpy.mockImplementation(() => {
throw new Error()
})

try {
await runBuild(APP_PATH, {
options: {
type: 'weapp',
platform: 'weapp'
}
})
} catch (error) {
// no handler
}
expect(emptyDirectoryMocked).toBeCalledWith(OUTPUT_PATH, { excludes: ['project.config.json'] })

exitSpy.mockRestore()
logSpy.mockRestore()
errorSpy.mockRestore()
})
})

describe('h5', () => {
it('output.clean = false ==> 保留dist文件夹', async () => {
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
const logSpy = jest.spyOn(console, 'log')
const errorSpy = jest.spyOn(console, 'error')
logSpy.mockImplementation(() => {})
errorSpy.mockImplementation(() => {})
exitSpy.mockImplementation(() => {
throw new Error()
})

try {
await runBuild(APP_PATH, {
options: {
type: 'h5',
platform: 'h5'
}
})
} catch (error) {
// no handler
}
expect(emptyDirectoryMocked).toBeCalledTimes(0)

exitSpy.mockRestore()
logSpy.mockRestore()
errorSpy.mockRestore()
})
})
})
105 changes: 105 additions & 0 deletions packages/taro-cli/src/__tests__/dotenv-parse.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { Kernel } from '@tarojs/service'
import * as path from 'path'

import CLI from '../cli'
import { dotenvParse } from '../util/index'

jest.mock('@tarojs/service')
const MockedKernel = Kernel as unknown as jest.Mock<Kernel>
const APP_PATH = path.join(__dirname, 'fixtures/default')

function setProcessArgv (cmd: string) {
// @ts-ignore
process.argv = [null, ...cmd.split(' ')]
}

describe('inspect', () => {
let cli: CLI

beforeAll(() => {
cli = new CLI(APP_PATH)
})

beforeEach(() => {
MockedKernel.mockClear()
process.argv = []
delete process.env.NODE_ENV
delete process.env.TARO_ENV
delete process.env.TARO_APP_TEST
delete process.env.TARO_APP_ID
delete process.env.JD_APP_TEST
delete process.env.TARO_APP_DEFAULT
delete process.env.TARO_APP_FOO
})

afterEach(() => {
MockedKernel.mockClear()
process.argv = []
delete process.env.NODE_ENV
delete process.env.TARO_ENV
delete process.env.TARO_APP_TEST
delete process.env.TARO_APP_ID
delete process.env.JD_APP_TEST
delete process.env.TARO_APP_DEFAULT
delete process.env.TARO_APP_FOO
})

describe('cli mode env', () => {

it('dotenvParse .env .env.dev should success', async () => {
expect(process.env.TARO_test).toBeUndefined()
dotenvParse(path.resolve(__dirname, 'env'), 'TARO_', 'dev')
expect(process.env.TARO_test).toBe('123')
expect(process.env._TARO_test).toBeUndefined()
})

it('--watch true => 默认加载 .env.development', async () => {
setProcessArgv('taro build --watch --type weapp')
await cli.run()
expect(process.env.TARO_APP_TEST).toEqual('env-development')
expect(process.env.TARO_APP_DEFAULT).toEqual('default')
})

it('--watch false => 默认加载 .env.production', async () => {
setProcessArgv('taro build --type weapp')
await cli.run()
expect(process.env.TARO_APP_TEST).toEqual('env-production')
expect(process.env.TARO_APP_DEFAULT).toEqual('default')
})

it('指定加载 .env.pre', async () => {
setProcessArgv('taro build --type weapp --mode pre')
await cli.run()
expect(process.env.TARO_APP_TEST).toEqual('env-pre')
expect(process.env.TARO_APP_DEFAULT).toEqual('default')
})

it('env.local 比 .env 优先级更高', async () => {
setProcessArgv('taro build --type weapp --mode find404')
await cli.run()
expect(process.env.TARO_APP_TEST).toEqual('env-local')
expect(process.env.TARO_APP_DEFAULT).toEqual('default')
})

it('env.uat.local 比 .env.uat 优先级更高', async () => {
setProcessArgv('taro build --type weapp --mode uat')
await cli.run()
expect(process.env.TARO_APP_TEST).toEqual('env-uat-local')
expect(process.env.TARO_APP_DEFAULT).toEqual('default')
})

it('自定义前缀: JD_APP_', async () => {
setProcessArgv('taro build --type weapp --mode uat --env-prefix JD_APP_')
await cli.run()
expect(process.env.JD_APP_TEST).toEqual('env-uat')
expect(process.env.TARO_APP_TEST).toEqual(undefined)
expect(process.env.TARO_APP_ID).toEqual('特殊变量appid')
})

it('环境变量可以相互引用', async () => {
setProcessArgv('taro build --type weapp --mode pre')
await cli.run()
expect(process.env.TARO_APP_FOO).toEqual('env-pre-foo')
})
})
})
13 changes: 0 additions & 13 deletions packages/taro-cli/src/__tests__/dotenvParse.spec.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/taro-cli/src/__tests__/fixtures/default/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TARO_APP_TEST=env

TARO_APP_DEFAULT=default
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TARO_APP_TEST=env-development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TARO_APP_TEST=env-local
3 changes: 3 additions & 0 deletions packages/taro-cli/src/__tests__/fixtures/default/.env.pre
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TARO_APP_TEST=env-pre

TARO_APP_FOO=${TARO_APP_TEST}-foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TARO_APP_TEST=env-production
5 changes: 5 additions & 0 deletions packages/taro-cli/src/__tests__/fixtures/default/.env.uat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TARO_APP_TEST=env-uat

JD_APP_TEST=env-uat

TARO_APP_ID=特殊变量appid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TARO_APP_TEST=env-uat-local
Loading