Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
fix: 修复入口文件使用平台扩展名时生成的模板名错误的问题 (#1198)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck committed Jul 29, 2020
1 parent 8823078 commit bdf4a67
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 29 deletions.
9 changes: 2 additions & 7 deletions packages/remax-cli/src/build/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ import * as path from 'path';
import { Options, Platform } from '@remax/types';
import getEntries from '../../getEntries';
import API from '../../API';
import extensions from '../../extensions';
import { targetExtensions } from '../../extensions';

export function searchJSFile(file: string, target: Platform) {
let ext = extensions.map(ext => `.${target}${ext}`);
if (target !== Platform.web) {
ext = ext.concat(extensions.map(ext => `.mini${ext}`));
}
ext = ext.concat(extensions);
for (const e of ext) {
for (const e of targetExtensions(target)) {
const extFile = file + e;
if (fs.existsSync(extFile)) {
return extFile;
Expand Down
9 changes: 2 additions & 7 deletions packages/remax-cli/src/build/webpack/config.mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import WebpackBar from 'webpackbar';
import { Options, Platform } from '@remax/types';
import { slash } from '@remax/shared';
import ejs from 'ejs';
import extensions, { moduleMatcher } from '../../extensions';
import { moduleMatcher, targetExtensions } from '../../extensions';
import getEntries from '../../getEntries';
import * as TurboPages from '../utils/turboPages';
import * as staticCompiler from '../babel/compiler/static';
Expand Down Expand Up @@ -65,12 +65,7 @@ export default function webpackConfig(api: API, options: Options, target: Platfo

config.devtool(false);

config.resolve.extensions.merge(
extensions
.map(ext => `.${target}${ext}`)
.concat(extensions.map(ext => `.mini${ext}`))
.concat(extensions)
);
config.resolve.extensions.merge(targetExtensions(options.target!));
config.output.filename('[name].js');
config.output.globalObject(meta.global);
config.output.publicPath(publicPath);
Expand Down
4 changes: 2 additions & 2 deletions packages/remax-cli/src/build/webpack/config.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import VirtualModulesPlugin from 'webpack-virtual-modules';
import * as RemaxPlugins from './plugins';
import ejs from 'ejs';
import { Platform } from '@remax/types';
import extensions, { moduleMatcher } from '../../extensions';
import { moduleMatcher, targetExtensions } from '../../extensions';
import getEntries from '../../getEntries';
import getAppConfig from '../utils/getAppConfig';
import { cssConfig, addCSSRule, RuleConfig } from './config/css';
Expand Down Expand Up @@ -43,7 +43,7 @@ export default function webpackConfig(api: API, options: Options): webpack.Confi

config.devtool(process.env.NODE_ENV === 'development' ? 'cheap-module-source-map' : false);
config.output.publicPath(publicPath);
config.resolve.extensions.merge(extensions.map(ex => `.web${ex}`).concat(extensions));
config.resolve.extensions.merge(targetExtensions(options.target!));
config.output.filename(process.env.NODE_ENV === 'production' ? '[name].[chunkhash:8].js' : '[name].js');
config.optimization.runtimeChunk({
name: 'runtime',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path';
import { compilation } from 'webpack';
import { Options, EntryInfo } from '@remax/types';
import { slash } from '@remax/shared';
import { matcher } from '../../../../extensions';
import { targetExtensions } from '../../../../extensions';
import readManifest from '../../../../readManifest';
import * as cacheable from './cacheable';
import { pageConfigFile } from '../../../utils/paths';
Expand All @@ -17,6 +17,7 @@ export default function createPageManifest(
api: API
) {
const rootPath = slash(path.join(options.cwd, options.rootDir) + '/');
const matcher = new RegExp(`(${targetExtensions(options.target!).join('|')})$`);
const manifestPath = page.filename.replace(matcher, '.json').replace(rootPath, '');
const config = readManifest(pageConfigFile(page.filename, options), options.target!);
const usingComponents = getUsingComponents(modules, options, compilation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ import * as path from 'path';
import { sortBy } from 'lodash';
import { compilation } from 'webpack';
import ejs from 'ejs';
import { Options, Meta } from '@remax/types';
import { Options, Meta, Platform } from '@remax/types';
import { slash } from '@remax/shared';
import * as componentManifest from '../../../../build/babel/componentManifest';
import { ensureDepth } from '../../../../defaultOptions/UNSAFE_wechatTemplateDepth';
import * as cacheable from './cacheable';
import API from '../../../../API';
import getUsingComponents from './getUsingComponents';
import { targetExtensions } from '../../../../extensions';

export function pageFilename(pagePath: string) {
let value = path.basename(pagePath);
const ext = path.extname(value);
value = value.replace(ext, '');

return value;
function pageFilename(pagePath: string, target: Platform) {
const filename = path.basename(pagePath);
for (const ext of targetExtensions(target)) {
if (filename.endsWith(ext)) {
return filename.replace(new RegExp(`${ext}$`), '');
}
}
}

export function createRenderOptions(
Expand Down Expand Up @@ -52,15 +54,15 @@ export default async function createPageTemplate(
const pagePath = path.relative(rootDir, pageFile);
const pageDir = path.dirname(pagePath);

const fileName = slash(path.join(pageDir, `${pageFilename(pagePath)}${meta.template.extension}`));
const fileName = slash(path.join(pageDir, `${pageFilename(pagePath, options.target!)}${meta.template.extension}`));

const ejsOptions: { [props: string]: any } = {
...createRenderOptions(api, options, modules, compilation),
baseTemplate: `/base${meta.template.extension}`,
};

if (meta.jsHelper) {
ejsOptions.jsHelper = `./${pageFilename(pagePath)}_helper${meta.jsHelper.extension}`;
ejsOptions.jsHelper = `./${pageFilename(pagePath, options.target!)}_helper${meta.jsHelper.extension}`;
}

let source: string = await ejs.renderFile(meta.ejs.page, ejsOptions, {
Expand Down
14 changes: 11 additions & 3 deletions packages/remax-cli/src/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const extensions = ['.mjs', '.js', '.jsx', '.json', '.ts', '.tsx'];
import { Platform } from '@remax/types';

export default extensions;
const extensions = ['.mjs', '.js', '.jsx', '.json', '.ts', '.tsx'];

export const matcher = new RegExp(`(${extensions.join('|')})$`);
const matcher = new RegExp(`(${extensions.join('|')})$`);

export const moduleMatcher = new RegExp(`(${extensions.filter(e => e !== '.json').join('|')})$`);

export const rename = (file: string, ext = '.js') => file.replace(matcher, ext);

export const targetExtensions = (target: Platform) => {
let ext = extensions.map(ext => `.${target}${ext}`);
if (target !== Platform.web) {
ext = ext.concat(extensions.map(ext => `.mini${ext}`));
}
return ext.concat(extensions);
};

1 comment on commit bdf4a67

@vercel
Copy link

@vercel vercel bot commented on bdf4a67 Jul 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.