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

add localAssetsFileDir to cli build option #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/minapp-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
},
"devDependencies": {
"@types/autoprefixer": "^6.7.3",
"@types/copy-webpack-plugin": "^4.4.2",
"@types/fs-extra": "^5.0.2",
"@types/inquirer": "0.0.41",
"@types/json5": "^0.0.29",
Expand All @@ -69,6 +70,7 @@
"@types/webpack": "^4.1.4",
"@types/webpack-dev-server": "^2.9.4",
"@types/webpack-sources": "^0.1.4",
"copy-webpack-plugin": "^4.6.0",
"ts-lint": "^4.5.1",
"typescript": "^2.8.0"
}
Expand Down
2 changes: 2 additions & 0 deletions packages/minapp-cli/src/bin/minapp-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const buildOptions: cli.Options = {
'p | publicPath': '<string> static file\'s publicPath, just like `output.publicPath` in webpack',
'w | watch': '<boolean> watch mode, without webpack-dev-server',
'e | empty': '<boolean> empty distDir before compile',
'l | useLocalAssetsFile': '<boolean> use Local assets path instead of publicPath'
}

/**
Expand All @@ -24,6 +25,7 @@ export function buildCommand(res: cli.Response) {
if (res.distDir) process.env.MINAPP_DIST_DIR = res.distDir
if (res.pretty) process.env.MINAPP_PRETTY = 'true'
if (res.publicPath) process.env.MINAPP_PUBLIC_PATH = res.publicPath
if (res.useLocalAssetsFile) process.env.MINAPP_USE_LOCAL_ASSETS_FILE = 'true'

compiler(res.watch, null, {empty: res.empty})
}
3 changes: 2 additions & 1 deletion packages/minapp-cli/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function getEnv() {

// 是否美化代码(即,不使用代码压缩工具)
const pretty = !!MINAPP.PRETTY

const useLocalAssetsFile = !!MINAPP.USE_LOCAL_ASSETS_FILE

// 入口文件
const entry = (minapp.component
Expand All @@ -93,6 +93,7 @@ export function getEnv() {
modulesDir,
// sourceMap,
pretty,
useLocalAssetsFile
}
debug(r)
return r
Expand Down
3 changes: 3 additions & 0 deletions packages/minapp-cli/src/config/minapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export namespace minapp {
srcDir: string
distDir: string

localAssetsFileDir: string

/** url-loader limit, 单位:B */
urlLoaderLimit: number

Expand Down Expand Up @@ -92,6 +94,7 @@ export function getMinappConfig(rootDir: string) {
browsers: ['last 7 android version', 'last 5 chrome version', 'last 5 safari version'],
unitTransformer: {},
devServer: {},
localAssetsFileDir: ''
}, minapp.compiler || {})

if (minapp.component) minapp.component = path.resolve(rootDir, minapp.component)
Expand Down
8 changes: 7 additions & 1 deletion packages/minapp-cli/src/config/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {localConfig} from './local'
import {env} from './env'
import {JSON_REGEXP} from '../base/helper'
import {getLoader, ExtractMinappCode, WriteFile, RemoveLessCache} from '../webpack/'
import copyWebpackPlugin = require('copy-webpack-plugin')

const {mode, entry, rootDir, srcDir, distDir, modulesDir} = env

Expand Down Expand Up @@ -63,8 +64,13 @@ const plugins: any[] = [
}),
new webpack.EnvironmentPlugin(['NODE_ENV']),
new ExtractMinappCode(env),
new RemoveLessCache(env),
new RemoveLessCache(env)
]
if (env.useLocalAssetsFile) {
plugins.push(new copyWebpackPlugin([
{ from: path.resolve(srcDir, env.minapp.compiler.localAssetsFileDir), to: env.minapp.compiler.localAssetsFileDir }
]))
}
if (env.hasServer) {
plugins.push(new WriteFile(env))
}
Expand Down
5 changes: 3 additions & 2 deletions packages/minapp-cli/src/webpack/loader/wxml-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Author Mora <qiuzhongleiabc@126.com> (https://github.com/qiu8310)
import * as parser from '@minapp/wxml-parser'
import {EOL} from 'os'
const debug = require('debug')('minapp:cli:wxml-loader')

import {env} from '../../config/env'
import {Loader} from './Loader'
import {map, STYLE_RESOURCE_REGEXP} from '../util'

Expand Down Expand Up @@ -127,7 +127,8 @@ export default class WxmlLoader extends Loader {
} else {
if (this.shouleMakeRequireFile(absFile)) {
if (this.isStaticFile(absFile) && typeof attr.value === 'string') {
attr.value = attr.value.replace(src, await this.loadStaticFile(absFile, src, false))
const file = await this.loadStaticFile(absFile, src, false, env.useLocalAssetsFile)
attr.value = attr.value.replace(src, file)
} else if (node.name === 'import' || node.name === 'include') {
attr.value = this.getExtractRequirePath(absFile, '.wxml')
requires.push(absFile)
Expand Down