Skip to content

Commit

Permalink
fix: 调整js处理顺序
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Mar 17, 2021
1 parent 0b09777 commit fa094ca
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 66 deletions.
34 changes: 23 additions & 11 deletions packages/plugin-cmd-roll-lib/src/modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,20 @@ export function modifyConfig(
])
rc.plugin('commonjs').use(commonjs).after('commonjs')

const sourcemap = !!resolvedConfig.sourcemap
const target = resolvedConfig.target
// esbuild
rc.plugin('$$esbuild').use(esbuildPlugin, [
{
include: /\.(jsx?|tsx?)$/,
target,
sourcemap
include: /\.(jsx|tsx?)$/,
exclude: /\.js$/
}
])
// minify
const target = resolvedConfig.target
rc.plugin('$$minify').use(minifyPlugin, [
{
minify: resolvedConfig.minify,
esbuildMinifyOps: {
target,
sourcemap
},
terserMinifyOps: {
sourceMap: sourcemap
target
}
}
])
Expand All @@ -60,7 +54,6 @@ export function modifyConfig(
{
injectScript: css.injectScript,
cssCodeSplit: css.cssCodeSplit,
sourceMap: sourcemap,
minify: !!resolvedConfig.minify,
modules: css.modules || {},
postcss: css.postcss,
Expand Down Expand Up @@ -97,6 +90,8 @@ export function modifyConfig(
])

// external
// TODO: excludeExternal config
getPkgDeps(api.cwd).forEach((dep) => rc.external.set(dep))

// onwarn
rc.onwarn((warning, onwarn) => {
Expand All @@ -109,3 +104,20 @@ function onRollupWarning(warning: RollupWarning, onwarn: WarningHandler) {
if (RollupWarningIgnoreList.includes(warning.code!)) return
onwarn(warning)
}

function getPkgDeps(root: string): string[] {
let pkg = null
try {
pkg = require(join(root, 'package.json'))
} catch {
//
}
let deps: string[] = []
if (pkg) {
deps = [
...Object.keys(pkg?.dependencies || {}),
...Object.keys(pkg?.peerDependencies || {})
]
}
return deps
}
11 changes: 3 additions & 8 deletions packages/plugin-cmd-roll-lib/src/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export type IPreprocessorOps = Partial<Record<string, any>>
interface ICssOps {
injectScript?: boolean
cssCodeSplit?: boolean
sourceMap?: boolean
minify?: boolean
// ops
modules?: ICssModulesOps
Expand All @@ -57,12 +56,8 @@ const commonjsProxyRE = /\?commonjs-proxy/
const cssCache = new Map<string, string>()
const opsToFullCssChunk = new WeakMap<NormalizedOutputOptions, string>()
export function cssPlugin(ops?: ICssOps): Plugin {
const {
cssCodeSplit = false,
injectScript = false,
sourceMap = false,
minify = false
} = ops || {}
const { cssCodeSplit = false, injectScript = false, minify = false } =
ops || {}
let hasGenFullStyle = false
return {
name: 'xus:rollup:css',
Expand Down Expand Up @@ -134,7 +129,7 @@ export function cssPlugin(ops?: ICssOps): Plugin {

return {
code: s.toString(),
map: sourceMap ? s.generateMap({ hires: true }) : undefined
map: ops.sourcemap ? s.generateMap({ hires: true }) : undefined
}
} else {
// bundle a full css
Expand Down
29 changes: 14 additions & 15 deletions packages/plugin-cmd-roll-lib/src/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ export function esbuildPlugin(ops: IEsbuildOps = {}): Plugin {
if (jsxInject && /\.(?:j|t)sx\b/.test(importer)) {
res.code = jsxInject + ';' + res.code
}
// log warn
logger.debug(`transform warnnings: `)
logger.debug(res.warnings)
let warnMsg = ''
res.warnings.forEach((w) => {
warnMsg += w.text + '\n'
})
warnMsg &&
logger.warn('esbuild transform ' + importer + ' warnning:' + warnMsg)

return {
code: res.code,
map: res.map
Expand Down Expand Up @@ -64,8 +74,8 @@ export async function transformByEsbuild(
...ops,
sourcefile: filename,
loader,
sourcemap: true,
minify: false
minify: false,
sourcemap: true
}
try {
logger.debug(`transform with options: `)
Expand All @@ -74,21 +84,10 @@ export async function transformByEsbuild(
source,
transformOps
)
logger.debug(`transform code: `)
logger.debug(code)
logger.debug(`transform sourcemap: `)
logger.debug(map)
// log warn
logger.debug(`transform warnnings: `)
logger.debug(warnings)
let warnMsg = ''
warnings.forEach((w) => {
warnMsg += w.text + '\n'
})
warnMsg && logger.warn('transform ' + filename + ' warnning:' + warnMsg)
return {
code,
map: map ? JSON.parse(map) : {}
warnings,
map: map ? JSON.parse(map) : { mappings: '' }
}
} catch (e) {
logger.debug(`esbuild error: `)
Expand Down
66 changes: 44 additions & 22 deletions packages/plugin-cmd-roll-lib/src/plugins/minify.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Logger } from '@xus/cli-shared'
import { Plugin } from 'rollup'
import { TransformOptions } from 'esbuild'
import { transformByEsbuild } from './esbuild'
Expand All @@ -9,34 +10,55 @@ export interface IMinifyOps {
terserMinifyOps?: MinifyOptions
}

export function minifyPlugin(ops: IMinifyOps = {}): Plugin {
const logger = new Logger(`xus:rollup:minify`)

export function minifyPlugin(ops?: IMinifyOps): Plugin {
const { minify = false, esbuildMinifyOps = {}, terserMinifyOps = {} } =
ops || {}
const isEsbuildMinify = minify === 'esbuild'
return {
name: 'xus:minify',
async renderChunk(source, chunk, outputOps) {
switch (ops.minify) {
case false:
return null
logger.debug(`minify ${chunk.fileName} transformer ${minify}`)

case 'esbuild':
return await transformByEsbuild(source, chunk.fileName, {
...ops.esbuildMinifyOps,
minify: true
})
let code = source
let map = chunk.map
// when legacy mode skip esbuild handle use babel
// @ts-ignore
if (!outputOps?.__xus__skip__esbuild) {
// transform js, minify when minify === esbuild
logger.debug(`esbuild minify with `, {
...esbuildMinifyOps,
minify: isEsbuildMinify
})
const result = await transformByEsbuild(source, chunk.fileName, {
...esbuildMinifyOps,
minify: isEsbuildMinify
})
code = result.code
map = result.map
logger.debug(`esbuild minify warning `, result.warnings)
}

case 'terser':
// eslint-disable-next-line no-case-declarations
const res = await terser.minify(source, {
...ops.terserMinifyOps,
module: outputOps.format.startsWith('es'),
toplevel: outputOps.format === 'cjs',
safari10: true
})
return {
code: res.code as string,
map: res.map
}
if (minify === 'terser') {
// eslint-disable-next-line no-case-declarations
const res = await terser.minify(code, {
...terserMinifyOps,
sourceMap: !!outputOps.sourcemap,
module: outputOps.format.startsWith('es'),
toplevel: outputOps.format === 'cjs',
safari10: true
})
return {
code: res.code as string,
map: res.map
}
} else {
return {
code,
map
}
}
return null
}
}
}
3 changes: 2 additions & 1 deletion packages/plugin-cmd-roll-lib/src/resolveConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export async function generateBuildOps(
if (rollupConfig) {
api.logger.debug(`[generate buildOps] in ${pkgRoot}`)
const { output, ...inputOps } = rollupConfig

// TODO: support from rollup-chain
inputOps.preserveEntrySignatures = 'strict'
// input to absolute
if (!inputOps.input && !resolvedConfig.entry) {
const fileMeta =
Expand Down
7 changes: 0 additions & 7 deletions packages/plugin-lib-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ export default (ops?: ILegacyOps) => {

api.modifyRollupConfig({
fn(rc) {
rc.plugin('$$esbuild').tap((ops) => {
// turn esbuild only handle .js/.ts/.tsx file
!ops[0] && (ops[0] = {})
ops[0].exclude = /\.js$/
ops[0].target = 'es2019'
return ops
})
// babel
rc.plugin('$$legacy')
.use(legacyPlugin, [
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-lib-legacy/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export function legacyPlugin(ops?: ILegacyPluginOps): Plugin {
return {
name: 'xus:rollup:legacy',
async renderChunk(code, chunk, ops) {
// trun off esbuild transform .js file
// @ts-ignore
ops.__xus__skip__esbuild = true
logger.debug(`transform ${chunk.facadeModuleId}`)
const presets = [
[
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-lib-vue-jsx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export default (ops?: IVueJsxOps) => {
api.modifyRollupConfig({
fn(rc) {
rc.plugin('$$esbuild').tap((ops) => {
// turn esbuild only handle .js file
// turn esbuild only handle .ts file
!ops[0] && (ops[0] = {})
ops[0].include = /\.(j|t)s$/
ops[0].include = /\.ts$/
return ops
})
// vue jsx transform
Expand Down

0 comments on commit fa094ca

Please sign in to comment.